author | Radek Brich <radek.brich@devl.cz> |
Tue, 05 Mar 2013 11:24:47 +0100 | |
changeset 71 | 4251068a251a |
parent 49 | 08e4dfe1b0cb |
child 74 | d4306261ddfb |
permissions | -rw-r--r-- |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
# -*- coding: utf-8 -*- |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
# |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
# MyManager - manage database connections (MySQL version) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
# |
71 | 5 |
# Requires: Python 2.6 / 2.7 / 3.2, MySQLdb |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
# |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
# Part of pgtoolkit |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
# http://hg.devl.cz/pgtoolkit |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
# |
71 | 10 |
# Copyright (c) 2011, 2013 Radek Brich <radek.brich@devl.cz> |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
# |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
# of this software and associated documentation files (the "Software"), to deal |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
# in the Software without restriction, including without limitation the rights |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
# copies of the Software, and to permit persons to whom the Software is |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
# furnished to do so, subject to the following conditions: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
# |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
# The above copyright notice and this permission notice shall be included in |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
# all copies or substantial portions of the Software. |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
# |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
# THE SOFTWARE. |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
"""MySQL database connection manager |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
|
71 | 32 |
MyManager wraps MySQLdb in same manner as PgManager wraps psycopg2. |
33 |
It's fully compatible so it should work as drop-in replacement for PgManager. |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
|
71 | 35 |
It adds following features over MySQLdb: |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
|
71 | 37 |
* Save and reuse database connection parameters |
38 |
||
39 |
* Connection pooling |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
|
71 | 41 |
* Easy query using the with statement |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
|
71 | 43 |
* Dictionary rows |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
Example: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
|
71 | 47 |
from mytoolkit import mymanager |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
|
71 | 49 |
dbm = mymanager.get_instance() |
50 |
dbm.create_conn(host='127.0.0.1', dbname='default') |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
|
71 | 52 |
with dbm.cursor() as curs: |
53 |
curs.execute('SELECT now() AS now') |
|
54 |
row = curs.fetchone_dict() |
|
55 |
print(row.now) |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
|
71 | 57 |
See PgManager docs for more information. |
18
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
58 |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
""" |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
from contextlib import contextmanager |
71 | 62 |
from collections import OrderedDict |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
import logging |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
import threading |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
import MySQLdb |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
import MySQLdb.cursors |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
from MySQLdb import DatabaseError, IntegrityError, OperationalError |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
70 |
|
71 | 71 |
from pgtoolkit.pgmanager import RowDict |
72 |
||
73 |
||
74 |
log_sql = logging.getLogger("mymanager_sql") |
|
75 |
||
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
76 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
class MyManagerError(Exception): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
pass |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
81 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
class ConnectionInfo: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
83 |
|
71 | 84 |
def __init__(self, name, isolation_level=None, |
85 |
init_statement=None, pool_size=1, **kw): |
|
86 |
self.name = name # connection name is logged with SQL queries |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
87 |
self.isolation_level = isolation_level |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
self.init_statement = init_statement |
71 | 89 |
self.pool_size = pool_size |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
self.parameters = kw |
16
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
91 |
self.adjust_parameters() |
49
08e4dfe1b0cb
Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
92 |
|
16
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
93 |
def adjust_parameters(self): |
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
94 |
'''Rename Postgres parameters to proper value for MySQL.''' |
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
95 |
m = {'dbname' : 'db', 'password' : 'passwd'} |
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
96 |
res = dict() |
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
97 |
for k, v in list(self.parameters.items()): |
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
98 |
if k in m: |
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
99 |
k = m[k] |
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
100 |
res[k] = v |
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
101 |
self.parameters = res |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
102 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
103 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
104 |
class Cursor(MySQLdb.cursors.Cursor): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
105 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
106 |
def execute(self, query, args=None): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
107 |
try: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
108 |
return super(Cursor, self).execute(query, args) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
109 |
finally: |
71 | 110 |
log_sql.debug(self._executed.decode('utf8')) |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
111 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
112 |
def callproc(self, procname, args=None): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
113 |
try: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
114 |
return super(Cursor, self).callproc(procname, args) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
115 |
finally: |
71 | 116 |
log_sql.debug(self._executed.decode('utf8')) |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
117 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
118 |
def row_dict(self, row, lstrip=None): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
119 |
adjustname = lambda a: a |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
120 |
if lstrip: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
121 |
adjustname = lambda a: a.lstrip(lstrip) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
122 |
return RowDict(zip([adjustname(desc[0]) for desc in self.description], row)) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
123 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
124 |
def fetchone_dict(self, lstrip=None): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
125 |
row = super(Cursor, self).fetchone() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
126 |
if row is None: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
127 |
return None |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
128 |
return self.row_dict(row, lstrip) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
129 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
130 |
def fetchall_dict(self, lstrip=None): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
131 |
rows = super(Cursor, self).fetchall() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
132 |
return [self.row_dict(row, lstrip) for row in rows] |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
133 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
134 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
135 |
class MyManager: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
136 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
137 |
def __init__(self): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
138 |
self.conn_known = {} # available connections |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
139 |
self.conn_pool = {} |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
140 |
self.lock = threading.Lock() |
71 | 141 |
self.pid = multiprocessing.current_process().pid # forking check |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
142 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
143 |
def __del__(self): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
144 |
for conn in tuple(self.conn_known.keys()): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
145 |
self.destroy_conn(conn) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
146 |
|
16
cb7e13711a99
MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents:
15
diff
changeset
|
147 |
def create_conn(self, name='default', isolation_level=None, **kw): |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
148 |
'''Create named connection.''' |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
149 |
if name in self.conn_known: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
150 |
raise MyManagerError('Connection name "%s" already registered.' % name) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
151 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
152 |
isolation_level = self._normalize_isolation_level(isolation_level) |
71 | 153 |
ci = ConnectionInfo(name, isolation_level, **kw) |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
154 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
155 |
self.conn_known[name] = ci |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
156 |
self.conn_pool[name] = [] |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
157 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
158 |
def close_conn(self, name='default'): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
159 |
'''Close all connections of given name. |
49
08e4dfe1b0cb
Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
160 |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
161 |
Connection credentials are still saved. |
49
08e4dfe1b0cb
Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
162 |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
163 |
''' |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
164 |
while len(self.conn_pool[name]): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
165 |
conn = self.conn_pool[name].pop() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
166 |
conn.close() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
167 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
168 |
def destroy_conn(self, name='default'): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
169 |
'''Destroy connection. |
49
08e4dfe1b0cb
Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
170 |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
171 |
Counterpart of create_conn. |
49
08e4dfe1b0cb
Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
172 |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
173 |
''' |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
174 |
if not name in self.conn_known: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
175 |
raise MyManagerError('Connection name "%s" not registered.' % name) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
176 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
177 |
self.close_conn(name) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
178 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
179 |
del self.conn_known[name] |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
180 |
del self.conn_pool[name] |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
181 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
182 |
def get_conn(self, name='default'): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
183 |
'''Get connection of name 'name' from pool.''' |
71 | 184 |
self._check_fork() |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
185 |
self.lock.acquire() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
186 |
try: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
187 |
if not name in self.conn_known: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
188 |
raise MyManagerError("Connection name '%s' not registered." % name) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
189 |
|
71 | 190 |
# connection from pool |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
191 |
conn = None |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
192 |
while len(self.conn_pool[name]) and conn is None: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
193 |
conn = self.conn_pool[name].pop() |
18
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
194 |
try: |
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
195 |
conn.ping() |
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
196 |
except MySQLdb.MySQLError: |
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
197 |
conn.close() |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
198 |
conn = None |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
199 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
200 |
if conn is None: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
201 |
ci = self.conn_known[name] |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
202 |
conn = self._connect(ci) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
203 |
finally: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
204 |
self.lock.release() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
205 |
return conn |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
206 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
207 |
def put_conn(self, conn, name='default'): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
208 |
'''Put connection back to pool. |
49
08e4dfe1b0cb
Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
209 |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
210 |
Name must be same as used for get_conn, |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
211 |
otherwise things become broken. |
49
08e4dfe1b0cb
Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
212 |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
213 |
''' |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
214 |
self.lock.acquire() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
215 |
try: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
216 |
if not name in self.conn_known: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
217 |
raise MyManagerError("Connection name '%s' not registered." % name) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
218 |
|
71 | 219 |
if len(self.conn_pool[name]) >= self.conn_known[name].pool_size: |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
220 |
conn.close() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
221 |
return |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
222 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
223 |
# connection returned to the pool must not be in transaction |
18
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
224 |
try: |
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
225 |
conn.rollback() |
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
226 |
except OperationalError: |
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
227 |
conn.close() |
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
228 |
return |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
229 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
230 |
self.conn_pool[name].append(conn) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
231 |
finally: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
232 |
self.lock.release() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
233 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
234 |
@contextmanager |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
235 |
def cursor(self, name='default'): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
236 |
'''Cursor context. |
49
08e4dfe1b0cb
Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
237 |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
238 |
Uses any connection of name 'name' from pool |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
239 |
and returns cursor for that connection. |
49
08e4dfe1b0cb
Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
240 |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
241 |
''' |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
242 |
conn = self.get_conn(name) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
243 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
244 |
try: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
245 |
curs = conn.cursor() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
246 |
yield curs |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
247 |
finally: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
248 |
curs.close() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
249 |
self.put_conn(conn, name) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
250 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
251 |
def _connect(self, ci): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
252 |
conn = MySQLdb.connect(cursorclass=Cursor, **ci.parameters) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
253 |
if not ci.isolation_level is None: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
254 |
if ci.isolation_level == 'AUTOCOMMIT': |
18
a9e12b7cc207
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
255 |
conn.autocommit(True) |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
256 |
else: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
257 |
curs = conn.cursor() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
258 |
curs.execute('SET SESSION TRANSACTION ISOLATION LEVEL ' + ci.isolation_level) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
259 |
curs.close() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
260 |
if ci.init_statement: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
261 |
curs = conn.cursor() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
262 |
curs.execute(ci.init_statement) |
71 | 263 |
curs.connection.commit() |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
264 |
curs.close() |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
265 |
return conn |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
266 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
267 |
def _normalize_isolation_level(self, level): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
268 |
if level is None: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
269 |
return level |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
270 |
if type(level) == str: |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
271 |
level = level.upper().replace('_', ' ') |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
272 |
if level in ( |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
273 |
'AUTOCOMMIT', |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
274 |
'READ UNCOMMITTED', |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
275 |
'READ COMMITTED', |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
276 |
'REPEATABLE READ', |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
277 |
'SERIALIZABLE'): |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
278 |
return level |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
279 |
raise MyManagerError('Unknown isolation level name: "%s"', level) |
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
280 |
|
71 | 281 |
def _check_fork(self): |
282 |
'''Check if process was forked (PID has changed). |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
283 |
|
71 | 284 |
If it was, clean parent's connections. |
285 |
New connections are created for children. |
|
286 |
Known connection credentials are inherited, but not shared. |
|
287 |
||
288 |
''' |
|
289 |
if self.pid == multiprocessing.current_process().pid: |
|
290 |
# PID has not changed |
|
291 |
return |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
292 |
|
71 | 293 |
# update saved PID |
294 |
self.pid = multiprocessing.current_process().pid |
|
295 |
# reinitialize lock |
|
296 |
self.lock = threading.Lock() |
|
297 |
# clean parent's connections |
|
298 |
for name in self.conn_pool: |
|
299 |
self.conn_pool[name] = [] |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
300 |
|
71 | 301 |
@classmethod |
302 |
def get_instance(cls): |
|
303 |
if not hasattr(cls, '_instance'): |
|
304 |
cls._instance = cls() |
|
305 |
return cls._instance |
|
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
306 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
307 |
|
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
308 |
def get_instance(): |
71 | 309 |
return MyManager.get_instance() |
15
93450b43e627
Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
310 |