changeset 31:1ddfada536ff

Add utility "library" for database conversion and creation scripts.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 06 Sep 2011 16:32:26 +0300
parents 3cf438db908a
children 96fdf24afd9c
files util_convert.tcl
diffstat 1 files changed, 61 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util_convert.tcl	Tue Sep 06 16:32:26 2011 +0300
@@ -0,0 +1,61 @@
+### Helper functions
+proc escape { str } {
+	return [string map {' ''} $str]
+}
+
+proc lescape { lst pos } {
+	return [escape [lindex $lst $pos]]
+}
+
+
+proc confirm_yesno { uprompt } {
+	puts -nonewline "$uprompt \[y/N\]? "
+	flush stdout
+	set response [gets stdin]
+	if {[string tolower $response] == "y"} {
+		return 1
+	} else {
+		return 0
+	}
+}
+
+proc open_db { dbfile } {
+	global dbh
+	if {[catch {sqlite3 dbh $dbfile} uerrmsg]} {
+		puts "Could not open SQLite3 database '$dbfile': $uerrmsg."
+		exit 2
+	}
+}
+
+proc drop_table { utable } {
+	global dbh
+	puts "Dropping current table '$utable'."
+	if {[catch {dbh eval "DROP TABLE $utable"} uerrmsg]} {
+		puts "Dropping table resulted in error (ignored): $uerrmsg."
+	}
+}
+
+proc create_table { utable usql } {
+	global dbh
+	puts "Creating new table '$utable'."
+	if {[catch {dbh eval "CREATE TABLE $utable ($usql)"} uerrmsg]} {
+		puts "Error creating table: $uerrmsg."
+		return 0
+	}
+	return 1
+}
+
+proc create_table_or_fail { utable usql } {
+	if {![create_table $utable $usql]} {
+		exit 3
+	}
+}
+
+proc create_table_urls { } {
+	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)"
+}
+
+proc create_table_quotes_votes { utable } {
+	create_table_or_fail "$utable" "id INTEGER PRIMARY KEY AUTOINCREMENT, utime INT, utext VARCHAR(2048), user VARCHAR(32), host VARCHAR(256), chan VARCHAR(32)"
+	create_table "${utable}_votes" "id INTEGER PRIMARY KEY AUTOINCREMENT, user VARCHAR(32), urlid INTEGER, vote INTEGER"
+}