nsmcp::DatabaseSession method compile (protected)

 <instance of nsmcp::DatabaseSession[i]> compile sql

Defined in /usr/local/ns/tcl/nsmcp/lib/database.tcl

Parameters:
sql (required)

Testcases:
No testcase defined.
Source code:
if {[dict exists ${:templates} $sql]} {return [dict get ${:templates} $sql]}
set statements {}; set parts {}; set literal {}; set length [string length $sql]
set quote {}; set comment {}; set active false
for {set i 0} {$i<$length} {incr i} {
    set char [string index $sql $i]; set next [string index $sql [expr {$i+1}]]
    if {$comment eq "line"} {
        append literal $char
        if {$char eq "\n"} {set comment {}}
        continue
    }
    if {$comment eq "block"} {
        append literal $char
        if {$char eq "*" && $next eq "/"} {append literal $next; incr i; set comment {}}
        continue
    }
    if {$quote ne ""} {
        append literal $char
        if {$char eq $quote} {
            if {$next eq $quote} {append literal $next; incr i} else {set quote {}}
        }
        continue
    }
    if {$char eq "-" && $next eq "-"} {set comment line; append literal $char $next; incr i; continue}
    if {$char eq "/" && $next eq "*"} {set comment block; append literal $char $next; incr i; continue}
    if {$char eq "'" || $char eq "\"" || $char eq "\x60"} {set active true; set quote $char; append literal $char; continue}
    if {$char eq "\u005b"} {set active true; set quote "\u005d"; append literal $char; continue}
    if {$char eq ";"} {
        lappend parts literal $literal
        if {$active} {lappend statements $parts}
        set parts {}; set literal {}; set active false; continue
    }
    if {![string is space -strict $char]} {set active true}
    if {$char eq "$" && [string match {[A-Za-z_]} $next]} {
        lappend parts literal $literal; set literal {}; set name {}
        while {$i+1<$length && [string match {[A-Za-z0-9_]} [string index $sql [expr {$i+1}]]]} {
            incr i; append name [string index $sql $i]
        }
        lappend parts variable $name
    } else {append literal $char}
}
if {$quote ne "" || $comment eq "block"} {error "unterminated trusted SQL template"}
lappend parts literal $literal
if {$active} {lappend statements $parts}
if {[dict size ${:templates}]>=64} {set :templates {}}
dict set :templates $sql $statements
return $statements
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: