on master help sign in
corrected doc indentation
commitefb2fa14c0ce4e13d0e44fe418535d4ea8bbba2e authormelissam <mfm6@st-andrews.ac.uk> dateThu 9 Aug 2012, 16:40 (14 years ago) parentb66020b
1 file changed+46−16
contribs/lib/SQLite3/sqlite3.tex+46−16
contribs/lib/SQLite3/sqlite3.tex+46−16
@@ -11,14 +11,23 @@
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 SQLite3 C/C++ Interface. This library supports some features of the low-level library, implemented in a higher level basis.
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.
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+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.
1621\section{Using the library}
1722\label{Using the library}
1823Simple examples have been provided in the testunit.idr file.
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.
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.
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 :
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 .
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,
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 .
30+The library supports limited number of queries at the moment. The supported sql commands are :
2231\newline
2332\newline
2433\textbf{SELECT,}
@@ -29,7 +38,8 @@ A query can then be builts using the supported data types . The library supports
2938\subsection{Creating Queries}
3039\label{Creating Queries}
3140
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?)
3343filling up the list with the received values. For instance the following is a valid query:
3444
3545let 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"))
3747\newline{This will be parsed as :}
3848\\“SELECT * FROM tbl1 WHERE data IS NULL “
3949
40−The evaluator returns a string and a list of indexed values. 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.
50+The evaluator returns a string and a list of indexed values.
51+The string part of the query could be parsed to prepare.
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.
4254For example:
4355
4456
@@ -51,9 +63,14 @@ SELECT * FROM tbl1 WHERE data = ?1 AND num = ?2 OR num = ?3
5163and returns the list as follows:
5264\newline{(Just (1, VStr "data0")) ,(Just (2, VInt 1)) , (Just (3, VInt 2))}
5365
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.
66+The values in the list are binded to the statement using bindMulti function.
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.
5569
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.
70+The function exec\_db\_v2 executes the statement.
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.
5774
5875The functions finalize\_db and close\_db must be called to clean up the open resources.
5976To 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
7592\label{Evaluating Queries}
7693\subsection{SQL Data Type}
7794\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.
79101\subsection{evalSQL Function}
80102\label{evalSQL Function}
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.
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.
107+It returns the well-quoted String expression and a list of indexed values which are passed to the bind function.
82108\section{Pointers passed to Idris functions}
83109\label{Pointers passed to Idris functions}
84110
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
111+The library uses FFI. In order to make the pointers passed to functions more explicit,
112+we have defined them as data types. For instance open\_db takes a String and returns a DBPointer and prepare\_db
86113takes a DBPointer and returns a StmtPtr(Statemenet Pointer).
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}.
114+Error handling is done using the \emph {DB a} data type which
115+is either \emph {IO String} which indicates an error or \emph{ IO a}.
88116To test the functions in main, you need to use runDB which takes a \emph{ DB a} and returns IO a.
89117\begin{center}
90118\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}
94122\section{Using the C Functions}
95123\label{Using the C Functions}
96124
97−For example calling get\_table after prepare\_db could lead to segmentation fault since get\_table calls sqlite3\_prepare etc
98−Functions such as exec\_db and get\_table are wrapper functions and calling these before finalizing a prepared query could cause segmentation fault.
125+For example calling get\_table after prepare\_db could lead to
126+segmentation fault since get\_table calls sqlite3\_prepare etc
127+Functions such as exec\_db and get\_table are wrapper functions
128+and calling these before finalizing a prepared query could cause segmentation fault.
99129Perhaps these functions could be removed from the library if no longer required in the future.
100130
101131\end{document}