blob: 4e9d94c5f3089135affe3173d74750a293b7552d [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* connection.h - definitions for the connection type
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
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
Inada Naoki29198ea2019-03-19 22:10:18 +090026#define PY_SSIZE_T_CLEAN
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027#include "Python.h"
28#include "pythread.h"
29#include "structmember.h"
30
31#include "cache.h"
32#include "module.h"
33
34#include "sqlite3.h"
35
36typedef struct
37{
38 PyObject_HEAD
39 sqlite3* db;
40
Thomas Wouters477c8d52006-05-27 19:21:47 +000041 /* the type detection mode. Only 0, PARSE_DECLTYPES, PARSE_COLNAMES or a
42 * bitwise combination thereof makes sense */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043 int detect_types;
44
45 /* the timeout value in seconds for database locks */
46 double timeout;
47
48 /* for internal use in the timeout handler: when did the timeout handler
49 * first get called with count=0? */
50 double timeout_started;
51
Serhiy Storchaka483405b2015-02-17 10:14:30 +020052 /* None for autocommit, otherwise a PyUnicode with the isolation level */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053 PyObject* isolation_level;
54
Serhiy Storchaka28914922016-09-01 22:18:03 +030055 /* NULL for autocommit, otherwise a string with the BEGIN statement */
56 const char* begin_statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057
Thomas Wouters477c8d52006-05-27 19:21:47 +000058 /* 1 if a check should be performed for each API call if the connection is
59 * used from the same thread it was created in */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060 int check_same_thread;
Thomas Wouters477c8d52006-05-27 19:21:47 +000061
Gerhard Häringf9cee222010-03-05 15:20:03 +000062 int initialized;
63
Thomas Wouters477c8d52006-05-27 19:21:47 +000064 /* thread identification of the thread the connection was created in */
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020065 unsigned long thread_ident;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000067 pysqlite_Cache* statement_cache;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068
Gerhard Häringf9cee222010-03-05 15:20:03 +000069 /* Lists of weak references to statements and cursors used within this connection */
Thomas Wouters477c8d52006-05-27 19:21:47 +000070 PyObject* statements;
Gerhard Häringf9cee222010-03-05 15:20:03 +000071 PyObject* cursors;
Thomas Wouters477c8d52006-05-27 19:21:47 +000072
Gerhard Häringf9cee222010-03-05 15:20:03 +000073 /* Counters for how many statements/cursors were created in the connection. May be
Thomas Wouters477c8d52006-05-27 19:21:47 +000074 * reset to 0 at certain intervals */
75 int created_statements;
Gerhard Häringf9cee222010-03-05 15:20:03 +000076 int created_cursors;
Thomas Wouters477c8d52006-05-27 19:21:47 +000077
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 PyObject* row_factory;
79
Thomas Wouters477c8d52006-05-27 19:21:47 +000080 /* Determines how bytestrings from SQLite are converted to Python objects:
81 * - PyUnicode_Type: Python Unicode objects are constructed from UTF-8 bytestrings
Petri Lehtinenbc35beb2012-02-09 21:09:03 +020082 * - PyBytes_Type: The bytestrings are returned as-is.
Thomas Wouters477c8d52006-05-27 19:21:47 +000083 * - Any custom callable: Any object returned from the callable called with the bytestring
84 * as single parameter.
85 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 PyObject* text_factory;
87
88 /* remember references to functions/classes used in
89 * create_function/create/aggregate, use these as dictionary keys, so we
90 * can keep the total system refcount constant by clearing that dictionary
91 * in connection_dealloc */
92 PyObject* function_pinboard;
93
94 /* a dictionary of registered collation name => collation callable mappings */
95 PyObject* collations;
96
97 /* Exception objects */
98 PyObject* Warning;
99 PyObject* Error;
100 PyObject* InterfaceError;
101 PyObject* DatabaseError;
102 PyObject* DataError;
103 PyObject* OperationalError;
104 PyObject* IntegrityError;
105 PyObject* InternalError;
106 PyObject* ProgrammingError;
107 PyObject* NotSupportedError;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000108} pysqlite_Connection;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000109
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000110extern PyTypeObject pysqlite_ConnectionType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000112PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware);
113void pysqlite_connection_dealloc(pysqlite_Connection* self);
114PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs);
115PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args);
116PyObject* _pysqlite_connection_begin(pysqlite_Connection* self);
117PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args);
118PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args);
119PyObject* pysqlite_connection_new(PyTypeObject* type, PyObject* args, PyObject* kw);
120int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000121
Gerhard Häringf9cee222010-03-05 15:20:03 +0000122int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000123int pysqlite_check_thread(pysqlite_Connection* self);
124int pysqlite_check_connection(pysqlite_Connection* con);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000126int pysqlite_connection_setup_types(void);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127
128#endif