on master help sign in
tidying up the code
commitfa011738855c39bc8f2234075b7a84f9c61be637 authormelissam <mfm6@st-andrews.ac.uk> dateFri 10 Aug 2012, 16:48 (14 years ago) parentb27e414
3 files changed+36−84
contribs/lib/SQLite3/._sqlite3api.c contribs/lib/SQLite3/sqlite3.tex+34−18 contribs/lib/SQLite3/sqlite3api.c+2−66
contribs/lib/SQLite3/._sqlite3api.c

Binary file not shown.

contribs/lib/SQLite3/sqlite3.tex+34−18
@@ -11,7 +11,7 @@
1111 \begin{document}
1212 \section{Introduction}
1313\label{Introduction}
14−This document provides overview of how the library for SQLite works. The code base is written on top of the
14+This document provides overview on how the library for SQLite works. The code base is written on top of the
1515SQLite3 C/C++ Interface.
1616This library supports some features of the low-level library, implemented in a higher level basis.
1717The library allows making use of data types as queries. The aim of this library is to avoid
@@ -66,7 +66,7 @@ and returns the list as follows:
6666\newline{(Just (1, VStr "data0")) ,(Just (2, VInt 1)) , (Just (3, VInt 2))}
6767
6868The values in the list are binded to the statement using bindMulti function.
69−This function takes the statement pointer returned by prepare and the list returned by evalSQL,
69+This function takes the statement pointer returned by prepare and the list returned by evalSQL
7070and binds the values by calling the appropriate functions.
7171
7272The function exec\_db\_v2 executes the statement.
@@ -75,7 +75,8 @@ around sqlite3\_prepare\_v2(), sqlite3\_step(), and sqlite3\_finalize(),
7575without having to use a lot of code, though the use of this function is not recommended in this case.
7676
7777The functions finalize\_db and close\_db must be called to clean up the open resources.
78−To print the result, the function toList\_v1 can be used which takes a DBPointer.
78+To print the result, the function toList\_v1 can be used which takes a DBPointer and returns
79+the resulting table.
7980
8081\subsection{More Examples}
8182\label{More Examples}
@@ -88,18 +89,26 @@ Parsed as :
8889SELECT data FROM (SELECT num data FROM tbl1 WHERE num = ?1)
8990
9091Note that in this example, the inner query must include the same column as the outer one otherwise this query will not validate.
91−Use tricky examples of things that work and things that dont work
9292
93−\section{Evaluating Queries}
94−\label{Evaluating Queries}
9593\subsection{SQL Data Type}
9694\label{SQL Data Type}
9795The SQL Data Types describe the queries that are supported by the library.
96+The implementation of these datatypes is similar to defining a grammar.
9897Data type Value consists of Int, String and Float.
9998The data type can be extended to further support queries.
10099The evaulator function makes use of smaller function to evaulate parts of the query.
101−For instance given a SELECT qeury, it calls evalSQL to obtain the list of tables,
100+For instance given a SELECT qeury, it calls \emph{evalSQL} to obtain the list of tables,
102101and clauseString to evaluate the rest of the expression which could include a WHERE clause or could be Empty.
102+
103+
104+\section{Evaluating Queries}
105+\label{Evaluating Queries}
106+The method used here is a divide and conquer manner which divides the query into smaller
107+parts and deals with each part and puts the results together to return a String representation
108+of the query and a list of values.
109+However this could result to a slow type checking process since it needs to go through all
110+the possibilities to type check. Maybe this can be improved?
111+
103112\subsection{evalSQL Function}
104113\label{evalSQL Function}
105114This is a recursive evaluator which makes a call to itself.
@@ -110,12 +119,13 @@ It returns the well-quoted String expression and a list of indexed values which
110119\section{Pointers passed to Idris functions}
111120\label{Pointers passed to Idris functions}
112121
113−The library uses FFI. In order to make the pointers passed to functions more explicit,
114−we have defined them as data types. For instance open\_db takes a String and returns a DBPointer and prepare\_db
122+The library uses FFI. In order to make the pointers passed to each function more explicit,
123+we have defined them as data types. For instance open\_db takes a String and returns a DBPointer
124+(Database Pointer) and prepare\_db
115125takes a DBPointer and returns a StmtPtr(Statemenet Pointer).
116126Error handling is done using the \emph {DB a} data type which
117−is either \emph {IO String} which indicates an error or \emph{ IO a}.
118−To test the functions in main, you need to use runDB which takes a \emph{ DB a} and returns IO a.
127+is either \emph {IO String} indicating an error or \emph{ IO a}.
128+To test the functions in main, you need to use runDB :
119129\begin{center}
120130\centering runDB : DB a $\rightarrow$ IO a
121131\end{center}
@@ -123,11 +133,17 @@ More examples on how to use the functions can be found in \emph{testunit.idr}
123133
124134\section{Using the C Functions}
125135\label{Using the C Functions}
126−
127−For example calling get\_table after prepare\_db could lead to
128−segmentation fault since get\_table calls sqlite3\_prepare etc
129−Functions such as exec\_db and get\_table are wrapper functions
130−and calling these before finalizing a prepared query could cause segmentation fault.
131−Perhaps these functions could be removed from the library if no longer required in the future.
132−
136+The library includes some wrapper functions such as get\_table
137+and exec\_db. Using these functions mixed with prepare, step or finalize
138+inappropriately could cause segmentation faults.
139+Perhaps these functions could be removed from the library if no longer required in the future
140+since the main purpose of this library is to make use of data types, or perhaps
141+these functionalities could be implemented internally without calling
142+the C Interface wrapper functions.
143+
144+\section{Further improving the library}
145+\label{Further improving the library}
146+There are many features that could be added to this library. These include adding more useful SQL Commands,
147+extending the SQL datatype to support more complex queries and possibly improving \emph{evalSQL}
148+for a faster performance.
133149\end{document}
contribs/lib/SQLite3/sqlite3api.c+2−66
@@ -17,7 +17,6 @@ static char sql_query_buffer[2000];
1717// array used to store the string
1818//value obtain by get_val_text
1919unsigned char* array;
20−
2120/*
2221 open an SQLite database file as
2322 specified by the filename argument.
@@ -38,7 +37,6 @@ void* sqlite3_open_idr(const char *filename){
3837 return dbi;
3938
4039}
41−
4240/*
4341 Frees the resource and returns 0 on success
4442 */
@@ -50,12 +48,10 @@ int sqlite3_close_idr(void* db){
5048 free(dbi);
5149 return 0;
5250 }
53−
5451 else {
5552 return res;
5653 }
5754}
58−
5955/*
6056 SQLite wrapper around sqlite3_prepare_v2(),
6157 sqlite3_step(), and sqlite3_finalize().
@@ -64,11 +60,9 @@ int sqlite3_close_idr(void* db){
6460 */
6561int sqlite3_exec_idr(void* db, const char *sql)
6662{
67−
6863 DBinfo* dbi =(DBinfo*) db;
6964 char* err;
7065 int rc;
71−
7266 rc = sqlite3_exec(dbi->db_ptr,sql,NULL, NULL, &err);
7367 if (rc != SQLITE_OK && err != NULL) {
7468 strncpy(dbi->buffer, err, sizeof(dbi->buffer));
@@ -76,7 +70,6 @@ int sqlite3_exec_idr(void* db, const char *sql)
7670 }
7771 return rc;
7872}
79−
8073/*
8174 Gets the error store in the buffer
8275 in the struct. Some certain functions
@@ -86,15 +79,12 @@ char* sqlite3_get_error(void* db) {
8679 DBinfo* dbi =(DBinfo*) db;
8780 return dbi->buffer;
8881}
89−
90−
9182/*
9283 Compiles the query into a byte-code program
9384 Returns a pointer to the sqlite3_stmt pointer
9485 and stores it in the struct.
9586 */
9687void* sqlite3_prepare_idr(void *db,const char *zSql){
97−
9888 sqlite3_stmt* stmt;
9989 const char *tail;
10090
@@ -107,10 +97,8 @@ void* sqlite3_prepare_idr(void *db,const char *zSql){
10797 if(rec != SQLITE_OK){
10898 return NULL;
10999 }
110−
111100 return dbi;
112101}
113−
114102/*
115103 Another wrapper interface that is preserved
116104 for backwards compatibility.
@@ -118,7 +106,6 @@ void* sqlite3_prepare_idr(void *db,const char *zSql){
118106 This was mainly used for testing within the
119107 library.
120108 */
121−
122109void* sqlite3_get_table_idr(void* db,
123110 const char *sql){
124111
@@ -127,9 +114,7 @@ void* sqlite3_get_table_idr(void* db,
127114
128115 Table* tbl = malloc(sizeof(Table));
129116 tbl->database = dbi;
130−
131117 int res = sqlite3_get_table(dbi->db_ptr,sql,&tbl->table_data,&tbl->num_row,&tbl->num_col,&err);
132−
133118 int array_size = sizeof(&tbl->table_data);
134119
135120 if( res != SQLITE_OK && err != NULL){
@@ -137,16 +122,11 @@ void* sqlite3_get_table_idr(void* db,
137122 sqlite3_free(err);
138123 return NULL;
139124 }
140−
141125 tbl -> data_size = array_size;
142− return tbl;
143−
126+ return tbl;
144127}
145128
146−
147−
148129/*
149−
150130 This function executes queries.
151131 This can be used after preparing a query
152132 In case of error or library misuse
@@ -171,7 +151,6 @@ int exec_db(void*p){
171151 if(rc == SQLITE_ERROR && rc == SQLITE_MISUSE){
172152 return 1;
173153 }
174−
175154 row_counter =0;
176155
177156 while (rc == SQLITE_ROW) {
@@ -184,7 +163,6 @@ int exec_db(void*p){
184163 dbi->row_count = row_counter;
185164 dbi->col_count = col;
186165 return rc;
187−
188166}
189167/*
190168 Returns row number from DB Struct
@@ -195,12 +173,9 @@ int sqlite3_get_num_row_v2(void* p){
195173 DBinfo* dbi =(DBinfo*) p;
196174 int row_number =dbi->row_count;
197175 return row_number;
198−
199−
200176}
201177
202178/*
203−
204179 Returns column number from DB Struct
205180 */
206181int sqlite3_get_num_col_v2(void* p){
@@ -208,11 +183,9 @@ int sqlite3_get_num_col_v2(void* p){
208183 DBinfo* dbi =(DBinfo*) p;
209184 int col_number =dbi-> col_count;
210185 return col_number;
211−
212186}
213187
214188/*
215−
216189 Another way of obtaining row number
217190 Thought this function gets the value
218191 from Table struct. The value is stored
@@ -225,12 +198,9 @@ int sqlite3_get_num_row(void* p){
225198 Table* tbl =(Table*) p;
226199 int row_number =tbl->num_row;
227200 return row_number;
228−
229−
230201}
231202
232203/*
233−
234204 Another way of obtaining column number
235205 Thought this function gets the value
236206 from Table struct. The value is stored
@@ -238,13 +208,11 @@ int sqlite3_get_num_row(void* p){
238208 Using this version is not recommended
239209 unless used with get_table
240210 */
241−
242211int sqlite3_get_num_col(void* p){
243212
244213 Table* tbl =(Table*) p;
245214 int col_number =tbl-> num_col;
246− return col_number;
247−
215+ return col_number;
248216}
249217
250218/*
@@ -255,7 +223,6 @@ int sqlite3_get_num_col(void* p){
255223 set the pointer to its initial state
256224 and then calls sqlite3_column_type
257225 to get the type
258−
259226*/
260227int sqlite3_get_data_type(void* p, int nRow, int nCol){
261228
@@ -277,24 +244,18 @@ int sqlite3_get_data_type(void* p, int nRow, int nCol){
277244
278245}
279246/*
280−
281247 Obtains the integer value in a given column
282248*/
283249
284250int sqlite3_get_val_int(void* p, int nCol){
285251
286−
287252 DBinfo* dbi =(DBinfo*) p;
288253 int val, col;
289−
290254 val =sqlite3_column_int(dbi->ppStmt, nCol);
291255 return val;
292−
293−
294256}
295257
296258/*
297−
298259 Obtains the text value
299260 Need to allocate memory to store the string
300261 Use GC_malloc since Boehm garbage collector
@@ -306,16 +267,12 @@ const unsigned char* sqlite3_get_val_text(void* p,int nCol){
306267 DBinfo* dbi =(DBinfo*) p;
307268 int rc,i, val, counter;
308269 const unsigned char* text_val;
309−
310270 array =(unsigned char *) GC_malloc(1000*sizeof(char));
311−
312271 text_val =sqlite3_column_text(dbi->ppStmt, nCol);
313272 memcpy(array, text_val, strlen(text_val));
314−
315273 return array;
316274
317275}
318−
319276float sqlite3_get_float(void* p, int nCol){
320277
321278 DBinfo* dbi =(DBinfo*) p;
@@ -326,7 +283,6 @@ float sqlite3_get_float(void* p, int nCol){
326283 return float_val;
327284
328285}
329−
330286/*
331287 frees the pointer returned by get_table.
332288 */
@@ -336,16 +292,12 @@ void sqlite3_free_table_idr(void* db){
336292 sqlite3_free_table(tbl->table_data);
337293 free(tbl);
338294}
339−
340−
341295int sqlite3_step_idr(void* db){
342296
343297 DBinfo* dbi =(DBinfo*) db;
344298 int rc =sqlite3_step(dbi->ppStmt);
345299 return rc;
346300}
347−
348−
349301/*
350302 Binds integer. This returns a pointer
351303 because of the implementation of BindMulti
@@ -375,7 +327,6 @@ void* sqlite3_bind_float_idr(void* p,int index, float val){
375327 if(rc != SQLITE_OK){
376328 return NULL;
377329 }
378−
379330 return dbi;
380331}
381332
@@ -401,10 +352,7 @@ void* sqlite3_bind_text_idr(void* p,const char* text, int index,int length){
401352 }
402353
403354 return dbi;
404−
405355}
406−
407−
408356/*
409357 Used for testing column count function.
410358 This function prepares query and by passing
@@ -440,17 +388,13 @@ int sqlite3_column_count_idr(void* db, const char* tbl_name){
440388 return rc;
441389}
442390
443−
444391int sqlite3_data_count_idr(void* db){
445392
446393 DBinfo* dbi =(DBinfo*) db;
447394 int rc = sqlite3_data_count(dbi->ppStmt);
448395
449396 return rc;
450−
451397}
452−
453−
454398/*
455399 Must be called after prepare to clean up
456400 the resources.
@@ -466,8 +410,6 @@ int sqlite3_complete_idr(const char *sql){
466410
467411 int rc = sqlite3_complete(sql);
468412 return rc;
469−
470−
471413}
472414
473415/*
@@ -481,12 +423,10 @@ int sqlite3_reset_idr(void* db){
481423 return rc;
482424
483425}
484−
485426/*
486427 The following routines may be used to
487428 obtain column related information.
488429 */
489−
490430const char *sqlite3_column_name_idr(void* db, int N){
491431
492432 DBinfo* dbi=(DBinfo*) db;
@@ -502,8 +442,6 @@ const char *sqlite3_column_decltype_idr(void* db,int n){
502442 return dectype;
503443
504444}
505−
506−
507445int sqlite3_column_bytes_idr(void* db, int n){
508446
509447 DBinfo* dbi=(DBinfo*) db;
@@ -512,7 +450,6 @@ int sqlite3_column_bytes_idr(void* db, int n){
512450
513451
514452}
515−
516453const void *sqlite3_column_blob_idr(void* db, int iCol){
517454 DBinfo* dbi=(DBinfo*) db;
518455 const void* data =sqlite3_column_blob(dbi-> ppStmt, iCol);
@@ -590,7 +527,6 @@ int sqlite3_backup_pagecount_idr(void *backup){
590527 return res;
591528
592529}
593−
594530/*
595531 Get the length of string
596532 Need this in Idris since length will not