changeset 24:ac38d12e3b72

Add conversion script for Spede, MN and Tuksu style quote databases.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 05 Sep 2011 18:39:03 +0300
parents 9ba859d9f931
children 65f94adedc98
files convert_spede_mn_tuksu.tcl
diffstat 1 files changed, 95 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/convert_spede_mn_tuksu.tcl	Mon Sep 05 18:39:03 2011 +0300
@@ -0,0 +1,95 @@
+#!/usr/bin/tclsh
+# TCL script for converting Spede, MN and Tuksu databases from
+# flatfile format to SQLite3 database.
+#
+# Written by Matti 'ccr' Hamalainen <ccr@tnsp.org>
+# (C) Copyright 2011 Tecnic Software productions (TNSP)
+#
+package require sqlite3
+
+### Check commandline arguments
+if {$argc != 3} {
+	puts "Usage: $argv0 <input_flat_file_db> <output_sqlite_db_file> <table_name>"
+	exit 0
+}
+
+set db_input [lindex $argv 0]
+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."
+	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
+}
+
+### Convert data
+puts -nonewline "Converting database, please wait "
+set nline 0
+seek $fd 0 start
+while {![eof $fd]} {
+	incr nline
+	set line [gets $fd]
+	if {$line != ""} {
+		set items [split $line "|"]
+		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]} {
+			puts "\nError ($nline): $uerrmsg on:\n$sql"
+			exit 15
+		}
+	}
+	if {[expr $nline % 10] == 1} {
+		puts -nonewline "."
+		flush stdout
+	}
+}
+
+urldb close
+close $fd
+
+puts "OK"
+puts "New SQLite3 database is in file '$db_output'"
+