aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJochen Topf <jochen@topf.org>2013-01-11 12:02:51 +0100
committerJochen Topf <jochen@topf.org>2013-01-11 12:02:51 +0100
commit71e1327b05eae06a39128b1bd288c4685861ab10 (patch)
tree34a6ef9e46f03bd09527a1827d85fadad0abccec
parent3df767aa62268e209a4c0749f82aa594919a0af5 (diff)
downloadtaginfo-71e1327b05eae06a39128b1bd288c4685861ab10.tar
taginfo-71e1327b05eae06a39128b1bd288c4685861ab10.tar.gz
Changed database initialization code.
All sources are now automatically attached.
-rw-r--r--web/lib/sql.rb32
-rwxr-xr-xweb/taginfo.rb12
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