diff options
author | Eugeniy Mikhailov <evgmik@gmail.com> | 2011-04-15 20:04:32 -0400 |
---|---|---|
committer | Eugeniy Mikhailov <evgmik@gmail.com> | 2011-04-15 20:04:32 -0400 |
commit | f0a034754528c975e6170307337e13c25e3c9b6c (patch) | |
tree | 92978f0bfea028d1f0d7ca31fc0e0da29bc9b752 | |
parent | 2d31d52191ace6bbeccf4cfede8734d6bb494da4 (diff) | |
download | GradeBook-f0a034754528c975e6170307337e13c25e3c9b6c.tar.gz GradeBook-f0a034754528c975e6170307337e13c25e3c9b6c.zip |
low level table operations moved to separate routine
Ignore-this: be0109b9771cb2b8ec28491b8d3154bc
darcs-hash:20110416000432-067c0-c8a54be0240ab9773121708e65d8835d6a769535.gz
-rwxr-xr-x | GradeBook_lib.tcl | 64 | ||||
-rwxr-xr-x | libBasicTableOperations.tcl | 74 |
2 files changed, 77 insertions, 61 deletions
diff --git a/GradeBook_lib.tcl b/GradeBook_lib.tcl index cc455da..267af15 100755 --- a/GradeBook_lib.tcl +++ b/GradeBook_lib.tcl @@ -5,6 +5,9 @@ exec tclsh "$0" "$@" # require Tcl version of at least 8.5 since I use 'ni' and 'in' in expressions for lists package require Tcl 8.5 + +source ./libBasicTableOperations.tcl + # internal version of this code set VERSION 1.5.0 @@ -74,67 +77,6 @@ proc isCalculateTotalForCategorySet { category } { return $flag } -proc getColListFromTable {table} { - set eval_str [concat SELECT * FROM \'$table\'] - set err [catch { - db eval $eval_str v { - set all_column_names $v(*) - } - } errStat ] - if { $err } { - dbg "we should never be here if GradesTable exists" 1 - dbg $errStat 1 - htmlErrorMsg $errStat - } - return $all_column_names -} - -proc SelectColvalueFromTable { table column_of_interest col row_value } { - # select value of 'column_of_interest' from 'table' where 'col'='row_value' - set value {} - set eval_str "SELECT \"$column_of_interest\" FROM \'$table\' WHERE \"$col\"=\"$row_value\"" - set err [catch { - db eval $eval_str v { - set value $v($column_of_interest) - } - } errStat ] - if { $err } { - set msg_text "the following error happen in proc SelectColvalueFromTable while selecting from table $table $errStat" - htmlErrorMsg $msg_text - dbg $msg_text 3 - set $value {} - } - return $value -} - -proc existsColumnWithRowvalueInTable { table column row_value } { - set sql_str [concat SELECT 1 FROM \'$table\' WHERE \"$column\"=\"$row_value\"] - if {![db exists $sql_str]} { - dbg "Column \'$column\' does not have row with value \'$row_value\' in table \'$table\'" 3 - return false - } else { - return true - } -} - -proc UpdateColumnWithValueInTableWhere { table column val where_column row_value } { - set eval_str [concat UPDATE \'$table\' SET \"$column\"='$val' WHERE \"$where_column\"=\'$row_value\'] - set err [catch {db eval $eval_str } errStat] - if { $err } { - set msg_str "Unable to update column $column in the table $table where $where_column=$row_value. The following error happen: $errStat" - htmlErrorMsg $msg_str - dbg $msg_str 3 - } -} - -proc doesColumnExists {col table} { - if { $col in [getColListFromTable $table] } { - return true; - } else { - return false; - } -} - proc allUserNamesInGroup { group } { set eval_str [concat SELECT UserName FROM \'GradesTable\' WHERE GroupName == \'$group\'] set username_list {} diff --git a/libBasicTableOperations.tcl b/libBasicTableOperations.tcl new file mode 100755 index 0000000..4eca8c8 --- /dev/null +++ b/libBasicTableOperations.tcl @@ -0,0 +1,74 @@ +#!/bin/sh +# FILE: "/home/evmik/src/my_src/GradeBook/libBasicTableOperations.tcl" +# LAST MODIFICATION: "Fri, 15 Apr 2011 20:01:22 -0400 (evmik)" +# (C) 2011 by Eugeniy Mikhailov, <evgmik@gmail.com> +# $Id:$ +# vim:set ft=tcl: \ +exec tclsh "$0" "$@" + +# ####################################################### +# everywhere below db must be initialized with command +# sqlite3 db $dbfile +# ####################################################### + +proc getColListFromTable {table} { + set eval_str [concat SELECT * FROM \'$table\'] + set err [catch { + db eval $eval_str v { + set all_column_names $v(*) + } + } errStat ] + if { $err } { + dbg "we should never be here if GradesTable exists" 1 + dbg $errStat 1 + htmlErrorMsg $errStat + } + return $all_column_names +} + +proc SelectColvalueFromTable { table column_of_interest col row_value } { + # select value of 'column_of_interest' from 'table' where 'col'='row_value' + set value {} + set eval_str "SELECT \"$column_of_interest\" FROM \'$table\' WHERE \"$col\"=\"$row_value\"" + set err [catch { + db eval $eval_str v { + set value $v($column_of_interest) + } + } errStat ] + if { $err } { + set msg_text "the following error happen in proc SelectColvalueFromTable while selecting from table $table $errStat" + htmlErrorMsg $msg_text + dbg $msg_text 3 + set $value {} + } + return $value +} + +proc existsColumnWithRowvalueInTable { table column row_value } { + set sql_str [concat SELECT 1 FROM \'$table\' WHERE \"$column\"=\"$row_value\"] + if {![db exists $sql_str]} { + dbg "Column \'$column\' does not have row with value \'$row_value\' in table \'$table\'" 3 + return false + } else { + return true + } +} + +proc UpdateColumnWithValueInTableWhere { table column val where_column row_value } { + set eval_str [concat UPDATE \'$table\' SET \"$column\"='$val' WHERE \"$where_column\"=\'$row_value\'] + set err [catch {db eval $eval_str } errStat] + if { $err } { + set msg_str "Unable to update column $column in the table $table where $where_column=$row_value. The following error happen: $errStat" + htmlErrorMsg $msg_str + dbg $msg_str 3 + } +} + +proc doesColumnExists {col table} { + if { $col in [getColListFromTable $table] } { + return true; + } else { + return false; + } +} + |