summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xdb_procs.tcl85
1 files changed, 63 insertions, 22 deletions
diff --git a/db_procs.tcl b/db_procs.tcl
index 728cb8d..5ad05f2 100755
--- a/db_procs.tcl
+++ b/db_procs.tcl
@@ -1,6 +1,6 @@
#!/bin/sh
-# FILE: "/mnt/light_huge_archive/home/evmik/src/my_src/GradeBook/db_procs.tcl"
-# LAST MODIFICATION: "Sun, 12 Dec 2010 23:50:57 -0500 (evmik)"
+# FILE: "/home/evmik/src/my_src/GradeBook/db_procs.tcl"
+# LAST MODIFICATION: "Mon, 13 Dec 2010 17:27:51 -0500 (evmik)"
# (C) 2010 by Eugeniy Mikhailov, <evgmik@gmail.com>
# $Id:$
# vim:set ft=tcl: \
@@ -8,48 +8,89 @@ exec tclsh "$0" "$@"
#load libtclsqlite3.so.0 Sqlite3
package require sqlite3
+package require ncgi
+::ncgi::header
+::ncgi::parse
+
+set script_name $env(SCRIPT_NAME)
+
+#set val [::ncgi::value fd]
+set sortCol [::ncgi::value sortCol LastName]
+
+proc dbg {msg {level 1}} {
+ if { $level <=2 } {
+ puts $msg
+ }
+}
set dbfile "./testdb"
+#set url_base
sqlite3 db $dbfile
proc createDB {db} {
- db eval {CREATE TABLE t1(FirstName text, LastName text)}
+ db eval {CREATE TABLE t1(FirstName text, LastName text, HW01 float)}
- db eval {INSERT INTO t1 VALUES('John','Lname1')}
- db eval {INSERT INTO t1 VALUES('Ale','Lname2')}
- db eval {INSERT INTO t1 VALUES('Dan','Lname3')}
+ db eval {INSERT INTO t1 VALUES('John','Lname1', 7)}
+ db eval {INSERT INTO t1 VALUES('Ale','Lname2', 5)}
+ db eval {INSERT INTO t1 VALUES('Dan','Lname3',9)}
#db1 eval {ALTER TABLE t1 ADD c int }
}
-proc htmlDBout {db} {
+proc htmlDBout {db {sort_col {}}} {
#set x [db eval {SELECT * FROM t1 ORDER BY a}]
#puts $x
+ global script_name
+ set defSortCol LastName
+
+ # testing for the existense of the sorting column
+ set eval_str [list SELECT * FROM t1 ORDER BY $sort_col]
+ set err [catch {db eval $eval_str } errStat]
+ if { $err } {
+ dbg $errStat 3
+ dbg "changing to default sorting column $defSortCol" 3
+ set sort_col $defSortCol
+ }
+
set show_header 1
- puts "<table>"
- db eval {SELECT * FROM t1 ORDER BY LastName} v {
- puts "<tr>"
- foreach index $v(*) {
- if { $show_header } {
- set show_header 0
- foreach col $v(*) {
- puts -nonewline "<td>$col</td>"
+ if { $sort_col == {} } {
+ set sort_col LastName
+ }
+ # show the table with grades
+ set eval_str [list SELECT * FROM t1 ORDER BY $sort_col]
+ set err [catch {
+ db eval $eval_str v {
+ if { $show_header } {
+ set show_header 0
+ puts {<table border="1">}
+ puts "<tr>"
+ foreach col $v(*) {
+ puts -nonewline "<th><a href=$script_name?sortCol=$col>$col</a></th>"
+ }
+ puts "</tr>"
+ puts "<tr>"
+ } else {
+ puts "<tr>"
+ }
+ foreach index $v(*) {
+ if { $index != "*" } {
+ puts -nonewline "<td>$v($index)</td>"
+ }
}
puts "</tr>"
- puts "<tr>"
}
- if { $index != "*" } {
- puts -nonewline "<td>$v($index)</td>"
- }
- }
- puts "</tr>"
+ } errStat ]
+ if { $err } {
+ dbg "we should never be here if $sortCol exist in the table" 1
+ dbg $errStat 1
+
}
puts "</table>"
}
#createDB db
-htmlDBout db
+htmlDBout db $sortCol