on 02de034 help sign in
543 lines 11 kB history raw
1/*
2 * sqlite3api.c
3 *
4 *
5 * Created by Melissa Farinaz MOZIFIAN on 22/06/2012.
6 * Copyright 2012. All rights reserved.
7 *
8 */
11#include "sqlite3api.h"
14// buffer used for storing queries
15// Used in high-level functions.
16static char sql_query_buffer[2000];
17// array used to store the string
18//value obtain by get_val_text
19unsigned char* array;
20/*
21 open an SQLite database file as
22 specified by the filename argument.
23 Returns pointer to DB struct on success
24 Null on failure.
25 */
26void* sqlite3_open_idr(const char *filename){
28 sqlite3 *db;
29 int res =sqlite3_open(filename,&db);
31 if(res != SQLITE_OK){
32 printf("Error occured while opening the databse.");
33 return NULL;
34 }
35 DBinfo *dbi = malloc(sizeof(DBinfo));
36 dbi->db_ptr = db;
37 return dbi;
40/*
41 Frees the resource and returns 0 on success
42 */
43int sqlite3_close_idr(void* db){
45 DBinfo* dbi =(DBinfo*) db;
46 int res =sqlite3_close(dbi->db_ptr);
47 if (res == SQLITE_OK){
48 free(dbi);
49 return 0;
50 }
51 else {
52 return res;
53 }
55/*
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.
60 */
61int sqlite3_exec_idr(void* db, const char *sql)
63 DBinfo* dbi =(DBinfo*) db;
64 char* err;
65 int rc;
66 rc = sqlite3_exec(dbi->db_ptr,sql,NULL, NULL, &err);
67 if (rc != SQLITE_OK && err != NULL) {
68 strncpy(dbi->buffer, err, sizeof(dbi->buffer));
69 sqlite3_free(err);
70 }
71 return rc;
73/*
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.
77 */
78char* sqlite3_get_error(void* db) {
79 DBinfo* dbi =(DBinfo*) db;
80 return dbi->buffer;
82/*
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.
86 */
87void* sqlite3_prepare_idr(void *db,const char *zSql){
88 sqlite3_stmt* stmt;
89 const char *tail;
91 DBinfo* dbi =(DBinfo*) db;
93 int rec = sqlite3_prepare_v2(dbi->db_ptr,zSql,-1,&stmt,&tail);
94 dbi ->ppStmt =stmt;
95 dbi ->Ptr_tail = tail;
97 if(rec != SQLITE_OK){
98 return NULL;
99 }
100 return dbi;
102/*
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
107 library.
108 */
109void* sqlite3_get_table_idr(void* db,
110 const char *sql){
112 DBinfo* dbi =(DBinfo*) db;
113 char* err;
115 Table* tbl = malloc(sizeof(Table));
116 tbl->database = dbi;
117 int res = sqlite3_get_table(dbi->db_ptr,sql,&tbl->table_data,&tbl->num_row,&tbl->num_col,&err);
118 int array_size = sizeof(&tbl->table_data);
120 if( res != SQLITE_OK && err != NULL){
121 strncpy(dbi->buffer, err, sizeof(dbi->buffer));
122 sqlite3_free(err);
123 return NULL;
124 }
125 tbl -> data_size = array_size;
126 return tbl;
129/*
130 This function executes queries.
131 This can be used after preparing a query
132 In case of error or library misuse
133 it returns 1.
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.
138 */
139int exec_db(void*p){
141 DBinfo* dbi =(DBinfo*) p;
142 int rc, col, row_counter;
144 const char* col_name;
146 rc = sqlite3_step(dbi->ppStmt);
148 if( rc == SQLITE_DONE){
149 return rc;
150 }
151 if(rc == SQLITE_ERROR && rc == SQLITE_MISUSE){
152 return 1;
153 }
154 row_counter =0;
156 while (rc == SQLITE_ROW) {
158 rc = sqlite3_step(dbi->ppStmt);
159 row_counter++;
160 }
162 col = sqlite3_column_count(dbi->ppStmt);
163 dbi->row_count = row_counter;
164 dbi->col_count = col;
165 return rc;
167/*
168 Returns row number from DB Struct
170 */
171int sqlite3_get_num_row_v2(void* p){
173 DBinfo* dbi =(DBinfo*) p;
174 int row_number =dbi->row_count;
175 return row_number;
178/*
179 Returns column number from DB Struct
180 */
181int sqlite3_get_num_col_v2(void* p){
183 DBinfo* dbi =(DBinfo*) p;
184 int col_number =dbi-> col_count;
185 return col_number;
188/*
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
195 */
196int sqlite3_get_num_row(void* p){
198 Table* tbl =(Table*) p;
199 int row_number =tbl->num_row;
200 return row_number;
203/*
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
210 */
211int sqlite3_get_num_col(void* p){
213 Table* tbl =(Table*) p;
214 int col_number =tbl-> num_col;
215 return col_number;
218/*
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
225 to get the type
226*/
227int sqlite3_get_data_type(void* p, int nRow, int nCol){
229 DBinfo* dbi =(DBinfo*) p;
230 int rc, type, row_counter;
231 const char* char_int;
233 rc = sqlite3_reset(dbi->ppStmt);
234 rc = sqlite3_step(dbi->ppStmt);
235 row_counter =0;
237 while (rc == SQLITE_ROW && row_counter < nRow) {
239 rc = sqlite3_step(dbi->ppStmt);
240 row_counter++;
241 }
242 type =sqlite3_column_type(dbi->ppStmt, nCol);
243 return type;
246/*
247 Obtains the integer value in a given column
248*/
250int sqlite3_get_val_int(void* p, int nCol){
252 DBinfo* dbi =(DBinfo*) p;
253 int val, col;
254 val =sqlite3_column_int(dbi->ppStmt, nCol);
255 return val;
258/*
259 Obtains the text value
260 Need to allocate memory to store the string
261 Use GC_malloc since Boehm garbage collector
262 frees the resources .
263 */
264const unsigned char* sqlite3_get_val_text(void* p,int nCol){
267 DBinfo* dbi =(DBinfo*) p;
268 int rc,i, val, counter;
269 const unsigned char* text_val;
270 array =(unsigned char *) malloc(1000*sizeof(char));
271 text_val =sqlite3_column_text(dbi->ppStmt, nCol);
272 memcpy(array, text_val, strlen(text_val));
273 return array;
276float sqlite3_get_float(void* p, int nCol){
278 DBinfo* dbi =(DBinfo*) p;
279 double double_val;
281 double_val =sqlite3_column_double(dbi->ppStmt, nCol);
282 float float_val =(float)double_val;
283 return float_val;
286/*
287 frees the pointer returned by get_table.
288 */
290void sqlite3_free_table_idr(void* db){
291 Table* tbl =(Table*) db;
292 sqlite3_free_table(tbl->table_data);
293 free(tbl);
295int sqlite3_step_idr(void* db){
297 DBinfo* dbi =(DBinfo*) db;
298 int rc =sqlite3_step(dbi->ppStmt);
299 return rc;
301/*
302 Binds integer. This returns a pointer
303 because of the implementation of BindMulti
304 which binds multiple values
305 */
306void* sqlite3_bind_int_idr(void* p,int index, int val){
308 DBinfo* dbi =(DBinfo*) p;
309 int rc;
311 rc =sqlite3_bind_int(dbi->ppStmt,index,val);
312 if(rc != SQLITE_OK){
313 return NULL;
314 }
316 return dbi;
319void* sqlite3_bind_float_idr(void* p,int index, float val){
321 DBinfo* dbi =(DBinfo*) p;
322 int rc;
323 double res =(float)val;
325 rc =sqlite3_bind_double(dbi->ppStmt,index,res);
327 if(rc != SQLITE_OK){
328 return NULL;
329 }
330 return dbi;
333void* sqlite3_bind_null_idr(void* p,int index){
335 DBinfo* dbi =(DBinfo*) p;
336 int rc;
337 rc =sqlite3_bind_null(dbi->ppStmt,index);
338 if(rc != SQLITE_OK){
339 return NULL;
340 }
342 return dbi;
345void* sqlite3_bind_text_idr(void* p,const char* text, int index,int length){
347 DBinfo* dbi =(DBinfo*) p;
348 int rc;
349 rc =sqlite3_bind_text(dbi->ppStmt,index,text,length,SQLITE_STATIC);
350 if(rc != SQLITE_OK){
351 return NULL;
352 }
354 return dbi;
356/*
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
362 */
364int sqlite3_column_count_idr(void* db, const char* tbl_name){
365 DBinfo* dbi =(DBinfo*) db;
366 sqlite3_stmt* stmt;
367 const char *tail;
368 int rc;
370 strcpy(sql_query_buffer, "select * from ");
371 strcat(sql_query_buffer, tbl_name);
373 rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
374 if(rc != SQLITE_OK){
375 fprintf(stderr, "SQL Prepare error");
376 return rc;
377 }
378 printf("Prepare successful %d\n", rc);
380 rc =sqlite3_column_count(stmt);
381 if(rc == 0){
382 fprintf(stderr, "SQL column count error\n");
383 return rc;
384 }
385 sqlite3_finalize(stmt);
387 // rc = actual column count
388 return rc;
391int sqlite3_data_count_idr(void* db){
393 DBinfo* dbi =(DBinfo*) db;
394 int rc = sqlite3_data_count(dbi->ppStmt);
396 return rc;
398/*
399 Must be called after prepare to clean up
400 the resources.
401 */
402int sqlite3_finalize_idr(void* db){
404 DBinfo* dbi=(DBinfo*) db;
405 int rc =sqlite3_finalize(dbi->ppStmt);
406 return rc;
409int sqlite3_complete_idr(const char *sql){
411 int rc = sqlite3_complete(sql);
412 return rc;
415/*
416 Resets a prepared statement pointer
417 to its initial state
418 */
419int sqlite3_reset_idr(void* db){
421 DBinfo* dbi=(DBinfo*) db;
422 int rc = sqlite3_reset(dbi-> ppStmt);
423 return rc;
426/*
427 The following routines may be used to
428 obtain column related information.
429 */
430const char *sqlite3_column_name_idr(void* db, int N){
432 DBinfo* dbi=(DBinfo*) db;
433 const char *name = sqlite3_column_name(dbi->ppStmt, N);
435 return name;
438const char *sqlite3_column_decltype_idr(void* db,int n){
439 DBinfo* dbi=(DBinfo*) db;
440 const char *dectype = sqlite3_column_decltype(dbi->ppStmt, n);
442 return dectype;
445int sqlite3_column_bytes_idr(void* db, int n){
447 DBinfo* dbi=(DBinfo*) db;
448 int res = sqlite3_column_bytes(dbi->ppStmt, n);
449 return res;
453const void *sqlite3_column_blob_idr(void* db, int iCol){
454 DBinfo* dbi=(DBinfo*) db;
455 const void* data =sqlite3_column_blob(dbi-> ppStmt, iCol);
456 return data;
459const unsigned char *sqlite3_column_text_idr(void* db, int iCol){
460 DBinfo* dbi=(DBinfo*) db;
461 const unsigned char* col_text =sqlite3_column_text(dbi->ppStmt, iCol);
462 return col_text;
466int sqlite3_column_int_idr(void* db, int iCol){
467 DBinfo* dbi=(DBinfo*) db;
468 int res =sqlite3_column_int(dbi-> ppStmt, iCol);
469 return res;
472/*
473 Some back up functions
474 */
476void* sqlite3_backup_init_idr(void* pDest,
477 const char *zDestName,
478 void* pSource,
479 const char *zSourceName
480 ){
482 DBinfo* dbi=(DBinfo*) pDest;
483 DBbackup* dbi2=(DBbackup*) pSource;
485 void* res = sqlite3_backup_init(dbi->db_ptr,zDestName,
486 dbi2->source_ptr,zSourceName);
488 if(res == NULL){
489 printf("Error number in initializing backup : %d\n", sqlite3_errcode(dbi->db_ptr));
490 }
492 dbi2->backup = res;
493 return dbi2;
498int sqlite3_backup_step_idr(void *backup, int nPage){
500 DBbackup* dbi=(DBbackup*) backup;
501 int res = sqlite3_backup_step(dbi->backup, nPage);
502 return res;
506int sqlite3_backup_finish_idr(void *backup){
508 DBbackup* dbi=(DBbackup*) backup;
509 int res = sqlite3_backup_finish(dbi->backup);
510 return res;
515int sqlite3_backup_remaining_idr(void *backup){
517 DBbackup* dbi=(DBbackup*) backup;
518 int res = sqlite3_backup_remaining(dbi->backup);
520 return res;
523int sqlite3_backup_pagecount_idr(void *backup){
525 DBbackup* dbi=(DBbackup*) backup;
526 int res =sqlite3_backup_pagecount(dbi-> backup);
527 return res;
530/*
531 Get the length of string
532 Need this in Idris since length will not
533 work on Strings.
534 */
535int strLength(const char * str){
537 int length = strlen(str);
538 return length;