diff options
author | Eugeniy Mikhailov <evgmik@gmail.com> | 2011-04-15 20:40:50 -0400 |
---|---|---|
committer | Eugeniy Mikhailov <evgmik@gmail.com> | 2011-04-15 20:40:50 -0400 |
commit | e1f40958894955957fa3c8d0c2039c087d14fec4 (patch) | |
tree | b5c494f5e6e2d7e7d0a2d0b331ad76eb5e03db21 /libBasicTableOperations.tcl | |
parent | 5b2d35448899a128148c2614cb6203d8c5986de0 (diff) | |
download | GradeBook-e1f40958894955957fa3c8d0c2039c087d14fec4.tar.gz GradeBook-e1f40958894955957fa3c8d0c2039c087d14fec4.zip |
delete column moved to low level table operation file
Ignore-this: d95aac67b1bc99c233bf8cada024b42c
darcs-hash:20110416004050-067c0-5f30fb39f0d5eacd59db2befd7e45b83459ca451.gz
Diffstat (limited to 'libBasicTableOperations.tcl')
-rwxr-xr-x | libBasicTableOperations.tcl | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/libBasicTableOperations.tcl b/libBasicTableOperations.tcl index 4eca8c8..68781dc 100755 --- a/libBasicTableOperations.tcl +++ b/libBasicTableOperations.tcl @@ -1,6 +1,6 @@ #!/bin/sh # FILE: "/home/evmik/src/my_src/GradeBook/libBasicTableOperations.tcl" -# LAST MODIFICATION: "Fri, 15 Apr 2011 20:01:22 -0400 (evmik)" +# LAST MODIFICATION: "Fri, 15 Apr 2011 20:38:36 -0400 (evmik)" # (C) 2011 by Eugeniy Mikhailov, <evgmik@gmail.com> # $Id:$ # vim:set ft=tcl: \ @@ -26,6 +26,18 @@ proc getColListFromTable {table} { return $all_column_names } +proc colList2sqlColStr { col_list } { + set sqlStr {} + foreach col $col_list { + if {$sqlStr ne ""} { + set sqlStr $sqlStr,\"$col\" + } else { + set sqlStr \"$col\" + } + } + return $sqlStr +} + proc SelectColvalueFromTable { table column_of_interest col row_value } { # select value of 'column_of_interest' from 'table' where 'col'='row_value' set value {} @@ -72,3 +84,32 @@ proc doesColumnExists {col table} { } } +proc DeleteColumnFromTable { table columnname } { + if { $columnname eq "" } { + htmlErrorMsg "empty column names are not permitted" + } + + if { [doesColumnExists $columnname $table] } { + # removing the column name to be deleted from total list + + set old_column_list [getColListFromTable $table] + set new_column_list [removeElementFromList $columnname $old_column_list] + set sql_new_column_str [colList2sqlColStr $new_column_list] + set eval_str "BEGIN TRANSACTION; + CREATE TEMPORARY TABLE \'${table}_backup\'($sql_new_column_str); + INSERT INTO \'${table}_backup\' SELECT $sql_new_column_str FROM \'${table}\'; + DROP TABLE \'${table}\'; + CREATE TABLE \'${table}\'($sql_new_column_str); + INSERT INTO \'${table}\' SELECT $sql_new_column_str FROM \'${table}_backup\'; + DROP TABLE \'${table}_backup\'; + COMMIT;" + set err [catch {db eval $eval_str } errStat] + if { $err } { + htmlErrorMsg $errStat + dbg "the following error happen: $errStat" 3 + } + } else { + htmlErrorMsg "No column $columnname in the table $table" + } +} + |