doc-dbquery.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. dbquery.c / dbquery.h
  2. Retrieve information from the database & displays it.
  3. Overview
  4. This component uses colors.c / colors.h.
  5. Every function that allocates memory also frees it.
  6. Advanced string manipulation such as concatenation is avoided. Instead
  7. strings are printed in parts.
  8. Exo File Dependencies
  9. SQL commands are stored in an outside file (.sql) and executed. The
  10. functions in this file are largely adaptable until
  11. void qresult(void * const, const char *);
  12. which prints the results of the query. This is specific to the query. All
  13. functions in this file could be considered non-modular because the
  14. specialized functions like 'qresult' are eventually called by the more
  15. adaptable functions. This may later be solved by calling function pointers
  16. to display table data, but currently many of the tables are similar enough
  17. that one function to print all tables suffices.
  18. Adding or Removing New Queries
  19. To add new queries the program may handle the following functions need to
  20. be adjusted.
  21. inline void print_tblheader(const char *);
  22. inline char *get_filename(const char *) ;
  23. void qresult(void * const, const char *);
  24. The file name of the query file should be added to the switch statement of
  25. function 'get_filename'. The starting html tags and labels should also be
  26. added to the switch statement of function 'print_tblheader'. For both
  27. forementioned functions, the switch statement operates on a variable 'c'
  28. which represents the first character of the query string. Currently, the
  29. case is that the different queries, '?map=' and '?player=', begin with
  30. different letters.
  31. The function 'qresult'- extra formatting and corresponding conditions
  32. should be added to the series of if/else-if statements. 'qresult's most
  33. applicable conditionals are player names and formatted times- if a new
  34. query will involve those, the conditionals should be added.
  35. Function Walkthrough
  36. Sorted by caller to callee.
  37. void getquery(char * const);
  38. Parameter 'char * const' is the query string retrieved from the
  39. environment. NULL is an acceptable parameter. Only the first character
  40. of the string pointed at by the pointer will be changed.
  41. A file name is then retrieved from 'get_filename', depending on the
  42. first character of the query string. Then the contents of the query
  43. file is read in to be later to query the database.
  44. Currently, this function determines the size of the file by using
  45. 'fseek' and 'ftell'. Often recommended against however:
  46. 1. The files to open are restricted to a specific set.
  47. 2. The files are plain text files.
  48. 3. The file sizes, in bytes, are very unlikely to exceed the
  49. maximum value representable by integers.
  50. 'ftell' returns a long int. It can determine the file size of a ~2 GB
  51. file.
  52. int executequery(const char *, const char *);
  53. Parameter #1 is the string containing the query for the database. NULL
  54. is not acceptable. If a null value is given, the program will output
  55. the following string to stderr:
  56. Could not prepare SQL statement: not an error
  57. (null)
  58. Parameter #2 is the query string. NULL is acceptable.
  59. This function opens and closes the connection to the database. Also
  60. prepares and finalizes the sql statement. However itself, it does not
  61. actually query the database.
  62. void qresult(void * const, const char *);
  63. Parameter #1 is the prepared sql statement.
  64. Parameter #2 is the first character of the query string- in use to
  65. identify the query.
  66. This function both executes the query and displays the data.
  67. The for loop iterates for each field of the row returned.
  68. Conditionals explained:
  69. The fields are hardcoded from the sql query files.
  70. mranks.sql - (*c = QOVERVIEW)
  71. select mapid, max(trank), min(tvalue), alias
  72. i = 0 1 2 3
  73. rplayers.sql - (*c = QRPLAYER)
  74. select alias, mapid, idrank
  75. i = 0 1 2
  76. mleaderboard.sql - (*c = QMLEADERBOARD)
  77. select trank, alias, tvalue
  78. i = 0 1 2
  79. if ((*c == QMLEADERBOARD && i == 1)
  80. || (*c == QOVERVIEW && i == 3)
  81. || (*c == QRPLAYER && i == 0))
  82. If the field is for map leaderboards and column 1, or the overview
  83. and column 3, or player ranks and column 0, then it is a player
  84. name; which should have the in-game color codes translated to html.
  85. if ((i == 0 && *c == QOVERVIEW) || (i == 1 && *c == QRPLAYER))
  86. If the field is for overview and column 0 or is player ranks column
  87. 1, then it is a map name, and it should have html tags linking it
  88. to the map leaderboards.
  89. if (i == 2 && (*c == QMLEADERBOARD || *c == QOVERVIEW))
  90. If the field is column 2 of either the overview or the map
  91. leaderboards, then it is a time which should be formatted as such.
  92. If none of the conditionals are met, then the field is printed as usual.
  93. void print_time(const unsigned char *);
  94. Parameter 'const unsigned char *' is the string that represents a
  95. number- quantity of centiseconds.
  96. The time as a string is converted to a number by 'strtoul'. Thus, the
  97. maximum value that is returned by 'strtoul' given a string is
  98. 21,474,836.47 seconds. This is by far, more than enough for a game mode
  99. focused on the lowest possible times.
  100. This function performs the conversion and prints the value.
  101. inline void print_tblheader(const char *);
  102. Parameter 'const char *' is a pointer to a string. However, only the
  103. first character is of interest because C can not switch on strings.
  104. This function prints the starting tag of the html table.
  105. inline char *get_filename(const char *) ;
  106. Parameter 'const char *' is a pointer to a string. However, only the
  107. first character is of interest because C can not switch on strings.
  108. This function returns the file name of a sql query file.