statement.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* statement.c - the statement type
  2. *
  3. * Copyright (C) 2005-2010 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 "connection.h"
  24. #include "statement.h"
  25. #include "util.h"
  26. /* prototypes */
  27. static const char *lstrip_sql(const char *sql);
  28. pysqlite_Statement *
  29. pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
  30. {
  31. pysqlite_state *state = connection->state;
  32. assert(PyUnicode_Check(sql));
  33. Py_ssize_t size;
  34. const char *sql_cstr = PyUnicode_AsUTF8AndSize(sql, &size);
  35. if (sql_cstr == NULL) {
  36. return NULL;
  37. }
  38. sqlite3 *db = connection->db;
  39. int max_length = sqlite3_limit(db, SQLITE_LIMIT_SQL_LENGTH, -1);
  40. if (size > max_length) {
  41. PyErr_SetString(connection->DataError,
  42. "query string is too large");
  43. return NULL;
  44. }
  45. if (strlen(sql_cstr) != (size_t)size) {
  46. PyErr_SetString(connection->ProgrammingError,
  47. "the query contains a null character");
  48. return NULL;
  49. }
  50. sqlite3_stmt *stmt;
  51. const char *tail;
  52. int rc;
  53. Py_BEGIN_ALLOW_THREADS
  54. rc = sqlite3_prepare_v2(db, sql_cstr, (int)size + 1, &stmt, &tail);
  55. Py_END_ALLOW_THREADS
  56. if (rc != SQLITE_OK) {
  57. _pysqlite_seterror(state, db);
  58. return NULL;
  59. }
  60. if (lstrip_sql(tail) != NULL) {
  61. PyErr_SetString(connection->ProgrammingError,
  62. "You can only execute one statement at a time.");
  63. goto error;
  64. }
  65. /* Determine if the statement is a DML statement.
  66. SELECT is the only exception. See #9924. */
  67. int is_dml = 0;
  68. const char *p = lstrip_sql(sql_cstr);
  69. if (p != NULL) {
  70. is_dml = (PyOS_strnicmp(p, "insert", 6) == 0)
  71. || (PyOS_strnicmp(p, "update", 6) == 0)
  72. || (PyOS_strnicmp(p, "delete", 6) == 0)
  73. || (PyOS_strnicmp(p, "replace", 7) == 0);
  74. }
  75. pysqlite_Statement *self = PyObject_GC_New(pysqlite_Statement,
  76. state->StatementType);
  77. if (self == NULL) {
  78. goto error;
  79. }
  80. self->st = stmt;
  81. self->is_dml = is_dml;
  82. PyObject_GC_Track(self);
  83. return self;
  84. error:
  85. (void)sqlite3_finalize(stmt);
  86. return NULL;
  87. }
  88. static void
  89. stmt_dealloc(pysqlite_Statement *self)
  90. {
  91. PyTypeObject *tp = Py_TYPE(self);
  92. PyObject_GC_UnTrack(self);
  93. if (self->st) {
  94. Py_BEGIN_ALLOW_THREADS
  95. sqlite3_finalize(self->st);
  96. Py_END_ALLOW_THREADS
  97. self->st = 0;
  98. }
  99. tp->tp_free(self);
  100. Py_DECREF(tp);
  101. }
  102. static int
  103. stmt_traverse(pysqlite_Statement *self, visitproc visit, void *arg)
  104. {
  105. Py_VISIT(Py_TYPE(self));
  106. return 0;
  107. }
  108. /*
  109. * Strip leading whitespace and comments from incoming SQL (null terminated C
  110. * string) and return a pointer to the first non-whitespace, non-comment
  111. * character.
  112. *
  113. * This is used to check if somebody tries to execute more than one SQL query
  114. * with one execute()/executemany() command, which the DB-API don't allow.
  115. *
  116. * It is also used to harden DML query detection.
  117. */
  118. static inline const char *
  119. lstrip_sql(const char *sql)
  120. {
  121. // This loop is borrowed from the SQLite source code.
  122. for (const char *pos = sql; *pos; pos++) {
  123. switch (*pos) {
  124. case ' ':
  125. case '\t':
  126. case '\f':
  127. case '\n':
  128. case '\r':
  129. // Skip whitespace.
  130. break;
  131. case '-':
  132. // Skip line comments.
  133. if (pos[1] == '-') {
  134. pos += 2;
  135. while (pos[0] && pos[0] != '\n') {
  136. pos++;
  137. }
  138. if (pos[0] == '\0') {
  139. return NULL;
  140. }
  141. continue;
  142. }
  143. return pos;
  144. case '/':
  145. // Skip C style comments.
  146. if (pos[1] == '*') {
  147. pos += 2;
  148. while (pos[0] && (pos[0] != '*' || pos[1] != '/')) {
  149. pos++;
  150. }
  151. if (pos[0] == '\0') {
  152. return NULL;
  153. }
  154. pos++;
  155. continue;
  156. }
  157. return pos;
  158. default:
  159. return pos;
  160. }
  161. }
  162. return NULL;
  163. }
  164. static PyType_Slot stmt_slots[] = {
  165. {Py_tp_dealloc, stmt_dealloc},
  166. {Py_tp_traverse, stmt_traverse},
  167. {0, NULL},
  168. };
  169. static PyType_Spec stmt_spec = {
  170. .name = MODULE_NAME ".Statement",
  171. .basicsize = sizeof(pysqlite_Statement),
  172. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  173. Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
  174. .slots = stmt_slots,
  175. };
  176. int
  177. pysqlite_statement_setup_types(PyObject *module)
  178. {
  179. PyObject *type = PyType_FromModuleAndSpec(module, &stmt_spec, NULL);
  180. if (type == NULL) {
  181. return -1;
  182. }
  183. pysqlite_state *state = pysqlite_get_state(module);
  184. state->StatementType = (PyTypeObject *)type;
  185. return 0;
  186. }