Skip to content

Commit cf41bfa

Browse files
authored
Merge pull request #33 from AnswerDotAI/revert-30-add-returning-to-update
Revert "Add RETURNING to Table.update()"
2 parents cfac77a + 0a5a9a9 commit cf41bfa

File tree

3 files changed

+17
-101
lines changed

3 files changed

+17
-101
lines changed

README.md

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -238,36 +238,13 @@ sqlite-minutils is different from sqlite-utils in that write actions
238238
affected without relying on `last_rowid`. It does this through the
239239
`RETURNING` SQL keyword.
240240

241-
Testing `INSERT`
242-
243241
``` python
244242
user = users.insert(dict(name='Turkey', age=2, pwd='gravy'))
245243
user
246244
```
247245

248-
{'id': 6, 'name': 'Turkey', 'age': 2, 'pwd': 'gravy'}
246+
{'id': 8, 'name': 'Turkey', 'age': 2, 'pwd': 'gravy'}
249247

250248
``` python
251249
test(user['name'], 'Turkey', equals)
252250
```
253-
254-
Testing `UPDATE`
255-
256-
``` python
257-
user = users.insert(dict(name='Flamingo', age=12, pwd='pink'))
258-
user
259-
```
260-
261-
{'id': 8, 'name': 'Flamingo', 'age': 12, 'pwd': 'pink'}
262-
263-
``` python
264-
user = users.update(user['id'], dict(name='Kiwi', pwd='pumpkin'))
265-
user
266-
```
267-
268-
{'id': 7, 'name': 'Kiwi', 'age': 12, 'pwd': 'pumpkin'}
269-
270-
``` python
271-
test(user['name'], 'Kiwi', equals)
272-
test(users.last_pk, user['id'], equals)
273-
```

nbs/index.ipynb

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
"cell_type": "code",
55
"execution_count": null,
66
"metadata": {},
7-
"outputs": [],
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"The autoreload extension is already loaded. To reload it, use:\n",
13+
" %reload_ext autoreload\n"
14+
]
15+
}
16+
],
817
"source": [
918
"#| hide\n",
1019
"%load_ext autoreload\n",
@@ -572,13 +581,6 @@
572581
"sqlite-minutils is different from sqlite-utils in that write actions (`INSERT`, `UPDATE`, `UPSERT`) return back the record(s) they have affected without relying on `last_rowid`. It does this through the `RETURNING` SQL keyword."
573582
]
574583
},
575-
{
576-
"cell_type": "markdown",
577-
"metadata": {},
578-
"source": [
579-
"Testing `INSERT`"
580-
]
581-
},
582584
{
583585
"cell_type": "code",
584586
"execution_count": null,
@@ -587,7 +589,7 @@
587589
{
588590
"data": {
589591
"text/plain": [
590-
"{'id': 6, 'name': 'Turkey', 'age': 2, 'pwd': 'gravy'}"
592+
"{'id': 8, 'name': 'Turkey', 'age': 2, 'pwd': 'gravy'}"
591593
]
592594
},
593595
"execution_count": null,
@@ -608,65 +610,6 @@
608610
"source": [
609611
"test(user['name'], 'Turkey', equals)"
610612
]
611-
},
612-
{
613-
"cell_type": "markdown",
614-
"metadata": {},
615-
"source": [
616-
"Testing `UPDATE`"
617-
]
618-
},
619-
{
620-
"cell_type": "code",
621-
"execution_count": null,
622-
"metadata": {},
623-
"outputs": [
624-
{
625-
"data": {
626-
"text/plain": [
627-
"{'id': 8, 'name': 'Flamingo', 'age': 12, 'pwd': 'pink'}"
628-
]
629-
},
630-
"execution_count": null,
631-
"metadata": {},
632-
"output_type": "execute_result"
633-
}
634-
],
635-
"source": [
636-
"user = users.insert(dict(name='Flamingo', age=12, pwd='pink'))\n",
637-
"user"
638-
]
639-
},
640-
{
641-
"cell_type": "code",
642-
"execution_count": null,
643-
"metadata": {},
644-
"outputs": [
645-
{
646-
"data": {
647-
"text/plain": [
648-
"{'id': 7, 'name': 'Kiwi', 'age': 12, 'pwd': 'pumpkin'}"
649-
]
650-
},
651-
"execution_count": null,
652-
"metadata": {},
653-
"output_type": "execute_result"
654-
}
655-
],
656-
"source": [
657-
"user = users.update(user['id'], dict(name='Kiwi', pwd='pumpkin'))\n",
658-
"user"
659-
]
660-
},
661-
{
662-
"cell_type": "code",
663-
"execution_count": null,
664-
"metadata": {},
665-
"outputs": [],
666-
"source": [
667-
"test(user['name'], 'Kiwi', equals)\n",
668-
"test(users.last_pk, user['id'], equals)"
669-
]
670613
}
671614
],
672615
"metadata": {

sqlite_minutils/db.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,7 +2700,7 @@ def update(
27002700
updates: Optional[dict] = None,
27012701
alter: bool = False,
27022702
conversions: Optional[dict] = None,
2703-
) -> Dict:
2703+
) -> "Table":
27042704
"""
27052705
Execute a SQL ``UPDATE`` against the specified row.
27062706
@@ -2731,27 +2731,23 @@ def update(
27312731
args.append(jsonify_if_needed(value))
27322732
wheres = ["[{}] = ?".format(pk_name) for pk_name in pks]
27332733
args.extend(pk_values)
2734-
sql = "update [{table}] set {sets} where {wheres} returning *".format(
2734+
sql = "update [{table}] set {sets} where {wheres}".format(
27352735
table=self.name, sets=", ".join(sets), wheres=" and ".join(wheres)
27362736
)
27372737
try:
2738-
cursor = self.db.execute(sql, args)
2739-
cursor.rowcount
2738+
rowcount = self.db.execute(sql, args).rowcount
27402739
except OperationalError as e:
27412740
if alter and (" column" in e.args[0]):
27422741
# Attempt to add any missing columns, then try again
27432742
self.add_missing_columns([updates])
2744-
cursor = self.db.execute(sql, args)
2745-
cursor.rowcount
2743+
rowcount = self.db.execute(sql, args).rowcount
27462744
else:
27472745
raise
27482746

27492747
# TODO: Test this works (rolls back) - use better exception:
27502748
# assert rowcount == 1
27512749
self.last_pk = pk_values[0] if len(pks) == 1 else pk_values
2752-
2753-
columns = [c[0] for c in cursor.description]
2754-
return dict(zip(columns, cursor.fetchone()))
2750+
return self
27552751

27562752
def build_insert_queries_and_params(
27572753
self,

0 commit comments

Comments
 (0)