Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for sqlalchemy 2 #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions simplekv/db/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,46 @@ def __init__(self, bind, metadata, tablename):
)

def _has_key(self, key):
return self.bind.execute(
select([exists().where(self.table.c.key == key)])
).scalar()
con = self.bind.connect()
with con.begin():
return con.execute(
select(exists().where(self.table.c.key == key))
).scalar()

def _delete(self, key):
self.bind.execute(
self.table.delete(self.table.c.key == key)
)
con = self.bind.connect()
with con.begin():
con.execute(
self.table.delete().where(self.table.c.key == key)
)

def _get(self, key):
rv = self.bind.execute(
select([self.table.c.value], self.table.c.key == key).limit(1)
).scalar()
con = self.bind.connect()
with con.begin():
rv = con.execute(
select(self.table.c.value).where(self.table.c.key == key).limit(1)
).scalar()

if not rv:
raise KeyError(key)
if not rv:
raise KeyError(key)

return rv
return rv

def _open(self, key):
return BytesIO(self._get(key))

def _copy(self, source, dest):
con = self.bind.connect()
with con.begin():
data = self.bind.execute(
select([self.table.c.value], self.table.c.key == source).limit(1)
data = con.execute(
select(self.table.c.value).where(self.table.c.key == source).limit(1)
).scalar()
if not data:
raise KeyError(source)

# delete the potential existing previous key
con.execute(self.table.delete(self.table.c.key == dest))
con.execute(self.table.insert({
con.execute(self.table.delete().where(self.table.c.key == dest))
con.execute(self.table.insert().values({
'key': dest,
'value': data,
}))
Expand All @@ -66,10 +72,10 @@ def _put(self, key, data):
con = self.bind.connect()
with con.begin():
# delete the old
con.execute(self.table.delete(self.table.c.key == key))
con.execute(self.table.delete().where(self.table.c.key == key))

# insert new
con.execute(self.table.insert({
con.execute(self.table.insert().values({
'key': key,
'value': data
}))
Expand All @@ -83,8 +89,10 @@ def _put_file(self, key, file):
return self._put(key, file.read())

def iter_keys(self, prefix=u""):
query = select([self.table.c.key])
query = select(self.table.c.key)
if prefix != "":
query = query.where(self.table.c.key.like(prefix + '%'))
return imap(lambda v: text_type(v[0]),
self.bind.execute(query))
con = self.bind.connect()
with con.begin():
return imap(lambda v: text_type(v[0]),
con.execute(query))
14 changes: 7 additions & 7 deletions tests/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ def engine(self, request):
pytest.skip('could not connect to database {}'.format(dsn))
return engine

@pytest.yield_fixture
@pytest.fixture
def store(self, engine):
metadata = MetaData(bind=engine)
metadata = MetaData()
store = SQLAlchemyStore(engine, metadata, 'simplekv_test')
# create table
store.table.create()
store.table.create(bind=engine)
yield store
metadata.drop_all()
metadata.drop_all(bind=engine)


class TestExtendedKeyspaceSQLAlchemyStore(TestSQLAlchemyStore,
Expand All @@ -54,9 +54,9 @@ class TestExtendedKeyspaceSQLAlchemyStore(TestSQLAlchemyStore,
def store(self, engine):
class ExtendedKeyspaceStore(ExtendedKeyspaceMixin, SQLAlchemyStore):
pass
metadata = MetaData(bind=engine)
metadata = MetaData()
store = ExtendedKeyspaceStore(engine, metadata, 'simplekv_test')
# create table
store.table.create()
store.table.create(bind=engine)
yield store
metadata.drop_all()
metadata.drop_all(bind=engine)