Add tables property to database.

This commit is contained in:
Friedrich Lindenberg 2013-04-01 22:09:16 +02:00
parent 0f81dbe52c
commit 6ee58e850d
2 changed files with 14 additions and 5 deletions

View File

@ -27,7 +27,13 @@ class Database(object):
self.engine = construct_engine(engine) self.engine = construct_engine(engine)
self.metadata = MetaData() self.metadata = MetaData()
self.metadata.bind = self.engine self.metadata.bind = self.engine
self.tables = {} self.metadata.reflect(self.engine)
self._tables = {}
@property
def tables(self):
""" Get a listing of all tables that exist in the database. """
return self.metadata.tables.keys()
def create_table(self, table_name): def create_table(self, table_name):
""" Creates a new table. Returns a :py:class:`dataset.Table` instance.""" """ Creates a new table. Returns a :py:class:`dataset.Table` instance."""
@ -37,7 +43,7 @@ class Database(object):
col = Column('id', Integer, primary_key=True) col = Column('id', Integer, primary_key=True)
table.append_column(col) table.append_column(col)
table.create(self.engine) table.create(self.engine)
self.tables[table_name] = table self._tables[table_name] = table
return Table(self, table) return Table(self, table)
def load_table(self, table_name): def load_table(self, table_name):
@ -45,7 +51,7 @@ class Database(object):
with self.lock: with self.lock:
log.debug("Loading table: %s on %r" % (table_name, self)) log.debug("Loading table: %s on %r" % (table_name, self))
table = SQLATable(table_name, self.metadata, autoload=True) table = SQLATable(table_name, self.metadata, autoload=True)
self.tables[table_name] = table self._tables[table_name] = table
return Table(self, table) return Table(self, table)
def get_table(self, table_name): def get_table(self, table_name):
@ -53,8 +59,8 @@ class Database(object):
Returns a :py:class:`dataset.Table` instance. Alternatively to *get_table* Returns a :py:class:`dataset.Table` instance. Alternatively to *get_table*
you can also get tables using the dict syntax.""" you can also get tables using the dict syntax."""
with self.lock: with self.lock:
if table_name in self.tables: if table_name in self._tables:
return Table(self, self.tables[table_name]) return Table(self, self._tables[table_name])
if self.engine.has_table(table_name): if self.engine.has_table(table_name):
return self.load_table(table_name) return self.load_table(table_name)
else: else:

View File

@ -12,6 +12,9 @@ class DatabaseTestCase(unittest.TestCase):
for row in TEST_DATA: for row in TEST_DATA:
self.tbl.insert(row) self.tbl.insert(row)
def test_tables(self):
assert self.db.tables==['weather'], self.db.tables
def test_create_table(self): def test_create_table(self):
table = self.db['foo'] table = self.db['foo']
assert table.table.exists() assert table.table.exists()