blob: 0c9734caf710c6363518bb66a271cefca7398959 [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
55 /* None for autocommit, otherwise a PyString with the isolation level */
56 PyObject* isolation_level;
57
Ezio Melotti13925002011-03-16 11:05:33 +020058 /* NULL for autocommit, otherwise a string with the BEGIN statement; will be
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059 * freed in connection destructor */
60 char* begin_statement;
61
Thomas Wouters477c8d52006-05-27 19:21:47 +000062 /* 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 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000064 int check_same_thread;
Thomas Wouters477c8d52006-05-27 19:21:47 +000065
Gerhard Häringf9cee222010-03-05 15:20:03 +000066 int initialized;
67
Thomas Wouters477c8d52006-05-27 19:21:47 +000068 /* thread identification of the thread the connection was created in */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069 long thread_ident;
70
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000071 pysqlite_Cache* statement_cache;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072
Gerhard Häringf9cee222010-03-05 15:20:03 +000073 /* Lists of weak references to statements and cursors used within this connection */
Thomas Wouters477c8d52006-05-27 19:21:47 +000074 PyObject* statements;
Gerhard Häringf9cee222010-03-05 15:20:03 +000075 PyObject* cursors;
Thomas Wouters477c8d52006-05-27 19:21:47 +000076
Gerhard Häringf9cee222010-03-05 15:20:03 +000077 /* Counters for how many statements/cursors were created in the connection. May be
Thomas Wouters477c8d52006-05-27 19:21:47 +000078 * reset to 0 at certain intervals */
79 int created_statements;
Gerhard Häringf9cee222010-03-05 15:20:03 +000080 int created_cursors;
Thomas Wouters477c8d52006-05-27 19:21:47 +000081
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 PyObject* row_factory;
83
Thomas Wouters477c8d52006-05-27 19:21:47 +000084 /* Determines how bytestrings from SQLite are converted to Python objects:
85 * - PyUnicode_Type: Python Unicode objects are constructed from UTF-8 bytestrings
Petri Lehtinenbc35beb2012-02-09 21:09:03 +020086 * - PyBytes_Type: The bytestrings are returned as-is.
Thomas Wouters477c8d52006-05-27 19:21:47 +000087 * - Any custom callable: Any object returned from the callable called with the bytestring
88 * as single parameter.
89 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000090 PyObject* text_factory;
91
92 /* remember references to functions/classes used in
93 * create_function/create/aggregate, use these as dictionary keys, so we
94 * can keep the total system refcount constant by clearing that dictionary
95 * in connection_dealloc */
96 PyObject* function_pinboard;
97
98 /* a dictionary of registered collation name => collation callable mappings */
99 PyObject* collations;
100
101 /* Exception objects */
102 PyObject* Warning;
103 PyObject* Error;
104 PyObject* InterfaceError;
105 PyObject* DatabaseError;
106 PyObject* DataError;
107 PyObject* OperationalError;
108 PyObject* IntegrityError;
109 PyObject* InternalError;
110 PyObject* ProgrammingError;
111 PyObject* NotSupportedError;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000112} pysqlite_Connection;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000114extern PyTypeObject pysqlite_ConnectionType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000116PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware);
117void pysqlite_connection_dealloc(pysqlite_Connection* self);
118PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs);
119PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args);
120PyObject* _pysqlite_connection_begin(pysqlite_Connection* self);
121PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args);
122PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args);
123PyObject* pysqlite_connection_new(PyTypeObject* type, PyObject* args, PyObject* kw);
124int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125
Gerhard Häringf9cee222010-03-05 15:20:03 +0000126int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000127int pysqlite_check_thread(pysqlite_Connection* self);
128int pysqlite_check_connection(pysqlite_Connection* con);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000129
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000130int pysqlite_connection_setup_types(void);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131
132#endif