annotate utillib.tcl @ 262:416642f28d1e

Rename util_convert.tcl to utillib.tcl
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 23 Jan 2015 10:02:11 +0200
parents util_convert.tcl@4c51eeba993f
children cad1041b5bc4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 ### Helper functions
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 proc escape { str } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
3 return [string map {' ''} $str]
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 proc lescape { lst pos } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
7 return [escape [lindex $lst $pos]]
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 proc confirm_yesno { uprompt } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
12 puts -nonewline "$uprompt \[y/N\]? "
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
13 flush stdout
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
14 set response [gets stdin]
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
15 if {[string tolower $response] == "y"} {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
16 return 1
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
17 } else {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
18 return 0
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
19 }
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 proc open_db { dbfile } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
23 global dbh
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
24 if {[catch {sqlite3 dbh $dbfile} uerrmsg]} {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
25 puts "Could not open SQLite3 database '$dbfile': $uerrmsg."
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
26 exit 2
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
27 }
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 proc drop_table { utable } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
31 global dbh
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
32 puts "Dropping current table '$utable'."
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
33 if {[catch {dbh eval "DROP TABLE $utable"} uerrmsg]} {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
34 puts "Dropping table resulted in error (ignored): $uerrmsg."
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
35 }
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 proc create_table { utable usql } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
39 global dbh
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
40 puts "Creating new table '$utable'."
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
41 if {[catch {dbh eval "CREATE TABLE $utable ($usql)"} uerrmsg]} {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
42 puts "Error creating table: $uerrmsg."
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
43 return 0
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
44 }
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
45 return 1
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 proc create_table_or_fail { utable usql } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
49 if {![create_table $utable $usql]} {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
50 exit 3
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
51 }
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 proc create_table_urls { } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
55 create_table_or_fail "urls" "id INTEGER PRIMARY KEY AUTOINCREMENT, utime INT, url VARCHAR(2048), user VARCHAR(32), host VARCHAR(256), chan VARCHAR(32), title VARCHAR(256)"
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
36
b24cb1dd0423 Fail under certain conditions only when creating tables.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
58 proc create_table_quotes_votes { utable ufail } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
59
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
60 set sql1 "id INTEGER PRIMARY KEY AUTOINCREMENT, utime INT, utext VARCHAR(2048), user VARCHAR(32), host VARCHAR(256), chan VARCHAR(32)"
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
61 set sql2 "id INTEGER PRIMARY KEY AUTOINCREMENT, user VARCHAR(32), urlid INTEGER, vote INTEGER"
36
b24cb1dd0423 Fail under certain conditions only when creating tables.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
62
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
63 if {$ufail} {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
64 create_table_or_fail "$utable" "$sql1"
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
65 create_table_or_fail "${utable}_votes" "$sql2"
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
66 } else {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
67 create_table "$utable" "$sql1"
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
68 create_table "${utable}_votes" "$sql2"
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
69 }
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 }
138
743c1bea2498 Add utility functions for feeds database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
71
743c1bea2498 Add utility functions for feeds database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
72
743c1bea2498 Add utility functions for feeds database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
73 proc drop_table_feeds { } {
142
4c51eeba993f Rename table.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
74 drop_table "feeds"
138
743c1bea2498 Add utility functions for feeds database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
75 }
743c1bea2498 Add utility functions for feeds database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
76
743c1bea2498 Add utility functions for feeds database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
77 proc create_table_feeds { } {
142
4c51eeba993f Rename table.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
78 create_table_or_fail "feeds" "feed VARCHAR(64), utime INT, url VARCHAR(512), title VARCHAR(256)"
138
743c1bea2498 Add utility functions for feeds database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
79 }