@@ -11,10 +11,18 @@
11#include "sqlite3api.h"
11#include "sqlite3api.h"
14−
14+// buffer used for storing queries
15+// Used in high-level functions.
15static char sql_query_buffer[2000];
16static char sql_query_buffer[2000];
17+// array used to store the string
18+//value obtain by get_val_text
16unsigned char* array;
19unsigned char* array;
21+ open an SQLite database file as
22+ specified by the filename argument.
23+ Returns pointer to DB struct on success
18void* sqlite3_open_idr(const char *filename){
26void* sqlite3_open_idr(const char *filename){
20 sqlite3 *db;
28 sqlite3 *db;
@@ -29,51 +37,54 @@ void* sqlite3_open_idr(const char *filename){
29 return dbi;
37 return dbi;
41+ Frees the resource and returns 0 on success
33int sqlite3_close_idr(void* db){
43int sqlite3_close_idr(void* db){
35 DBinfo* dbi =(DBinfo*) db;
45 DBinfo* dbi =(DBinfo*) db;
36 int res =sqlite3_close(dbi->db_ptr);
46 int res =sqlite3_close(dbi->db_ptr);
37 if (res == SQLITE_OK){
47 if (res == SQLITE_OK){
38 free(dbi);
48 free(dbi);
39− printf("Successfuly closed the databse\n");
43− if(res == SQLITE_BUSY){
49− return 1;
52+ return res;
56+ SQLite wrapper around sqlite3_prepare_v2(),
57+ sqlite3_step(), and sqlite3_finalize().
58+ This version of exec cannot be used with prepare.
59+ For executing queries, must use the exec_db function.
53int sqlite3_exec_idr(void* db, const char *sql)
61int sqlite3_exec_idr(void* db, const char *sql)
56 DBinfo* dbi =(DBinfo*) db;
63 DBinfo* dbi =(DBinfo*) db;
57 char* err;
64 char* err;
60 rc = sqlite3_exec(dbi->db_ptr,sql,NULL, NULL, &err);
66 rc = sqlite3_exec(dbi->db_ptr,sql,NULL, NULL, &err);
62 if (rc != SQLITE_OK && err != NULL) {
67 if (rc != SQLITE_OK && err != NULL) {
63 strncpy(dbi->buffer, err, sizeof(dbi->buffer));
68 strncpy(dbi->buffer, err, sizeof(dbi->buffer));
64 sqlite3_free(err);
69 sqlite3_free(err);
66 return rc;
71 return rc;
69−
74+ Gets the error store in the buffer
75+ in the struct. Some certain functions
76+ have the feature to store errors in a buffer.
70char* sqlite3_get_error(void* db) {
78char* sqlite3_get_error(void* db) {
71 DBinfo* dbi =(DBinfo*) db;
79 DBinfo* dbi =(DBinfo*) db;
72 return dbi->buffer;
80 return dbi->buffer;
83+ Compiles the query into a byte-code program
84+ Returns a pointer to the sqlite3_stmt pointer
85+ and stores it in the struct.
75void* sqlite3_prepare_idr(void *db,const char *zSql){
87void* sqlite3_prepare_idr(void *db,const char *zSql){
77 sqlite3_stmt* stmt;
88 sqlite3_stmt* stmt;
78 const char *tail;
89 const char *tail;
@@ -86,10 +97,15 @@ void* sqlite3_prepare_idr(void *db,const char *zSql){
86 if(rec != SQLITE_OK){
97 if(rec != SQLITE_OK){
87 return NULL;
98 return NULL;
90 return dbi;
100 return dbi;
103+ Another wrapper interface that is preserved
104+ for backwards compatibility.
105+ Use of this interface is not recommended.
106+ This was mainly used for testing within the
93void* sqlite3_get_table_idr(void* db,
109void* sqlite3_get_table_idr(void* db,
94 const char *sql){
110 const char *sql){
@@ -98,9 +114,7 @@ void* sqlite3_get_table_idr(void* db,
99 Table* tbl = malloc(sizeof(Table));
115 Table* tbl = malloc(sizeof(Table));
100 tbl->database = dbi;
116 tbl->database = dbi;
102 int res = sqlite3_get_table(dbi->db_ptr,sql,&tbl->table_data,&tbl->num_row,&tbl->num_col,&err);
117 int res = sqlite3_get_table(dbi->db_ptr,sql,&tbl->table_data,&tbl->num_row,&tbl->num_col,&err);
104 int array_size = sizeof(&tbl->table_data);
118 int array_size = sizeof(&tbl->table_data);
106 if( res != SQLITE_OK && err != NULL){
120 if( res != SQLITE_OK && err != NULL){
@@ -108,12 +122,20 @@ void* sqlite3_get_table_idr(void* db,
108 sqlite3_free(err);
122 sqlite3_free(err);
109 return NULL;
123 return NULL;
112 tbl -> data_size = array_size;
125 tbl -> data_size = array_size;
113− return tbl;
126+ return tbl;
130+ This function executes queries.
131+ This can be used after preparing a query
132+ In case of error or library misuse
134+ It also calls step in order to obtain
135+ the row and column number and stores them
136+ in the struct. The row number is needed
137+ later on in get_data_type function.
117int exec_db(void*p){
139int exec_db(void*p){
119 DBinfo* dbi =(DBinfo*) p;
141 DBinfo* dbi =(DBinfo*) p;
@@ -129,7 +151,6 @@ int exec_db(void*p){
129 if(rc == SQLITE_ERROR && rc == SQLITE_MISUSE){
151 if(rc == SQLITE_ERROR && rc == SQLITE_MISUSE){
130 return 1;
152 return 1;
133 row_counter =0;
154 row_counter =0;
135 while (rc == SQLITE_ROW) {
156 while (rc == SQLITE_ROW) {
@@ -142,44 +163,68 @@ int exec_db(void*p){
142 dbi->row_count = row_counter;
163 dbi->row_count = row_counter;
143 dbi->col_count = col;
164 dbi->col_count = col;
144 return rc;
165 return rc;
168+ Returns row number from DB Struct
148int sqlite3_get_num_row_v2(void* p){
171int sqlite3_get_num_row_v2(void* p){
150 DBinfo* dbi =(DBinfo*) p;
173 DBinfo* dbi =(DBinfo*) p;
151 int row_number =dbi->row_count;
174 int row_number =dbi->row_count;
152 return row_number;
175 return row_number;
179+ Returns column number from DB Struct
157int sqlite3_get_num_col_v2(void* p){
181int sqlite3_get_num_col_v2(void* p){
159 DBinfo* dbi =(DBinfo*) p;
183 DBinfo* dbi =(DBinfo*) p;
160 int col_number =dbi-> col_count;
184 int col_number =dbi-> col_count;
161 return col_number;
185 return col_number;
189+ Another way of obtaining row number
190+ Thought this function gets the value
191+ from Table struct. The value is stored
192+ in the struct after calling get_table.
193+ Using this version is not recommended
194+ unless used with get_table
166int sqlite3_get_num_row(void* p){
196int sqlite3_get_num_row(void* p){
168 Table* tbl =(Table*) p;
198 Table* tbl =(Table*) p;
169 int row_number =tbl->num_row;
199 int row_number =tbl->num_row;
170 return row_number;
200 return row_number;
204+ Another way of obtaining column number
205+ Thought this function gets the value
206+ from Table struct. The value is stored
207+ in the struct after calling get_table.
208+ Using this version is not recommended
209+ unless used with get_table
175int sqlite3_get_num_col(void* p){
211int sqlite3_get_num_col(void* p){
177 Table* tbl =(Table*) p;
213 Table* tbl =(Table*) p;
178 int col_number =tbl-> num_col;
214 int col_number =tbl-> num_col;
179− return col_number;
215+ return col_number;
219+ This routine returns the type of value
220+ and must be called after prepare and exec.
221+ Since exec steps through database to obtain
222+ row number, this function calls reset to
223+ set the pointer to its initial state
224+ and then calls sqlite3_column_type
182−int sqlite3_get_data_type_v2(void* p, int nRow, int nCol){
227+int sqlite3_get_data_type(void* p, int nRow, int nCol){
184 DBinfo* dbi =(DBinfo*) p;
229 DBinfo* dbi =(DBinfo*) p;
185 int rc, type, row_counter;
230 int rc, type, row_counter;
@@ -198,193 +243,37 @@ int sqlite3_get_data_type_v2(void* p, int nRow, int nCol){
198 return type;
243 return type;
247+ Obtains the integer value in a given column
203−int sqlite3_get_val_int_v2(void* p, int nCol){
250+int sqlite3_get_val_int(void* p, int nCol){
206 DBinfo* dbi =(DBinfo*) p;
252 DBinfo* dbi =(DBinfo*) p;
207 int val, col;
253 int val, col;
209 val =sqlite3_column_int(dbi->ppStmt, nCol);
254 val =sqlite3_column_int(dbi->ppStmt, nCol);
210 return val;
255 return val;
215−int sqlite3_get_data_type(void* p,const char* tbl_name, int nRow, int nCol){
217− DBinfo* dbi =(DBinfo*) p;
220− int rc,i, val, counter;
221− const char* char_int;
224− strcpy(sql_query_buffer, "select * from ");
225− strcat(sql_query_buffer, tbl_name);
227− rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
228− if(rc != SQLITE_OK){
229− fprintf(stderr, "SQL Prepare error\n");
232− rc = sqlite3_step(stmt);
234− while (rc == SQLITE_ROW && counter < nRow){
236− rc = sqlite3_step(stmt);
241− val =sqlite3_column_type(stmt, nCol);
243− rc =sqlite3_finalize(stmt);
244− if( rc != SQLITE_OK){
245− printf("Couldnt finalize:\n");
252−int sqlite3_get_val_int(void* p,const char* tbl_name, int nRow, int nCol){
259+ Obtains the text value
253−
260+ Need to allocate memory to store the string
254−
261+ Use GC_malloc since Boehm garbage collector
255− DBinfo* dbi =(DBinfo*) p;
262+ frees the resources .
256− sqlite3_stmt* stmt;
263+ */
258− int rc,i, val, counter;
259− const char* char_int;
262− strcpy(sql_query_buffer, "select * from ");
263− strcat(sql_query_buffer, tbl_name);
265− rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
266− if(rc != SQLITE_OK){
267− fprintf(stderr, "SQL Prepare error\n");
270− rc = sqlite3_step(stmt);
272− while (rc == SQLITE_ROW && counter < nRow){
274− rc = sqlite3_step(stmt);
279− val =sqlite3_column_int(stmt, nCol);
280− rc =sqlite3_finalize(stmt);
281− if( rc != SQLITE_OK){
282− printf("Couldnt finalize:\n");
289−const unsigned char* sqlite3_get_val_text_v2(void* p,int nCol){
264+const unsigned char* sqlite3_get_val_text(void* p,int nCol){
292 DBinfo* dbi =(DBinfo*) p;
267 DBinfo* dbi =(DBinfo*) p;
293 int rc,i, val, counter;
268 int rc,i, val, counter;
294 const unsigned char* text_val;
269 const unsigned char* text_val;
296 array =(unsigned char *) GC_malloc(1000*sizeof(char));
270 array =(unsigned char *) GC_malloc(1000*sizeof(char));
298 text_val =sqlite3_column_text(dbi->ppStmt, nCol);
271 text_val =sqlite3_column_text(dbi->ppStmt, nCol);
299 memcpy(array, text_val, strlen(text_val));
272 memcpy(array, text_val, strlen(text_val));
300− int size = strlen(text_val);
302 return array;
273 return array;
308−const unsigned char* sqlite3_get_val_text(void* p,const char* tbl_name,int nRow, int nCol){
311− DBinfo* dbi =(DBinfo*) p;
314− int rc,i, val, counter;
315− const unsigned char* text_val;
318− strcpy(sql_query_buffer, "select * from ");
319− strcat(sql_query_buffer, tbl_name);
321− rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
322− if(rc != SQLITE_OK){
323− fprintf(stderr, "SQL Prepare error\n");
326− rc = sqlite3_step(stmt);
328− while (rc == SQLITE_ROW && counter < nRow){
330− rc = sqlite3_step(stmt);
336− array =(unsigned char *) GC_malloc(1000*sizeof(char));
338− text_val =sqlite3_column_text(stmt, nCol);
339− memcpy(array, text_val, sizeof(text_val));
340− rc =sqlite3_finalize(stmt);
341− if( rc != SQLITE_OK){
342− printf("Couldn't finalize.\n");
350−float sqlite3_get_float(void* p,const char* tbl_name, int nRow, int nCol){
276+float sqlite3_get_float(void* p, int nCol){
352− DBinfo* dbi =(DBinfo*) p;
355− int rc,i, val, counter;
360− strcpy(sql_query_buffer, "select * from ");
361− strcat(sql_query_buffer, tbl_name);
363− rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
364− if(rc != SQLITE_OK){
365− fprintf(stderr, "SQL Prepare error\n");
368− rc = sqlite3_step(stmt);
370− while (rc == SQLITE_ROW && counter < nRow){
373− rc = sqlite3_step(stmt);
377− double_val =sqlite3_column_double(stmt, nCol);
378− float float_val =(float)double_val;
379− rc =sqlite3_finalize(stmt);
380− if( rc != SQLITE_OK){
381− printf("Couldn't finalize.\n");
387−float sqlite3_get_float_v2(void* p, int nCol){
389 DBinfo* dbi =(DBinfo*) p;
278 DBinfo* dbi =(DBinfo*) p;
390 double double_val;
279 double double_val;
@@ -394,37 +283,40 @@ float sqlite3_get_float_v2(void* p, int nCol){
394 return float_val;
283 return float_val;
287+ frees the pointer returned by get_table.
399void sqlite3_free_table_idr(void* db){
290void sqlite3_free_table_idr(void* db){
400 Table* tbl =(Table*) db;
291 Table* tbl =(Table*) db;
401 sqlite3_free_table(tbl->table_data);
292 sqlite3_free_table(tbl->table_data);
402 free(tbl);
293 free(tbl);
406int sqlite3_step_idr(void* db){
295int sqlite3_step_idr(void* db){
408 DBinfo* dbi =(DBinfo*) db;
297 DBinfo* dbi =(DBinfo*) db;
409 int rc =sqlite3_step(dbi->ppStmt);
298 int rc =sqlite3_step(dbi->ppStmt);
410 return rc;
299 return rc;
302+ Binds integer. This returns a pointer
303+ because of the implementation of BindMulti
304+ which binds multiple values
413−int sqlite3_bind_int_idr(void* p,int index, int val){
306+void* sqlite3_bind_int_idr(void* p,int index, int val){
415 DBinfo* dbi =(DBinfo*) p;
308 DBinfo* dbi =(DBinfo*) p;
418− printf("\nThe statement has %d wildcards\n", sqlite3_bind_parameter_count(dbi->ppStmt));
419 rc =sqlite3_bind_int(dbi->ppStmt,index,val);
311 rc =sqlite3_bind_int(dbi->ppStmt,index,val);
420 if(rc != SQLITE_OK){
312 if(rc != SQLITE_OK){
421− return 1;
313+ return NULL;
424− return rc;
316+ return dbi;
427−int sqlite3_bind_float_idr(void* p,int index, float val){
319+void* sqlite3_bind_float_idr(void* p,int index, float val){
429 DBinfo* dbi =(DBinfo*) p;
321 DBinfo* dbi =(DBinfo*) p;
@@ -433,36 +325,42 @@ int sqlite3_bind_float_idr(void* p,int index, float val){
433 rc =sqlite3_bind_double(dbi->ppStmt,index,res);
325 rc =sqlite3_bind_double(dbi->ppStmt,index,res);
435 if(rc != SQLITE_OK){
327 if(rc != SQLITE_OK){
436− return 1;
328+ return NULL;
439− return rc;
330+ return dbi;
442−int sqlite3_bind_null_idr(void* p,int index){
333+void* sqlite3_bind_null_idr(void* p,int index){
444 DBinfo* dbi =(DBinfo*) p;
335 DBinfo* dbi =(DBinfo*) p;
446 rc =sqlite3_bind_null(dbi->ppStmt,index);
337 rc =sqlite3_bind_null(dbi->ppStmt,index);
447 if(rc != SQLITE_OK){
338 if(rc != SQLITE_OK){
448− return 1;
339+ return NULL;
451− return rc;
342+ return dbi;
454−int sqlite3_bind_text_idr(void* p,const char* text, int index,int length){
345+void* sqlite3_bind_text_idr(void* p,const char* text, int index,int length){
456 DBinfo* dbi =(DBinfo*) p;
347 DBinfo* dbi =(DBinfo*) p;
458 rc =sqlite3_bind_text(dbi->ppStmt,index,text,length,SQLITE_STATIC);
349 rc =sqlite3_bind_text(dbi->ppStmt,index,text,length,SQLITE_STATIC);
459 if(rc != SQLITE_OK){
350 if(rc != SQLITE_OK){
460− return 1;
351+ return NULL;
463− return rc;
354+ return dbi;
357+ Used for testing column count function.
358+ This function prepares query and by passing
359+ select all, gets the count for column number
360+ Could be used for testing. Not recommended to
361+ be used with prepare and exec
466int sqlite3_column_count_idr(void* db, const char* tbl_name){
364int sqlite3_column_count_idr(void* db, const char* tbl_name){
467 DBinfo* dbi =(DBinfo*) db;
365 DBinfo* dbi =(DBinfo*) db;
468 sqlite3_stmt* stmt;
366 sqlite3_stmt* stmt;
@@ -490,16 +388,17 @@ int sqlite3_column_count_idr(void* db, const char* tbl_name){
490 return rc;
388 return rc;
494int sqlite3_data_count_idr(void* db){
391int sqlite3_data_count_idr(void* db){
496 DBinfo* dbi =(DBinfo*) db;
393 DBinfo* dbi =(DBinfo*) db;
497 int rc = sqlite3_data_count(dbi->ppStmt);
394 int rc = sqlite3_data_count(dbi->ppStmt);
499 return rc;
396 return rc;
399+ Must be called after prepare to clean up
503int sqlite3_finalize_idr(void* db){
402int sqlite3_finalize_idr(void* db){
505 DBinfo* dbi=(DBinfo*) db;
404 DBinfo* dbi=(DBinfo*) db;
@@ -511,9 +410,12 @@ int sqlite3_complete_idr(const char *sql){
512 int rc = sqlite3_complete(sql);
411 int rc = sqlite3_complete(sql);
513 return rc;
412 return rc;
416+ Resets a prepared statement pointer
417+ to its initial state
517int sqlite3_reset_idr(void* db){
419int sqlite3_reset_idr(void* db){
519 DBinfo* dbi=(DBinfo*) db;
421 DBinfo* dbi=(DBinfo*) db;
@@ -521,7 +423,10 @@ int sqlite3_reset_idr(void* db){
521 return rc;
423 return rc;
427+ The following routines may be used to
428+ obtain column related information.
525const char *sqlite3_column_name_idr(void* db, int N){
430const char *sqlite3_column_name_idr(void* db, int N){
527 DBinfo* dbi=(DBinfo*) db;
432 DBinfo* dbi=(DBinfo*) db;
@@ -537,8 +442,6 @@ const char *sqlite3_column_decltype_idr(void* db,int n){
537 return dectype;
442 return dectype;
542int sqlite3_column_bytes_idr(void* db, int n){
445int sqlite3_column_bytes_idr(void* db, int n){
544 DBinfo* dbi=(DBinfo*) db;
447 DBinfo* dbi=(DBinfo*) db;
@@ -547,7 +450,6 @@ int sqlite3_column_bytes_idr(void* db, int n){
551const void *sqlite3_column_blob_idr(void* db, int iCol){
453const void *sqlite3_column_blob_idr(void* db, int iCol){
552 DBinfo* dbi=(DBinfo*) db;
454 DBinfo* dbi=(DBinfo*) db;
553 const void* data =sqlite3_column_blob(dbi-> ppStmt, iCol);
455 const void* data =sqlite3_column_blob(dbi-> ppStmt, iCol);
@@ -567,7 +469,9 @@ int sqlite3_column_int_idr(void* db, int iCol){
567 return res;
469 return res;
473+ Some back up functions
572void* sqlite3_backup_init_idr(void* pDest,
476void* sqlite3_backup_init_idr(void* pDest,
573 const char *zDestName,
477 const char *zDestName,
@@ -595,10 +499,6 @@ int sqlite3_backup_step_idr(void *backup, int nPage){
596 DBbackup* dbi=(DBbackup*) backup;
500 DBbackup* dbi=(DBbackup*) backup;
597 int res = sqlite3_backup_step(dbi->backup, nPage);
501 int res = sqlite3_backup_step(dbi->backup, nPage);
599− if( res == SQLITE_OK){
600− printf("Backup Success\n");
602 return res;
502 return res;
@@ -627,23 +527,17 @@ int sqlite3_backup_pagecount_idr(void *backup){
627 return res;
527 return res;
631−int sqlite3_value_bytes_idr(void* value){
531+ Get the length of string
632−
532+ Need this in Idris since length will not
633− DBinfo* dbi=(DBinfo*) value;
533+ work on Strings.
634− int res = sqlite3_value_bytes(dbi->value);
534+ */
635− return res;
535+int strLength(const char * str){
637−}
537+ int length = strlen(str);
639−int sqlite3_value_bytes16_idr(void* value){
641− DBinfo* dbi=(DBinfo*) value;
642− int res =sqlite3_value_bytes16(dbi->value);