Mercurial > hg > egg-tcls
annotate util_convert.tcl @ 178:9b8ec700ede4
Clean up the weather data parser backend a bit.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 03 Jun 2014 15:10:47 +0300 |
parents | 4c51eeba993f |
children |
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 | 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 | 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 | 12 puts -nonewline "$uprompt \[y/N\]? " |
13 flush stdout | |
14 set response [gets stdin] | |
15 if {[string tolower $response] == "y"} { | |
16 return 1 | |
17 } else { | |
18 return 0 | |
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 | 23 global dbh |
24 if {[catch {sqlite3 dbh $dbfile} uerrmsg]} { | |
25 puts "Could not open SQLite3 database '$dbfile': $uerrmsg." | |
26 exit 2 | |
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 | 31 global dbh |
32 puts "Dropping current table '$utable'." | |
33 if {[catch {dbh eval "DROP TABLE $utable"} uerrmsg]} { | |
34 puts "Dropping table resulted in error (ignored): $uerrmsg." | |
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 | 39 global dbh |
40 puts "Creating new table '$utable'." | |
41 if {[catch {dbh eval "CREATE TABLE $utable ($usql)"} uerrmsg]} { | |
42 puts "Error creating table: $uerrmsg." | |
43 return 0 | |
44 } | |
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 | 49 if {![create_table $utable $usql]} { |
50 exit 3 | |
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 | 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 | 59 |
60 set sql1 "id INTEGER PRIMARY KEY AUTOINCREMENT, utime INT, utext VARCHAR(2048), user VARCHAR(32), host VARCHAR(256), chan VARCHAR(32)" | |
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 | 63 if {$ufail} { |
64 create_table_or_fail "$utable" "$sql1" | |
65 create_table_or_fail "${utable}_votes" "$sql2" | |
66 } else { | |
67 create_table "$utable" "$sql1" | |
68 create_table "${utable}_votes" "$sql2" | |
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 | 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 | 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 } |