diff convert_spede_mn_tuksu.tcl @ 33:8a6bfcf1f57b

Use the functions that were implemented in the utility library. Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 06 Sep 2011 16:33:26 +0300
parents ac38d12e3b72
children dccd6c47f9cd
line wrap: on
line diff
--- a/convert_spede_mn_tuksu.tcl	Tue Sep 06 16:32:51 2011 +0300
+++ b/convert_spede_mn_tuksu.tcl	Tue Sep 06 16:33:26 2011 +0300
@@ -6,6 +6,7 @@
 # (C) Copyright 2011 Tecnic Software productions (TNSP)
 #
 package require sqlite3
+source util_convert.tcl
 
 ### Check commandline arguments
 if {$argc != 3} {
@@ -17,52 +18,24 @@
 set db_output [lindex $argv 1]
 set db_table [lindex $argv 2]
 
-
 ### Ask for confirmation
 puts "Conversion of '$db_input' to SQLite database '$db_output', to table '$db_table'."
 puts "NOTICE! This WILL destroy the current data in table '$db_table'!"
 
-puts -nonewline "Proceed \[y/N\]? "
-flush stdout
-set response [gets stdin]
-if {[string tolower $response] != "y"} {
-	puts "OK, aborting."
+if {![confirm_yesno "Proceed"]} {
 	exit 0
 }
 
-### Helper functions
-proc escape { str } {
-	return [string map {' ''} $str]
-}
-
-proc lescape { lst pos } {
-	return [escape [lindex $lst $pos]]
-}
-
 ### Open flatfile for reading
 if {[catch {set fd [open $db_input r]} uerrmsg]} {
 	puts "Could not open file '$db_input' for reading: $uerrmsg"
 	exit 1
 }
 
-### Open SQLite database
-if {[catch {sqlite3 urldb $db_output} uerrmsg]} {
-	puts "Could not open SQLite3 database '$db_output': $uerrmsg"
-	exit 2
-}
-
-### Drop old table
-puts "Dropping current '$db_table' table."
-if {[catch {urldb eval "DROP TABLE $db_table"} uerrmsg]} {
-	puts "Dropping table resulted in error (ignored): $uerrmsg"
-}
-
-### Create new
-puts "Creating new table '$db_table'."
-if {[catch {urldb eval "CREATE TABLE $db_table (id INTEGER PRIMARY KEY AUTOINCREMENT, utime INT, utext VARCHAR(2048), user VARCHAR(32), host VARCHAR(256), chan VARCHAR(32))"} uerrmsg]} {
-	puts "Error creating table! $uerrmsg"
-	exit 3
-}
+### Open SQLite database, drop old tables, create new
+open_db $db_output
+drop_table $db_table
+create_table_quotes_votes $db_table
 
 ### Convert data
 puts -nonewline "Converting database, please wait "
@@ -76,7 +49,7 @@
 		set host [lindex $items 3]
 		set uid [lindex $items 4]
 		set sql "INSERT INTO $db_table (id,utime,utext,user,host,chan) VALUES ([lindex $items 0], [lindex $items 2], '[lescape $items 1]', '[lescape $items 3]', '[lescape $items 4]', '[lescape $items 5]')"
-		if {[catch {urldb eval $sql} uerrmsg]} {
+		if {[catch {dbh eval $sql} uerrmsg]} {
 			puts "\nError ($nline): $uerrmsg on:\n$sql"
 			exit 15
 		}
@@ -87,9 +60,7 @@
 	}
 }
 
-urldb close
+dbh close
 close $fd
 
-puts "OK"
-puts "New SQLite3 database is in file '$db_output'"
-
+puts "DONE."