checking if a table has been dropped
This commit is contained in:
parent
3cff900564
commit
fe3e5f5df5
@ -5,6 +5,7 @@ from sqlalchemy.sql import and_, expression
|
|||||||
from sqlalchemy.schema import Column, Index
|
from sqlalchemy.schema import Column, Index
|
||||||
|
|
||||||
from dataset.persistence.util import guess_type
|
from dataset.persistence.util import guess_type
|
||||||
|
from dataset.util import DatasetException
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -16,6 +17,7 @@ class Table(object):
|
|||||||
self.indexes = {}
|
self.indexes = {}
|
||||||
self.database = database
|
self.database = database
|
||||||
self.table = table
|
self.table = table
|
||||||
|
self._is_dropped = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def columns(self):
|
def columns(self):
|
||||||
@ -37,10 +39,15 @@ class Table(object):
|
|||||||
the table, make sure to get a fresh instance from the
|
the table, make sure to get a fresh instance from the
|
||||||
:py:class:`Database <dataset.Database>`.
|
:py:class:`Database <dataset.Database>`.
|
||||||
"""
|
"""
|
||||||
|
self._is_dropped = True
|
||||||
with self.database.lock:
|
with self.database.lock:
|
||||||
self.database.tables.pop(self.table.name, None)
|
self.database.tables.pop(self.table.name, None)
|
||||||
self.table.drop(engine)
|
self.table.drop(engine)
|
||||||
|
|
||||||
|
def _check_dropped(self):
|
||||||
|
if self._is_dropped:
|
||||||
|
raise DatasetException('the table has been dropped. this object should not be used again.')
|
||||||
|
|
||||||
def insert(self, row, ensure=True, types={}):
|
def insert(self, row, ensure=True, types={}):
|
||||||
"""
|
"""
|
||||||
Add a row (type: dict) by inserting it into the table.
|
Add a row (type: dict) by inserting it into the table.
|
||||||
@ -57,6 +64,7 @@ class Table(object):
|
|||||||
data = dict(title='I am a banana!')
|
data = dict(title='I am a banana!')
|
||||||
table.insert(data)
|
table.insert(data)
|
||||||
"""
|
"""
|
||||||
|
self._check_dropped()
|
||||||
if ensure:
|
if ensure:
|
||||||
self._ensure_columns(row, types=types)
|
self._ensure_columns(row, types=types)
|
||||||
self.database.engine.execute(self.table.insert(row))
|
self.database.engine.execute(self.table.insert(row))
|
||||||
@ -79,6 +87,7 @@ class Table(object):
|
|||||||
for row in chunk:
|
for row in chunk:
|
||||||
self._ensure_columns(row, types=types)
|
self._ensure_columns(row, types=types)
|
||||||
self.table.insert().execute(chunk)
|
self.table.insert().execute(chunk)
|
||||||
|
self._check_dropped()
|
||||||
chunk = []
|
chunk = []
|
||||||
i = 0
|
i = 0
|
||||||
for row in rows:
|
for row in rows:
|
||||||
@ -107,6 +116,7 @@ class Table(object):
|
|||||||
they will be created based on the settings of ``ensure`` and
|
they will be created based on the settings of ``ensure`` and
|
||||||
``types``, matching the behavior of :py:meth:`insert() <dataset.Table.insert>`.
|
``types``, matching the behavior of :py:meth:`insert() <dataset.Table.insert>`.
|
||||||
"""
|
"""
|
||||||
|
self._check_dropped()
|
||||||
if not len(keys):
|
if not len(keys):
|
||||||
return False
|
return False
|
||||||
clause = [(u, row.get(u)) for u in keys]
|
clause = [(u, row.get(u)) for u in keys]
|
||||||
@ -129,6 +139,7 @@ class Table(object):
|
|||||||
data = dict(id=10, title='I am a banana!')
|
data = dict(id=10, title='I am a banana!')
|
||||||
table.upsert(data, ['id'])
|
table.upsert(data, ['id'])
|
||||||
"""
|
"""
|
||||||
|
self._check_dropped()
|
||||||
if ensure:
|
if ensure:
|
||||||
self.create_index(keys)
|
self.create_index(keys)
|
||||||
|
|
||||||
@ -146,6 +157,7 @@ class Table(object):
|
|||||||
|
|
||||||
If no arguments are given, all records are deleted.
|
If no arguments are given, all records are deleted.
|
||||||
"""
|
"""
|
||||||
|
self._check_dropped()
|
||||||
q = self._args_to_clause(filter)
|
q = self._args_to_clause(filter)
|
||||||
stmt = self.table.delete(q)
|
stmt = self.table.delete(q)
|
||||||
self.database.engine.execute(stmt)
|
self.database.engine.execute(stmt)
|
||||||
@ -175,6 +187,7 @@ class Table(object):
|
|||||||
|
|
||||||
table.create_column('created_at', sqlalchemy.DateTime)
|
table.create_column('created_at', sqlalchemy.DateTime)
|
||||||
"""
|
"""
|
||||||
|
self._check_dropped()
|
||||||
with self.database.lock:
|
with self.database.lock:
|
||||||
if name not in self.table.columns.keys():
|
if name not in self.table.columns.keys():
|
||||||
col = Column(name, type)
|
col = Column(name, type)
|
||||||
@ -188,6 +201,7 @@ class Table(object):
|
|||||||
|
|
||||||
table.create_index(['name', 'country'])
|
table.create_index(['name', 'country'])
|
||||||
"""
|
"""
|
||||||
|
self._check_dropped()
|
||||||
with self.database.lock:
|
with self.database.lock:
|
||||||
if not name:
|
if not name:
|
||||||
sig = abs(hash('||'.join(columns)))
|
sig = abs(hash('||'.join(columns)))
|
||||||
@ -210,6 +224,7 @@ class Table(object):
|
|||||||
|
|
||||||
row = table.find_one(country='United States')
|
row = table.find_one(country='United States')
|
||||||
"""
|
"""
|
||||||
|
self._check_dropped()
|
||||||
res = list(self.find(_limit=1, **filter))
|
res = list(self.find(_limit=1, **filter))
|
||||||
if not len(res):
|
if not len(res):
|
||||||
return None
|
return None
|
||||||
@ -245,6 +260,7 @@ class Table(object):
|
|||||||
|
|
||||||
For more complex queries, please use :py:meth:`db.query() <dataset.Database.query>`
|
For more complex queries, please use :py:meth:`db.query() <dataset.Database.query>`
|
||||||
instead."""
|
instead."""
|
||||||
|
self._check_dropped()
|
||||||
if isinstance(order_by, (str, unicode)):
|
if isinstance(order_by, (str, unicode)):
|
||||||
order_by = [order_by]
|
order_by = [order_by]
|
||||||
order_by = [self._args_to_order_by(o) for o in order_by]
|
order_by = [self._args_to_order_by(o) for o in order_by]
|
||||||
@ -286,6 +302,7 @@ class Table(object):
|
|||||||
# you can also combine this with a filter
|
# you can also combine this with a filter
|
||||||
table.distinct('year', country='China')
|
table.distinct('year', country='China')
|
||||||
"""
|
"""
|
||||||
|
self._check_dropped()
|
||||||
qargs = []
|
qargs = []
|
||||||
try:
|
try:
|
||||||
columns = [self.table.c[c] for c in columns]
|
columns = [self.table.c[c] for c in columns]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user