@@ -51,47 +51,50 @@ typedef enum {
51
51
pysqlite_Statement *
52
52
pysqlite_statement_create (pysqlite_Connection * connection , PyObject * sql )
53
53
{
54
- const char * tail ;
55
- int rc ;
56
- const char * sql_cstr ;
57
- Py_ssize_t sql_cstr_len ;
58
- const char * p ;
59
-
60
54
assert (PyUnicode_Check (sql ));
61
-
62
- sql_cstr = PyUnicode_AsUTF8AndSize (sql , & sql_cstr_len );
55
+ Py_ssize_t size ;
56
+ const char * sql_cstr = PyUnicode_AsUTF8AndSize (sql , & size );
63
57
if (sql_cstr == NULL ) {
64
58
PyErr_Format (pysqlite_Warning ,
65
59
"SQL is of wrong type ('%s'). Must be string." ,
66
60
Py_TYPE (sql )-> tp_name );
67
61
return NULL ;
68
62
}
69
63
70
- int max_length = sqlite3_limit (connection -> db , SQLITE_LIMIT_LENGTH , -1 );
71
- if (sql_cstr_len >= max_length ) {
64
+ sqlite3 * db = connection -> db ;
65
+ int max_length = sqlite3_limit (db , SQLITE_LIMIT_LENGTH , -1 );
66
+ if (size >= max_length ) {
72
67
PyErr_SetString (pysqlite_DataError , "query string is too large" );
73
68
return NULL ;
74
69
}
75
- if (strlen (sql_cstr ) != (size_t )sql_cstr_len ) {
70
+ if (strlen (sql_cstr ) != (size_t )size ) {
76
71
PyErr_SetString (PyExc_ValueError ,
77
72
"the query contains a null character" );
78
73
return NULL ;
79
74
}
80
75
81
- pysqlite_Statement * self = PyObject_GC_New (pysqlite_Statement ,
82
- pysqlite_StatementType );
83
- if (self == NULL ) {
76
+ sqlite3_stmt * stmt ;
77
+ const char * tail ;
78
+ int rc ;
79
+ Py_BEGIN_ALLOW_THREADS
80
+ rc = sqlite3_prepare_v2 (db , sql_cstr , (int )size + 1 , & stmt , & tail );
81
+ Py_END_ALLOW_THREADS
82
+
83
+ if (rc != SQLITE_OK ) {
84
+ _pysqlite_seterror (db );
84
85
return NULL ;
85
86
}
86
87
87
- self -> st = NULL ;
88
- self -> in_use = 0 ;
89
- self -> is_dml = 0 ;
90
- self -> in_weakreflist = NULL ;
88
+ if (pysqlite_check_remaining_sql (tail )) {
89
+ PyErr_SetString (pysqlite_Warning ,
90
+ "You can only execute one statement at a time." );
91
+ goto error ;
92
+ }
91
93
92
94
/* Determine if the statement is a DML statement.
93
95
SELECT is the only exception. See #9924. */
94
- for (p = sql_cstr ; * p != 0 ; p ++ ) {
96
+ int is_dml = 0 ;
97
+ for (const char * p = sql_cstr ; * p != 0 ; p ++ ) {
95
98
switch (* p ) {
96
99
case ' ' :
97
100
case '\r' :
@@ -100,40 +103,29 @@ pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
100
103
continue ;
101
104
}
102
105
103
- self -> is_dml = (PyOS_strnicmp (p , "insert" , 6 ) == 0 )
104
- || (PyOS_strnicmp (p , "update" , 6 ) == 0 )
105
- || (PyOS_strnicmp (p , "delete" , 6 ) == 0 )
106
- || (PyOS_strnicmp (p , "replace" , 7 ) == 0 );
106
+ is_dml = (PyOS_strnicmp (p , "insert" , 6 ) == 0 )
107
+ || (PyOS_strnicmp (p , "update" , 6 ) == 0 )
108
+ || (PyOS_strnicmp (p , "delete" , 6 ) == 0 )
109
+ || (PyOS_strnicmp (p , "replace" , 7 ) == 0 );
107
110
break ;
108
111
}
109
112
110
- Py_BEGIN_ALLOW_THREADS
111
- rc = sqlite3_prepare_v2 (connection -> db ,
112
- sql_cstr ,
113
- (int )sql_cstr_len + 1 ,
114
- & self -> st ,
115
- & tail );
116
- Py_END_ALLOW_THREADS
117
-
118
- PyObject_GC_Track (self );
119
-
120
- if (rc != SQLITE_OK ) {
121
- _pysqlite_seterror (connection -> db );
113
+ pysqlite_Statement * self = PyObject_GC_New (pysqlite_Statement ,
114
+ pysqlite_StatementType );
115
+ if (self == NULL ) {
122
116
goto error ;
123
117
}
124
118
125
- if (rc == SQLITE_OK && pysqlite_check_remaining_sql (tail )) {
126
- (void )sqlite3_finalize (self -> st );
127
- self -> st = NULL ;
128
- PyErr_SetString (pysqlite_Warning ,
129
- "You can only execute one statement at a time." );
130
- goto error ;
131
- }
119
+ self -> st = stmt ;
120
+ self -> in_use = 0 ;
121
+ self -> is_dml = is_dml ;
122
+ self -> in_weakreflist = NULL ;
132
123
124
+ PyObject_GC_Track (self );
133
125
return self ;
134
126
135
127
error :
136
- Py_DECREF ( self );
128
+ ( void ) sqlite3_finalize ( stmt );
137
129
return NULL ;
138
130
}
139
131
0 commit comments