on master help sign in
commented C code and got rid of some unwated functions
commitb27e41418bc38c78f8942f8c80fa518d7e459e6b authormelissam <mfm6@st-andrews.ac.uk> dateFri 10 Aug 2012, 12:53 (14 years ago) parent8011e70
6 files changed+175−230
contribs/lib/SQLite3/._sqlite3.idr contribs/lib/SQLite3/._sqlite3api.c contribs/lib/SQLite3/._sqlite3api.h contribs/lib/SQLite3/sqlite3.idr+12−9 contribs/lib/SQLite3/sqlite3api.c+150−199 contribs/lib/SQLite3/sqlite3api.h+13−22
contribs/lib/SQLite3/._sqlite3.idr

Binary file not shown.

contribs/lib/SQLite3/._sqlite3api.c

Binary file not shown.

contribs/lib/SQLite3/._sqlite3api.h

Binary file not shown.

contribs/lib/SQLite3/sqlite3.idr+12−9
@@ -23,21 +23,21 @@ data DBVal = DBInt Int
2323 | DBFloat Float
2424 | DBNull
2525
26−
26+-- Database pointer
2727data DBPointer = MkDBPointer Ptr
28−
28+-- Table pointer
2929data TBPointer = MkTBPointer Ptr
30−
30+-- Statement pointer
3131data StmtPtr = MkStmtPtr Ptr
3232
3333Table : Set
3434Table = List (List DBVal)
3535
36−
36+-- ForMap used in to_list_v1
3737forM : Monad m => List a -> (a -> m b) -> m (List b)
3838forM f xs = mapM xs f
3939
40−
40+-- Neater error handling done using DB
4141data DB a = MkDB (IO (Either String a))
4242
4343instance Monad DB where
@@ -275,18 +275,18 @@ get_data (MkDBPointer pointer) row col = do
275275 helper ty
276276 where
277277 get_data_type : Ptr ->Int -> Int -> IO Int
278− get_data_type pointer row col =mkForeign (FFun "sqlite3_get_data_type_v2" [FPtr, FInt, FInt] FInt)pointer row col
278+ get_data_type pointer row col =mkForeign (FFun "sqlite3_get_data_type" [FPtr, FInt, FInt] FInt)pointer row col
279279
280280 get_data_val_int : Ptr -> Int -> IO Int
281− get_data_val_int pointer col =mkForeign (FFun "sqlite3_get_val_int_v2" [FPtr,FInt] FInt)pointer col
281+ get_data_val_int pointer col =mkForeign (FFun "sqlite3_get_val_int" [FPtr,FInt] FInt)pointer col
282282
283283 get_data_val_text : Ptr -> Int -> IO String
284− get_data_val_text pointer col =mkForeign (FFun "sqlite3_get_val_text_v2" [FPtr, FInt] FString)pointer col
284+ get_data_val_text pointer col =mkForeign (FFun "sqlite3_get_val_text" [FPtr, FInt] FString)pointer col
285285
286286
287287
288288 get_data_val_float : Ptr -> Int -> IO Float
289− get_data_val_float pointer col =mkForeign (FFun "sqlite3_get_float_v2" [FPtr, FInt] FFloat)pointer col
289+ get_data_val_float pointer col =mkForeign (FFun "sqlite3_get_float" [FPtr, FInt] FFloat)pointer col
290290
291291 helper : Int -> IO DBVal
292292 helper 1 = do i <- get_data_val_int pointer col ; return (DBInt i)
@@ -316,6 +316,7 @@ strcat str1 str2 = (str1 ++ str2)
316316
317317-----------------------------------------------------------------------------
318318-- | This version of toList retunrs result from get_table
319+-- This could be removed if not needed in the future.
319320-----------------------------------------------------------------------------
320321
321322--toList_v2 : String -> String -> DBPointer -> DB Table
@@ -335,6 +336,8 @@ strcat str1 str2 = (str1 ++ str2)
335336
336337-----------------------------------------------------------------------------
337338-- | These routines support binding the types supported by Idris.
339+-- These return a pointer to statement pointer since we want to allow
340+-- binding multiple values by implementing a recursive function.
338341-----------------------------------------------------------------------------
339342
340343bind_int : StmtPtr -> Int -> Int -> DB StmtPtr
contribs/lib/SQLite3/sqlite3api.c+150−199
@@ -11,10 +11,19 @@
1111#include "sqlite3api.h"
1212
1313
14−
14+// buffer used for storing queries
15+// Used in high-level functions.
1516static char sql_query_buffer[2000];
17+// array used to store the string
18+//value obtain by get_val_text
1619unsigned char* array;
1720
21+/*
22+ open an SQLite database file as
23+ specified by the filename argument.
24+ Returns pointer to DB struct on success
25+ Null on failure.
26+ */
1827void* sqlite3_open_idr(const char *filename){
1928
2029 sqlite3 *db;
@@ -30,26 +39,29 @@ void* sqlite3_open_idr(const char *filename){
3039
3140}
3241
42+/*
43+ Frees the resource and returns 0 on success
44+ */
3345int sqlite3_close_idr(void* db){
3446
3547 DBinfo* dbi =(DBinfo*) db;
3648 int res =sqlite3_close(dbi->db_ptr);
3749 if (res == SQLITE_OK){
3850 free(dbi);
39− printf("Successfuly closed the databse\n");
4051 return 0;
4152 }
4253
43− if(res == SQLITE_BUSY){
44− printf("SQL Busy");
45− return 5;
46− }
47−
4854 else {
49− return 1;
55+ return res;
5056 }
5157}
5258
59+/*
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.
64+ */
5365int sqlite3_exec_idr(void* db, const char *sql)
5466{
5567
@@ -58,7 +70,6 @@ int sqlite3_exec_idr(void* db, const char *sql)
5870 int rc;
5971
6072 rc = sqlite3_exec(dbi->db_ptr,sql,NULL, NULL, &err);
61− printf("%d\n", rc);
6273 if (rc != SQLITE_OK && err != NULL) {
6374 strncpy(dbi->buffer, err, sizeof(dbi->buffer));
6475 sqlite3_free(err);
@@ -66,12 +77,22 @@ int sqlite3_exec_idr(void* db, const char *sql)
6677 return rc;
6778}
6879
69−
80+/*
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.
84+ */
7085char* sqlite3_get_error(void* db) {
7186 DBinfo* dbi =(DBinfo*) db;
7287 return dbi->buffer;
7388}
7489
90+
91+/*
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.
95+ */
7596void* sqlite3_prepare_idr(void *db,const char *zSql){
7697
7798 sqlite3_stmt* stmt;
@@ -90,6 +111,14 @@ void* sqlite3_prepare_idr(void *db,const char *zSql){
90111 return dbi;
91112}
92113
114+/*
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
119+ library.
120+ */
121+
93122void* sqlite3_get_table_idr(void* db,
94123 const char *sql){
95124
@@ -114,6 +143,19 @@ void* sqlite3_get_table_idr(void* db,
114143
115144}
116145
146+
147+
148+/*
149+
150+ This function executes queries.
151+ This can be used after preparing a query
152+ In case of error or library misuse
153+ it returns 1.
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.
158+ */
117159int exec_db(void*p){
118160
119161 DBinfo* dbi =(DBinfo*) p;
@@ -144,7 +186,10 @@ int exec_db(void*p){
144186 return rc;
145187
146188}
147−
189+/*
190+ Returns row number from DB Struct
191+
192+ */
148193int sqlite3_get_num_row_v2(void* p){
149194
150195 DBinfo* dbi =(DBinfo*) p;
@@ -154,6 +199,10 @@ int sqlite3_get_num_row_v2(void* p){
154199
155200}
156201
202+/*
203+
204+ Returns column number from DB Struct
205+ */
157206int sqlite3_get_num_col_v2(void* p){
158207
159208 DBinfo* dbi =(DBinfo*) p;
@@ -162,7 +211,15 @@ int sqlite3_get_num_col_v2(void* p){
162211
163212}
164213
165−
214+/*
215+
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
222+ */
166223int sqlite3_get_num_row(void* p){
167224
168225 Table* tbl =(Table*) p;
@@ -172,6 +229,16 @@ int sqlite3_get_num_row(void* p){
172229
173230}
174231
232+/*
233+
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
240+ */
241+
175242int sqlite3_get_num_col(void* p){
176243
177244 Table* tbl =(Table*) p;
@@ -179,7 +246,18 @@ int sqlite3_get_num_col(void* p){
179246 return col_number;
180247
181248}
182−int sqlite3_get_data_type_v2(void* p, int nRow, int nCol){
249+
250+/*
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
257+ to get the type
258+
259+*/
260+int sqlite3_get_data_type(void* p, int nRow, int nCol){
183261
184262 DBinfo* dbi =(DBinfo*) p;
185263 int rc, type, row_counter;
@@ -198,9 +276,12 @@ int sqlite3_get_data_type_v2(void* p, int nRow, int nCol){
198276 return type;
199277
200278}
279+/*
280+
281+ Obtains the integer value in a given column
282+*/
201283
202−
203−int sqlite3_get_val_int_v2(void* p, int nCol){
284+int sqlite3_get_val_int(void* p, int nCol){
204285
205286
206287 DBinfo* dbi =(DBinfo*) p;
@@ -212,81 +293,14 @@ int sqlite3_get_val_int_v2(void* p, int nCol){
212293
213294}
214295
215−int sqlite3_get_data_type(void* p,const char* tbl_name, int nRow, int nCol){
216−
217− DBinfo* dbi =(DBinfo*) p;
218− sqlite3_stmt* stmt;
219− const char *tail;
220− int rc,i, val, counter;
221− const char* char_int;
222−
223− counter =0;
224− strcpy(sql_query_buffer, "select * from ");
225− strcat(sql_query_buffer, tbl_name);
226−
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");
230− return rc;
231− }
232− rc = sqlite3_step(stmt);
233−
234− while (rc == SQLITE_ROW && counter < nRow){
235−
236− rc = sqlite3_step(stmt);
237− counter++;
238−
239− }
240−
241− val =sqlite3_column_type(stmt, nCol);
242−
243− rc =sqlite3_finalize(stmt);
244− if( rc != SQLITE_OK){
245− printf("Couldnt finalize:\n");
246− }
247− return val;
248−
249−}
250−
251−
252−int sqlite3_get_val_int(void* p,const char* tbl_name, int nRow, int nCol){
253−
254−
255− DBinfo* dbi =(DBinfo*) p;
256− sqlite3_stmt* stmt;
257− const char *tail;
258− int rc,i, val, counter;
259− const char* char_int;
260−
261− counter =0;
262− strcpy(sql_query_buffer, "select * from ");
263− strcat(sql_query_buffer, tbl_name);
264−
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");
268− return rc;
269− }
270− rc = sqlite3_step(stmt);
271−
272− while (rc == SQLITE_ROW && counter < nRow){
273−
274− rc = sqlite3_step(stmt);
275− counter++;
276−
277− }
278−
279− val =sqlite3_column_int(stmt, nCol);
280− rc =sqlite3_finalize(stmt);
281− if( rc != SQLITE_OK){
282− printf("Couldnt finalize:\n");
283− }
284− return val;
285−
286−
287−}
288−
289−const unsigned char* sqlite3_get_val_text_v2(void* p,int nCol){
296+/*
297+
298+ Obtains the text value
299+ Need to allocate memory to store the string
300+ Use GC_malloc since Boehm garbage collector
301+ frees the resources .
302+ */
303+const unsigned char* sqlite3_get_val_text(void* p,int nCol){
290304
291305
292306 DBinfo* dbi =(DBinfo*) p;
@@ -302,88 +316,7 @@ const unsigned char* sqlite3_get_val_text_v2(void* p,int nCol){
302316
303317}
304318
305−
306−
307−const unsigned char* sqlite3_get_val_text(void* p,const char* tbl_name,int nRow, int nCol){
308−
309−
310− DBinfo* dbi =(DBinfo*) p;
311− sqlite3_stmt* stmt;
312− const char *tail;
313− int rc,i, val, counter;
314− const unsigned char* text_val;
315−
316− counter =0;
317− strcpy(sql_query_buffer, "select * from ");
318− strcat(sql_query_buffer, tbl_name);
319−
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");
323− return NULL;
324− }
325− rc = sqlite3_step(stmt);
326−
327− while (rc == SQLITE_ROW && counter < nRow){
328−
329− rc = sqlite3_step(stmt);
330− counter++;
331−
332− }
333−
334−
335− array =(unsigned char *) GC_malloc(1000*sizeof(char));
336−
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");
342− }
343−
344− return array;
345−
346−}
347−
348−
349−float sqlite3_get_float(void* p,const char* tbl_name, int nRow, int nCol){
350−
351− DBinfo* dbi =(DBinfo*) p;
352− sqlite3_stmt* stmt;
353− const char *tail;
354− int rc,i, val, counter;
355− double double_val;
356−
357−
358− counter =0;
359− strcpy(sql_query_buffer, "select * from ");
360− strcat(sql_query_buffer, tbl_name);
361−
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");
365−
366− }
367− rc = sqlite3_step(stmt);
368−
369− while (rc == SQLITE_ROW && counter < nRow){
370−
371−
372− rc = sqlite3_step(stmt);
373− counter++;
374−
375− }
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");
381− }
382− return float_val;
383−
384−}
385−
386−float sqlite3_get_float_v2(void* p, int nCol){
319+float sqlite3_get_float(void* p, int nCol){
387320
388321 DBinfo* dbi =(DBinfo*) p;
389322 double double_val;
@@ -394,6 +327,9 @@ float sqlite3_get_float_v2(void* p, int nCol){
394327
395328}
396329
330+/*
331+ frees the pointer returned by get_table.
332+ */
397333
398334void sqlite3_free_table_idr(void* db){
399335 Table* tbl =(Table*) db;
@@ -409,15 +345,19 @@ int sqlite3_step_idr(void* db){
409345 return rc;
410346}
411347
348+
349+/*
350+ Binds integer. This returns a pointer
351+ because of the implementation of BindMulti
352+ which binds multiple values
353+ */
412354void* sqlite3_bind_int_idr(void* p,int index, int val){
413355
414356 DBinfo* dbi =(DBinfo*) p;
415357 int rc;
416358
417− //printf("\nThe statement has %d wildcards\n", sqlite3_bind_parameter_count(dbi->ppStmt));
418359 rc =sqlite3_bind_int(dbi->ppStmt,index,val);
419360 if(rc != SQLITE_OK){
420− printf("Error in binding\n");
421361 return NULL;
422362 }
423363
@@ -439,16 +379,16 @@ void* sqlite3_bind_float_idr(void* p,int index, float val){
439379 return dbi;
440380}
441381
442−int sqlite3_bind_null_idr(void* p,int index){
382+void* sqlite3_bind_null_idr(void* p,int index){
443383
444384 DBinfo* dbi =(DBinfo*) p;
445385 int rc;
446386 rc =sqlite3_bind_null(dbi->ppStmt,index);
447387 if(rc != SQLITE_OK){
448− return 1;
388+ return NULL;
449389 }
450390
451− return rc;
391+ return dbi;
452392}
453393
454394void* 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){
457397 int rc;
458398 rc =sqlite3_bind_text(dbi->ppStmt,index,text,length,SQLITE_STATIC);
459399 if(rc != SQLITE_OK){
460− printf("Error in binding\n");
461400 return NULL;
462401 }
463402
464403 return dbi;
465404
466405}
406+
407+
408+/*
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
414+ */
415+
467416int sqlite3_column_count_idr(void* db, const char* tbl_name){
468417 DBinfo* dbi =(DBinfo*) db;
469418 sqlite3_stmt* stmt;
@@ -501,6 +450,11 @@ int sqlite3_data_count_idr(void* db){
501450
502451}
503452
453+
454+/*
455+ Must be called after prepare to clean up
456+ the resources.
457+ */
504458int sqlite3_finalize_idr(void* db){
505459
506460 DBinfo* dbi=(DBinfo*) db;
@@ -515,6 +469,11 @@ int sqlite3_complete_idr(const char *sql){
515469
516470
517471}
472+
473+/*
474+ Resets a prepared statement pointer
475+ to its initial state
476+ */
518477int sqlite3_reset_idr(void* db){
519478
520479 DBinfo* dbi=(DBinfo*) db;
@@ -523,6 +482,11 @@ int sqlite3_reset_idr(void* db){
523482
524483}
525484
485+/*
486+ The following routines may be used to
487+ obtain column related information.
488+ */
489+
526490const char *sqlite3_column_name_idr(void* db, int N){
527491
528492 DBinfo* dbi=(DBinfo*) db;
@@ -568,7 +532,9 @@ int sqlite3_column_int_idr(void* db, int iCol){
568532 return res;
569533
570534}
571−
535+/*
536+ Some back up functions
537+ */
572538
573539void* sqlite3_backup_init_idr(void* pDest,
574540 const char *zDestName,
@@ -596,10 +562,6 @@ int sqlite3_backup_step_idr(void *backup, int nPage){
596562
597563 DBbackup* dbi=(DBbackup*) backup;
598564 int res = sqlite3_backup_step(dbi->backup, nPage);
599−
600− if( res == SQLITE_OK){
601− printf("Backup Success\n");
602− }
603565 return res;
604566
605567
@@ -629,22 +591,11 @@ int sqlite3_backup_pagecount_idr(void *backup){
629591
630592}
631593
632−int sqlite3_value_bytes_idr(void* value){
633−
634− DBinfo* dbi=(DBinfo*) value;
635− int res = sqlite3_value_bytes(dbi->value);
636− return res;
637−
638−}
639−
640−int sqlite3_value_bytes16_idr(void* value){
641−
642− DBinfo* dbi=(DBinfo*) value;
643− int res =sqlite3_value_bytes16(dbi->value);
644− return res;
645−
646−}
647−
594+/*
595+ Get the length of string
596+ Need this in Idris since length will not
597+ work on Strings.
598+ */
648599int strLength(const char * str){
649600
650601 int length = strlen(str);
contribs/lib/SQLite3/sqlite3api.h+13−22
@@ -14,22 +14,25 @@
1414#include <gc.h>
1515
1616typedef struct {
17− sqlite3 *db_ptr;
18− sqlite3_stmt *ppStmt;
19− char buffer[1000];
17+ sqlite3 *db_ptr; // database pointer
18+ sqlite3_stmt *ppStmt; // statement pointer
19+ char buffer[1000]; // bufer to store errors returned by certain functions
2020 const char *Ptr_tail;
21− sqlite3_blob *blob_ptr;
2221 sqlite3_value *value;
23− int row_count;
22+ int row_count; // row number store by exec function
2423 int col_count;
2524
2625} DBinfo;
2726
27+// struct used for backup functions
2828typedef struct {
2929 sqlite3 *source_ptr;
3030 sqlite3_backup *backup;
3131}DBbackup ;
3232
33+// Table struct used by get_table function
34+// stores row and column
35+// returned by get_table
3336typedef struct {
3437 int num_row;
3538 int num_col;
@@ -50,9 +53,7 @@ int sqlite3_exec_idr(void*, const char *sql);
5053
5154char* sqlite3_get_error(void* db);
5255
53−const unsigned char* sqlite3_get_val_text(void* p,const char* tbl_name,int nRow, int nCol);
54−
55−const unsigned char* sqlite3_get_val_text_v2(void* p,int nCol);
56+const unsigned char* sqlite3_get_val_text(void* p,int nCol);
5657
5758void* sqlite3_get_table_idr(void* db, const char *sql);
5859
@@ -66,17 +67,11 @@ int sqlite3_get_num_row_v2(void* p);
6667
6768int sqlite3_get_num_col_v2(void* p);
6869
69−int sqlite3_get_data_type(void* p,const char* tbl_name,int nRow, int nCol);
70−
71−int sqlite3_get_data_type_v2(void* p, int nRow, int nCol);
72−
73−int sqlite3_get_val_int_v2(void* p,int nCo);
70+int sqlite3_get_data_type(void* p, int nRow, int nCol);
7471
75−int sqlite3_get_val_int(void* p,const char* tbl_name, int nRow, int nCol);
72+int sqlite3_get_val_int(void* p,int nCo);
7673
77−float sqlite3_get_float(void* p,const char* tbl_name, int nRow, int nCol);
78−
79−float sqlite3_get_float_v2(void* p, int nCol);
74+float sqlite3_get_float(void* p, int nCol);
8075
8176void* sqlite3_prepare_idr(
8277 void *db, /* Database handle */
@@ -89,7 +84,7 @@ void* sqlite3_bind_float_idr(void* p,int index, float val);
8984
9085void* sqlite3_bind_int_idr(void* p,int index , int val);
9186
92−int sqlite3_bind_null_idr(void* p,int index);
87+void* sqlite3_bind_null_idr(void* p,int index);
9388
9489void* sqlite3_bind_text_idr(void* p,const char* text, int index,int length);
9590
@@ -132,8 +127,4 @@ int sqlite3_backup_remaining_idr(void *backup);
132127
133128int sqlite3_backup_pagecount_idr(void *backup);
134129
135−int sqlite3_value_bytes_idr(void* value);
136−
137−int sqlite3_value_bytes16_idr(void* value);
138−
139130int strLength(const char * str);