on master help sign in
corrected doc indentation
efb2fa1melissam
contribs/lib/SQLite3/sqlite3.tex+46−16
@@ -11,14 +11,23 @@
11 \begin{document}
11 \begin{document}
12 \section{Introduction}
12 \section{Introduction}
13\label{Introduction}
13\label{Introduction}
14−This document provides overview of how the library for SQLite works. The code base is written on top of the SQLite3 C/C++ Interface. This library supports some features of the low-level library, implemented in a higher level basis.
14+This document provides overview of how the library for SQLite works. The code base is written on top of the SQLite3 C/C++ Interface.
15−The library allows making use of data types as queries. The aim of this library is to avoid SQL injections which are the most common security issues. To avoid this we have defined a datatype which describes qqueries. The queries are written using these data types and parsed as quoted string while replacing values with indexed question mark to be binded with the statement.
15+This library supports some features of the low-level library, implemented in a higher level basis.
16+The library allows making use of data types as queries. The aim of this library is to avoid
17+SQL injections which are the most common security issues.
18+To avoid this we have defined a datatype which describes qqueries.
19+The queries are written using these data types and parsed as quoted string
20+while replacing values with indexed question mark to be binded with the statement.
16\section{Using the library}
21\section{Using the library}
17\label{Using the library}
22\label{Using the library}
18Simple examples have been provided in the testunit.idr file.
23Simple examples have been provided in the testunit.idr file.
24+To open a database, use the open\_db function which takes the filename as a string.
25+This function implements the sqlite3\_open function .
19−To open a database, use the open\_db function which takes the filename as a string. This function implements the sqlite3\_open function . This function returns a pointer to the database which can be used by other function to access the database object.
26+This function returns a pointer to the database which can be used by other function to access the database object.
27+Whether or not an error occurs when it is opened,
20−Whether or not an error occurs when it is opened, resources should be released by passing it to close\_db when it is no longer required.
28+resources should be released by passing it to close\_db when it is no longer required.
29+A query can then be builts using the supported data types .
21−A query can then be builts using the supported data types . The library supports limited number of queries at the moment. The supported sql commands are :
30+The library supports limited number of queries at the moment. The supported sql commands are :
22\newline
31\newline
23\newline
32\newline
24\textbf{SELECT,}
33\textbf{SELECT,}
@@ -29,7 +38,8 @@ A query can then be builts using the supported data types . The library supports
29\subsection{Creating Queries}
38\subsection{Creating Queries}
30\label{Creating Queries}
39\label{Creating Queries}
31
40
32−To create a query, use the evalSQL function which takes an empty list and the SQL query. The routine evaluates the expression in a divide and conquer manner (or recursive?)
41+To create a query, use the evalSQL function which takes an empty list and the SQL query.
42+The routine evaluates the expression in a divide and conquer manner (or recursive?)
33filling up the list with the received values. For instance the following is a valid query:
43filling up the list with the received values. For instance the following is a valid query:
34
44
35let sql = evalSQL [ ] ((SELECT ALL)(TBL ["tbl1"]) (MkCond (MkNULL (VCol "data"))))
45let sql = evalSQL [ ] ((SELECT ALL)(TBL ["tbl1"]) (MkCond (MkNULL (VCol "data"))))
@@ -37,8 +47,10 @@ let sql = evalSQL [ ] ((SELECT ALL)(TBL ["tbl1"]) (MkCond (MkNULL (VCol "data"))
37\newline{This will be parsed as :}
47\newline{This will be parsed as :}
38\\“SELECT * FROM tbl1 WHERE data IS NULL “
48\\“SELECT * FROM tbl1 WHERE data IS NULL “
39
49
40−The evaluator returns a string and a list of indexed values. The string part of the query could be parsed to prepare.
50+The evaluator returns a string and a list of indexed values.
51+The string part of the query could be parsed to prepare.
41−evalSQL parses the query as a string replacing values with ? and an index starting from one, storing them in the list given to it as an argument.
52+evalSQL parses the query as a string replacing values with ? and an index starting from one,
53+storing them in the list given to it as an argument.
42For example:
54For example:
43
55
44
56
@@ -51,9 +63,14 @@ SELECT * FROM tbl1 WHERE data = ?1 AND num = ?2 OR num = ?3
51and returns the list as follows:
63and returns the list as follows:
52\newline{(Just (1, VStr "data0")) ,(Just (2, VInt 1)) , (Just (3, VInt 2))}
64\newline{(Just (1, VStr "data0")) ,(Just (2, VInt 1)) , (Just (3, VInt 2))}
53
65
66+The values in the list are binded to the statement using bindMulti function.
54−The values in the list are binded to the statement using bindMulti function. This function takes the statement pointer returned by prepare and the list returned by evalSQL, and binds the values by calling the appropriate functions.
67+This function takes the statement pointer returned by prepare and the list returned by evalSQL,
68+and binds the values by calling the appropriate functions.
55
69
70+The function exec\_db\_v2 executes the statement.
56−The function exec\_db\_v2 executes the statement. Note that the other version of exec\_db implements the sqlite3\_exec() interface which is a wrapper around sqlite3\_prepare\_v2(), sqlite3\_step(), and sqlite3\_finalize(), without having to use a lot of code, though the use of this function is not recommended in this case.
71+Note that the other version of exec\_db implements the sqlite3\_exec() interface which is a wrapper
72+around sqlite3\_prepare\_v2(), sqlite3\_step(), and sqlite3\_finalize(),
73+without having to use a lot of code, though the use of this function is not recommended in this case.
57
74
58The functions finalize\_db and close\_db must be called to clean up the open resources.
75The functions finalize\_db and close\_db must be called to clean up the open resources.
59To print the result, the function toList\_v1 can be used which takes a DBPointer.
76To print the result, the function toList\_v1 can be used which takes a DBPointer.
@@ -75,16 +92,27 @@ Use tricky examples of things that work and things that dont work
75\label{Evaluating Queries}
92\label{Evaluating Queries}
76\subsection{SQL Data Type}
93\subsection{SQL Data Type}
77\label{SQL Data Type}
94\label{SQL Data Type}
78−The SQL Data Types describe the queries that are supported by the library. Data type Value consists of Int, String and Float. The data type can be extended to further support queries. The evaulator function makes use of smaller function to evaulate parts of the query. For instance given a SELECT qeury, it calls evalSQL to obtain the list of tables, and clauseString to evaluate the rest of the expression which could include a WHERE clause or could be Empty.
95+The SQL Data Types describe the queries that are supported by the library.
96+Data type Value consists of Int, String and Float.
97+The data type can be extended to further support queries.
98+The evaulator function makes use of smaller function to evaulate parts of the query.
99+For instance given a SELECT qeury, it calls evalSQL to obtain the list of tables,
100+and clauseString to evaluate the rest of the expression which could include a WHERE clause or could be Empty.
79\subsection{evalSQL Function}
101\subsection{evalSQL Function}
80\label{evalSQL Function}
102\label{evalSQL Function}
103+This is a recursive evaluator which makes a call to itself.
104+An SQL could consist of a SELECT ALL or Columns followed by another SQL
105+which could be a nested query or just the name of the table(s).
106+It then call the appropriate functions to evaluate the query.
81−This is a recursive evaluator which makes a call to itself. An SQL could consist of a SELECT ALL or Columns followed by another SQL which could be a nested query or just the name of the table(s). It then call the appropriate functions to evaluate the query. It returns the well-quoted String expression and a list of indexed values which are passed to the bind function.
107+It returns the well-quoted String expression and a list of indexed values which are passed to the bind function.
82\section{Pointers passed to Idris functions}
108\section{Pointers passed to Idris functions}
83\label{Pointers passed to Idris functions}
109\label{Pointers passed to Idris functions}
84
110
111+The library uses FFI. In order to make the pointers passed to functions more explicit,
85−The library uses FFI. In order to make the pointers passed to functions more explicit, we have defined them as data types. For instance open\_db takes a String and returns a DBPointer and prepare\_db
112+we have defined them as data types. For instance open\_db takes a String and returns a DBPointer and prepare\_db
86takes a DBPointer and returns a StmtPtr(Statemenet Pointer).
113takes a DBPointer and returns a StmtPtr(Statemenet Pointer).
114+Error handling is done using the \emph {DB a} data type which
87−Error handling is done using the \emph {DB a} data type which is either \emph {IO String} which indicates an error or \emph{ IO a}.
115+is either \emph {IO String} which indicates an error or \emph{ IO a}.
88To test the functions in main, you need to use runDB which takes a \emph{ DB a} and returns IO a.
116To test the functions in main, you need to use runDB which takes a \emph{ DB a} and returns IO a.
89\begin{center}
117\begin{center}
90\centering runDB : DB a $\rightarrow$ IO a
118\centering runDB : DB a $\rightarrow$ IO a
@@ -94,8 +122,10 @@ More examples on how to use the functions can be found in \emph{testunit.idr}
94\section{Using the C Functions}
122\section{Using the C Functions}
95\label{Using the C Functions}
123\label{Using the C Functions}
96
124
125+For example calling get\_table after prepare\_db could lead to
97−For example calling get\_table after prepare\_db could lead to segmentation fault since get\_table calls sqlite3\_prepare etc
126+segmentation fault since get\_table calls sqlite3\_prepare etc
127+Functions such as exec\_db and get\_table are wrapper functions
98−Functions such as exec\_db and get\_table are wrapper functions and calling these before finalizing a prepared query could cause segmentation fault.
128+and calling these before finalizing a prepared query could cause segmentation fault.
99Perhaps these functions could be removed from the library if no longer required in the future.
129Perhaps these functions could be removed from the library if no longer required in the future.
100
130
101\end{document}
131\end{document}