view util_convert.tcl @ 35:f4dd229b17d1

Fail if votes table exists.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 06 Sep 2011 18:16:13 +0300
parents 1ddfada536ff
children b24cb1dd0423
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 } {
	create_table_or_fail "${utable}_votes" "id INTEGER PRIMARY KEY AUTOINCREMENT, user VARCHAR(32), urlid INTEGER, vote INTEGER"
	create_table_or_fail "$utable" "id INTEGER PRIMARY KEY AUTOINCREMENT, utime INT, utext VARCHAR(2048), user VARCHAR(32), host VARCHAR(256), chan VARCHAR(32)"
}