aboutsummaryrefslogtreecommitdiff
path: root/libBasicTableOperations.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'libBasicTableOperations.tcl')
-rwxr-xr-xlibBasicTableOperations.tcl43
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"
+ }
+}
+