blob: adbfb54523235e555bbbf5ee7bb0ddc967319085 [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
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
35typedef struct
36{
37 PyObject_HEAD
38 sqlite3* db;
39
Thomas Wouters477c8d52006-05-27 19:21:47 +000040 /* 1 if we are currently within a transaction, i. e. if a BEGIN has been
41 * issued */
Martin v. Löwisa2e7d9b2010-10-17 19:48:29 +000042 char inTransaction;
Thomas Wouters477c8d52006-05-27 19:21:47 +000043
44 /* the type detection mode. Only 0, PARSE_DECLTYPES, PARSE_COLNAMES or a
45 * bitwise combination thereof makes sense */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000046 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
Serhiy Storchaka483405b2015-02-17 10:14:30 +020055 /* None for autocommit, otherwise a PyUnicode with the isolation level */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056 PyObject* isolation_level;
57
Serhiy Storchaka28914922016-09-01 22:18:03 +030058 /* NULL for autocommit, otherwise a string with the BEGIN statement */
59 const char* begin_statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060
Thomas Wouters477c8d52006-05-27 19:21:47 +000061 /* 1 if a check should be performed for each API call if the connection is
62 * used from the same thread it was created in */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063 int check_same_thread;
Thomas Wouters477c8d52006-05-27 19:21:47 +000064
Gerhard Häringf9cee222010-03-05 15:20:03 +000065 int initialized;
66
Thomas Wouters477c8d52006-05-27 19:21:47 +000067 /* thread identification of the thread the connection was created in */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068 long thread_ident;
69
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000070 pysqlite_Cache* statement_cache;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071
Gerhard Häringf9cee222010-03-05 15:20:03 +000072 /* Lists of weak references to statements and cursors used within this connection */
Thomas Wouters477c8d52006-05-27 19:21:47 +000073 PyObject* statements;
Gerhard Häringf9cee222010-03-05 15:20:03 +000074 PyObject* cursors;
Thomas Wouters477c8d52006-05-27 19:21:47 +000075
Gerhard Häringf9cee222010-03-05 15:20:03 +000076 /* Counters for how many statements/cursors were created in the connection. May be
Thomas Wouters477c8d52006-05-27 19:21:47 +000077 * reset to 0 at certain intervals */
78 int created_statements;
Gerhard Häringf9cee222010-03-05 15:20:03 +000079 int created_cursors;
Thomas Wouters477c8d52006-05-27 19:21:47 +000080
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081 PyObject* row_factory;
82
Thomas Wouters477c8d52006-05-27 19:21:47 +000083 /* Determines how bytestrings from SQLite are converted to Python objects:
84 * - PyUnicode_Type: Python Unicode objects are constructed from UTF-8 bytestrings
Petri Lehtinenbc35beb2012-02-09 21:09:03 +020085 * - PyBytes_Type: The bytestrings are returned as-is.
Thomas Wouters477c8d52006-05-27 19:21:47 +000086 * - Any custom callable: Any object returned from the callable called with the bytestring
87 * as single parameter.
88 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000089 PyObject* text_factory;
90
91 /* remember references to functions/classes used in
92 * create_function/create/aggregate, use these as dictionary keys, so we
93 * can keep the total system refcount constant by clearing that dictionary
94 * in connection_dealloc */
95 PyObject* function_pinboard;
96
97 /* a dictionary of registered collation name => collation callable mappings */
98 PyObject* collations;
99
100 /* Exception objects */
101 PyObject* Warning;
102 PyObject* Error;
103 PyObject* InterfaceError;
104 PyObject* DatabaseError;
105 PyObject* DataError;
106 PyObject* OperationalError;
107 PyObject* IntegrityError;
108 PyObject* InternalError;
109 PyObject* ProgrammingError;
110 PyObject* NotSupportedError;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000111} pysqlite_Connection;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000113extern PyTypeObject pysqlite_ConnectionType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000115PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware);
116void pysqlite_connection_dealloc(pysqlite_Connection* self);
117PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs);
118PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args);
119PyObject* _pysqlite_connection_begin(pysqlite_Connection* self);
120PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args);
121PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args);
122PyObject* pysqlite_connection_new(PyTypeObject* type, PyObject* args, PyObject* kw);
123int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000124
Gerhard Häringf9cee222010-03-05 15:20:03 +0000125int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000126int pysqlite_check_thread(pysqlite_Connection* self);
127int pysqlite_check_connection(pysqlite_Connection* con);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000129int pysqlite_connection_setup_types(void);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130
131#endif