Class: Osmer::Schema::Osm2pgsql

Inherits:
Base
  • Object
show all
Defined in:
lib/osmer/schema/osm2pgsql.rb

Defined Under Namespace

Classes: Dsl

Instance Attribute Summary collapse

Attributes inherited from Base

#name, #ns, #projection

Instance Method Summary collapse

Methods inherited from Base

#recreate!, #table_prefix

Methods included from Configurable

#configure

Constructor Details

#initialize(*args) ⇒ Osm2pgsql

Returns a new instance of Osm2pgsql.



7
8
9
10
# File 'lib/osmer/schema/osm2pgsql.rb', line 7

def initialize(*args)
  @osm2pgsql_binary = 'osm2pgsql'
  super
end

Instance Attribute Details

#bboxObject

Returns the value of attribute bbox.



5
6
7
# File 'lib/osmer/schema/osm2pgsql.rb', line 5

def bbox
  @bbox
end

#osm2pgsql_binaryObject

Returns the value of attribute osm2pgsql_binary.



5
6
7
# File 'lib/osmer/schema/osm2pgsql.rb', line 5

def osm2pgsql_binary
  @osm2pgsql_binary
end

#updaterObject

Returns the value of attribute updater.



5
6
7
# File 'lib/osmer/schema/osm2pgsql.rb', line 5

def updater
  @updater
end

Instance Method Details

#attach_listener!(conn, collection, name, fields) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/osmer/schema/osm2pgsql.rb', line 60

def attach_listener!(conn, collection, name, fields)
  table  = collection_table collection
  args = fields.map{|f| collection_field(collection, f) }.join(', ')

  conn.exec %Q{CREATE OR REPLACE FUNCTION #{name}_insert_proxy() RETURNS trigger AS $$
    BEGIN
      PERFORM #{name}_insert(NEW.osm_id, #{args});
      RETURN NULL;
    END; $$ LANGUAGE plpgsql}

  conn.exec %Q{CREATE OR REPLACE FUNCTION #{name}_update_proxy() RETURNS trigger AS $$
    BEGIN
      PERFORM #{name}_update(NEW.osm_id, #{args});
      RETURN NULL;
    END; $$ LANGUAGE plpgsql}

  conn.exec %Q{CREATE OR REPLACE FUNCTION #{name}_delete_proxy() RETURNS trigger AS $$
    BEGIN
      PERFORM #{name}_delete(OLD.osm_id);
      RETURN NULL;
    END; $$ LANGUAGE plpgsql}

  # Create new triggers
  conn.exec "CREATE TRIGGER #{name}_insert_trigger AFTER INSERT ON #{table} FOR EACH ROW EXECUTE PROCEDURE #{name}_insert_proxy()"
  conn.exec "CREATE TRIGGER #{name}_update_trigger AFTER UPDATE ON #{table} FOR EACH ROW EXECUTE PROCEDURE #{name}_update_proxy()"
  conn.exec "CREATE TRIGGER #{name}_delete_trigger AFTER DELETE ON #{table} FOR EACH ROW EXECUTE PROCEDURE #{name}_delete_proxy()"

  # Prepopulate dependency
  conn.exec "SELECT #{name}_insert(NEW.osm_id, #{args}) FROM #{table} NEW"
end

#create!(db) ⇒ Object

Create schema in given database



13
14
15
16
17
18
19
# File 'lib/osmer/schema/osm2pgsql.rb', line 13

def create!(db)
  db.in_transaction do |conn|
    raise StandardError.new("Schema #{name} already created!") unless schema_tables(conn).empty?
  end

  osm2pgsql_exec db, "'#{empty_file}'", "creating osm2pgsql schema"
end

#detach_listener!(conn, collection, name, fields) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/osmer/schema/osm2pgsql.rb', line 91

def detach_listener!(conn, collection, name, fields)
  table  = collection_table collection

  # Drop triggers
  conn.exec "DROP TRIGGER IF EXISTS #{name}_insert_trigger ON #{table}"
  conn.exec "DROP TRIGGER IF EXISTS #{name}_update_trigger ON #{table}"
  conn.exec "DROP TRIGGER IF EXISTS #{name}_delete_trigger ON #{table}"
end

#drop!(db) ⇒ Object

Drop schema in given database



22
23
24
25
26
27
28
# File 'lib/osmer/schema/osm2pgsql.rb', line 22

def drop!(db)
  db.in_transaction do |conn|
    schema_tables(conn).each do |table|
      conn.exec "DROP TABLE IF EXISTS #{table}"
    end
  end
end

#import_data!(db, file = nil) ⇒ Object

Raises:

  • (StandardError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osmer/schema/osm2pgsql.rb', line 30

def import_data!(db, file = nil)
  raise StandardError.new("No file or updater specified") unless file or updater

  db.in_transaction do |conn|
    schema_tables(conn).each do |table|
      conn.exec "DELETE FROM #{table}"
    end
  end

  if file
    osm2pgsql_exec db, "-a '#{file}'", "importing data with osm2pgsql from #{file}"
  else
    updater.load_dump db, self do |f|
      osm2pgsql_exec db, "-a '#{f}'", "importing data with osm2pgsql from #{f}"
    end
  end
end

#update_data!(db, file = nil) ⇒ Object

Raises:

  • (StandardError)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/osmer/schema/osm2pgsql.rb', line 48

def update_data!(db, file = nil)
  raise StandardError.new("No file or updater specified") unless file or updater

  if file
    osm2pgsql_exec db, "-a '#{file}'", "importing data with osm2pgsql from #{file}"
  else
    updater.load_diffs db, self do |f|
      osm2pgsql_exec db, "-a '#{f}'", "importing data with osm2pgsql from #{f}"
    end
  end
end