Skip to content

add methods to deal with sql statement.#6

Merged
kkharji merged 9 commits intomasterfrom
stmt_methods
Jan 5, 2021
Merged

add methods to deal with sql statement.#6
kkharji merged 9 commits intomasterfrom
stmt_methods

Conversation

@kkharji
Copy link
Copy Markdown
Owner

@kkharji kkharji commented Jan 1, 2021

Implement the following building blocks function to construct more complex use cases:

Main

  • stmt:parse()

creates new statement object.

  • stmt:parse()

registers db connection in s.conn.

  • stmt:parse()

registers unparsed statement in s.str.

  • stmt:parse()

parses sql statement in s.pstmt.

  • stmt:parse()

requires s:finalize() in order to close connection.

Helpers

  • stmt:step()

a function must be called to evaluate the (next iteration) of the prepared statement.
returns flags.busy (unable to acquire the locks needed), flags.done (finished
executing successfully), flags.row (a new row is ready for processing) and
error,misuse.

  • stmt:reset():

resets SQL statement so that it is ready to be re-executed. Any
statement variables that had values bound to them using the stmt:bind
functions retain their values.

  • stmt:finalize():

Frees the prepared statement, if the statement is successful, or
not execute at all then return flags.ok

BIND

  • stmt:bind(n, value):

binds/maps type value to statement parameter n: string->text,
number->int, boolean->0/1, binary_string->blob and if nil the value is removed. On success returns flags.ok or else error code.

  • stmt:bind_names(nametable):

binds the values in nametable to statement parameters.
If the statement parameters are named, it should look for appropriately named fields in nametable;
If the statement parameters are anonymous, i.e. "?", it should look for numerical fields 1 to the
number of statement parameters.
On success returns flags.ok or else error
code.

  • stmt:param_size():

Returns the number of parameters in the parsed statement e.g. stmt:param_size() --> number

  • stmt:param_name(n):

Returns the name of the n-th parameter in the parsed statement
e.g. stmt:param_name(0) --> string

  • stmt:clear_bindings():

clears bindings

  • stmt:bind_values():

Binds the given values to statement parameters.
Returns sqlite3.OK on success or else a numerical error code (see
Numerical error and result codes).

GET

  • stmt:nkeys()

returns the number of columns/keys in results.

  • stmt:nrows()

returns the number of rows in the results.

  • stmt:types()

returns the type of each columns/keys in results.

  • stmt:type()

returns the type of columns/keys by idx.

  • stmt:keys()

returns key/column names

  • stmt:key()

returns key/column name by idx

  • stmt:kt()

returns a dict of key name and their type.

  • stmt:kv()

returns (with stmt:each) a key value pairs of all rows

  • stmt:val()

returns (with stmt:each) a list of vals in all the rows at idx.

  • stmt:vals()

returns (with stmt:each) a nested list all the vals in all the rows.

  • stmt:kvrows()

returns a nested kv-pairs all the rows. if no callback.

  • stmt:vrows()

returns a nested list all values in all the rows. if no callback.

Misc

  • stmt:last_insert_rowid():

Returns the rowid of the most recent INSERT into the database corresponding to this statement.

lua/sql/stmt.lua Outdated
Comment on lines +97 to +84
function M:iter(self)
return self:next(), self.pstmt
end
Copy link
Copy Markdown
Collaborator

@Conni2461 Conni2461 Jan 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have a iter we need to make sure it works in a for loop. Right?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think M:loop would be enough. or should we have iter and next?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know would be cool if you can do for statement:iter() do but i don't think we can write custom iterators in lua. So lets skip on that i think

@Conni2461 Conni2461 force-pushed the stmt_methods branch 2 times, most recently from 9d869a4 to a64d939 Compare January 1, 2021 16:41
@kkharji kkharji force-pushed the stmt_methods branch 3 times, most recently from f4eda5c to bb35c7b Compare January 3, 2021 10:33
@Conni2461 Conni2461 force-pushed the stmt_methods branch 5 times, most recently from b58b452 to 74fdfa3 Compare January 4, 2021 13:38
tami5 added 2 commits January 5, 2021 07:02
It goes well with keys/key types/type val/vals
- combine bind_index and bind_names into bind()
- remove index from bind_blob and bind_zeroblob
- remove value from bind_next
@kkharji kkharji merged commit f4cbcf5 into master Jan 5, 2021
@kkharji kkharji deleted the stmt_methods branch January 5, 2021 08:45
kkharji pushed a commit that referenced this pull request Jan 5, 2021
* master:
  add methods, tests and inline docs for sql statements (#6)
kkharji pushed a commit that referenced this pull request Jan 5, 2021
* master:
  add methods, tests and inline docs for sql statements (#6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants