sql.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #ifndef _SQL_H
  2. #define _SQL_H
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sqlite3.h>
  6. #define SQL_NOERR_LIMIT 1
  7. #define SQL_OK SQL_NOERR_LIMIT + SQLITE_OK
  8. #define SQL_ROW SQL_NOERR_LIMIT + SQLITE_ROW
  9. #define SQL_DONE SQL_NOERR_LIMIT + SQLITE_DONE
  10. #define SQL_ERR_LIMIT -6667
  11. #define SQL_ERROR SQL_ERR_LIMIT + SQLITE_ERROR
  12. #define SQL_INTERNAL SQL_ERR_LIMIT + SQLITE_INTERNAL
  13. #define SQL_PERM SQL_ERR_LIMIT + SQLITE_PERM
  14. #define SQL_ABORT SQL_ERR_LIMIT + SQLITE_ABORT
  15. #define SQL_BUSY SQL_ERR_LIMIT + SQLITE_BUSY
  16. #define SQL_LOCKED SQL_ERR_LIMIT + SQLITE_LOCKED
  17. #define SQL_NOMEM SQL_ERR_LIMIT + SQLITE_NOMEM
  18. #define SQL_READONLY SQL_ERR_LIMIT + SQLITE_READONLY
  19. #define SQL_INTERRUPT SQL_ERR_LIMIT + SQLITE_INTERRUPT
  20. #define SQL_IOERR SQL_ERR_LIMIT + SQLITE_IOERR
  21. #define SQL_CORRUPT SQL_ERR_LIMIT + SQLITE_CORRUPT
  22. #define SQL_NOTFOUND SQL_ERR_LIMIT + SQLITE_NOTFOUND
  23. #define SQL_FULL SQL_ERR_LIMIT + SQLITE_FULL
  24. #define SQL_CANTOPEN SQL_ERR_LIMIT + SQLITE_CANTOPEN
  25. #define SQL_PROTOCOL SQL_ERR_LIMIT + SQLITE_PROTOCOL
  26. #define SQL_EMPTY SQL_ERR_LIMIT + SQLITE_EMPTY
  27. #define SQL_SCHEMA SQL_ERR_LIMIT + SQLITE_SCHEMA
  28. #define SQL_TOOBIG SQL_ERR_LIMIT + SQLITE_TOOBIG
  29. #define SQL_CONSTRAINT SQL_ERR_LIMIT + SQLITE_CONSTRAINT
  30. #define SQL_MISMATCH SQL_ERR_LIMIT + SQLITE_MISMATCH
  31. #define SQL_MISUSE SQL_ERR_LIMIT + SQLITE_MISUSE
  32. #define SQL_NOLFS SQL_ERR_LIMIT + SQLITE_NOLFS
  33. #define SQL_AUTH SQL_ERR_LIMIT + SQLITE_AUTH
  34. #define SQL_FORMAT SQL_ERR_LIMIT + SQLITE_FORMAT
  35. #define SQL_RANGE SQL_ERR_LIMIT + SQLITE_RANGE
  36. #define SQL_NOTADB SQL_ERR_LIMIT + SQLITE_NOTADB
  37. /* RESULTCODES > 101 is IGNORED */
  38. #define SQL_OUTOFMEM SQL_ERR_LIMIT - 1
  39. #define SQL_NULL_ABORTION SQL_ERR_LIMIT - 2
  40. /**
  41. * Structure: sql_db
  42. * Typedef: sql_db_t
  43. * Author: Joachim M. Giæver
  44. *
  45. * Description:
  46. * Stores information about an SQL-handler and it relations.
  47. **/
  48. typedef struct sql_db sql_db_t;
  49. /**
  50. * Structure: sql_stmt
  51. * Typedef: sql_stmt_t
  52. * Author: Joachim M. Giæver
  53. *
  54. * Description:
  55. * Stores information about an SQL-statement and its relations.
  56. *
  57. **/
  58. typedef struct sql_stmt sql_stmt_t;
  59. /**
  60. * Function: sql_create
  61. * Author: Joachim M. Giæver
  62. *
  63. * Parameters:
  64. * - char *db_file: The DB-file to open
  65. * - int flags: The flags, doesnt support MUTEX and CACHE flags
  66. *
  67. * Description:
  68. * Creates an SQL-object; init the SQL
  69. *
  70. * Returns: sql_db_t *, db object
  71. **/
  72. sql_db_t *sql_create( char *db_file, int flags );
  73. /**
  74. * Function: sql_ok
  75. * Author: Joachim M. Giæver
  76. *
  77. * Parameters:
  78. * - sql_db_t *sql: The SQL-handler to check.
  79. *
  80. * Description:
  81. * Checks if last activity agains the DB (e.g a query)
  82. * was successfull.
  83. *
  84. * Returns: int, 1 on success, 0 on failure
  85. **/
  86. int sql_ok( sql_db_t *sql );
  87. /**
  88. * Function: sql_done
  89. * Author: Joachim M. Giæver
  90. *
  91. * Parameters:
  92. * - sql_db_t *sql: The SQL-handler to check.
  93. *
  94. * Description:
  95. * Determines if the SQL-task is done.
  96. *
  97. * Returns: int, 1 on success, 0 on failure
  98. **/
  99. int sql_done( sql_db_t *sql );
  100. /**
  101. * Function: sql_errmsg
  102. * Author: Joachim M. Giæver
  103. *
  104. * Parameters:
  105. * - sql_db_t *sql: The SQL-handler to look for errors in.
  106. *
  107. * Description:
  108. * Returns the corresponding SQL-error message.
  109. * If sql_ok() returns an error, there is an
  110. * error present.
  111. *
  112. * Returns: char *, the error message
  113. **/
  114. const char *sql_errmsg( sql_db_t *sql );
  115. /**
  116. * Function: sql_close
  117. * Author: Joachim M. Giæver
  118. *
  119. * Parameters:
  120. * - sql_db_t *sql: The SQL-handler to close
  121. *
  122. * Description:
  123. * Close an SQL-handler. This will also free related
  124. * statements for the handler, which as unessacary.
  125. * Statements should be finalized by designer, but are
  126. * implemented as a "gb-collection".
  127. *
  128. * Returns: void
  129. **/
  130. void sql_close( sql_db_t *sql );
  131. /**
  132. * Function: sql_query
  133. * Author: Joachim M. Giæver
  134. *
  135. * Parameters:
  136. * - sql_db_t *sql: The SQL-handler to query agains
  137. * - const char *query: The SQL-statement/-query
  138. *
  139. * Description:
  140. * Creates an SQL-statement and queries the DB.
  141. *
  142. * Returns: sql_stmt_t *, the SQL-statement object.
  143. **/
  144. sql_stmt_t *sql_query( sql_db_t *sql, const char *query );
  145. /**
  146. * Function: sql_column_count
  147. * Author: Joachim M. Giæver
  148. *
  149. * Parameters:
  150. * - sql_stmt_t *stmt: SQL-statement to check
  151. *
  152. * Description:
  153. * Returns the amount of colums returned from a query
  154. *
  155. * Returns: int, #colums returned
  156. **/
  157. int sql_column_count( sql_stmt_t *stmt );
  158. /**
  159. * Function: sql_column_name
  160. * Author: Joachim M. Giæver
  161. *
  162. * Parameters:
  163. * - sql_stmt_t *stmt: SQL-statment to read from
  164. *
  165. * Description:
  166. * Returns the columname specified by the index.
  167. *
  168. * Returns: char *, colum name
  169. **/
  170. const char *sql_column_name( sql_stmt_t *stmt, int idx );
  171. /**
  172. * Function: sql_column_text
  173. * Author: Joachim M. Giæver
  174. *
  175. * Parameters:
  176. * - sql_stmt_t *stmt: SQL-statement to read from
  177. *
  178. * Description:
  179. * Return the text stored in the column specified by the index.
  180. *
  181. * Returns: const unsigned char *, column text
  182. **/
  183. const unsigned char *sql_column_text( sql_stmt_t *stmt, int idx );
  184. /**
  185. * Function: sql_stmt_step
  186. * Author: Joachim M. Giæver
  187. *
  188. * Parameters:
  189. * - sql_stmt_t *stmt: SQL-statement to read from
  190. *
  191. * Description:
  192. * Go to next entry in the retured query.
  193. *
  194. * Returns: int, 1 on success, 0 on failure.
  195. **/
  196. int sql_stmt_step( sql_stmt_t *stmt );
  197. /**
  198. * Function: sql_stmt_sql
  199. * Author: Joachim M. Giæver
  200. *
  201. * Parameters:
  202. * - sql_stmt_t *stmt: SQL-statement to load handler from
  203. *
  204. * Description:
  205. * Returns the related SQL-handler for the SQL-statement.
  206. *
  207. * Returns: sql_db_t *, SQL-object
  208. **/
  209. sql_db_t *sql_stmt_sql( sql_stmt_t *stmt );
  210. /**
  211. * Function: sql_stmt_tail
  212. * Author: Joachim M. Giæver
  213. *
  214. * Parameters:
  215. * - sql_stmt_t *stmt: The parent statement
  216. *
  217. * Description:
  218. * Queries the SQL with the next tail-query.
  219. *
  220. * Note: This finalizes the parent statement.
  221. *
  222. * Returns: sql_stmt_t *, tail statement
  223. **/
  224. sql_stmt_t *sql_stmt_tail( sql_stmt_t *stmt );
  225. /**
  226. * Function: sql_stmt_finalize
  227. * Author: Joachim M. Giæver
  228. *
  229. * Parameters:
  230. * - sql_stmt_t *stmt: SQL-statement to finalize
  231. *
  232. * Description:
  233. * Finalizes an SQL-statement AKA closing it.
  234. *
  235. * Returns: void
  236. **/
  237. void sql_stmt_finalize( sql_stmt_t *stmt );
  238. #endif