summaryrefslogtreecommitdiff
path: root/db_procs.tcl
blob: 5ad05f226bb96ab901967e747ef07799b293eac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh
# 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: \
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, HW01 float)}

	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 {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
	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>"
			}
		} 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 $sortCol