/Modules/_sqlite/connection.c

http://unladen-swallow.googlecode.com/ · C · 1487 lines · 1187 code · 238 blank · 62 comment · 246 complexity · 1e0136e98300cead768e69dc57f0eeb5 MD5 · raw file

  1. /* connection.c - the connection type
  2. *
  3. * Copyright (C) 2004-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 "cache.h"
  24. #include "module.h"
  25. #include "connection.h"
  26. #include "statement.h"
  27. #include "cursor.h"
  28. #include "prepare_protocol.h"
  29. #include "util.h"
  30. #include "sqlitecompat.h"
  31. #include "pythread.h"
  32. #define ACTION_FINALIZE 1
  33. #define ACTION_RESET 2
  34. static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
  35. static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
  36. {
  37. /* in older SQLite versions, calling sqlite3_result_error in callbacks
  38. * triggers a bug in SQLite that leads either to irritating results or
  39. * segfaults, depending on the SQLite version */
  40. #if SQLITE_VERSION_NUMBER >= 3003003
  41. sqlite3_result_error(ctx, errmsg, len);
  42. #else
  43. PyErr_SetString(pysqlite_OperationalError, errmsg);
  44. #endif
  45. }
  46. int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  47. {
  48. static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
  49. PyObject* database;
  50. int detect_types = 0;
  51. PyObject* isolation_level = NULL;
  52. PyObject* factory = NULL;
  53. int check_same_thread = 1;
  54. int cached_statements = 100;
  55. double timeout = 5.0;
  56. int rc;
  57. PyObject* class_attr = NULL;
  58. PyObject* class_attr_str = NULL;
  59. int is_apsw_connection = 0;
  60. PyObject* database_utf8;
  61. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOi", kwlist,
  62. &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
  63. {
  64. return -1;
  65. }
  66. self->begin_statement = NULL;
  67. self->statement_cache = NULL;
  68. self->statements = NULL;
  69. Py_INCREF(Py_None);
  70. self->row_factory = Py_None;
  71. Py_INCREF(&PyUnicode_Type);
  72. self->text_factory = (PyObject*)&PyUnicode_Type;
  73. if (PyString_Check(database) || PyUnicode_Check(database)) {
  74. if (PyString_Check(database)) {
  75. database_utf8 = database;
  76. Py_INCREF(database_utf8);
  77. } else {
  78. database_utf8 = PyUnicode_AsUTF8String(database);
  79. if (!database_utf8) {
  80. return -1;
  81. }
  82. }
  83. Py_BEGIN_ALLOW_THREADS
  84. rc = sqlite3_open(PyString_AsString(database_utf8), &self->db);
  85. Py_END_ALLOW_THREADS
  86. Py_DECREF(database_utf8);
  87. if (rc != SQLITE_OK) {
  88. _pysqlite_seterror(self->db, NULL);
  89. return -1;
  90. }
  91. } else {
  92. /* Create a pysqlite connection from a APSW connection */
  93. class_attr = PyObject_GetAttrString(database, "__class__");
  94. if (class_attr) {
  95. class_attr_str = PyObject_Str(class_attr);
  96. if (class_attr_str) {
  97. if (strcmp(PyString_AsString(class_attr_str), "<type 'apsw.Connection'>") == 0) {
  98. /* In the APSW Connection object, the first entry after
  99. * PyObject_HEAD is the sqlite3* we want to get hold of.
  100. * Luckily, this is the same layout as we have in our
  101. * pysqlite_Connection */
  102. self->db = ((pysqlite_Connection*)database)->db;
  103. Py_INCREF(database);
  104. self->apsw_connection = database;
  105. is_apsw_connection = 1;
  106. }
  107. }
  108. }
  109. Py_XDECREF(class_attr_str);
  110. Py_XDECREF(class_attr);
  111. if (!is_apsw_connection) {
  112. PyErr_SetString(PyExc_ValueError, "database parameter must be string or APSW Connection object");
  113. return -1;
  114. }
  115. }
  116. if (!isolation_level) {
  117. isolation_level = PyString_FromString("");
  118. if (!isolation_level) {
  119. return -1;
  120. }
  121. } else {
  122. Py_INCREF(isolation_level);
  123. }
  124. self->isolation_level = NULL;
  125. pysqlite_connection_set_isolation_level(self, isolation_level);
  126. Py_DECREF(isolation_level);
  127. self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
  128. if (PyErr_Occurred()) {
  129. return -1;
  130. }
  131. self->statements = PyList_New(0);
  132. if (!self->statements) {
  133. return -1;
  134. }
  135. self->created_statements = 0;
  136. /* By default, the Cache class INCREFs the factory in its initializer, and
  137. * decrefs it in its deallocator method. Since this would create a circular
  138. * reference here, we're breaking it by decrementing self, and telling the
  139. * cache class to not decref the factory (self) in its deallocator.
  140. */
  141. self->statement_cache->decref_factory = 0;
  142. Py_DECREF(self);
  143. self->inTransaction = 0;
  144. self->detect_types = detect_types;
  145. self->timeout = timeout;
  146. (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
  147. #ifdef WITH_THREAD
  148. self->thread_ident = PyThread_get_thread_ident();
  149. #endif
  150. self->check_same_thread = check_same_thread;
  151. self->function_pinboard = PyDict_New();
  152. if (!self->function_pinboard) {
  153. return -1;
  154. }
  155. self->collations = PyDict_New();
  156. if (!self->collations) {
  157. return -1;
  158. }
  159. self->Warning = pysqlite_Warning;
  160. self->Error = pysqlite_Error;
  161. self->InterfaceError = pysqlite_InterfaceError;
  162. self->DatabaseError = pysqlite_DatabaseError;
  163. self->DataError = pysqlite_DataError;
  164. self->OperationalError = pysqlite_OperationalError;
  165. self->IntegrityError = pysqlite_IntegrityError;
  166. self->InternalError = pysqlite_InternalError;
  167. self->ProgrammingError = pysqlite_ProgrammingError;
  168. self->NotSupportedError = pysqlite_NotSupportedError;
  169. return 0;
  170. }
  171. /* Empty the entire statement cache of this connection */
  172. void pysqlite_flush_statement_cache(pysqlite_Connection* self)
  173. {
  174. pysqlite_Node* node;
  175. pysqlite_Statement* statement;
  176. node = self->statement_cache->first;
  177. while (node) {
  178. statement = (pysqlite_Statement*)(node->data);
  179. (void)pysqlite_statement_finalize(statement);
  180. node = node->next;
  181. }
  182. Py_DECREF(self->statement_cache);
  183. self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
  184. Py_DECREF(self);
  185. self->statement_cache->decref_factory = 0;
  186. }
  187. /* action in (ACTION_RESET, ACTION_FINALIZE) */
  188. void pysqlite_do_all_statements(pysqlite_Connection* self, int action)
  189. {
  190. int i;
  191. PyObject* weakref;
  192. PyObject* statement;
  193. for (i = 0; i < PyList_Size(self->statements); i++) {
  194. weakref = PyList_GetItem(self->statements, i);
  195. statement = PyWeakref_GetObject(weakref);
  196. if (statement != Py_None) {
  197. if (action == ACTION_RESET) {
  198. (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
  199. } else {
  200. (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
  201. }
  202. }
  203. }
  204. }
  205. void pysqlite_connection_dealloc(pysqlite_Connection* self)
  206. {
  207. PyObject* ret = NULL;
  208. Py_XDECREF(self->statement_cache);
  209. /* Clean up if user has not called .close() explicitly. */
  210. if (self->db) {
  211. Py_BEGIN_ALLOW_THREADS
  212. sqlite3_close(self->db);
  213. Py_END_ALLOW_THREADS
  214. } else if (self->apsw_connection) {
  215. ret = PyObject_CallMethod(self->apsw_connection, "close", "");
  216. Py_XDECREF(ret);
  217. Py_XDECREF(self->apsw_connection);
  218. }
  219. if (self->begin_statement) {
  220. PyMem_Free(self->begin_statement);
  221. }
  222. Py_XDECREF(self->isolation_level);
  223. Py_XDECREF(self->function_pinboard);
  224. Py_XDECREF(self->row_factory);
  225. Py_XDECREF(self->text_factory);
  226. Py_XDECREF(self->collations);
  227. Py_XDECREF(self->statements);
  228. self->ob_type->tp_free((PyObject*)self);
  229. }
  230. PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  231. {
  232. static char *kwlist[] = {"factory", NULL, NULL};
  233. PyObject* factory = NULL;
  234. PyObject* cursor;
  235. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
  236. &factory)) {
  237. return NULL;
  238. }
  239. if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
  240. return NULL;
  241. }
  242. if (factory == NULL) {
  243. factory = (PyObject*)&pysqlite_CursorType;
  244. }
  245. cursor = PyObject_CallFunction(factory, "O", self);
  246. if (cursor && self->row_factory != Py_None) {
  247. Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
  248. Py_INCREF(self->row_factory);
  249. ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
  250. }
  251. return cursor;
  252. }
  253. PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
  254. {
  255. PyObject* ret;
  256. int rc;
  257. if (!pysqlite_check_thread(self)) {
  258. return NULL;
  259. }
  260. pysqlite_do_all_statements(self, ACTION_FINALIZE);
  261. if (self->db) {
  262. if (self->apsw_connection) {
  263. ret = PyObject_CallMethod(self->apsw_connection, "close", "");
  264. Py_XDECREF(ret);
  265. Py_XDECREF(self->apsw_connection);
  266. self->apsw_connection = NULL;
  267. self->db = NULL;
  268. } else {
  269. Py_BEGIN_ALLOW_THREADS
  270. rc = sqlite3_close(self->db);
  271. Py_END_ALLOW_THREADS
  272. if (rc != SQLITE_OK) {
  273. _pysqlite_seterror(self->db, NULL);
  274. return NULL;
  275. } else {
  276. self->db = NULL;
  277. }
  278. }
  279. }
  280. Py_INCREF(Py_None);
  281. return Py_None;
  282. }
  283. /*
  284. * Checks if a connection object is usable (i. e. not closed).
  285. *
  286. * 0 => error; 1 => ok
  287. */
  288. int pysqlite_check_connection(pysqlite_Connection* con)
  289. {
  290. if (!con->db) {
  291. PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
  292. return 0;
  293. } else {
  294. return 1;
  295. }
  296. }
  297. PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
  298. {
  299. int rc;
  300. const char* tail;
  301. sqlite3_stmt* statement;
  302. Py_BEGIN_ALLOW_THREADS
  303. rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
  304. Py_END_ALLOW_THREADS
  305. if (rc != SQLITE_OK) {
  306. _pysqlite_seterror(self->db, statement);
  307. goto error;
  308. }
  309. rc = pysqlite_step(statement, self);
  310. if (rc == SQLITE_DONE) {
  311. self->inTransaction = 1;
  312. } else {
  313. _pysqlite_seterror(self->db, statement);
  314. }
  315. Py_BEGIN_ALLOW_THREADS
  316. rc = sqlite3_finalize(statement);
  317. Py_END_ALLOW_THREADS
  318. if (rc != SQLITE_OK && !PyErr_Occurred()) {
  319. _pysqlite_seterror(self->db, NULL);
  320. }
  321. error:
  322. if (PyErr_Occurred()) {
  323. return NULL;
  324. } else {
  325. Py_INCREF(Py_None);
  326. return Py_None;
  327. }
  328. }
  329. PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
  330. {
  331. int rc;
  332. const char* tail;
  333. sqlite3_stmt* statement;
  334. if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
  335. return NULL;
  336. }
  337. if (self->inTransaction) {
  338. Py_BEGIN_ALLOW_THREADS
  339. rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
  340. Py_END_ALLOW_THREADS
  341. if (rc != SQLITE_OK) {
  342. _pysqlite_seterror(self->db, NULL);
  343. goto error;
  344. }
  345. rc = pysqlite_step(statement, self);
  346. if (rc == SQLITE_DONE) {
  347. self->inTransaction = 0;
  348. } else {
  349. _pysqlite_seterror(self->db, statement);
  350. }
  351. Py_BEGIN_ALLOW_THREADS
  352. rc = sqlite3_finalize(statement);
  353. Py_END_ALLOW_THREADS
  354. if (rc != SQLITE_OK && !PyErr_Occurred()) {
  355. _pysqlite_seterror(self->db, NULL);
  356. }
  357. }
  358. error:
  359. if (PyErr_Occurred()) {
  360. return NULL;
  361. } else {
  362. Py_INCREF(Py_None);
  363. return Py_None;
  364. }
  365. }
  366. PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
  367. {
  368. int rc;
  369. const char* tail;
  370. sqlite3_stmt* statement;
  371. if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
  372. return NULL;
  373. }
  374. if (self->inTransaction) {
  375. pysqlite_do_all_statements(self, ACTION_RESET);
  376. Py_BEGIN_ALLOW_THREADS
  377. rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
  378. Py_END_ALLOW_THREADS
  379. if (rc != SQLITE_OK) {
  380. _pysqlite_seterror(self->db, NULL);
  381. goto error;
  382. }
  383. rc = pysqlite_step(statement, self);
  384. if (rc == SQLITE_DONE) {
  385. self->inTransaction = 0;
  386. } else {
  387. _pysqlite_seterror(self->db, statement);
  388. }
  389. Py_BEGIN_ALLOW_THREADS
  390. rc = sqlite3_finalize(statement);
  391. Py_END_ALLOW_THREADS
  392. if (rc != SQLITE_OK && !PyErr_Occurred()) {
  393. _pysqlite_seterror(self->db, NULL);
  394. }
  395. }
  396. error:
  397. if (PyErr_Occurred()) {
  398. return NULL;
  399. } else {
  400. Py_INCREF(Py_None);
  401. return Py_None;
  402. }
  403. }
  404. void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
  405. {
  406. long longval;
  407. const char* buffer;
  408. Py_ssize_t buflen;
  409. PyObject* stringval;
  410. if ((!py_val) || PyErr_Occurred()) {
  411. sqlite3_result_null(context);
  412. } else if (py_val == Py_None) {
  413. sqlite3_result_null(context);
  414. } else if (PyInt_Check(py_val)) {
  415. longval = PyInt_AsLong(py_val);
  416. sqlite3_result_int64(context, (PY_LONG_LONG)longval);
  417. } else if (PyFloat_Check(py_val)) {
  418. sqlite3_result_double(context, PyFloat_AsDouble(py_val));
  419. } else if (PyBuffer_Check(py_val)) {
  420. if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
  421. PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
  422. } else {
  423. sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
  424. }
  425. } else if (PyString_Check(py_val)) {
  426. sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
  427. } else if (PyUnicode_Check(py_val)) {
  428. stringval = PyUnicode_AsUTF8String(py_val);
  429. if (stringval) {
  430. sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
  431. Py_DECREF(stringval);
  432. }
  433. } else {
  434. /* TODO: raise error */
  435. }
  436. }
  437. PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
  438. {
  439. PyObject* args;
  440. int i;
  441. sqlite3_value* cur_value;
  442. PyObject* cur_py_value;
  443. const char* val_str;
  444. PY_LONG_LONG val_int;
  445. Py_ssize_t buflen;
  446. void* raw_buffer;
  447. args = PyTuple_New(argc);
  448. if (!args) {
  449. return NULL;
  450. }
  451. for (i = 0; i < argc; i++) {
  452. cur_value = argv[i];
  453. switch (sqlite3_value_type(argv[i])) {
  454. case SQLITE_INTEGER:
  455. val_int = sqlite3_value_int64(cur_value);
  456. cur_py_value = PyInt_FromLong((long)val_int);
  457. break;
  458. case SQLITE_FLOAT:
  459. cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
  460. break;
  461. case SQLITE_TEXT:
  462. val_str = (const char*)sqlite3_value_text(cur_value);
  463. cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
  464. /* TODO: have a way to show errors here */
  465. if (!cur_py_value) {
  466. PyErr_Clear();
  467. Py_INCREF(Py_None);
  468. cur_py_value = Py_None;
  469. }
  470. break;
  471. case SQLITE_BLOB:
  472. buflen = sqlite3_value_bytes(cur_value);
  473. cur_py_value = PyBuffer_New(buflen);
  474. if (!cur_py_value) {
  475. break;
  476. }
  477. if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) {
  478. Py_DECREF(cur_py_value);
  479. cur_py_value = NULL;
  480. break;
  481. }
  482. memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen);
  483. break;
  484. case SQLITE_NULL:
  485. default:
  486. Py_INCREF(Py_None);
  487. cur_py_value = Py_None;
  488. }
  489. if (!cur_py_value) {
  490. Py_DECREF(args);
  491. return NULL;
  492. }
  493. PyTuple_SetItem(args, i, cur_py_value);
  494. }
  495. return args;
  496. }
  497. void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
  498. {
  499. PyObject* args;
  500. PyObject* py_func;
  501. PyObject* py_retval = NULL;
  502. #ifdef WITH_THREAD
  503. PyGILState_STATE threadstate;
  504. threadstate = PyGILState_Ensure();
  505. #endif
  506. py_func = (PyObject*)sqlite3_user_data(context);
  507. args = _pysqlite_build_py_params(context, argc, argv);
  508. if (args) {
  509. py_retval = PyObject_CallObject(py_func, args);
  510. Py_DECREF(args);
  511. }
  512. if (py_retval) {
  513. _pysqlite_set_result(context, py_retval);
  514. Py_DECREF(py_retval);
  515. } else {
  516. if (_enable_callback_tracebacks) {
  517. PyErr_Print();
  518. } else {
  519. PyErr_Clear();
  520. }
  521. _sqlite3_result_error(context, "user-defined function raised exception", -1);
  522. }
  523. #ifdef WITH_THREAD
  524. PyGILState_Release(threadstate);
  525. #endif
  526. }
  527. static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
  528. {
  529. PyObject* args;
  530. PyObject* function_result = NULL;
  531. PyObject* aggregate_class;
  532. PyObject** aggregate_instance;
  533. PyObject* stepmethod = NULL;
  534. #ifdef WITH_THREAD
  535. PyGILState_STATE threadstate;
  536. threadstate = PyGILState_Ensure();
  537. #endif
  538. aggregate_class = (PyObject*)sqlite3_user_data(context);
  539. aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
  540. if (*aggregate_instance == 0) {
  541. *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
  542. if (PyErr_Occurred()) {
  543. *aggregate_instance = 0;
  544. if (_enable_callback_tracebacks) {
  545. PyErr_Print();
  546. } else {
  547. PyErr_Clear();
  548. }
  549. _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
  550. goto error;
  551. }
  552. }
  553. stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
  554. if (!stepmethod) {
  555. goto error;
  556. }
  557. args = _pysqlite_build_py_params(context, argc, params);
  558. if (!args) {
  559. goto error;
  560. }
  561. function_result = PyObject_CallObject(stepmethod, args);
  562. Py_DECREF(args);
  563. if (!function_result) {
  564. if (_enable_callback_tracebacks) {
  565. PyErr_Print();
  566. } else {
  567. PyErr_Clear();
  568. }
  569. _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
  570. }
  571. error:
  572. Py_XDECREF(stepmethod);
  573. Py_XDECREF(function_result);
  574. #ifdef WITH_THREAD
  575. PyGILState_Release(threadstate);
  576. #endif
  577. }
  578. void _pysqlite_final_callback(sqlite3_context* context)
  579. {
  580. PyObject* function_result = NULL;
  581. PyObject** aggregate_instance;
  582. PyObject* aggregate_class;
  583. #ifdef WITH_THREAD
  584. PyGILState_STATE threadstate;
  585. threadstate = PyGILState_Ensure();
  586. #endif
  587. aggregate_class = (PyObject*)sqlite3_user_data(context);
  588. aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
  589. if (!*aggregate_instance) {
  590. /* this branch is executed if there was an exception in the aggregate's
  591. * __init__ */
  592. goto error;
  593. }
  594. function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
  595. if (!function_result) {
  596. if (_enable_callback_tracebacks) {
  597. PyErr_Print();
  598. } else {
  599. PyErr_Clear();
  600. }
  601. _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
  602. } else {
  603. _pysqlite_set_result(context, function_result);
  604. }
  605. error:
  606. Py_XDECREF(*aggregate_instance);
  607. Py_XDECREF(function_result);
  608. #ifdef WITH_THREAD
  609. PyGILState_Release(threadstate);
  610. #endif
  611. }
  612. void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
  613. {
  614. PyObject* new_list;
  615. PyObject* weakref;
  616. int i;
  617. /* we only need to do this once in a while */
  618. if (self->created_statements++ < 200) {
  619. return;
  620. }
  621. self->created_statements = 0;
  622. new_list = PyList_New(0);
  623. if (!new_list) {
  624. return;
  625. }
  626. for (i = 0; i < PyList_Size(self->statements); i++) {
  627. weakref = PyList_GetItem(self->statements, i);
  628. if (PyWeakref_GetObject(weakref) != Py_None) {
  629. if (PyList_Append(new_list, weakref) != 0) {
  630. Py_DECREF(new_list);
  631. return;
  632. }
  633. }
  634. }
  635. Py_DECREF(self->statements);
  636. self->statements = new_list;
  637. }
  638. PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  639. {
  640. static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
  641. PyObject* func;
  642. char* name;
  643. int narg;
  644. int rc;
  645. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
  646. &name, &narg, &func))
  647. {
  648. return NULL;
  649. }
  650. rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
  651. if (rc != SQLITE_OK) {
  652. /* Workaround for SQLite bug: no error code or string is available here */
  653. PyErr_SetString(pysqlite_OperationalError, "Error creating function");
  654. return NULL;
  655. } else {
  656. PyDict_SetItem(self->function_pinboard, func, Py_None);
  657. Py_INCREF(Py_None);
  658. return Py_None;
  659. }
  660. }
  661. PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  662. {
  663. PyObject* aggregate_class;
  664. int n_arg;
  665. char* name;
  666. static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
  667. int rc;
  668. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
  669. kwlist, &name, &n_arg, &aggregate_class)) {
  670. return NULL;
  671. }
  672. rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback);
  673. if (rc != SQLITE_OK) {
  674. /* Workaround for SQLite bug: no error code or string is available here */
  675. PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
  676. return NULL;
  677. } else {
  678. PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None);
  679. Py_INCREF(Py_None);
  680. return Py_None;
  681. }
  682. }
  683. static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
  684. {
  685. PyObject *ret;
  686. int rc;
  687. #ifdef WITH_THREAD
  688. PyGILState_STATE gilstate;
  689. gilstate = PyGILState_Ensure();
  690. #endif
  691. ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
  692. if (!ret) {
  693. if (_enable_callback_tracebacks) {
  694. PyErr_Print();
  695. } else {
  696. PyErr_Clear();
  697. }
  698. rc = SQLITE_DENY;
  699. } else {
  700. if (PyInt_Check(ret)) {
  701. rc = (int)PyInt_AsLong(ret);
  702. } else {
  703. rc = SQLITE_DENY;
  704. }
  705. Py_DECREF(ret);
  706. }
  707. #ifdef WITH_THREAD
  708. PyGILState_Release(gilstate);
  709. #endif
  710. return rc;
  711. }
  712. static int _progress_handler(void* user_arg)
  713. {
  714. int rc;
  715. PyObject *ret;
  716. #ifdef WITH_THREAD
  717. PyGILState_STATE gilstate;
  718. gilstate = PyGILState_Ensure();
  719. #endif
  720. ret = PyObject_CallFunction((PyObject*)user_arg, "");
  721. if (!ret) {
  722. if (_enable_callback_tracebacks) {
  723. PyErr_Print();
  724. } else {
  725. PyErr_Clear();
  726. }
  727. /* abort query if error occurred */
  728. rc = 1;
  729. } else {
  730. rc = (int)PyObject_IsTrue(ret);
  731. Py_DECREF(ret);
  732. }
  733. #ifdef WITH_THREAD
  734. PyGILState_Release(gilstate);
  735. #endif
  736. return rc;
  737. }
  738. PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  739. {
  740. PyObject* authorizer_cb;
  741. static char *kwlist[] = { "authorizer_callback", NULL };
  742. int rc;
  743. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
  744. kwlist, &authorizer_cb)) {
  745. return NULL;
  746. }
  747. rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
  748. if (rc != SQLITE_OK) {
  749. PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
  750. return NULL;
  751. } else {
  752. PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None);
  753. Py_INCREF(Py_None);
  754. return Py_None;
  755. }
  756. }
  757. PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  758. {
  759. PyObject* progress_handler;
  760. int n;
  761. static char *kwlist[] = { "progress_handler", "n", NULL };
  762. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
  763. kwlist, &progress_handler, &n)) {
  764. return NULL;
  765. }
  766. if (progress_handler == Py_None) {
  767. /* None clears the progress handler previously set */
  768. sqlite3_progress_handler(self->db, 0, 0, (void*)0);
  769. } else {
  770. sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
  771. PyDict_SetItem(self->function_pinboard, progress_handler, Py_None);
  772. }
  773. Py_INCREF(Py_None);
  774. return Py_None;
  775. }
  776. int pysqlite_check_thread(pysqlite_Connection* self)
  777. {
  778. #ifdef WITH_THREAD
  779. if (self->check_same_thread) {
  780. if (PyThread_get_thread_ident() != self->thread_ident) {
  781. PyErr_Format(pysqlite_ProgrammingError,
  782. "SQLite objects created in a thread can only be used in that same thread."
  783. "The object was created in thread id %ld and this is thread id %ld",
  784. self->thread_ident, PyThread_get_thread_ident());
  785. return 0;
  786. }
  787. }
  788. #endif
  789. return 1;
  790. }
  791. static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
  792. {
  793. Py_INCREF(self->isolation_level);
  794. return self->isolation_level;
  795. }
  796. static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
  797. {
  798. if (!pysqlite_check_connection(self)) {
  799. return NULL;
  800. } else {
  801. return Py_BuildValue("i", sqlite3_total_changes(self->db));
  802. }
  803. }
  804. static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
  805. {
  806. PyObject* res;
  807. PyObject* begin_statement;
  808. char* begin_statement_str;
  809. Py_XDECREF(self->isolation_level);
  810. if (self->begin_statement) {
  811. PyMem_Free(self->begin_statement);
  812. self->begin_statement = NULL;
  813. }
  814. if (isolation_level == Py_None) {
  815. Py_INCREF(Py_None);
  816. self->isolation_level = Py_None;
  817. res = pysqlite_connection_commit(self, NULL);
  818. if (!res) {
  819. return -1;
  820. }
  821. Py_DECREF(res);
  822. self->inTransaction = 0;
  823. } else {
  824. Py_INCREF(isolation_level);
  825. self->isolation_level = isolation_level;
  826. begin_statement = PyString_FromString("BEGIN ");
  827. if (!begin_statement) {
  828. return -1;
  829. }
  830. PyString_Concat(&begin_statement, isolation_level);
  831. if (!begin_statement) {
  832. return -1;
  833. }
  834. begin_statement_str = PyString_AsString(begin_statement);
  835. if (!begin_statement_str) {
  836. Py_DECREF(begin_statement);
  837. return -1;
  838. }
  839. self->begin_statement = PyMem_Malloc(strlen(begin_statement_str) + 2);
  840. if (!self->begin_statement) {
  841. Py_DECREF(begin_statement);
  842. return -1;
  843. }
  844. strcpy(self->begin_statement, begin_statement_str);
  845. Py_DECREF(begin_statement);
  846. }
  847. return 0;
  848. }
  849. PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  850. {
  851. PyObject* sql;
  852. pysqlite_Statement* statement;
  853. PyObject* weakref;
  854. int rc;
  855. if (!PyArg_ParseTuple(args, "O", &sql)) {
  856. return NULL;
  857. }
  858. _pysqlite_drop_unused_statement_references(self);
  859. statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
  860. if (!statement) {
  861. return NULL;
  862. }
  863. rc = pysqlite_statement_create(statement, self, sql);
  864. if (rc != SQLITE_OK) {
  865. if (rc == PYSQLITE_TOO_MUCH_SQL) {
  866. PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
  867. } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
  868. PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
  869. } else {
  870. (void)pysqlite_statement_reset(statement);
  871. _pysqlite_seterror(self->db, NULL);
  872. }
  873. Py_CLEAR(statement);
  874. } else {
  875. weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
  876. if (!weakref) {
  877. Py_CLEAR(statement);
  878. goto error;
  879. }
  880. if (PyList_Append(self->statements, weakref) != 0) {
  881. Py_CLEAR(weakref);
  882. goto error;
  883. }
  884. Py_DECREF(weakref);
  885. }
  886. error:
  887. return (PyObject*)statement;
  888. }
  889. PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  890. {
  891. PyObject* cursor = 0;
  892. PyObject* result = 0;
  893. PyObject* method = 0;
  894. cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
  895. if (!cursor) {
  896. goto error;
  897. }
  898. method = PyObject_GetAttrString(cursor, "execute");
  899. if (!method) {
  900. Py_CLEAR(cursor);
  901. goto error;
  902. }
  903. result = PyObject_CallObject(method, args);
  904. if (!result) {
  905. Py_CLEAR(cursor);
  906. }
  907. error:
  908. Py_XDECREF(result);
  909. Py_XDECREF(method);
  910. return cursor;
  911. }
  912. PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  913. {
  914. PyObject* cursor = 0;
  915. PyObject* result = 0;
  916. PyObject* method = 0;
  917. cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
  918. if (!cursor) {
  919. goto error;
  920. }
  921. method = PyObject_GetAttrString(cursor, "executemany");
  922. if (!method) {
  923. Py_CLEAR(cursor);
  924. goto error;
  925. }
  926. result = PyObject_CallObject(method, args);
  927. if (!result) {
  928. Py_CLEAR(cursor);
  929. }
  930. error:
  931. Py_XDECREF(result);
  932. Py_XDECREF(method);
  933. return cursor;
  934. }
  935. PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
  936. {
  937. PyObject* cursor = 0;
  938. PyObject* result = 0;
  939. PyObject* method = 0;
  940. cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
  941. if (!cursor) {
  942. goto error;
  943. }
  944. method = PyObject_GetAttrString(cursor, "executescript");
  945. if (!method) {
  946. Py_CLEAR(cursor);
  947. goto error;
  948. }
  949. result = PyObject_CallObject(method, args);
  950. if (!result) {
  951. Py_CLEAR(cursor);
  952. }
  953. error:
  954. Py_XDECREF(result);
  955. Py_XDECREF(method);
  956. return cursor;
  957. }
  958. /* ------------------------- COLLATION CODE ------------------------ */
  959. static int
  960. pysqlite_collation_callback(
  961. void* context,
  962. int text1_length, const void* text1_data,
  963. int text2_length, const void* text2_data)
  964. {
  965. PyObject* callback = (PyObject*)context;
  966. PyObject* string1 = 0;
  967. PyObject* string2 = 0;
  968. #ifdef WITH_THREAD
  969. PyGILState_STATE gilstate;
  970. #endif
  971. PyObject* retval = NULL;
  972. int result = 0;
  973. #ifdef WITH_THREAD
  974. gilstate = PyGILState_Ensure();
  975. #endif
  976. if (PyErr_Occurred()) {
  977. goto finally;
  978. }
  979. string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length);
  980. string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length);
  981. if (!string1 || !string2) {
  982. goto finally; /* failed to allocate strings */
  983. }
  984. retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
  985. if (!retval) {
  986. /* execution failed */
  987. goto finally;
  988. }
  989. result = PyInt_AsLong(retval);
  990. if (PyErr_Occurred()) {
  991. result = 0;
  992. }
  993. finally:
  994. Py_XDECREF(string1);
  995. Py_XDECREF(string2);
  996. Py_XDECREF(retval);
  997. #ifdef WITH_THREAD
  998. PyGILState_Release(gilstate);
  999. #endif
  1000. return result;
  1001. }
  1002. static PyObject *
  1003. pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
  1004. {
  1005. PyObject* retval = NULL;
  1006. if (!pysqlite_check_connection(self)) {
  1007. goto finally;
  1008. }
  1009. sqlite3_interrupt(self->db);
  1010. Py_INCREF(Py_None);
  1011. retval = Py_None;
  1012. finally:
  1013. return retval;
  1014. }
  1015. /* Function author: Paul Kippes <kippesp@gmail.com>
  1016. * Class method of Connection to call the Python function _iterdump
  1017. * of the sqlite3 module.
  1018. */
  1019. static PyObject *
  1020. pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
  1021. {
  1022. PyObject* retval = NULL;
  1023. PyObject* module = NULL;
  1024. PyObject* module_dict;
  1025. PyObject* pyfn_iterdump;
  1026. if (!pysqlite_check_connection(self)) {
  1027. goto finally;
  1028. }
  1029. module = PyImport_ImportModule(MODULE_NAME ".dump");
  1030. if (!module) {
  1031. goto finally;
  1032. }
  1033. module_dict = PyModule_GetDict(module);
  1034. if (!module_dict) {
  1035. goto finally;
  1036. }
  1037. pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
  1038. if (!pyfn_iterdump) {
  1039. PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
  1040. goto finally;
  1041. }
  1042. args = PyTuple_New(1);
  1043. if (!args) {
  1044. goto finally;
  1045. }
  1046. Py_INCREF(self);
  1047. PyTuple_SetItem(args, 0, (PyObject*)self);
  1048. retval = PyObject_CallObject(pyfn_iterdump, args);
  1049. finally:
  1050. Py_XDECREF(args);
  1051. Py_XDECREF(module);
  1052. return retval;
  1053. }
  1054. static PyObject *
  1055. pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
  1056. {
  1057. PyObject* callable;
  1058. PyObject* uppercase_name = 0;
  1059. PyObject* name;
  1060. PyObject* retval;
  1061. char* chk;
  1062. int rc;
  1063. if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
  1064. goto finally;
  1065. }
  1066. if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyString_Type, &name, &callable)) {
  1067. goto finally;
  1068. }
  1069. uppercase_name = PyObject_CallMethod(name, "upper", "");
  1070. if (!uppercase_name) {
  1071. goto finally;
  1072. }
  1073. chk = PyString_AsString(uppercase_name);
  1074. while (*chk) {
  1075. if ((*chk >= '0' && *chk <= '9')
  1076. || (*chk >= 'A' && *chk <= 'Z')
  1077. || (*chk == '_'))
  1078. {
  1079. chk++;
  1080. } else {
  1081. PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
  1082. goto finally;
  1083. }
  1084. }
  1085. if (callable != Py_None && !PyCallable_Check(callable)) {
  1086. PyErr_SetString(PyExc_TypeError, "parameter must be callable");
  1087. goto finally;
  1088. }
  1089. if (callable != Py_None) {
  1090. PyDict_SetItem(self->collations, uppercase_name, callable);
  1091. } else {
  1092. PyDict_DelItem(self->collations, uppercase_name);
  1093. }
  1094. rc = sqlite3_create_collation(self->db,
  1095. PyString_AsString(uppercase_name),
  1096. SQLITE_UTF8,
  1097. (callable != Py_None) ? callable : NULL,
  1098. (callable != Py_None) ? pysqlite_collation_callback : NULL);
  1099. if (rc != SQLITE_OK) {
  1100. PyDict_DelItem(self->collations, uppercase_name);
  1101. _pysqlite_seterror(self->db, NULL);
  1102. goto finally;
  1103. }
  1104. finally:
  1105. Py_XDECREF(uppercase_name);
  1106. if (PyErr_Occurred()) {
  1107. retval = NULL;
  1108. } else {
  1109. Py_INCREF(Py_None);
  1110. retval = Py_None;
  1111. }
  1112. return retval;
  1113. }
  1114. /* Called when the connection is used as a context manager. Returns itself as a
  1115. * convenience to the caller. */
  1116. static PyObject *
  1117. pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
  1118. {
  1119. Py_INCREF(self);
  1120. return (PyObject*)self;
  1121. }
  1122. /** Called when the connection is used as a context manager. If there was any
  1123. * exception, a rollback takes place; otherwise we commit. */
  1124. static PyObject *
  1125. pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
  1126. {
  1127. PyObject* exc_type, *exc_value, *exc_tb;
  1128. char* method_name;
  1129. PyObject* result;
  1130. if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
  1131. return NULL;
  1132. }
  1133. if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
  1134. method_name = "commit";
  1135. } else {
  1136. method_name = "rollback";
  1137. }
  1138. result = PyObject_CallMethod((PyObject*)self, method_name, "");
  1139. if (!result) {
  1140. return NULL;
  1141. }
  1142. Py_DECREF(result);
  1143. Py_INCREF(Py_False);
  1144. return Py_False;
  1145. }
  1146. static char connection_doc[] =
  1147. PyDoc_STR("SQLite database connection object.");
  1148. static PyGetSetDef connection_getset[] = {
  1149. {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
  1150. {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
  1151. {NULL}
  1152. };
  1153. static PyMethodDef connection_methods[] = {
  1154. {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
  1155. PyDoc_STR("Return a cursor for the connection.")},
  1156. {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
  1157. PyDoc_STR("Closes the connection.")},
  1158. {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
  1159. PyDoc_STR("Commit the current transaction.")},
  1160. {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
  1161. PyDoc_STR("Roll back the current transaction.")},
  1162. {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
  1163. PyDoc_STR("Creates a new function. Non-standard.")},
  1164. {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
  1165. PyDoc_STR("Creates a new aggregate. Non-standard.")},
  1166. {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
  1167. PyDoc_STR("Sets authorizer callback. Non-standard.")},
  1168. {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
  1169. PyDoc_STR("Sets progress handler callback. Non-standard.")},
  1170. {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
  1171. PyDoc_STR("Executes a SQL statement. Non-standard.")},
  1172. {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
  1173. PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
  1174. {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
  1175. PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
  1176. {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
  1177. PyDoc_STR("Creates a collation function. Non-standard.")},
  1178. {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
  1179. PyDoc_STR("Abort any pending database operation. Non-standard.")},
  1180. {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
  1181. PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
  1182. {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
  1183. PyDoc_STR("For context manager. Non-standard.")},
  1184. {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
  1185. PyDoc_STR("For context manager. Non-standard.")},
  1186. {NULL, NULL}
  1187. };
  1188. static struct PyMemberDef connection_members[] =
  1189. {
  1190. {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), RO},
  1191. {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), RO},
  1192. {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), RO},
  1193. {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), RO},
  1194. {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), RO},
  1195. {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), RO},
  1196. {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), RO},
  1197. {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), RO},
  1198. {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), RO},
  1199. {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), RO},
  1200. {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
  1201. {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
  1202. {NULL}
  1203. };
  1204. PyTypeObject pysqlite_ConnectionType = {
  1205. PyVarObject_HEAD_INIT(NULL, 0)
  1206. MODULE_NAME ".Connection", /* tp_name */
  1207. sizeof(pysqlite_Connection), /* tp_basicsize */
  1208. 0, /* tp_itemsize */
  1209. (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
  1210. 0, /* tp_print */
  1211. 0, /* tp_getattr */
  1212. 0, /* tp_setattr */
  1213. 0, /* tp_compare */
  1214. 0, /* tp_repr */
  1215. 0, /* tp_as_number */
  1216. 0, /* tp_as_sequence */
  1217. 0, /* tp_as_mapping */
  1218. 0, /* tp_hash */
  1219. (ternaryfunc)pysqlite_connection_call, /* tp_call */
  1220. 0, /* tp_str */
  1221. 0, /* tp_getattro */
  1222. 0, /* tp_setattro */
  1223. 0, /* tp_as_buffer */
  1224. Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
  1225. connection_doc, /* tp_doc */
  1226. 0, /* tp_traverse */
  1227. 0, /* tp_clear */
  1228. 0, /* tp_richcompare */
  1229. 0, /* tp_weaklistoffset */
  1230. 0, /* tp_iter */
  1231. 0, /* tp_iternext */
  1232. connection_methods, /* tp_methods */
  1233. connection_members, /* tp_members */
  1234. connection_getset, /* tp_getset */
  1235. 0, /* tp_base */
  1236. 0, /* tp_dict */
  1237. 0, /* tp_descr_get */
  1238. 0, /* tp_descr_set */
  1239. 0, /* tp_dictoffset */
  1240. (initproc)pysqlite_connection_init, /* tp_init */
  1241. 0, /* tp_alloc */
  1242. 0, /* tp_new */
  1243. 0 /* tp_free */
  1244. };
  1245. extern int pysqlite_connection_setup_types(void)
  1246. {
  1247. pysqlite_ConnectionType.tp_new = PyType_GenericNew;
  1248. return PyType_Ready(&pysqlite_ConnectionType);
  1249. }