From e1f40958894955957fa3c8d0c2039c087d14fec4 Mon Sep 17 00:00:00 2001 From: Eugeniy Mikhailov Date: Fri, 15 Apr 2011 20:40:50 -0400 Subject: delete column moved to low level table operation file Ignore-this: d95aac67b1bc99c233bf8cada024b42c darcs-hash:20110416004050-067c0-5f30fb39f0d5eacd59db2befd7e45b83459ca451.gz --- libBasicTableOperations.tcl | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'libBasicTableOperations.tcl') 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, # $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" + } +} + -- cgit v1.2.3