view util_convert.tcl @ 58:0da3461eb871

quotedb: Retrieving total number of quotes in the table does not seem to be working, so commenting out the code.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 08 Sep 2011 22:14:27 +0300
parents a825a4e27627
children 7b03971c6d28
line wrap: on
line source

### 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 ufail } {
	
	set sql1 "id INTEGER PRIMARY KEY AUTOINCREMENT, utime INT, utext VARCHAR(2048), user VARCHAR(32), host VARCHAR(256), chan VARCHAR(32)"
	set sql2 "id INTEGER PRIMARY KEY AUTOINCREMENT, user VARCHAR(32), urlid INTEGER, vote INTEGER"

	if {$ufail} {
		create_table_or_fail "$utable" "$sql1"
		create_table_or_fail "${utable}_votes" "$sql2"
	} else {
		create_table "$utable" "$sql1"
		create_table "${utable}_votes" "$sql2"
	}
}