Skip to content

Releases: ThomasTJdev/nim_sqlbuilder

Refactor

21 Dec 07:54
a896871
Compare
Choose a tag to compare
Merge pull request #6 from ThomasTJdev/RefactorSelectIsDeleted

Refactor

Dynamic selection of columns

12 Jun 08:10
Compare
Choose a tag to compare

This release includes 2 changes:

  1. You are not bound to string or int when generating arguments - all normal types are accepted.
  2. Dynamic selection of columns (se below)

Dynamic columns

Lets say, that you are importing data from a spreadsheet with static columns, and you need to update your DB with the new values.

Sometimes the spreadsheet only contains 5 out of the 10 static columns you were expecting. Now you don't know the values of the last 5 columns, which will result in updating the DB with NULL/empty values.

The template genArgsColumns() allows you to use the same query-builder, but it is only selecting the columns which shall be updated. When importing your spreadsheet, check if the column exists (bool), and pass that as the use: bool param. If the column does not exists, it will be skipped in the query.

Let try an example:

 let (s, a) = genArgsColumns((true, "name", "Thomas"), (true, "age", 30), (false, "nim", ""), (true, "", "154"))
 # We are using the column `name` and `age` and ignoring the column `nim`, since that column did not
 # exist in our spreadsheet. We are using the value `154` as our identifier, therefor the column is not 
 # specified

 echo $a.args
 # ==> Args: @["Thomas", "30", "154"]
 
 let a3 = sqlUpdate("my-table", s, ["id"], a.query)
 # ==> UPDATE my-table SET name = ?, age = ? WHERE id = ?

The tuples passed to genArgsColumns() shall be formatted like this:

 (use: bool, column: string, value: auto)

 # use  => Should we include this in creation of the query
 # column => The column to use. Leave empty, if the tuple is used for WHERE args
 # value = > The value to use.