Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1 | /* connection.h - definitions for the connection type |
| 2 | * |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame^] | 3 | * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 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 | |
| 24 | #ifndef PYSQLITE_CONNECTION_H |
| 25 | #define PYSQLITE_CONNECTION_H |
| 26 | #include "Python.h" |
| 27 | #include "pythread.h" |
| 28 | #include "structmember.h" |
| 29 | |
| 30 | #include "cache.h" |
| 31 | #include "module.h" |
| 32 | |
| 33 | #include "sqlite3.h" |
| 34 | |
| 35 | typedef struct |
| 36 | { |
| 37 | PyObject_HEAD |
| 38 | sqlite3* db; |
| 39 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 40 | /* 1 if we are currently within a transaction, i. e. if a BEGIN has been |
| 41 | * issued */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 42 | int inTransaction; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 43 | |
| 44 | /* the type detection mode. Only 0, PARSE_DECLTYPES, PARSE_COLNAMES or a |
| 45 | * bitwise combination thereof makes sense */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 46 | int detect_types; |
| 47 | |
| 48 | /* the timeout value in seconds for database locks */ |
| 49 | double timeout; |
| 50 | |
| 51 | /* for internal use in the timeout handler: when did the timeout handler |
| 52 | * first get called with count=0? */ |
| 53 | double timeout_started; |
| 54 | |
| 55 | /* None for autocommit, otherwise a PyString with the isolation level */ |
| 56 | PyObject* isolation_level; |
| 57 | |
| 58 | /* NULL for autocommit, otherwise a string with the BEGIN statment; will be |
| 59 | * freed in connection destructor */ |
| 60 | char* begin_statement; |
| 61 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 62 | /* 1 if a check should be performed for each API call if the connection is |
| 63 | * used from the same thread it was created in */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 64 | int check_same_thread; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 65 | |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame^] | 66 | int initialized; |
| 67 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 68 | /* thread identification of the thread the connection was created in */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 69 | long thread_ident; |
| 70 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 71 | pysqlite_Cache* statement_cache; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 72 | |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame^] | 73 | /* Lists of weak references to statements and cursors used within this connection */ |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 74 | PyObject* statements; |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame^] | 75 | PyObject* cursors; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 76 | |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame^] | 77 | /* Counters for how many statements/cursors were created in the connection. May be |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 78 | * reset to 0 at certain intervals */ |
| 79 | int created_statements; |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame^] | 80 | int created_cursors; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 81 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 82 | PyObject* row_factory; |
| 83 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 84 | /* Determines how bytestrings from SQLite are converted to Python objects: |
| 85 | * - PyUnicode_Type: Python Unicode objects are constructed from UTF-8 bytestrings |
| 86 | * - OptimizedUnicode: Like before, but for ASCII data, only PyStrings are created. |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 87 | * - PyString_Type: PyStrings are created as-is. |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 88 | * - Any custom callable: Any object returned from the callable called with the bytestring |
| 89 | * as single parameter. |
| 90 | */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 91 | PyObject* text_factory; |
| 92 | |
| 93 | /* remember references to functions/classes used in |
| 94 | * create_function/create/aggregate, use these as dictionary keys, so we |
| 95 | * can keep the total system refcount constant by clearing that dictionary |
| 96 | * in connection_dealloc */ |
| 97 | PyObject* function_pinboard; |
| 98 | |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 99 | /* a dictionary of registered collation name => collation callable mappings */ |
| 100 | PyObject* collations; |
| 101 | |
Gerhard Häring | 1cc60ed | 2008-02-29 22:08:41 +0000 | [diff] [blame] | 102 | /* if our connection was created from a APSW connection, we keep a |
| 103 | * reference to the APSW connection around and get rid of it in our |
| 104 | * destructor */ |
| 105 | PyObject* apsw_connection; |
| 106 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 107 | /* Exception objects */ |
| 108 | PyObject* Warning; |
| 109 | PyObject* Error; |
| 110 | PyObject* InterfaceError; |
| 111 | PyObject* DatabaseError; |
| 112 | PyObject* DataError; |
| 113 | PyObject* OperationalError; |
| 114 | PyObject* IntegrityError; |
| 115 | PyObject* InternalError; |
| 116 | PyObject* ProgrammingError; |
| 117 | PyObject* NotSupportedError; |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 118 | } pysqlite_Connection; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 119 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 120 | extern PyTypeObject pysqlite_ConnectionType; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 121 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 122 | PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware); |
| 123 | void pysqlite_connection_dealloc(pysqlite_Connection* self); |
| 124 | PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs); |
| 125 | PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args); |
| 126 | PyObject* _pysqlite_connection_begin(pysqlite_Connection* self); |
| 127 | PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args); |
| 128 | PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args); |
| 129 | PyObject* pysqlite_connection_new(PyTypeObject* type, PyObject* args, PyObject* kw); |
| 130 | int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 131 | |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame^] | 132 | int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor); |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 133 | int pysqlite_check_thread(pysqlite_Connection* self); |
| 134 | int pysqlite_check_connection(pysqlite_Connection* con); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 135 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 136 | int pysqlite_connection_setup_types(void); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 137 | |
| 138 | #endif |