/Modules/_sqlite/statement.c

http://unladen-swallow.googlecode.com/ · C · 543 lines · 442 code · 60 blank · 41 comment · 136 complexity · 222cea57cacc1c57f358780069bd1e1f MD5 · raw file

  1. /* statement.c - the statement type
  2. *
  3. * Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
  4. *
  5. * This file is part of pysqlite.
  6. *
  7. * This software is provided 'as-is', without any express or implied
  8. * warranty. In no event will the authors be held liable for any damages
  9. * arising from the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software. If you use this software
  17. * in a product, an acknowledgment in the product documentation would be
  18. * appreciated but is not required.
  19. * 2. Altered source versions must be plainly marked as such, and must not be
  20. * misrepresented as being the original software.
  21. * 3. This notice may not be removed or altered from any source distribution.
  22. */
  23. #include "statement.h"
  24. #include "cursor.h"
  25. #include "connection.h"
  26. #include "microprotocols.h"
  27. #include "prepare_protocol.h"
  28. #include "sqlitecompat.h"
  29. /* prototypes */
  30. static int pysqlite_check_remaining_sql(const char* tail);
  31. typedef enum {
  32. LINECOMMENT_1,
  33. IN_LINECOMMENT,
  34. COMMENTSTART_1,
  35. IN_COMMENT,
  36. COMMENTEND_1,
  37. NORMAL
  38. } parse_remaining_sql_state;
  39. typedef enum {
  40. TYPE_INT,
  41. TYPE_LONG,
  42. TYPE_FLOAT,
  43. TYPE_STRING,
  44. TYPE_UNICODE,
  45. TYPE_BUFFER,
  46. TYPE_UNKNOWN
  47. } parameter_type;
  48. int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql)
  49. {
  50. const char* tail;
  51. int rc;
  52. PyObject* sql_str;
  53. char* sql_cstr;
  54. self->st = NULL;
  55. self->in_use = 0;
  56. if (PyString_Check(sql)) {
  57. sql_str = sql;
  58. Py_INCREF(sql_str);
  59. } else if (PyUnicode_Check(sql)) {
  60. sql_str = PyUnicode_AsUTF8String(sql);
  61. if (!sql_str) {
  62. rc = PYSQLITE_SQL_WRONG_TYPE;
  63. return rc;
  64. }
  65. } else {
  66. rc = PYSQLITE_SQL_WRONG_TYPE;
  67. return rc;
  68. }
  69. self->in_weakreflist = NULL;
  70. self->sql = sql_str;
  71. sql_cstr = PyString_AsString(sql_str);
  72. Py_BEGIN_ALLOW_THREADS
  73. rc = sqlite3_prepare(connection->db,
  74. sql_cstr,
  75. -1,
  76. &self->st,
  77. &tail);
  78. Py_END_ALLOW_THREADS
  79. self->db = connection->db;
  80. if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
  81. (void)sqlite3_finalize(self->st);
  82. self->st = NULL;
  83. rc = PYSQLITE_TOO_MUCH_SQL;
  84. }
  85. return rc;
  86. }
  87. int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter, int allow_8bit_chars)
  88. {
  89. int rc = SQLITE_OK;
  90. long longval;
  91. PY_LONG_LONG longlongval;
  92. const char* buffer;
  93. char* string;
  94. Py_ssize_t buflen;
  95. PyObject* stringval;
  96. parameter_type paramtype;
  97. char* c;
  98. if (parameter == Py_None) {
  99. rc = sqlite3_bind_null(self->st, pos);
  100. goto final;
  101. }
  102. if (PyInt_CheckExact(parameter)) {
  103. paramtype = TYPE_INT;
  104. } else if (PyLong_CheckExact(parameter)) {
  105. paramtype = TYPE_LONG;
  106. } else if (PyFloat_CheckExact(parameter)) {
  107. paramtype = TYPE_FLOAT;
  108. } else if (PyString_CheckExact(parameter)) {
  109. paramtype = TYPE_STRING;
  110. } else if (PyUnicode_CheckExact(parameter)) {
  111. paramtype = TYPE_UNICODE;
  112. } else if (PyBuffer_Check(parameter)) {
  113. paramtype = TYPE_BUFFER;
  114. } else if (PyInt_Check(parameter)) {
  115. paramtype = TYPE_INT;
  116. } else if (PyLong_Check(parameter)) {
  117. paramtype = TYPE_LONG;
  118. } else if (PyFloat_Check(parameter)) {
  119. paramtype = TYPE_FLOAT;
  120. } else if (PyString_Check(parameter)) {
  121. paramtype = TYPE_STRING;
  122. } else if (PyUnicode_Check(parameter)) {
  123. paramtype = TYPE_UNICODE;
  124. } else {
  125. paramtype = TYPE_UNKNOWN;
  126. }
  127. if (paramtype == TYPE_STRING && !allow_8bit_chars) {
  128. string = PyString_AS_STRING(parameter);
  129. for (c = string; *c != 0; c++) {
  130. if (*c & 0x80) {
  131. 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.");
  132. rc = -1;
  133. goto final;
  134. }
  135. }
  136. }
  137. switch (paramtype) {
  138. case TYPE_INT:
  139. longval = PyInt_AsLong(parameter);
  140. rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
  141. break;
  142. case TYPE_LONG:
  143. longlongval = PyLong_AsLongLong(parameter);
  144. /* in the overflow error case, longlongval is -1, and an exception is set */
  145. rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
  146. break;
  147. case TYPE_FLOAT:
  148. rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
  149. break;
  150. case TYPE_STRING:
  151. string = PyString_AS_STRING(parameter);
  152. rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
  153. break;
  154. case TYPE_UNICODE:
  155. stringval = PyUnicode_AsUTF8String(parameter);
  156. string = PyString_AsString(stringval);
  157. rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
  158. Py_DECREF(stringval);
  159. break;
  160. case TYPE_BUFFER:
  161. if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
  162. rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
  163. } else {
  164. PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
  165. rc = -1;
  166. }
  167. break;
  168. case TYPE_UNKNOWN:
  169. rc = -1;
  170. }
  171. final:
  172. return rc;
  173. }
  174. /* returns 0 if the object is one of Python's internal ones that don't need to be adapted */
  175. static int _need_adapt(PyObject* obj)
  176. {
  177. if (pysqlite_BaseTypeAdapted) {
  178. return 1;
  179. }
  180. if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj)
  181. || PyFloat_CheckExact(obj) || PyString_CheckExact(obj)
  182. || PyUnicode_CheckExact(obj) || PyBuffer_Check(obj)) {
  183. return 0;
  184. } else {
  185. return 1;
  186. }
  187. }
  188. void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters, int allow_8bit_chars)
  189. {
  190. PyObject* current_param;
  191. PyObject* adapted;
  192. const char* binding_name;
  193. int i;
  194. int rc;
  195. int num_params_needed;
  196. int num_params;
  197. Py_BEGIN_ALLOW_THREADS
  198. num_params_needed = sqlite3_bind_parameter_count(self->st);
  199. Py_END_ALLOW_THREADS
  200. if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) {
  201. /* parameters passed as sequence */
  202. if (PyTuple_CheckExact(parameters)) {
  203. num_params = PyTuple_GET_SIZE(parameters);
  204. } else if (PyList_CheckExact(parameters)) {
  205. num_params = PyList_GET_SIZE(parameters);
  206. } else {
  207. num_params = PySequence_Size(parameters);
  208. }
  209. if (num_params != num_params_needed) {
  210. PyErr_Format(pysqlite_ProgrammingError, "Incorrect number of bindings supplied. The current statement uses %d, and there are %d supplied.",
  211. num_params_needed, num_params);
  212. return;
  213. }
  214. for (i = 0; i < num_params; i++) {
  215. if (PyTuple_CheckExact(parameters)) {
  216. current_param = PyTuple_GET_ITEM(parameters, i);
  217. Py_XINCREF(current_param);
  218. } else if (PyList_CheckExact(parameters)) {
  219. current_param = PyList_GET_ITEM(parameters, i);
  220. Py_XINCREF(current_param);
  221. } else {
  222. current_param = PySequence_GetItem(parameters, i);
  223. }
  224. if (!current_param) {
  225. return;
  226. }
  227. if (!_need_adapt(current_param)) {
  228. adapted = current_param;
  229. } else {
  230. adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
  231. if (adapted) {
  232. Py_DECREF(current_param);
  233. } else {
  234. PyErr_Clear();
  235. adapted = current_param;
  236. }
  237. }
  238. rc = pysqlite_statement_bind_parameter(self, i + 1, adapted, allow_8bit_chars);
  239. Py_DECREF(adapted);
  240. if (rc != SQLITE_OK) {
  241. if (!PyErr_Occurred()) {
  242. PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
  243. }
  244. return;
  245. }
  246. }
  247. } else if (PyDict_Check(parameters)) {
  248. /* parameters passed as dictionary */
  249. for (i = 1; i <= num_params_needed; i++) {
  250. Py_BEGIN_ALLOW_THREADS
  251. binding_name = sqlite3_bind_parameter_name(self->st, i);
  252. Py_END_ALLOW_THREADS
  253. if (!binding_name) {
  254. PyErr_Format(pysqlite_ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i);
  255. return;
  256. }
  257. binding_name++; /* skip first char (the colon) */
  258. if (PyDict_CheckExact(parameters)) {
  259. current_param = PyDict_GetItemString(parameters, binding_name);
  260. Py_XINCREF(current_param);
  261. } else {
  262. current_param = PyMapping_GetItemString(parameters, (char*)binding_name);
  263. }
  264. if (!current_param) {
  265. PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i);
  266. return;
  267. }
  268. if (!_need_adapt(current_param)) {
  269. adapted = current_param;
  270. } else {
  271. adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
  272. if (adapted) {
  273. Py_DECREF(current_param);
  274. } else {
  275. PyErr_Clear();
  276. adapted = current_param;
  277. }
  278. }
  279. rc = pysqlite_statement_bind_parameter(self, i, adapted, allow_8bit_chars);
  280. Py_DECREF(adapted);
  281. if (rc != SQLITE_OK) {
  282. if (!PyErr_Occurred()) {
  283. PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
  284. }
  285. return;
  286. }
  287. }
  288. } else {
  289. PyErr_SetString(PyExc_ValueError, "parameters are of unsupported type");
  290. }
  291. }
  292. int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
  293. {
  294. const char* tail;
  295. int rc;
  296. char* sql_cstr;
  297. sqlite3_stmt* new_st;
  298. sql_cstr = PyString_AsString(self->sql);
  299. Py_BEGIN_ALLOW_THREADS
  300. rc = sqlite3_prepare(self->db,
  301. sql_cstr,
  302. -1,
  303. &new_st,
  304. &tail);
  305. Py_END_ALLOW_THREADS
  306. if (rc == SQLITE_OK) {
  307. /* The efficient sqlite3_transfer_bindings is only available in SQLite
  308. * version 3.2.2 or later. For older SQLite releases, that might not
  309. * even define SQLITE_VERSION_NUMBER, we do it the manual way.
  310. */
  311. #ifdef SQLITE_VERSION_NUMBER
  312. #if SQLITE_VERSION_NUMBER >= 3002002
  313. /* The check for the number of parameters is necessary to not trigger a
  314. * bug in certain SQLite versions (experienced in 3.2.8 and 3.3.4). */
  315. if (sqlite3_bind_parameter_count(self->st) > 0) {
  316. (void)sqlite3_transfer_bindings(self->st, new_st);
  317. }
  318. #endif
  319. #else
  320. statement_bind_parameters(self, params);
  321. #endif
  322. (void)sqlite3_finalize(self->st);
  323. self->st = new_st;
  324. }
  325. return rc;
  326. }
  327. int pysqlite_statement_finalize(pysqlite_Statement* self)
  328. {
  329. int rc;
  330. rc = SQLITE_OK;
  331. if (self->st) {
  332. Py_BEGIN_ALLOW_THREADS
  333. rc = sqlite3_finalize(self->st);
  334. Py_END_ALLOW_THREADS
  335. self->st = NULL;
  336. }
  337. self->in_use = 0;
  338. return rc;
  339. }
  340. int pysqlite_statement_reset(pysqlite_Statement* self)
  341. {
  342. int rc;
  343. rc = SQLITE_OK;
  344. if (self->in_use && self->st) {
  345. Py_BEGIN_ALLOW_THREADS
  346. rc = sqlite3_reset(self->st);
  347. Py_END_ALLOW_THREADS
  348. if (rc == SQLITE_OK) {
  349. self->in_use = 0;
  350. }
  351. }
  352. return rc;
  353. }
  354. void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
  355. {
  356. self->in_use = 1;
  357. }
  358. void pysqlite_statement_dealloc(pysqlite_Statement* self)
  359. {
  360. int rc;
  361. if (self->st) {
  362. Py_BEGIN_ALLOW_THREADS
  363. rc = sqlite3_finalize(self->st);
  364. Py_END_ALLOW_THREADS
  365. }
  366. self->st = NULL;
  367. Py_XDECREF(self->sql);
  368. if (self->in_weakreflist != NULL) {
  369. PyObject_ClearWeakRefs((PyObject*)self);
  370. }
  371. Py_TYPE(self)->tp_free((PyObject*)self);
  372. }
  373. /*
  374. * Checks if there is anything left in an SQL string after SQLite compiled it.
  375. * This is used to check if somebody tried to execute more than one SQL command
  376. * with one execute()/executemany() command, which the DB-API and we don't
  377. * allow.
  378. *
  379. * Returns 1 if there is more left than should be. 0 if ok.
  380. */
  381. static int pysqlite_check_remaining_sql(const char* tail)
  382. {
  383. const char* pos = tail;
  384. parse_remaining_sql_state state = NORMAL;
  385. for (;;) {
  386. switch (*pos) {
  387. case 0:
  388. return 0;
  389. case '-':
  390. if (state == NORMAL) {
  391. state = LINECOMMENT_1;
  392. } else if (state == LINECOMMENT_1) {
  393. state = IN_LINECOMMENT;
  394. }
  395. break;
  396. case ' ':
  397. case '\t':
  398. break;
  399. case '\n':
  400. case 13:
  401. if (state == IN_LINECOMMENT) {
  402. state = NORMAL;
  403. }
  404. break;
  405. case '/':
  406. if (state == NORMAL) {
  407. state = COMMENTSTART_1;
  408. } else if (state == COMMENTEND_1) {
  409. state = NORMAL;
  410. } else if (state == COMMENTSTART_1) {
  411. return 1;
  412. }
  413. break;
  414. case '*':
  415. if (state == NORMAL) {
  416. return 1;
  417. } else if (state == LINECOMMENT_1) {
  418. return 1;
  419. } else if (state == COMMENTSTART_1) {
  420. state = IN_COMMENT;
  421. } else if (state == IN_COMMENT) {
  422. state = COMMENTEND_1;
  423. }
  424. break;
  425. default:
  426. if (state == COMMENTEND_1) {
  427. state = IN_COMMENT;
  428. } else if (state == IN_LINECOMMENT) {
  429. } else if (state == IN_COMMENT) {
  430. } else {
  431. return 1;
  432. }
  433. }
  434. pos++;
  435. }
  436. return 0;
  437. }
  438. PyTypeObject pysqlite_StatementType = {
  439. PyVarObject_HEAD_INIT(NULL, 0)
  440. MODULE_NAME ".Statement", /* tp_name */
  441. sizeof(pysqlite_Statement), /* tp_basicsize */
  442. 0, /* tp_itemsize */
  443. (destructor)pysqlite_statement_dealloc, /* tp_dealloc */
  444. 0, /* tp_print */
  445. 0, /* tp_getattr */
  446. 0, /* tp_setattr */
  447. 0, /* tp_compare */
  448. 0, /* tp_repr */
  449. 0, /* tp_as_number */
  450. 0, /* tp_as_sequence */
  451. 0, /* tp_as_mapping */
  452. 0, /* tp_hash */
  453. 0, /* tp_call */
  454. 0, /* tp_str */
  455. 0, /* tp_getattro */
  456. 0, /* tp_setattro */
  457. 0, /* tp_as_buffer */
  458. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
  459. 0, /* tp_doc */
  460. 0, /* tp_traverse */
  461. 0, /* tp_clear */
  462. 0, /* tp_richcompare */
  463. offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */
  464. 0, /* tp_iter */
  465. 0, /* tp_iternext */
  466. 0, /* tp_methods */
  467. 0, /* tp_members */
  468. 0, /* tp_getset */
  469. 0, /* tp_base */
  470. 0, /* tp_dict */
  471. 0, /* tp_descr_get */
  472. 0, /* tp_descr_set */
  473. 0, /* tp_dictoffset */
  474. (initproc)0, /* tp_init */
  475. 0, /* tp_alloc */
  476. 0, /* tp_new */
  477. 0 /* tp_free */
  478. };
  479. extern int pysqlite_statement_setup_types(void)
  480. {
  481. pysqlite_StatementType.tp_new = PyType_GenericNew;
  482. return PyType_Ready(&pysqlite_StatementType);
  483. }