Skip to content

Commit

Permalink
Cockroach 1.3 does not support fetchrow
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon Navarro Bosch committed Jul 18, 2017
1 parent ee2826b commit df76ffc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions guillotina/db/storages/cockroach.py
Expand Up @@ -300,3 +300,9 @@ async def commit(self, transaction):
logger.warning('Do not have db transaction to commit')

return transaction._tid

# Cockroach cant use at version 1.0.3 row count (no fetch)
async def get_one_row(self, smt, *args):
# Helper function to provide easy adaptation to cockroach
result = await smt.fetch(*args)
return result[0] if len(result) > 0 else None
16 changes: 10 additions & 6 deletions guillotina/db/storages/pg.py
Expand Up @@ -446,7 +446,7 @@ async def close(self, con):
async def load(self, txn, oid):
async with txn._lock:
smt = await txn._db_conn.prepare(GET_OID)
objects = await smt.fetchrow(oid)
objects = await self.get_one_row(smt, oid)
if objects is None:
raise KeyError(oid)
return objects
Expand Down Expand Up @@ -531,6 +531,10 @@ async def get_current_tid(self, txn):
# again, use storage lock here instead of trns lock
return await self._stmt_max_tid.fetchval()

async def get_one_row(self, smt, *args):
# Helper function to provide easy adaptation to cockroach
return await smt.fetchrow(*args)

def _db_transaction_factory(self, txn):
# make sure asycpg knows this is a new transaction
if txn._db_conn._con is not None:
Expand Down Expand Up @@ -610,13 +614,13 @@ async def keys(self, txn, oid):
async def get_child(self, txn, parent_oid, id):
async with txn._lock:
smt = await txn._db_conn.prepare(GET_CHILD)
result = await smt.fetchrow(parent_oid, id)
result = await self.get_one_row(smt, parent_oid, id)
return result

async def has_key(self, txn, parent_oid, id):
async with txn._lock:
smt = await txn._db_conn.prepare(EXIST_CHILD)
result = await smt.fetchrow(parent_oid, id)
result = await self.get_one_row(smt, parent_oid, id)
if result is None:
return False
else:
Expand All @@ -639,7 +643,7 @@ async def items(self, txn, oid):
async def get_annotation(self, txn, oid, id):
async with txn._lock:
smt = await txn._db_conn.prepare(GET_ANNOTATION)
result = await smt.fetchrow(oid, id)
result = await self.get_one_row(smt, oid, id)
return result

async def get_annotation_keys(self, txn, oid):
Expand All @@ -651,7 +655,7 @@ async def get_annotation_keys(self, txn, oid):
async def write_blob_chunk(self, txn, bid, oid, chunk_index, data):
async with txn._lock:
smt = await txn._db_conn.prepare(HAS_OBJECT)
result = await smt.fetchrow(oid)
result = await self.get_one_row(smt, oid)
if result is None:
# check if we have a referenced ob, could be new and not in db yet.
# if so, create a stub for it here...
Expand All @@ -666,7 +670,7 @@ async def write_blob_chunk(self, txn, bid, oid, chunk_index, data):
async def read_blob_chunk(self, txn, bid, chunk=0):
async with txn._lock:
smt = await txn._db_conn.prepare(READ_BLOB_CHUNK)
return await smt.fetchrow(bid, chunk)
return await self.get_one_row(smt, bid, chunk)

async def read_blob_chunks(self, txn, bid):
async with txn._lock:
Expand Down

0 comments on commit df76ffc

Please sign in to comment.