view util_convert.tcl @ 178:9b8ec700ede4

Clean up the weather data parser backend a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 03 Jun 2014 15:10:47 +0300
parents 4c51eeba993f
children
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"
  }
}


proc drop_table_feeds { } {
  drop_table "feeds"
}

proc create_table_feeds { } {
  create_table_or_fail "feeds" "feed VARCHAR(64), utime INT, url VARCHAR(512), title VARCHAR(256)"
}