sqlite.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #define HL_NAME(n) sqlite_##n
  2. #include <hl.h>
  3. #include <string.h>
  4. #include <sqlite3.h>
  5. /**
  6. <doc>
  7. <h1>SQLite</h1>
  8. <p>
  9. Sqlite is a small embeddable SQL database that store all its data into
  10. a single file. See http://sqlite.org for more details.
  11. </p>
  12. </doc>
  13. **/
  14. typedef struct _database sqlite_database;
  15. typedef struct _result sqlite_result;
  16. struct _database {
  17. void (*finalize)( sqlite_database * );
  18. sqlite3 *db;
  19. sqlite_result *last;
  20. };
  21. struct _result {
  22. void (*finalize)(sqlite_result * );
  23. sqlite_database *db;
  24. int ncols;
  25. int count;
  26. int *names; //hashed_names
  27. int *bools;
  28. int done;
  29. int first;
  30. sqlite3_stmt *r;
  31. };
  32. static void HL_NAME(error)( sqlite3 *db, bool close ) {
  33. hl_buffer *b = hl_alloc_buffer();
  34. hl_buffer_str(b, USTR("SQLite error: "));
  35. hl_buffer_str(b, sqlite3_errmsg16(db));
  36. if ( close )
  37. sqlite3_close(db);
  38. hl_error("%s",hl_buffer_content(b,NULL));
  39. }
  40. static void HL_NAME(finalize_request)(sqlite_result *r, bool exc ) {
  41. r->first = 0;
  42. r->done = 1;
  43. if( r->ncols == 0 )
  44. r->count = sqlite3_changes(r->db->db);
  45. if( sqlite3_finalize(r->r) != SQLITE_OK && exc )
  46. hl_error("SQLite error: Could not finalize request");
  47. r->r = NULL;
  48. r->db->last = NULL;
  49. r->db = NULL;
  50. free(r->names);
  51. free(r->bools);
  52. r->names = NULL;
  53. r->bools = NULL;
  54. }
  55. static void HL_NAME(finalize_result)(sqlite_result *r ) {
  56. if (r) HL_NAME(finalize_request)(r, false);
  57. }
  58. /**
  59. close : 'db -> void
  60. <doc>Closes the database.</doc>
  61. **/
  62. HL_PRIM void HL_NAME(close)( sqlite_database *db ) {
  63. if (db->last != NULL)
  64. HL_NAME(finalize_request)(db->last, false);
  65. if (sqlite3_close(db->db) != SQLITE_OK) {
  66. // No exception : we shouldn't alloc memory in a finalizer anyway
  67. }
  68. }
  69. static void HL_NAME(finalize_database)( sqlite_database *db ) {
  70. if (db) HL_NAME(close)(db);
  71. }
  72. /**
  73. connect : filename:string -> 'db
  74. <doc>Open or create the database stored in the specified file.</doc>
  75. **/
  76. HL_PRIM sqlite_database *HL_NAME(connect)( vbyte *filename ) {
  77. sqlite_database *db;
  78. sqlite3 *sqlite;
  79. if( sqlite3_open16(filename, &sqlite) != SQLITE_OK ) {
  80. HL_NAME(error)(sqlite, true);
  81. }
  82. db = (sqlite_database*)hl_gc_alloc_finalizer(sizeof(sqlite_database));
  83. db->finalize = HL_NAME(finalize_database);
  84. db->db = sqlite;
  85. db->last = NULL;
  86. return db;
  87. }
  88. /**
  89. last_insert_id : 'db -> int
  90. <doc>Returns the last inserted auto_increment id.</doc>
  91. **/
  92. HL_PRIM int HL_NAME(last_id)(sqlite_database *db ) {
  93. return (int)sqlite3_last_insert_rowid(db->db);
  94. }
  95. /**
  96. request : 'db -> sql:string -> 'result
  97. <doc>Executes the SQL request and returns its result</doc>
  98. **/
  99. HL_PRIM sqlite_result *HL_NAME(request)(sqlite_database *db, vbyte *sql ) {
  100. sqlite_result *r;
  101. const char *tl;
  102. int i,j;
  103. r = (sqlite_result*)hl_gc_alloc_finalizer(sizeof(sqlite_result));
  104. r->finalize = HL_NAME(finalize_result);
  105. r->db = db;
  106. if( sqlite3_prepare16_v2(db->db, sql, -1, &r->r, &tl) != SQLITE_OK ) {
  107. HL_NAME(error)(db->db, false);
  108. }
  109. if( *tl ) {
  110. sqlite3_finalize(r->r);
  111. hl_error("SQLite error: Cannot execute several SQL requests at the same time");
  112. }
  113. r->ncols = sqlite3_column_count(r->r);
  114. r->names = (int*)malloc(sizeof(int)*r->ncols);
  115. r->bools = (int*)malloc(sizeof(int)*r->ncols);
  116. r->first = 1;
  117. r->done = 0;
  118. for(i=0;i<r->ncols;i++) {
  119. int id = hl_hash_gen((uchar*)sqlite3_column_name16(r->r,i), true);
  120. const char *dtype = sqlite3_column_decltype(r->r,i);
  121. for(j=0;j<i;j++)
  122. if( r->names[j] == id ) {
  123. if( strcmp(sqlite3_column_name16(r->r,i), sqlite3_column_name16(r->r,j)) == 0 ) {
  124. sqlite3_finalize(r->r);
  125. hl_buffer *b = hl_alloc_buffer();
  126. hl_buffer_str(b, USTR("SQLite error: Same field is two times in the request: "));
  127. hl_buffer_str(b, (uchar*)sql);
  128. hl_error("%s",hl_buffer_content(b, NULL));
  129. } else {
  130. hl_buffer *b = hl_alloc_buffer();
  131. hl_buffer_str(b, USTR("SQLite error: Same field ids for: "));
  132. hl_buffer_str(b, sqlite3_column_name16(r->r,i));
  133. hl_buffer_str(b, USTR(" and "));
  134. hl_buffer_str(b, sqlite3_column_name16(r->r,j));
  135. sqlite3_finalize(r->r);
  136. hl_error("%s",hl_buffer_content(b, NULL));
  137. }
  138. }
  139. r->names[i] = id;
  140. r->bools[i] = dtype?(strcmp(dtype,"BOOL") == 0):0;
  141. }
  142. // changes in an update/delete
  143. if( db->last != NULL )
  144. HL_NAME(finalize_request)(db->last, false);
  145. db->last = r;
  146. return db->last;
  147. }
  148. /**
  149. result_get_length : 'result -> int
  150. <doc>Returns the number of rows in the result or the number of rows changed by the request.</doc>
  151. **/
  152. HL_PRIM vdynamic *HL_NAME(result_get_length)( sqlite_result *r ) {
  153. if( r->ncols != 0 )
  154. return NULL;
  155. return hl_make_dyn(&r->count, &hlt_i32);
  156. }
  157. /**
  158. result_get_nfields : 'result -> int
  159. <doc>Returns the number of fields in the result.</doc>
  160. **/
  161. HL_PRIM int HL_NAME(result_get_nfields)( sqlite_result *r ) {
  162. return r->ncols;
  163. }
  164. /**
  165. result_get_fields : 'result -> array<string>
  166. <doc>Returns the array of field names in the result.</doc>
  167. **/
  168. HL_PRIM varray *HL_NAME(result_get_fields)( sqlite_result *r ) {
  169. varray *a = hl_alloc_array(&hlt_bytes, r->ncols);
  170. int i;
  171. for (i = 0; i < r->ncols; i++)
  172. {
  173. hl_aptr(a, vbyte*)[i] = (vbyte *)sqlite3_column_name16(r->r, i);
  174. }
  175. return a;
  176. }
  177. /**
  178. result_next : 'result -> object?
  179. <doc>Returns the next row in the result or [null] if no more result.</doc>
  180. **/
  181. HL_PRIM varray *HL_NAME(result_next)( sqlite_result *r ) {
  182. if( r->done )
  183. return NULL;
  184. switch( sqlite3_step(r->r) ) {
  185. case SQLITE_ROW:
  186. r->first = 0;
  187. varray *a = hl_alloc_array(&hlt_dyn, r->ncols);
  188. int i;
  189. for(i=0;i<r->ncols;i++)
  190. {
  191. vdynamic *v;
  192. switch( sqlite3_column_type(r->r,i) ) {
  193. case SQLITE_NULL:
  194. v = NULL;
  195. break;
  196. case SQLITE_INTEGER:
  197. {
  198. int vint = sqlite3_column_int(r->r, i);
  199. if (r->bools[i])
  200. v = hl_make_dyn(&vint, &hlt_bool);
  201. else
  202. v = hl_make_dyn(&vint, &hlt_i32);
  203. break;
  204. }
  205. case SQLITE_FLOAT:
  206. {
  207. double d = sqlite3_column_double(r->r, i);
  208. v = hl_make_dyn(&d, &hlt_f64);
  209. break;
  210. }
  211. case SQLITE_TEXT:
  212. {
  213. uchar *text16 = (uchar *)sqlite3_column_text16(r->r, i);
  214. v = hl_make_dyn(&text16, &hlt_bytes);
  215. break;
  216. }
  217. case SQLITE_BLOB:
  218. {
  219. vbyte *blob = (vbyte *)sqlite3_column_blob(r->r, i);
  220. v = hl_make_dyn(&blob, &hlt_bytes);
  221. break;
  222. }
  223. default:
  224. hl_error("SQLite error: Unknown type #%d", sqlite3_column_type(r->r,i));
  225. }
  226. hl_aptr(a, vdynamic*)[i] = v;
  227. }
  228. return a;
  229. case SQLITE_DONE:
  230. HL_NAME(finalize_request)(r, true);
  231. return NULL;
  232. case SQLITE_BUSY:
  233. hl_error("SQLite error: Database is busy");
  234. case SQLITE_ERROR:
  235. HL_NAME(error)(r->db->db, false);
  236. default:
  237. return NULL;
  238. }
  239. return NULL;
  240. }
  241. /**
  242. result_get : 'result -> n:int -> string
  243. <doc>Return the [n]th field of the current result row.</doc>
  244. **/
  245. HL_PRIM vbyte *HL_NAME(result_get)( sqlite_result *r, int n ) {
  246. if (n < 0 || n >= r->ncols)
  247. return NULL;
  248. if( r->first )
  249. HL_NAME(result_next)(r);
  250. if( r->done )
  251. return NULL;
  252. return (vbyte*)sqlite3_column_text16(r->r, n);
  253. }
  254. /**
  255. result_get_int : 'result -> n:int -> int
  256. <doc>Return the [n]th field of the current result row as an integer.</doc>
  257. **/
  258. HL_PRIM vdynamic *HL_NAME(result_get_int)( sqlite_result *r, int n) {
  259. if (n < 0 || n >= r->ncols)
  260. return NULL;
  261. if( r->first )
  262. HL_NAME(result_next)(r);
  263. if( r->done )
  264. return NULL;
  265. int value = sqlite3_column_int(r->r, n);
  266. return hl_make_dyn(&value, &hlt_i32);
  267. }
  268. /**
  269. result_get_float : 'result -> n:int -> float
  270. <doc>Return the [n]th field of the current result row as a float.</doc>
  271. **/
  272. HL_PRIM vdynamic *HL_NAME(result_get_float)( sqlite_result *r, int n ) {
  273. if( n < 0 || n >= r->ncols )
  274. return NULL;
  275. if( r->first )
  276. HL_NAME(result_next)(r);
  277. if( r->done )
  278. return NULL;
  279. double value = sqlite3_column_double(r->r, n);
  280. return hl_make_dyn(&value, &hlt_f64);
  281. }
  282. #define _CONNECTION _ABSTRACT( sqlite_database )
  283. #define _RESULT _ABSTRACT( sqlite_result )
  284. DEFINE_PRIM(_CONNECTION, connect, _BYTES);
  285. DEFINE_PRIM(_VOID, close, _CONNECTION);
  286. DEFINE_PRIM(_RESULT, request, _CONNECTION _BYTES);
  287. DEFINE_PRIM(_I32, last_id, _CONNECTION);
  288. DEFINE_PRIM(_ARR, result_next, _RESULT);
  289. DEFINE_PRIM(_NULL(_BYTES), result_get, _RESULT _I32);
  290. DEFINE_PRIM(_NULL(_I32), result_get_int, _RESULT _I32);
  291. DEFINE_PRIM(_NULL(_F64), result_get_float, _RESULT _I32);
  292. DEFINE_PRIM(_NULL(_I32), result_get_length, _RESULT);
  293. DEFINE_PRIM(_I32, result_get_nfields, _RESULT);
  294. DEFINE_PRIM(_ARR, result_get_fields, _RESULT);