def __init__(self, options): self.options = options self.osmdata = OsmSourceTables(MetaData(), nodestore=self.get_option('nodestore'), status_table=self.get_option('status', True)) if not self.get_option('no_engine'): dba = URL('postgresql', username=options.username, password=options.password, database=options.database) self.engine = create_engine(dba, echo=self.get_option('echo_sql', False)) self.metadata = MetaData(schema=self.get_option('schema')) self.tables = self.create_tables()
def init_tables(self, mapdb): class Config: table_name = 'test' node_tag = 'rwn_ref' src = mapdb.add_table( 'src', OsmSourceTables.create_node_table(mapdb.metadata)) mapdb.add_table('test', NetworkNodes(mapdb.metadata, src, Config)) mapdb.create()
def init_tables(self, mapdb, segment_table, countries, shields): rels = mapdb.add_table( 'src_rels', OsmSourceTables.create_relation_table(mapdb.metadata)) hier = mapdb.add_table( 'hierarchy', RelationHierarchy(mapdb.metadata, 'hierarchy', rels)) mapdb.add_table( 'test', Routes(mapdb.metadata, rels, segment_table, hier, countries, self.Config, shields)) mapdb.create()
def step_impl(context): meta = MetaData() context.osmdata = OsmSourceTables(meta, nodestore=context.nodestore_file) meta.create_all(context.engine) grid = dict(context.nodegrid) if 'nodegrid' in context else None with context.engine.begin() as conn: for row in context.table: _insert_element(row, context.osmdata, conn, grid, context.tagsets) if grid is not None: for oid in tuple(grid.keys()): _insert_node(oid, {}, None, context.osmdata, conn, grid)
def init_tables(self, mapdb, route_table, segment_table): rels = mapdb.add_table( 'src_rels', OsmSourceTables.create_relation_table(mapdb.metadata)) hier = mapdb.add_table( 'hierarchy', RelationHierarchy(mapdb.metadata, 'hierarchy', rels)) uptable = mapdb.add_table( 'updates', UpdatedGeometriesTable(mapdb.metadata, 'updates')) mapdb.add_table( 'test', StyleTable(mapdb.metadata, route_table, segment_table, hier, self.Config(), uptable)) mapdb.create() mapdb.insert_into('routes')\ .line(1, name='A')\ .line(2, name='B')\ .line(3, name='C')
class MapDB: """Basic class for creation and modification of a complete database. Subclass this for each database and supply the create_tables() function. `options` may be extended with arbitrary attributes by subclasses. MapDB currently makes use of the following: * '''nodestore''' - filename of the location for the the node store. * '''schema''' - schema associated with this DB. The only effect this currently has is that the create action will attempt to create the schema. * '''ro_user''' - read-only user to grant rights to for all tables. Only used for create action. """ def __init__(self, options): self.options = options self.osmdata = OsmSourceTables(MetaData(), nodestore=self.get_option('nodestore'), status_table=self.get_option('status', True)) if not self.get_option('no_engine'): dba = URL('postgresql', username=options.username, password=options.password, database=options.database) self.engine = create_engine(dba, echo=self.get_option('echo_sql', False)) self.metadata = MetaData(schema=self.get_option('schema')) self.tables = self.create_tables() def get_option(self, option, default=None): """Return the value of the given option or None if not set. """ return getattr(self.options, option, default) def has_option(self, option): return hasattr(self.options, option) def create(self): schema = self.get_option('schema') rouser = self.get_option('ro_user') with self.engine.begin() as conn: if schema is not None: conn.execute(CreateSchema(schema)) if rouser is not None: conn.execute('GRANT USAGE ON SCHEMA %s TO "%s"' % (schema, rouser)) for t in self.tables: t.create(conn) if rouser is not None: for t in self.tables: if schema: tname = '%s.%s' % (schema, str(t.data.name)) else: tname = str(t.data.name) conn.execute('GRANT SELECT ON TABLE %s TO "%s"' % (tname, rouser)) def construct(self): if self.has_option('schema'): tname = '%s.%%s' % self.options.schema else: tname = '%s' for tab in self.tables: log.info("Importing %s..." % str(tab.data.name)) tab.construct(self.engine) self.osmdata.set_status_from(self.engine, tname % str(tab.data.name), 'base') def update(self): base_state = self.osmdata.get_status(self.engine) schema = self.get_option('schema') if self.has_option('schema'): tname = '%s.%%s' % self.options.schema else: tname = '%s' for tab in self.tables: status_name = tname % str(tab.data.name) if base_state is not None: table_state = self.osmdata.get_status(self.engine, status_name) if table_state is not None and table_state >= base_state: log.info("Table %s already up-to-date." % tab) continue if hasattr(tab, 'before_update'): tab.before_update(self.engine) log.info("Updating %s..." % str(tab.data.name)) tab.update(self.engine) if hasattr(tab, 'after_update'): tab.after_update(self.engine) self.osmdata.set_status_from(self.engine, status_name, 'base') def finalize(self, dovacuum): conn = self.engine.connect()\ .execution_options(isolation_level="AUTOCOMMIT") with conn.begin() as trans: for tab in self.tables: conn.execute(Analyse(tab.data, dovacuum));