@@ -4,298 +4,105 @@ The connection class
4
4
5
5
.. sectionauthor :: Georg Richter <georg@mariadb.com>
6
6
7
- .. class :: mariadb.connection
8
-
9
- Handles the connection to a MariaDB or MySQL database server. It encapsulates a database session.
10
-
11
- Connections are created using the method :func: `~mariadb.connect() `.
7
+ .. autoclass :: mariadb.connections.Connection
12
8
13
9
-----------------------
14
10
Connection constructors
15
11
-----------------------
16
12
17
- .. method :: cursor(\*\*kwargs)
18
-
19
- Returns a new cursor object using the current connection.
20
-
21
- By default the result will be unbuffered, which means before executing another statement with the same connection the entire result set must be fetched.
22
-
23
- fetch methods of the cursor class by default return result set values as a tuple, unless named_tuple or dictionary was specified. The latter one exists for compatibility reasons and should be avoided due to possible inconsistency in case two or more fields in a result set have the same name.
24
-
25
- If cursor_type is set to mariadb.CURSOR_TYPE_READ_ONLY, a cursor is opened for the statement invoked with cursors execute() method.
26
-
27
- Supported keywords:
28
-
29
- - **named_tuple ** (bool): When set to `True ` results from fetch methods will be returned as named tuple.
30
- - **cursor_type ** (type): When set to `CURSOR_TYPE_READ_ONLY `, server side cursor will be used.
31
- - **prefetch_size ** (integer): The number of rows prefetched. This option will be ignored, if *cursor_type * is not `CURSOR_TYPE_READ_ONLY `
32
- - **buffered ** (bool): When set to `True ` the entire result set from a SELECT/SHOW statement will be stored in client memory
33
- - **prepared ** (bool): When set to `True ` cursor will remain in prepared state after the first :func: `~cursor.execute ` method was called. Further calls to *execute() * method will ignore the sql statement.
34
- - **binary ** (bool): When set to `True ` cursor will be executed using the MariaDB client/server binary protocol.
35
-
36
- :return: Returns a cursor object or raises an exception if an error occurred.
13
+ .. automethod :: mariadb.connections.Connection.cursor
37
14
38
15
.. versionadded :: 1.0.1
39
16
40
- .. method :: xid(format_id, global_transaction_id, branch_qualifier)
41
-
42
- Returns a transaction ID object suitable for passing to the tpc methods of this connection
43
-
44
- :param format_id: Format id. If not set default value `0 ` will be used.
45
- :type format_id: integer
46
- :param global_transaction_id: Global transaction qualifier, which must be unique. The maximum length of the global transaction id is limited to 64 characters.
47
- :type global transaction_id: string
48
- :param branch_qualifier: Branch qualifier which represents a local transaction identifier. The maximum length of the branch qualifier is limited to 64 characters.
49
- :type branch_qualifier: string
17
+ .. automethod :: mariadb.connections.Connection.xid(format_id: int, global_transaction_id: str, brach_qualifier: str)
50
18
51
19
------------------
52
20
Connection methods
53
21
------------------
54
22
55
23
.. versionadded :: 1.1.0
56
- .. method :: begin()
57
-
58
- Start a new transaction which can be committed by .commit() method,
59
- or cancelled by .rollback() method.
60
-
61
- .. method :: commit()
62
-
63
- Commit any pending transaction to the database.
64
-
65
- .. note ::
66
-
67
- This method has no effect, when autocommit was set to True, or the storage engine in use doesn't support transactions.
68
-
69
- .. method :: change_user(user, password, database)
24
+ .. automethod :: mariadb.connections.Connection.begin
70
25
71
- Changes the *user * and default *database * of the current connection.
72
- In order to successfully change users a valid username and password
73
- parameters must be provided and that user must have sufficient
74
- permissions to access the desired database.
26
+ .. automethod :: mariadb.connections.Connection.commit
75
27
76
- :param user: The user name for server authentication
77
- :type user: string
78
- :param password: The passoword of the user
79
- :type password: string
80
- :param database: The default database
81
- :type database: string
28
+ .. automethod :: mariadb.connections.Connection.change_user(user, password, database)
82
29
83
30
If for any reason authorization fails an exception will be raised and the current user authentication will remain.
84
31
85
- .. method :: close()
32
+ .. automethod :: mariadb.connections.Connection. close
86
33
87
- Close the connection now (rather than whenever .__del__() is called).
88
-
89
- The connection will be unusable from this point forward; an Error
90
- (or subclass) exception will be raised if any operation is attempted
91
- with the connection. The same applies to all cursor objects trying to
92
- use the connection. If the connection was obtained by *ConnectionPool *,
93
- the connection will not be closed but returned to the pool.
94
-
95
- .. method :: get_server_version()
96
-
97
- Returns numeric version of connected database server as tuple.
98
- The form of the tuple is (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH).
99
-
100
- The get_server_version() method was added for compatibility. New applications should use the connection attribute server_version_info.
34
+ .. automethod :: mariadb.connections.Connection.get_server_version
101
35
102
36
.. versionadded :: 1.0.5
103
37
104
- .. method :: escape_string(escape_str)
38
+ .. automethod :: mariadb.connections.Connection. escape_string(escape_str)
105
39
106
- This function is used to create a legal SQL string that you can use in
107
- an SQL statement. The given string is encoded and returned as an escaped string.
108
-
109
- :param escape_str: The string that is to be escaped.
110
- :type escape_str: string
111
-
112
- :returns: the escaped string or NULL on error.
113
-
114
- .. method :: kill(thread_id)
115
-
116
- This function is used to ask the server to terminate a database connection, specified
117
- by the *thread_id * parameter.
118
-
119
- :param thread_id: An id which represents a database connection.
120
- :type thread_id: integer
40
+ .. automethod :: mariadb.connections.Connection.kill(thread_id)
121
41
122
- .. note ::
123
- A thread_id from other connections can be determined by executing the SQL statement ``SHOW PROCESSLIST ``
124
- The thread_id of the current connection the current connection is stored in :data: `connection_id ` attribute.
42
+ .. note ::
43
+ A thread_id from other connections can be determined by executing the SQL statement ``SHOW PROCESSLIST ``
44
+ The thread_id of the current connection the current connection is stored in :data: `connection_id ` attribute.
125
45
126
- .. method :: ping()
46
+ .. automethod :: mariadb.connections.Connection. ping()
127
47
128
- Checks if the connection to the database server is still available.
48
+ .. automethod :: mariadb.connections.Connection.reconnect
129
49
130
- .. note ::
131
- If :data: `~auto_reconnect ` was set to True, an attempt will be made to reconnect to the database server in case the connection was lost
132
-
133
- If the connection is not available an InterfaceError will be raised.
134
-
135
- .. method :: reconnect()
136
-
137
- Tries to reconnect to a server in case the connection died due to timeout
138
- or other errors. It uses the same credentials which were specified in
139
- :func: `module.connect() ` method.
140
-
141
- .. method :: reset()
142
-
143
- Tries to reconnect to a server in case the connection died due to timeout
144
- or other errors. It uses the same credentials which were specified in
145
- connect() method.
146
-
147
- .. method :: rollback()
148
-
149
- Causes the database to roll back to the start of any pending transaction
150
-
151
- Closing a connection without committing the changes first will cause an
152
- implicit rollback to be performed.
50
+ .. automethod :: mariadb.connections.Connection.reset
153
51
154
- .. note ::
52
+ .. automethod :: mariadb.connections.Connection.rollback()
155
53
156
- rollback() will not work as expected if autocommit mode was set to True or the storage engine does not support transactions.
157
-
158
- .. method :: tpc_begin([xid])
159
-
160
- Begins a TPC transaction with the given transaction ID xid, which
161
- was created by xid() method.
162
-
163
- This method should be called outside of a transaction
164
- (i.e. nothing may have executed since the last .commit()
165
- or .rollback()).
166
-
167
- Furthermore, it is an error to call commit() or rollback() within
168
- the TPC transaction. A ProgrammingError is raised, if the application
169
- calls commit() or rollback() during an active TPC transaction.
170
-
171
- :param xid: A transaction id which was previously created by :func: `xid ` method.
172
- :type xid: Dictionary
173
-
174
- .. method :: tpc_commit([xid])
175
-
176
- When called with no arguments, tpc_commit() commits a TPC transaction
177
- previously prepared with tpc_prepare().
178
-
179
- If tpc_commit() is called prior to tpc_prepare(), a single phase commit
180
- is performed. A transaction manager may choose to do this if only a
181
- single resource is participating in the global transaction.
182
-
183
- When called with a transaction ID xid, the database commits the given
184
- transaction. If an invalid transaction ID is provided, a ProgrammingError
185
- will be raised. This form should be called outside of a transaction, and
186
- is intended for use in recovery.
187
-
188
- .. method :: tpc_prepare([ xid])
54
+ .. versionadded :: 1.1.0
55
+ .. automethod :: mariadb.connections.Connection.select_db
189
56
190
- Performs the first phase of a transaction started with tpc_begin().
57
+ .. automethod :: mariadb.connections.Connection.show_warnings
191
58
192
- A ProgrammingError will be raised if this method outside of a TPC
193
- transaction.
59
+ .. automethod :: mariadb.connections.Connection.tpc_begin
194
60
195
- After calling tpc_prepare(), no statements can be executed until
196
- :func: `~tpc_commit ` or :func: `~tpc_rollback ` have been called.
61
+ .. automethod :: mariadb.connections.Connection.tpc_commit
197
62
198
- .. method :: tpc_recover()
63
+ .. automethod :: mariadb.connections.Connection.tpc_prepare
199
64
200
- Returns a list of pending transaction IDs suitable for use with
201
- tpc_commit(xid) or tpc_rollback(xid).
65
+ .. automethod :: mariadb.connections.Connection.tpc_recover
202
66
203
- .. method :: tpc_rollback([ xid])
67
+ .. automethod :: mariadb.connections.Connection. tpc_rollback
204
68
205
- When called with no arguments, tpc_rollback() rolls back a TPC
206
- transaction. It may be called before or after :func: `tpc_prepare `.
207
-
208
- When called with a transaction ID xid, it rolls back the given
209
- transaction.
210
-
211
69
---------------------
212
70
Connection attributes
213
71
---------------------
214
72
215
- .. data :: auto_reconnect
216
-
217
- Enable or disable automatic reconnection to the server if the connection
218
- is found to have been lost.
219
-
220
- When enabled, client tries to reconnect to a database server in case
221
- the connection to a database server died due to timeout or other errors.
222
-
223
- .. data :: autocommit
224
-
225
- Toggles autocommit mode on or off for the current database connection.
226
-
227
- Autocommit mode only affects operations on transactional table types.
228
- Be aware that :func: `~rollback ` will not work, if autocommit mode was switched on.
229
-
230
- By default autocommit mode is set to False.
73
+ .. autoattribute :: mariadb.connections.Connection.auto_reconnect
231
74
232
- .. data :: character_set
75
+ .. autoattribute :: mariadb.connections.Connection.autocommit
233
76
234
- Returns the character set used for the connection
77
+ .. autoattribute :: mariadb.connections.Connection.character_set
235
78
236
- .. data :: collation
79
+ .. autoattribute :: mariadb.connections.Connection. collation
237
80
238
- Returns character set collation used for the connection
239
-
240
- .. data :: connection_id
81
+ .. autoattribute :: mariadb.connections.Connection.connection_id
241
82
242
- Returns the (thread) id for the current connection.
243
-
244
- If :data: `~auto_reconnect ` was set to True, the id might change if the client reconnects to the database server
245
-
246
- .. data :: database
83
+ .. autoattribute :: mariadb.connections.Connection.database
247
84
248
- Returns or sets the default database for the current connection
249
-
250
- If the used database will not change, the preferred way is to specify
251
- the default database when establishing the connection.
252
-
253
- .. data :: server_info
85
+ .. autoattribute :: mariadb.connections.Connection.server_info
254
86
255
- Returns the alphanumeric version of connected database. The numeric version
256
- can be obtained via server_version() property.
87
+ .. autoattribute :: mariadb.connections.Connection.server_name
257
88
258
- .. data :: server_name
89
+ .. autoattribute :: mariadb.connections.Connection.server_port
259
90
260
- Returns name or IP address of database server
261
-
262
- .. data :: server_port
263
-
264
- Returns the database server TCP/IP port
91
+ .. versionadded :: 1.1.0
92
+ .. autoattribute :: mariadb.connections.Connection.server_status
265
93
266
- .. data :: server_version
94
+ .. autoattribute :: mariadb.connections.Connection. server_version
267
95
268
- Returns numeric version of connected database server. The form of the version
269
- number is VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH
270
-
271
- .. data :: server_version_info
272
-
273
- Returns numeric version of connected database server as tuple.
274
- The form of the tuple is (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH)
96
+ .. autoattribute :: mariadb.connections.Connection.server_version_info
275
97
276
98
.. versionadded :: 1.0.5
277
99
278
- .. data :: tls_cipher
279
-
280
- Returns TLS cipher suite in use by connection
281
-
282
- .. data :: tls_version
283
-
284
- Returns TLS protocol version used by connection
100
+ .. autoattribute :: mariadb.connections.Connection.tls_cipher
285
101
286
- .. data :: unix_socket
102
+ .. autoattribute :: mariadb.connections.Connection.tls_version
287
103
288
- Returns Unix socket name
104
+ .. autoattribute :: mariadb.connections.Connection.unix_socket
289
105
290
- .. data :: user
291
-
292
- Returns user name for the current connection
293
-
294
- .. data :: warnings
295
-
296
- Returns the number of warnings from the last executed statement, or zero
297
- if there are no warnings.
298
-
299
- .. note ::
106
+ .. autoattribute :: mariadb.connections.Connection.user
300
107
301
- If the sql mode `` TRADITIONAL `` is enabled an error instead of a warning will be returned. To retrieve warnings the SQL statement `` SHOW WARNINGS `` has to be used.
108
+ .. autoattribute :: mariadb.connections.Connection.warnings
0 commit comments