annotate utillib.tcl @ 432:124b97f5a19d

utillib: Add some comments, bump copyright.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Jan 2017 03:55:53 +0200
parents 04021e2e26c3
children cd53828bb724
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
1 ##########################################################################
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
2 #
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
3 # TCL functions library by Matti 'ccr' Hamalainen <ccr@tnsp.org>
432
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
4 # (C) Copyright 2015-2017 Tecnic Software productions (TNSP)
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
5 #
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
6 # This script is freely distributable under GNU GPL (version 2) license.
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
7 #
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
8 ##########################################################################
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
9
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
10 set utl_html_ent_str "&#45;|-|&#39;|'|—|-|&rlm;||&#8212;|-|&#8211;|--|&#x202a;||&#x202c;|"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
11 append utl_html_ent_str "|&lrm;||&aring;|å|&Aring;|Å|&eacute;|é|&#58;|:|&nbsp;| "
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
12 append utl_html_ent_str "|&#8221;|\"|&#8220;|\"|&laquo;|<<|&raquo;|>>|&quot;|\""
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
13 append utl_html_ent_str "|&auml;|ä|&ouml;|ö|&Auml;|Ä|&Ouml;|Ö|&amp;|&|&lt;|<|&gt;|>"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
14 append utl_html_ent_str "|&#228;|ä|&#229;|ö|&mdash;|-|&#039;|'|&ndash;|-|&#034;|\""
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
15 append utl_html_ent_str "|&#124;|-|&#8217;|'|&uuml;|ü|&Uuml;|Ü|&bull;|*|&euro;|€"
317
994c398cb3fc utillib: Add some entity conversions.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
16 append utl_html_ent_str "|&rdquo;|\"|&#8216;|'|&#xe4;|ä|&#xb7;|*|&#174;|®|&acute;|'"
330
c42c900daf08 utillib: Add more entity conversions.
Matti Hamalainen <ccr@tnsp.org>
parents: 326
diff changeset
17 append utl_html_ent_str "|&#246;|ö|&#xf6;|ö|&#35;|#|&apos;|'|&shy;||–|-|”|\""
337
e01963cb88fd utillib: Add some more entity etc. conversions.
Matti Hamalainen <ccr@tnsp.org>
parents: 333
diff changeset
18 append utl_html_ent_str "|&#x2026;|...|&hellip;|...|&#34;|\"|&#xad;||&#064;|#"
e01963cb88fd utillib: Add some more entity etc. conversions.
Matti Hamalainen <ccr@tnsp.org>
parents: 333
diff changeset
19 append utl_html_ent_str "|&#xa0;| "
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
20 set utl_html_ent_list [split [encoding convertfrom "utf-8" $utl_html_ent_str] "|"]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
21
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
22
432
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
23 # Convert given string, containing HTML/XML style entities into a normal
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
24 # UTF-8 Unicode string, using the above entity->character mapping
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
25 proc utl_convert_html_ent {udata} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
26 global utl_html_ent_list
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
27 return [string map -nocase $utl_html_ent_list [string map $utl_html_ent_list $udata]]
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
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
30
432
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
31 # Split given string "str" into a list of sub-strings of maximum length
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
32 # "maxlen", by attempting to split at "words", if possible.
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
33 proc utl_str_split {str maxlen} {
424
825cac46b1cb Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 422
diff changeset
34 set pos 0
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
35 set len [string length $str]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
36 set ulen 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
37 set ustr ""
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
38 set result {}
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
39 while {$pos < $len} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
40 set end [string wordend $str $pos]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
41 set new [expr $end - $pos + 1]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
42 if {$ulen + $new < $maxlen} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
43 append ustr [string range $str $pos $end]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
44 set ulen [expr $ulen + $new]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
45 } else {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
46 append ustr [string range $str $pos $end]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
47 lappend result $ustr
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
48 set ustr ""
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
49 set ulen 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
50 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
51 set pos [expr $end + 1]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
52 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
53 if {$ustr != ""} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
54 lappend result $ustr
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
55 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
56 return $result
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
314
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
60 proc utl_match_delim_list {ulist ustr} {
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
61 foreach ukey [split $ulist ";"] {
316
682adbf6f09f urllog: 10L Bugfix.
Matti Hamalainen <ccr@tnsp.org>
parents: 314
diff changeset
62 if {[string match $ukey $ustr]} {
314
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
63 return 1
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
64 }
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
65 }
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
66 return 0
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
67 }
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
68
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
69
432
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
70 # Send IRC message with given message type, splitting the
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
71 # string to fit to IRCNet (etc.) max message length.
426
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
72 proc utl_msg_do {upreferredmsg upublic unick uchan umsg} {
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
73 foreach uline [utl_str_split $umsg 450] {
428
04021e2e26c3 utillib: Fix silly bugs.
Matti Hamalainen <ccr@tnsp.org>
parents: 426
diff changeset
74 if {$upublic == 1} {
04021e2e26c3 utillib: Fix silly bugs.
Matti Hamalainen <ccr@tnsp.org>
parents: 426
diff changeset
75 putserv "$upreferredmsg $uchan :$uline"
426
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
76 } else {
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
77 putserv "$upreferredmsg $unick :$uline"
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
78 }
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
79 }
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
80 }
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
81
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
82
432
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
83 # Return formatted time for given UNIX timestamp
422
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
84 proc utl_ctime {utime} {
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
85 if {$utime == "" || $utime == "*"} {
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
86 set utime 0
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
87 }
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
88 return [clock format $utime -format "%d.%m.%Y %H:%M"]
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
89 }
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
90
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
91
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
92 ###
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
93 ### SQL database handling
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
94 ###
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
95 proc utl_tdbc_sql_init {ndb_handle db_type db_name db_host db_port db_user db_pass} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
96 upvar $ndb_handle udb_handle
424
825cac46b1cb Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 422
diff changeset
97
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
98 if {$db_type == "sqlite"} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
99 if {$db_name == ""} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
100 putlog " SQLite3 database file not set."
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
101 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
102 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
103 if {![file exists $db_name]} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
104 putlog " URLLog SQLite3 database file '$db_name' not found, or not accessible!"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
105 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
106 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
107 package require tdbc::sqlite3 1.0
310
d56509a6f435 utillib: Simplify SQL connection creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 295
diff changeset
108 if {[catch {set udb_handle [tdbc::sqlite3::connection new $db_name]} db_errmsg]} {
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
109 putlog " Could not open SQLite3 database '$db_name': $db_errmsg"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
110 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
111 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
112 putlog " (Using SQLite3 database $db_name)"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
113 } elseif {$db_type == "postgres" || $db_type == "mysql"} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
114 # Check parameters
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
115 set db_args {}
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
116 if {$db_user != ""} { lappend db_args -user $db_user }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
117 if {$db_pass != ""} { lappend db_args -passwd $db_pass }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
118 if {$db_host != ""} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
119 lappend db_args -host $db_pass
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
120 if {$db_posrt != 0} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
121 lappend db_args -port $db_port
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
122 set db_host "${db_host}:${db_port}"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
123 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
124 } else {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
125 set db_host "localhost"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
126 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
127 if {$db_name == ""} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
128 putlog " Database name not set."
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
129 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
130 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
131 lappend db_args -database $db_name
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
132
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
133 if {$db_type == "postgres"} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
134 package require tdbc::postgres 1.0
310
d56509a6f435 utillib: Simplify SQL connection creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 295
diff changeset
135 if {[catch {set udb_handle [tdbc::postgres::connection new {*}$db_args]} db_errmsg]} {
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
136 putlog " Could not connect to PostgreSQL database '$db_name @ $db_host': $db_errmsg"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
137 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
138 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
139 putlog " (Using PostgreSQL database $db_name @ $db_host)"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
140 } elseif {$db_type == "mysql"} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
141 package require tdbc::mysql 1.0
310
d56509a6f435 utillib: Simplify SQL connection creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 295
diff changeset
142 if {[catch {set udb_handle [tdbc::mysql::connection new {*}$db_args]} db_errmsg]} {
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
143 putlog " Could not connect to MySQL database '$db_name @ $db_host': $db_errmsg"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
144 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
145 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
146 putlog " (Using MySQL database $db_name @ $db_host)"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
147 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
148 } else {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
149 putlog " Invalid or unsupported database type: '$db_type'."
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
150 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
151 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
152 return 1
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
153 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
154
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
155
295
141bb4a2b76f utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents: 290
diff changeset
156 proc utl_escape {str} {
141bb4a2b76f utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents: 290
diff changeset
157 return [string map {' ''} $str]
141bb4a2b76f utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents: 290
diff changeset
158 }
141bb4a2b76f utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents: 290
diff changeset
159
141bb4a2b76f utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents: 290
diff changeset
160
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
161 ###
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
162 ### Functions for certain scripts
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
163 ###
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
164 proc utl_confirm_yesno { uprompt } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
165 puts -nonewline "$uprompt \[y/N\]? "
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
166 flush stdout
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
167 set response [gets stdin]
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
168 if {[string tolower $response] == "y"} {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
169 return 1
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
170 } else {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
171 return 0
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
172 }
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
176 proc utl_drop_table { dbh utable } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
177 puts "Dropping current table '$utable'."
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
178 if {[catch {dbh eval "DROP TABLE $utable"} uerrmsg]} {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
179 puts "Dropping table resulted in error (ignored): $uerrmsg."
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
180 }
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
183
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
184 proc utl_create_table { dbh utable usql } {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
185 puts "Creating new table '$utable'."
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
186 if {[catch {dbh eval "CREATE TABLE $utable ($usql)"} uerrmsg]} {
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
187 puts "Error creating table: $uerrmsg."
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
188 return 0
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
189 }
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
190 return 1
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
193
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
194 proc utl_create_table_or_fail { dbh utable usql } {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
195 if {![utl_create_table $dbh $utable $usql]} {
63
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
196 exit 3
7b03971c6d28 Remove tabs and reindent.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
197 }
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 }