@@ -11,10 +11,19 @@
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;
22+ open an SQLite database file as
23+ specified by the filename argument.
24+ Returns pointer to DB struct on success
18void* sqlite3_open_idr(const char *filename){
27void* sqlite3_open_idr(const char *filename){
20 sqlite3 *db;
29 sqlite3 *db;
@@ -30,26 +39,29 @@ void* sqlite3_open_idr(const char *filename){
43+ Frees the resource and returns 0 on success
33int sqlite3_close_idr(void* db){
45int sqlite3_close_idr(void* db){
35 DBinfo* dbi =(DBinfo*) db;
47 DBinfo* dbi =(DBinfo*) db;
36 int res =sqlite3_close(dbi->db_ptr);
48 int res =sqlite3_close(dbi->db_ptr);
37 if (res == SQLITE_OK){
49 if (res == SQLITE_OK){
38 free(dbi);
50 free(dbi);
39− printf("Successfuly closed the databse\n");
43− if(res == SQLITE_BUSY){
49− return 1;
55+ return res;
60+ SQLite wrapper around sqlite3_prepare_v2(),
61+ sqlite3_step(), and sqlite3_finalize().
62+ This version of exec cannot be used with prepare.
63+ For executing queries, must use the exec_db function.
53int sqlite3_exec_idr(void* db, const char *sql)
65int sqlite3_exec_idr(void* db, const char *sql)
@@ -58,7 +70,6 @@ int sqlite3_exec_idr(void* db, const char *sql)
60 rc = sqlite3_exec(dbi->db_ptr,sql,NULL, NULL, &err);
72 rc = sqlite3_exec(dbi->db_ptr,sql,NULL, NULL, &err);
62 if (rc != SQLITE_OK && err != NULL) {
73 if (rc != SQLITE_OK && err != NULL) {
63 strncpy(dbi->buffer, err, sizeof(dbi->buffer));
74 strncpy(dbi->buffer, err, sizeof(dbi->buffer));
64 sqlite3_free(err);
75 sqlite3_free(err);
@@ -66,12 +77,22 @@ int sqlite3_exec_idr(void* db, const char *sql)
66 return rc;
77 return rc;
81+ Gets the error store in the buffer
82+ in the struct. Some certain functions
83+ have the feature to store errors in a buffer.
70char* sqlite3_get_error(void* db) {
85char* sqlite3_get_error(void* db) {
71 DBinfo* dbi =(DBinfo*) db;
86 DBinfo* dbi =(DBinfo*) db;
72 return dbi->buffer;
87 return dbi->buffer;
92+ Compiles the query into a byte-code program
93+ Returns a pointer to the sqlite3_stmt pointer
94+ and stores it in the struct.
75void* sqlite3_prepare_idr(void *db,const char *zSql){
96void* sqlite3_prepare_idr(void *db,const char *zSql){
77 sqlite3_stmt* stmt;
98 sqlite3_stmt* stmt;
@@ -90,6 +111,14 @@ void* sqlite3_prepare_idr(void *db,const char *zSql){
90 return dbi;
111 return dbi;
115+ Another wrapper interface that is preserved
116+ for backwards compatibility.
117+ Use of this interface is not recommended.
118+ This was mainly used for testing within the
93void* sqlite3_get_table_idr(void* db,
122void* sqlite3_get_table_idr(void* db,
94 const char *sql){
123 const char *sql){
@@ -114,6 +143,19 @@ void* sqlite3_get_table_idr(void* db,
150+ This function executes queries.
151+ This can be used after preparing a query
152+ In case of error or library misuse
154+ It also calls step in order to obtain
155+ the row and column number and stores them
156+ in the struct. The row number is needed
157+ later on in get_data_type function.
117int exec_db(void*p){
159int exec_db(void*p){
119 DBinfo* dbi =(DBinfo*) p;
161 DBinfo* dbi =(DBinfo*) p;
@@ -144,7 +186,10 @@ int exec_db(void*p){
144 return rc;
186 return rc;
190+ Returns row number from DB Struct
148int sqlite3_get_num_row_v2(void* p){
193int sqlite3_get_num_row_v2(void* p){
150 DBinfo* dbi =(DBinfo*) p;
195 DBinfo* dbi =(DBinfo*) p;
@@ -154,6 +199,10 @@ int sqlite3_get_num_row_v2(void* p){
204+ Returns column number from DB Struct
157int sqlite3_get_num_col_v2(void* p){
206int sqlite3_get_num_col_v2(void* p){
159 DBinfo* dbi =(DBinfo*) p;
208 DBinfo* dbi =(DBinfo*) p;
@@ -162,7 +211,15 @@ int sqlite3_get_num_col_v2(void* p){
216+ Another way of obtaining row number
217+ Thought this function gets the value
218+ from Table struct. The value is stored
219+ in the struct after calling get_table.
220+ Using this version is not recommended
221+ unless used with get_table
166int sqlite3_get_num_row(void* p){
223int sqlite3_get_num_row(void* p){
168 Table* tbl =(Table*) p;
225 Table* tbl =(Table*) p;
@@ -172,6 +229,16 @@ int sqlite3_get_num_row(void* p){
234+ Another way of obtaining column number
235+ Thought this function gets the value
236+ from Table struct. The value is stored
237+ in the struct after calling get_table.
238+ Using this version is not recommended
239+ unless used with get_table
175int sqlite3_get_num_col(void* p){
242int sqlite3_get_num_col(void* p){
177 Table* tbl =(Table*) p;
244 Table* tbl =(Table*) p;
@@ -179,7 +246,18 @@ int sqlite3_get_num_col(void* p){
179 return col_number;
246 return col_number;
251+ This routine returns the type of value
252+ and must be called after prepare and exec.
253+ Since exec steps through database to obtain
254+ row number, this function calls reset to
255+ set the pointer to its initial state
256+ and then calls sqlite3_column_type
182−int sqlite3_get_data_type_v2(void* p, int nRow, int nCol){
260+int sqlite3_get_data_type(void* p, int nRow, int nCol){
184 DBinfo* dbi =(DBinfo*) p;
262 DBinfo* dbi =(DBinfo*) p;
185 int rc, type, row_counter;
263 int rc, type, row_counter;
@@ -198,9 +276,12 @@ int sqlite3_get_data_type_v2(void* p, int nRow, int nCol){
198 return type;
276 return type;
281+ Obtains the integer value in a given column
203−int sqlite3_get_val_int_v2(void* p, int nCol){
284+int sqlite3_get_val_int(void* p, int nCol){
206 DBinfo* dbi =(DBinfo*) p;
287 DBinfo* dbi =(DBinfo*) p;
@@ -212,81 +293,14 @@ int sqlite3_get_val_int_v2(void* p, int nCol){
215−int sqlite3_get_data_type(void* p,const char* tbl_name, int nRow, int nCol){
296+/*
217− DBinfo* dbi =(DBinfo*) p;
298+ Obtains the text value
218− sqlite3_stmt* stmt;
299+ Need to allocate memory to store the string
219− const char *tail;
300+ Use GC_malloc since Boehm garbage collector
220− int rc,i, val, counter;
301+ frees the resources .
221− const char* char_int;
302+ */
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){
255− DBinfo* dbi =(DBinfo*) p;
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){
303+const unsigned char* sqlite3_get_val_text(void* p,int nCol){
292 DBinfo* dbi =(DBinfo*) p;
306 DBinfo* dbi =(DBinfo*) p;
@@ -302,88 +316,7 @@ const unsigned char* sqlite3_get_val_text_v2(void* p,int nCol){
307−const unsigned char* sqlite3_get_val_text(void* p,const char* tbl_name,int nRow, int nCol){
310− DBinfo* dbi =(DBinfo*) p;
313− int rc,i, val, counter;
314− const unsigned char* text_val;
317− strcpy(sql_query_buffer, "select * from ");
318− strcat(sql_query_buffer, tbl_name);
320− rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
321− if(rc != SQLITE_OK){
322− fprintf(stderr, "SQL Prepare error\n");
325− rc = sqlite3_step(stmt);
327− while (rc == SQLITE_ROW && counter < nRow){
329− rc = sqlite3_step(stmt);
335− array =(unsigned char *) GC_malloc(1000*sizeof(char));
337− text_val =sqlite3_column_text(stmt, nCol);
338− memcpy(array, text_val, sizeof(text_val));
339− rc =sqlite3_finalize(stmt);
340− if( rc != SQLITE_OK){
341− printf("Couldn't finalize.\n");
349−float sqlite3_get_float(void* p,const char* tbl_name, int nRow, int nCol){
319+float sqlite3_get_float(void* p, int nCol){
351− DBinfo* dbi =(DBinfo*) p;
354− int rc,i, val, counter;
359− strcpy(sql_query_buffer, "select * from ");
360− strcat(sql_query_buffer, tbl_name);
362− rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
363− if(rc != SQLITE_OK){
364− fprintf(stderr, "SQL Prepare error\n");
367− rc = sqlite3_step(stmt);
369− while (rc == SQLITE_ROW && counter < nRow){
372− rc = sqlite3_step(stmt);
376− double_val =sqlite3_column_double(stmt, nCol);
377− float float_val =(float)double_val;
378− rc =sqlite3_finalize(stmt);
379− if( rc != SQLITE_OK){
380− printf("Couldn't finalize.\n");
386−float sqlite3_get_float_v2(void* p, int nCol){
388 DBinfo* dbi =(DBinfo*) p;
321 DBinfo* dbi =(DBinfo*) p;
389 double double_val;
322 double double_val;
@@ -394,6 +327,9 @@ float sqlite3_get_float_v2(void* p, int nCol){
331+ frees the pointer returned by get_table.
398void sqlite3_free_table_idr(void* db){
334void sqlite3_free_table_idr(void* db){
399 Table* tbl =(Table*) db;
335 Table* tbl =(Table*) db;
@@ -409,15 +345,19 @@ int sqlite3_step_idr(void* db){
409 return rc;
345 return rc;
350+ Binds integer. This returns a pointer
351+ because of the implementation of BindMulti
352+ which binds multiple values
412void* sqlite3_bind_int_idr(void* p,int index, int val){
354void* sqlite3_bind_int_idr(void* p,int index, int val){
414 DBinfo* dbi =(DBinfo*) p;
356 DBinfo* dbi =(DBinfo*) p;
417− //printf("\nThe statement has %d wildcards\n", sqlite3_bind_parameter_count(dbi->ppStmt));
418 rc =sqlite3_bind_int(dbi->ppStmt,index,val);
359 rc =sqlite3_bind_int(dbi->ppStmt,index,val);
419 if(rc != SQLITE_OK){
360 if(rc != SQLITE_OK){
420− printf("Error in binding\n");
421 return NULL;
361 return NULL;
@@ -439,16 +379,16 @@ void* sqlite3_bind_float_idr(void* p,int index, float val){
439 return dbi;
379 return dbi;
442−int sqlite3_bind_null_idr(void* p,int index){
382+void* sqlite3_bind_null_idr(void* p,int index){
444 DBinfo* dbi =(DBinfo*) p;
384 DBinfo* dbi =(DBinfo*) p;
446 rc =sqlite3_bind_null(dbi->ppStmt,index);
386 rc =sqlite3_bind_null(dbi->ppStmt,index);
447 if(rc != SQLITE_OK){
387 if(rc != SQLITE_OK){
448− return 1;
388+ return NULL;
451− return rc;
391+ return dbi;
454void* sqlite3_bind_text_idr(void* p,const char* text, int index,int length){
394void* sqlite3_bind_text_idr(void* p,const char* text, int index,int length){
@@ -457,13 +397,22 @@ void* sqlite3_bind_text_idr(void* p,const char* text, int index,int length){
458 rc =sqlite3_bind_text(dbi->ppStmt,index,text,length,SQLITE_STATIC);
398 rc =sqlite3_bind_text(dbi->ppStmt,index,text,length,SQLITE_STATIC);
459 if(rc != SQLITE_OK){
399 if(rc != SQLITE_OK){
460− printf("Error in binding\n");
461 return NULL;
400 return NULL;
464 return dbi;
403 return dbi;
409+ Used for testing column count function.
410+ This function prepares query and by passing
411+ select all, gets the count for column number
412+ Could be used for testing. Not recommended to
413+ be used with prepare and exec
467int sqlite3_column_count_idr(void* db, const char* tbl_name){
416int sqlite3_column_count_idr(void* db, const char* tbl_name){
468 DBinfo* dbi =(DBinfo*) db;
417 DBinfo* dbi =(DBinfo*) db;
469 sqlite3_stmt* stmt;
418 sqlite3_stmt* stmt;
@@ -501,6 +450,11 @@ int sqlite3_data_count_idr(void* db){
455+ Must be called after prepare to clean up
504int sqlite3_finalize_idr(void* db){
458int sqlite3_finalize_idr(void* db){
506 DBinfo* dbi=(DBinfo*) db;
460 DBinfo* dbi=(DBinfo*) db;
@@ -515,6 +469,11 @@ int sqlite3_complete_idr(const char *sql){
474+ Resets a prepared statement pointer
475+ to its initial state
518int sqlite3_reset_idr(void* db){
477int sqlite3_reset_idr(void* db){
520 DBinfo* dbi=(DBinfo*) db;
479 DBinfo* dbi=(DBinfo*) db;
@@ -523,6 +482,11 @@ int sqlite3_reset_idr(void* db){
486+ The following routines may be used to
487+ obtain column related information.
526const char *sqlite3_column_name_idr(void* db, int N){
490const char *sqlite3_column_name_idr(void* db, int N){
528 DBinfo* dbi=(DBinfo*) db;
492 DBinfo* dbi=(DBinfo*) db;
@@ -568,7 +532,9 @@ int sqlite3_column_int_idr(void* db, int iCol){
568 return res;
532 return res;
536+ Some back up functions
573void* sqlite3_backup_init_idr(void* pDest,
539void* sqlite3_backup_init_idr(void* pDest,
574 const char *zDestName,
540 const char *zDestName,
@@ -596,10 +562,6 @@ int sqlite3_backup_step_idr(void *backup, int nPage){
597 DBbackup* dbi=(DBbackup*) backup;
563 DBbackup* dbi=(DBbackup*) backup;
598 int res = sqlite3_backup_step(dbi->backup, nPage);
564 int res = sqlite3_backup_step(dbi->backup, nPage);
600− if( res == SQLITE_OK){
601− printf("Backup Success\n");
603 return res;
565 return res;
@@ -629,22 +591,11 @@ int sqlite3_backup_pagecount_idr(void *backup){
632−int sqlite3_value_bytes_idr(void* value){
594+/*
633−
595+ Get the length of string
634− DBinfo* dbi=(DBinfo*) value;
596+ Need this in Idris since length will not
635− int res = sqlite3_value_bytes(dbi->value);
597+ work on Strings.
640−int sqlite3_value_bytes16_idr(void* value){
642− DBinfo* dbi=(DBinfo*) value;
643− int res =sqlite3_value_bytes16(dbi->value);
648int strLength(const char * str){
599int strLength(const char * str){
650 int length = strlen(str);
601 int length = strlen(str);