sqlitetcl

Is there any advantage or disadvantage to placing more SQL in a single "dbcmd eval" versus using it multiple times?


I assume that this isn't all that important but would like to understand what is taking place. I had some longer CTEs that needed split because one of the intermediate tables needed to be used in another location and would be lost when the CTE completed; thus, I wrote it to an in-memory database and then continued the CTE in a second query.

dbcmd eval $sql
# split into
dbcmd eval ${sql_a}${sql_b}

Is this more work for Tcl than using two dbcmd eval such as:

dbcmd eval $sql_a
dbcmd eval $sql_b

Does Tcl first append those two SQL strings together in another memory location, when it might just pass a pointer to each eval and do less?

Unless query results are being written back to the application code, I tend to try to put as much in a single eval as possible but really have no reason for that. Is there a preferred approach?

If it matters, nearly all the queries are in a separate namespace and are not built within the script other than declaring them within {}s and using the : operator to bind parameters.

Thank you.


Solution

  • The difference in the amount of work to Tcl is trivial relative to the cost of even accessing an in-memory database. You should write code that both works and is as clear to you as practical.

    There is no concatenation of the pieces in

    dbcmd eval $sql_a
    dbcmd eval $sql_b
    

    The $sql_a and $sql_b strings should each contain complete statements (or several of them) as that is what the underlying API accepts.


    You are recommended to always use {brace-quoted} literal strings for SQL in Tcl. String substitutions to generate SQL can be (and often is) a sign of critical vulnerability of the code to SQL injection attacks.