~pythoneers/ubuntu/lucid/python-pysqlite2/ltsppa

« back to all changes in this revision

Viewing changes to src/statement.c

  • Committer: Bazaar Package Importer
  • Author(s): Joel Rosdahl
  • Date: 2008-09-28 10:14:22 UTC
  • mfrom: (1.1.9 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20080928101422-9sz5fh2pwz0vonlj
Tags: 2.5.0-2
Moved documentation to a separate package:
python-pysqlite2-doc. Closes: #500425.

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
 
80
80
    sql_cstr = PyString_AsString(sql_str);
81
81
 
 
82
    Py_BEGIN_ALLOW_THREADS
82
83
    rc = sqlite3_prepare(connection->db,
83
84
                         sql_cstr,
84
85
                         -1,
85
86
                         &self->st,
86
87
                         &tail);
 
88
    Py_END_ALLOW_THREADS
87
89
 
88
90
    self->db = connection->db;
89
91
 
96
98
    return rc;
97
99
}
98
100
 
99
 
int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
 
101
int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter, int allow_8bit_chars)
100
102
{
101
103
    int rc = SQLITE_OK;
102
104
    long longval;
103
 
#ifdef HAVE_LONG_LONG
104
105
    PY_LONG_LONG longlongval;
105
 
#endif
106
106
    const char* buffer;
107
107
    char* string;
108
108
    Py_ssize_t buflen;
109
109
    PyObject* stringval;
110
110
    parameter_type paramtype;
 
111
    char* c;
111
112
 
112
113
    if (parameter == Py_None) {
113
114
        rc = sqlite3_bind_null(self->st, pos);
140
141
        paramtype = TYPE_UNKNOWN;
141
142
    }
142
143
 
 
144
    if (paramtype == TYPE_STRING && !allow_8bit_chars) {
 
145
        string = PyString_AS_STRING(parameter);
 
146
        for (c = string; *c != 0; c++) {
 
147
            if (*c & 0x80) {
 
148
                PyErr_SetString(pysqlite_ProgrammingError, "You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.");
 
149
                rc = -1;
 
150
                goto final;
 
151
            }
 
152
        }
 
153
    }
 
154
 
143
155
    switch (paramtype) {
144
156
        case TYPE_INT:
145
157
            longval = PyInt_AsLong(parameter);
146
158
            rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
147
159
            break;
148
 
#ifdef HAVE_LONG_LONG
149
160
        case TYPE_LONG:
150
161
            longlongval = PyLong_AsLongLong(parameter);
151
162
            /* in the overflow error case, longlongval is -1, and an exception is set */
152
163
            rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
153
164
            break;
154
 
#endif
155
165
        case TYPE_FLOAT:
156
166
            rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
157
167
            break;
197
207
    }
198
208
}
199
209
 
200
 
void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
 
210
void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters, int allow_8bit_chars)
201
211
{
202
212
    PyObject* current_param;
203
213
    PyObject* adapted;
242
252
            if (!_need_adapt(current_param)) {
243
253
                adapted = current_param;
244
254
            } else {
245
 
                adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
 
255
                adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
246
256
                if (adapted) {
247
257
                    Py_DECREF(current_param);
248
258
                } else {
251
261
                }
252
262
            }
253
263
 
254
 
            rc = pysqlite_statement_bind_parameter(self, i + 1, adapted);
 
264
            rc = pysqlite_statement_bind_parameter(self, i + 1, adapted, allow_8bit_chars);
255
265
            Py_DECREF(adapted);
256
266
 
257
267
            if (rc != SQLITE_OK) {
258
 
                PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
 
268
                if (!PyErr_Occurred()) {
 
269
                    PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
 
270
                }
259
271
                return;
260
272
            }
261
273
        }
285
297
            if (!_need_adapt(current_param)) {
286
298
                adapted = current_param;
287
299
            } else {
288
 
                adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
 
300
                adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
289
301
                if (adapted) {
290
302
                    Py_DECREF(current_param);
291
303
                } else {
294
306
                }
295
307
            }
296
308
 
297
 
            rc = pysqlite_statement_bind_parameter(self, i, adapted);
 
309
            rc = pysqlite_statement_bind_parameter(self, i, adapted, allow_8bit_chars);
298
310
            Py_DECREF(adapted);
299
311
 
300
312
            if (rc != SQLITE_OK) {
301
 
                PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
 
313
                if (!PyErr_Occurred()) {
 
314
                    PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
 
315
                }
302
316
                return;
303
317
           }
304
318
        }
316
330
 
317
331
    sql_cstr = PyString_AsString(self->sql);
318
332
 
 
333
    Py_BEGIN_ALLOW_THREADS
319
334
    rc = sqlite3_prepare(self->db,
320
335
                         sql_cstr,
321
336
                         -1,
322
337
                         &new_st,
323
338
                         &tail);
 
339
    Py_END_ALLOW_THREADS
324
340
 
325
341
    if (rc == SQLITE_OK) {
326
342
        /* The efficient sqlite3_transfer_bindings is only available in SQLite