From 71e1327b05eae06a39128b1bd288c4685861ab10 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Fri, 11 Jan 2013 12:02:51 +0100 Subject: Changed database initialization code. All sources are now automatically attached. --- web/lib/sql.rb | 32 ++++++++++++++++++++++++++------ web/taginfo.rb | 12 ++---------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/web/lib/sql.rb b/web/lib/sql.rb index 4bfb020..7240f05 100644 --- a/web/lib/sql.rb +++ b/web/lib/sql.rb @@ -6,20 +6,40 @@ module SQL # Wrapper for a database connection. class Database - def initialize(dir) - filename = dir + '/taginfo-master.db' - @db = SQLite3::Database.new(filename) - @db.results_as_hash = true + # This has to be called once to initialize the context for the database + def self.init(dir) + @@dir = dir - [:db, :wiki, :josm, :potlatch, :merkaartor, :search].each do |dbname| - @db.execute("ATTACH DATABASE '#{dir}/taginfo-#{dbname}.db' AS #{dbname}") + db = SQL::Database.new + + db.select('SELECT * FROM sources ORDER BY no').execute().each do |source| + Source.new(source['id'], source['name'], source['data_until'], source['update_start'], source['update_end'], source['visible'].to_i == 1) end + data_until = db.select("SELECT min(data_until) FROM sources").get_first_value().sub(/:..$/, '') + + db.close + + data_until + end + + def initialize + filename = @@dir + '/taginfo-master.db' + @db = SQLite3::Database.new(filename) + @db.results_as_hash = true + @db.execute('SELECT * FROM languages') do |row| Language.new(row) end end + def attach_sources + Source.each do |source| + @db.execute("ATTACH DATABASE ? AS ?", "#{ @@dir }/#{ source.dbname }", source.id.to_s) + end + self + end + def close @db.close @db = nil diff --git a/web/taginfo.rb b/web/taginfo.rb index 386bc51..ab6a96e 100755 --- a/web/taginfo.rb +++ b/web/taginfo.rb @@ -56,15 +56,7 @@ TaginfoConfig.read BCP47::read_registry -db = SQL::Database.new('../../data') - -db.select('SELECT * FROM sources ORDER BY no').execute().each do |source| - Source.new source['id'], source['name'], source['data_until'], source['update_start'], source['update_end'], source['visible'].to_i == 1 -end - -DATA_UNTIL = db.select("SELECT min(data_until) FROM sources").get_first_value().sub(/:..$/, '') - -db.close +DATA_UNTIL = SQL::Database.init('../../data'); class Taginfo < Sinatra::Base @@ -124,7 +116,7 @@ class Taginfo < Sinatra::Base # (otherwise switching languages doesn't work) expires 0, :no_cache - @db = SQL::Database.new('../../data') + @db = SQL::Database.new.attach_sources @data_until = DATA_UNTIL end -- cgit v1.2.3