changeset 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 96fdf24afd9c
children dccd6c47f9cd
files convert_spede_mn_tuksu.tcl convert_urllog_db.tcl
diffstat 2 files changed, 19 insertions(+), 78 deletions(-) [+]
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."
--- a/convert_urllog_db.tcl	Tue Sep 06 16:32:51 2011 +0300
+++ b/convert_urllog_db.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 != 2} {
@@ -17,53 +18,24 @@
 set db_output [lindex $argv 1]
 set db_table "urls"
 
-
 ### 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 '$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, url VARCHAR(2048), user VARCHAR(32), host VARCHAR(256), chan VARCHAR(32))}} uerrmsg]" {
-	puts "Error creating table! $uerrmsg"
-	exit 3
-}
+### Open SQLite database, drop old table, create new
+open_db $db_output
+drop_table "urls"
+create_table_urls
 
 ### Detect URL database version
 puts -nonewline "Detecting database version: "
@@ -90,7 +62,6 @@
 ### Show some information
 puts "Database contains $nline records, with $minentries / $maxentries entries."
 
-
 ### Begin conversion
 puts -nonewline "Converting database, please wait ... round #1 "
 set nline 0
@@ -107,7 +78,7 @@
 		set uid [lindex $items 4]
 		if {$uid != ""} {
 			set sql "INSERT INTO $db_table (id,utime,url,user,host) VALUES ($uid, [lindex $items 1], '[lescape $items 0]', '[lescape $items 2]', '[escape $host]')"
-			if {[catch {urldb eval $sql} uerrmsg]} {
+			if {[catch {dbh eval $sql} uerrmsg]} {
 				puts "\nError ($nline): $uerrmsg on:\n$sql"
 				exit 15
 			}
@@ -135,7 +106,7 @@
 		set uid [lindex $items 4]
 		if {$uid == ""} {
 			set sql "INSERT INTO $db_table (utime,url,user,host) VALUES ([lindex $items 1], '[lescape $items 0]', '[lescape $items 2]', '[escape $host]')"
-			if {[catch {urldb eval $sql} uerrmsg]} {
+			if {[catch {dbh eval $sql} uerrmsg]} {
 				puts "\nError ($nline): $uerrmsg on:\n$sql"
 				exit 15
 			}
@@ -147,8 +118,7 @@
 	}
 }
 
-urldb close
+dbh close
 close $fd
 
-puts "OK"
-puts "New SQLite3 database is in file '$db_output'"
+puts "DONE."