Skip to content

Commit 7f3d91d

Browse files
committed
fix precommit and doc
1 parent 0607c56 commit 7f3d91d

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

Doc/library/sqlite3.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,11 @@ Cursor objects
15521552
print(json_result)
15531553
# Output: [{"id":1,"name":"Alice","email":"[email protected]"},{"id":2,"name":"Bob","email":"[email protected]"}]
15541554

1555+
.. testoutput:: sqlite3.cursor
1556+
:hide:
1557+
1558+
[{"id":1,"name":"Alice","email":"[email protected]"},{"id":2,"name":"Bob","email":"[email protected]"}]
1559+
15551560
.. versionadded:: 3.15
15561561

15571562
.. method:: executemany(sql, parameters, /)

Lib/test/test_sqlite3/test_execute_json.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_execute_json_with_parameters(self):
7575

7676
def test_execute_json_with_named_parameters(self):
7777
# Test execute_json with named parameters
78-
result = self.cu.execute_json("select * from test where income > :min_income",
78+
result = self.cu.execute_json("select * from test where income > :min_income",
7979
{"min_income": 150})
8080
data = json.loads(result)
8181
self.assertIsInstance(data, list)
@@ -92,12 +92,12 @@ def test_execute_json_invalid_sql(self):
9292

9393
def test_execute_json_non_select(self):
9494
# Test execute_json with non-SELECT statement
95-
result = self.cu.execute_json("insert into test(name, income) values (?, ?)",
95+
result = self.cu.execute_json("insert into test(name, income) values (?, ?)",
9696
("new_entry", 400))
9797
data = json.loads(result)
9898
self.assertIsInstance(data, list)
9999
self.assertEqual(len(data), 0)
100100

101101

102102
if __name__ == "__main__":
103-
unittest.main()
103+
unittest.main()

Modules/_sqlite/cursor.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -998,88 +998,88 @@ pysqlite_cursor_execute_json_impl(pysqlite_Cursor *self, PyObject *sql,
998998
return NULL;
999999
}
10001000
Py_DECREF(result);
1001-
1001+
10021002
// Check if we have a statement with results
10031003
if (!self->statement || sqlite3_column_count(self->statement->st) == 0) {
10041004
// No results to convert to JSON, return empty JSON array
10051005
return PyUnicode_FromString("[]");
10061006
}
1007-
1007+
10081008
// Build a JSON query that wraps the original query
10091009
sqlite3_stmt *stmt = self->statement->st;
10101010
int numcols = sqlite3_column_count(stmt);
1011-
1011+
10121012
// Build the json_object parameters
10131013
PyObject *column_list = PyList_New(0);
10141014
if (!column_list) {
10151015
return NULL;
10161016
}
1017-
1017+
10181018
for (int i = 0; i < numcols; i++) {
10191019
const char *colname = sqlite3_column_name(stmt, i);
10201020
if (!colname) {
10211021
Py_DECREF(column_list);
10221022
return PyErr_NoMemory();
10231023
}
1024-
1024+
10251025
// Add column name as quoted string literal for json_object keys
10261026
PyObject *colname_obj = PyUnicode_FromFormat("'%s'", colname);
10271027
if (!colname_obj) {
10281028
Py_DECREF(column_list);
10291029
return NULL;
10301030
}
1031-
1031+
10321032
if (PyList_Append(column_list, colname_obj) < 0) {
10331033
Py_DECREF(colname_obj);
10341034
Py_DECREF(column_list);
10351035
return NULL;
10361036
}
10371037
Py_DECREF(colname_obj);
1038-
1038+
10391039
// Add column reference
10401040
PyObject *colref_obj = PyUnicode_FromFormat("row.%s", colname);
10411041
if (!colref_obj) {
10421042
Py_DECREF(column_list);
10431043
return NULL;
10441044
}
1045-
1045+
10461046
if (PyList_Append(column_list, colref_obj) < 0) {
10471047
Py_DECREF(colref_obj);
10481048
Py_DECREF(column_list);
10491049
return NULL;
10501050
}
10511051
Py_DECREF(colref_obj);
10521052
}
1053-
1053+
10541054
// Join the column list with commas
10551055
PyObject *comma = PyUnicode_FromString(",");
10561056
if (!comma) {
10571057
Py_DECREF(column_list);
10581058
return NULL;
10591059
}
1060-
1060+
10611061
PyObject *column_str = PyUnicode_Join(comma, column_list);
10621062
Py_DECREF(comma);
10631063
if (!column_str) {
10641064
Py_DECREF(column_list);
10651065
return NULL;
10661066
}
1067-
1067+
10681068
// Complete the JSON query
10691069
PyObject *full_query = PyUnicode_FromFormat("SELECT json_group_array(json_object(%S)) FROM (%U) AS row", column_str, sql);
10701070
Py_DECREF(column_str);
10711071
Py_DECREF(column_list);
10721072
if (!full_query) {
10731073
return NULL;
10741074
}
1075-
1075+
10761076
// Execute the JSON query
10771077
pysqlite_Statement *json_stmt = pysqlite_statement_create(self->connection, full_query);
10781078
Py_DECREF(full_query);
10791079
if (!json_stmt) {
10801080
return NULL;
10811081
}
1082-
1082+
10831083
// Bind parameters if needed
10841084
if (parameters != NULL && parameters != Py_None) {
10851085
bind_parameters(self->connection->state, json_stmt, parameters);
@@ -1088,7 +1088,7 @@ pysqlite_cursor_execute_json_impl(pysqlite_Cursor *self, PyObject *sql,
10881088
return NULL;
10891089
}
10901090
}
1091-
1091+
10921092
// Execute the statement
10931093
int rc = stmt_step(json_stmt->st);
10941094
if (rc != SQLITE_ROW) {
@@ -1102,7 +1102,7 @@ pysqlite_cursor_execute_json_impl(pysqlite_Cursor *self, PyObject *sql,
11021102
return NULL;
11031103
}
11041104
}
1105-
1105+
11061106
// Get the JSON result
11071107
const char *json_result = (const char*)sqlite3_column_text(json_stmt->st, 0);
11081108
PyObject *result_str = NULL;
@@ -1112,7 +1112,7 @@ pysqlite_cursor_execute_json_impl(pysqlite_Cursor *self, PyObject *sql,
11121112
// NULL result, return empty JSON array
11131113
result_str = PyUnicode_FromString("[]");
11141114
}
1115-
1115+
11161116
Py_DECREF(json_stmt);
11171117
return result_str;
11181118
}

0 commit comments

Comments
 (0)