# ad-aolserver-3.tcl.preload,v 3.1 2000/02/27 19:16:01 jsalz Exp # File: ad-aolserver-3.tcl.preload # Author: Jon Salz , from code by markd # Date: 27 Feb 2000 # Description: Contains procedures specific to AOLserver 3 (mostly recreating # functionality dropped from AOLserver 2). # # If name contains a space, then it is surrounded by double quotes. # This is useful for names in SQL statements that may contain spaces. proc ns_dbquotename {name} { if [regexp " " $name] { return "\"$name\"" } else { return $name } } # ns_dbquotevalue: # # Prepares a value string for inclusion in an SQL statement. # "" is translated into NULL. # All values of any numeric type are left alone. # All other values are surrounded by single quotes and any # single quotes included in the value are escaped (ie. translated # into 2 single quotes). proc ns_dbquotevalue {value {type text}} { if [string match $value ""] { return "NULL" } if {$type == "decimal" \ || $type == "double" \ || $type == "integer" \ || $type == "int" \ || $type == "real" \ || $type == "smallint" \ || $type == "bigint" \ || $type == "bit" \ || $type == "float" \ || $type == "numeric" \ || $type == "tinyint"} { return $value } regsub -all "'" $value "''" value return "'$value'" } # -1 = Not there or value was "" # 0 = NULL, set value to NULL. # 1 = Got value, set value to it. proc ns_dbformvalue {formdata column type valuebyref} { upvar $valuebyref value if {[ns_set get $formdata ColValue.[ns_urlencode $column].NULL] == "t"} { set value "" return 0 } set value [ns_set get $formdata ColValue.[ns_urlencode $column]] if [string match $value ""] { switch $type { date { set value [ns_buildsqldate \ [ns_set get $formdata ColValue.[ns_urlencode $column].month] \ [ns_set get $formdata ColValue.[ns_urlencode $column].day] \ [ns_set get $formdata ColValue.[ns_urlencode $column].year]] } time { set value [ns_buildsqltime \ [ns_set get $formdata ColValue.[ns_urlencode $column].time] \ [ns_set get $formdata ColValue.[ns_urlencode $column].ampm]] } datetime - timestamp { set value [ns_buildsqltimestamp \ [ns_set get $formdata ColValue.[ns_urlencode $column].month] \ [ns_set get $formdata ColValue.[ns_urlencode $column].day] \ [ns_set get $formdata ColValue.[ns_urlencode $column].year] \ [ns_set get $formdata ColValue.[ns_urlencode $column].time] \ [ns_set get $formdata ColValue.[ns_urlencode $column].ampm]] } default { } } } if [string match $value ""] { return -1 } else { return 1 } } proc ns_dbformvalueput {htmlform column type value} { switch $type { date { set retval [ns_formvalueput $htmlform ColValue.[ns_urlencode $column].NULL f] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].month \ [ns_parsesqldate month $value]] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].day \ [ns_parsesqldate day $value]] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].year \ [ns_parsesqldate year $value]] } time { set retval [ns_formvalueput $htmlform ColValue.[ns_urlencode $column].NULL f] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].time \ [ns_parsesqltime time $value]] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].ampm \ [ns_parsesqltime ampm $value]] } datetime - timestamp { set retval [ns_formvalueput $htmlform ColValue.[ns_urlencode $column].NULL f] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].month \ [ns_parsesqltimestamp month $value]] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].day \ [ns_parsesqltimestamp day $value]] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].year \ [ns_parsesqltimestamp year $value]] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].time \ [ns_parsesqltimestamp time $value]] set retval [ns_formvalueput $retval ColValue.[ns_urlencode $column].ampm \ [ns_parsesqltimestamp ampm $value]] } default { set retval [ns_formvalueput $htmlform ColValue.[ns_urlencode $column] $value] } } return $retval } # Special thanks to Brian Tivol at Hearst New Media Center and MIT # for providing the core of this code. proc ns_formvalueput {htmlpiece dataname datavalue} { set newhtml "" while {$htmlpiece != ""} { if {[string index $htmlpiece 0] == "<"} { regexp {<([^>]*)>(.*)} $htmlpiece m tag htmlpiece set tag [string trim $tag] set CAPTAG [string toupper $tag] switch -regexp $CAPTAG { {^INPUT} { if {[regexp {TYPE=("IMAGE"|"SUBMIT"|"RESET"|IMAGE|SUBMIT|RESET)} $CAPTAG]} { append newhtml <$tag> } elseif {[regexp {TYPE=("CHECKBOX"|CHECKBOX|"RADIO"|RADIO)} $CAPTAG]} { set name [ns_tagelement $tag NAME] if {$name == $dataname} { set value [ns_tagelement $tag VALUE] regsub -all -nocase { *CHECKED} $tag {} tag if {$value == $datavalue} { append tag " CHECKED" } } append newhtml <$tag> } else { ## If it's an INPUT TYPE that hasn't been covered # (text, password, hidden, other (defaults to text)) ## then we add/replace the VALUE tag set name [ns_tagelement $tag NAME] if {$name == $dataname} { ns_tagelementset tag VALUE $datavalue } append newhtml <$tag> } } {^TEXTAREA} { ### # Fill in the middle of this tag ### set name [ns_tagelement $tag NAME] if {$name == $dataname} { while {![regexp -nocase {^<( *)/TEXTAREA} $htmlpiece]} { regexp {^.[^<]*(.*)} $htmlpiece m htmlpiece } append newhtml <$tag>$datavalue } else { append newhtml <$tag> } } {^SELECT} { ### Set flags so OPTION and /SELECT know what to look for: # snam is the variable name, sflg is 1 if nothing's ### been added, smul is 1 if it's MULTIPLE selection if {[ns_tagelement $tag NAME] == $dataname} { set inkeyselect 1 set addoption 1 } else { set inkeyselect 0 set addoption 0 } append newhtml <$tag> } {^OPTION} { ### # Find the value for this ### if {$inkeyselect} { regsub -all -nocase { *SELECTED} $tag {} tag set value [ns_tagelement $tag VALUE] regexp {^([^<]*)(.*)} $htmlpiece m txt htmlpiece if [string match "" $value] { set value [string trim $txt] } if {$value == $datavalue} { append tag " SELECTED" set addoption 0 } append newhtml <$tag>$txt } else { append newhtml <$tag> } } {^/SELECT} { ### # Do we need to add to the end? ### if {$inkeyselect && $addoption} { append newhtml "