Skip to content

Commit

Permalink
Back-out changes to use sqlite returning.
Browse files Browse the repository at this point in the history
This caused some unexpected issues when the returning cursor was not
consumed (e.g. within a savepoint), that cause the savepoint to not be
able to be released. Since this is backwards-incompatible, I am going to
roll it back for now.
  • Loading branch information
coleifer committed Oct 28, 2021
1 parent 18181da commit 8c80725
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
9 changes: 1 addition & 8 deletions playhouse/apsw_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,7 @@ def load_extension(self, extension):
conn.loadextension(extension)

def last_insert_id(self, cursor, query_type=None):
if not self.returning_clause:
return cursor.getconnection().last_insert_rowid()
elif query_type == Insert.SIMPLE:
try:
return cursor[0][0]
except (AttributeError, IndexError, TypeError):
return cursor.getconnection().last_insert_rowid()
return cursor
return cursor.getconnection().last_insert_rowid()

def rows_affected(self, cursor):
return cursor.getconnection().changes()
Expand Down
1 change: 0 additions & 1 deletion playhouse/sqlcipher_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@


class _SqlCipherDatabase(object):
returning_clause = __sqlcipher_version__ >= (3, 35, 0)
server_version = __sqlcipher_version__

def _connect(self):
Expand Down
13 changes: 0 additions & 13 deletions playhouse/sqlite_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from peewee import VirtualField
from peewee import merge_dict
from peewee import sqlite3
from peewee import __sqlite_version__
try:
from playhouse._sqlite_ext import (
backup,
Expand Down Expand Up @@ -944,8 +943,6 @@ def _sqlite_regexp(regex, value):


class SqliteExtDatabase(SqliteDatabase):
returning_clause = __sqlite_version__ >= (3, 35, 0)

def __init__(self, database, c_extensions=None, rank_functions=True,
hash_functions=False, regexp_function=False,
bloomfilter=False, json_contains=False, *args, **kwargs):
Expand Down Expand Up @@ -986,16 +983,6 @@ def _add_conn_hooks(self, conn):
if self._row_factory:
conn.row_factory = self._row_factory

def last_insert_id(self, cursor, query_type=None):
if not self.returning_clause:
return cursor.lastrowid
elif query_type == Insert.SIMPLE:
try:
return cursor[0][0]
except (AttributeError, IndexError, TypeError):
return cursor.lastrowid
return cursor

def row_factory(self, fn):
self._row_factory = fn

Expand Down
24 changes: 16 additions & 8 deletions tests/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2275,14 +2275,16 @@ class KVR(TestModel):
value = IntegerField()


@skip_unless(database.returning_clause, 'returning clause required')
@skip_unless(database.server_version >= (3, 35, 0), 'sqlite returning clause required')
class TestSqliteReturning(ModelTestCase):
database = database
requires = [Person, User, KVR]

def test_sqlite_returning(self):
iq = User.insert_many([{'username': 'u%s' % i} for i in range(3)])
self.assertEqual([r for r, in iq.execute()], [1, 2, 3])
iq = (User
.insert_many([{'username': 'u%s' % i} for i in range(3)])
.returning(User.id))
self.assertEqual([r.id for r in iq.execute()], [1, 2, 3])

res = (User
.insert_many([{'username': 'u%s' % i} for i in (4, 5)])
Expand All @@ -2295,8 +2297,11 @@ def test_sqlite_returning(self):
res = User.insert(username='u6').execute()
self.assertEqual(res, 6)

iq = User.insert_many([{'username': 'u%s' % i} for i in (7, 8, 9)])
curs = iq.namedtuples().execute()
iq = (User
.insert_many([{'username': 'u%s' % i} for i in (7, 8, 9)])
.returning(User)
.namedtuples())
curs = iq.execute()
self.assertEqual([u.id for u in curs], [7, 8, 9])

def test_sqlite_on_conflict_returning(self):
Expand All @@ -2323,11 +2328,14 @@ def test_text_pk(self):
self.assertEqual((res.key, res.value), ('k1', 1))

res = KVR.insert(key='k2', value=2).execute()
self.assertEqual(res, 'k2')
self.assertEqual(res, 2)
#self.assertEqual(res, 'k2')

# insert_many() returns the primary-key as usual.
iq = KVR.insert_many([{'key': 'k%s' % i, 'value': i} for i in (3, 4)])
self.assertEqual([r for r, in iq.execute()], ['k3', 'k4'])
iq = (KVR
.insert_many([{'key': 'k%s' % i, 'value': i} for i in (3, 4)])
.returning(KVR.key))
self.assertEqual([r.key for r in iq.execute()], ['k3', 'k4'])

iq = KVR.insert_many([{'key': 'k%s' % i, 'value': i} for i in (4, 5)])
iq = iq.on_conflict(conflict_target=[KVR.key],
Expand Down

0 comments on commit 8c80725

Please sign in to comment.