@@ -6,9 +6,14 @@ local clib = require "sqlite.defs"
66local flags = clib .flags
77
88--- @class sqlstmt @Object to deal with sqlite statements
9+ --- @field pstmt sqlite_blob sqlite_pstmt
10+ --- @field conn sqlite_blob sqlite3 object
11+ --- @field str string original statement
912local sqlstmt = {}
1013sqlstmt .__index = sqlstmt
1114
15+ --- TODO: refactor and make parser.lua required here only.
16+
1217--- Parse a statement
1318--- @param conn sqlite3 : the database connection.
1419--- @param str string : the sqlite statement to be parsed.
@@ -18,29 +23,21 @@ sqlstmt.__index = sqlstmt
1823function sqlstmt :parse (conn , str )
1924 assert (clib .type_of (conn ) == clib .type_of_db_ptr , " Invalid connection passed to sqlstmt:parse" )
2025 assert (type (str ) == " string" , " Invalid second argument passed to sqlstmt:parse" )
21- local o = {
26+ local o = setmetatable ( {
2227 str = str ,
2328 conn = conn ,
2429 finalized = false ,
25- }
26- setmetatable (o , self )
27- o :__parse ()
28- return o
29- end
30+ }, sqlstmt )
3031
31- --- Parse self.str into an sqlite representation and set it to self.pstmt.
32- function sqlstmt :__parse ()
3332 local pstmt = clib .get_new_stmt_ptr ()
34- local code = clib .prepare_v2 (self .conn , self .str , # self .str , pstmt , nil )
33+ local code = clib .prepare_v2 (o .conn , o .str , # o .str , pstmt , nil )
34+
3535 assert (
3636 code == flags .ok ,
37- string.format (
38- " sqlite.lua: sql statement parse, , stmt: `%s`, err: `(`%s`)`" ,
39- self .str ,
40- clib .to_str (clib .errmsg (self .conn ))
41- )
37+ (" sqlite.lua: sql statement parse, , stmt: `%s`, err: `(`%s`)`" ):format (o .str , clib .last_errmsg (o .conn ))
4238 )
43- self .pstmt = pstmt [0 ]
39+ o .pstmt = pstmt [0 ]
40+ return o
4441end
4542
4643--- Resets the parsed statement. required for parsed statements to be re-executed.
@@ -69,7 +66,7 @@ function sqlstmt:finalize()
6966end
7067
7168--- Called before evaluating the (next iteration) of the prepared statement.
72- --- @return sqlite_flag : Possible Flags : { flags.busy , flags.done , flags.row , flags.error , flags.misuse }
69+ --- @return sqlite_flags : Possible Flags : { flags.busy , flags.done , flags.row , flags.error , flags.misuse }
7370function sqlstmt :step ()
7471 local step_code = clib .step (self .pstmt )
7572 assert (
@@ -283,7 +280,6 @@ local bind_type_to_func = {
283280--- If {args[1]} is a number and {args[2]} is a value then it binds by index.
284281--- Else first argument is a table, then it binds the table to indicies, and it
285282--- works with named and unnamed.
286- --- @param ...: Either a index value pair or a table
287283--- @vararg s if {args[1] } number and {args[2] } or {args[1] } table
288284--- @see sqlstmt :nparam
289285--- @see sqlstmt :param
@@ -342,15 +338,15 @@ end
342338--- @param idx number : index starting at 1
343339--- @param pointer sqlite_blob : blob to bind
344340--- @param size number : pointer size
345- --- @return sqlite_flag
341+ --- @return sqlite_flags
346342function sqlstmt :bind_blob (idx , pointer , size )
347343 return clib .bind_blob64 (self .pstmt , idx , pointer , size , nil ) -- Always 64? or two functions
348344end
349345
350346--- Binds zeroblob at {idx} with {size}
351347--- @param idx number : index starting at 1
352348--- @param size number : zeroblob size
353- --- @return sqlite_flag
349+ --- @return sqlite_flags
354350function sqlstmt :bind_zeroblob (idx , size )
355351 return clib .bind_zeroblob64 (self .pstmt , idx , size )
356352end
@@ -385,15 +381,15 @@ function sqlstmt:params()
385381end
386382
387383--- Clear the current bindings.
388- --- @return sqlite_flag
384+ --- @return sqlite_flags
389385function sqlstmt :bind_clear ()
390386 self .current_bind_index = nil
391387 return clib .clear_bindings (self .pstmt )
392388end
393389
394390--- Bind the value at the next index until all values are bound
395391--- @param value any : value to bind
396- --- @return sqlite_flag
392+ --- @return sqlite_flags
397393--- @TODO does it return sqlite_flag in all cases? @conni
398394function sqlstmt :bind_next (value )
399395 if not self .current_bind_index then
0 commit comments