mytoolkit/mymanager.py
author Radek Brich <radek.brich@devl.cz>
Thu, 07 Mar 2013 15:42:47 +0100
changeset 74 d4306261ddfb
parent 71 4251068a251a
child 75 39f777341db4
permissions -rw-r--r--
Fix MyManager.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
     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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    32
MyManager wraps MySQLdb in same manner as PgManager wraps psycopg2.
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    37
 * Save and reuse database connection parameters
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    38
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    49
    dbm = mymanager.get_instance()
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    52
    with dbm.cursor() as curs:
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    53
        curs.execute('SELECT now() AS now')
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    54
        row = curs.fetchone_dict()
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    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
74
d4306261ddfb Fix MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    65
import multiprocessing
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
    66
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
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
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
    69
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
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
    71
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    72
from pgtoolkit.pgmanager import RowDict
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    73
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    74
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    75
log_sql = logging.getLogger("mymanager_sql")
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    76
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
    77
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
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
    79
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
    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
    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
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
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
    84
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    85
    def __init__(self, name, isolation_level=None,
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    86
                 init_statement=None, pool_size=1, **kw):
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    87
        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
    88
        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
    89
        self.init_statement = init_statement
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    90
        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
    91
        self.parameters = kw
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    92
        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
    93
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    94
    def adjust_parameters(self):
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    95
        '''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
    96
        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
    97
        res = dict()
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    98
        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
    99
            if k in m:
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   100
                k = m[k]
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   101
            res[k] = v
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   102
        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
   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
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
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
   106
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
    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
   108
        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
   109
            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
   110
        finally:
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   111
            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
   112
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
    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
   114
        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
   115
            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
   116
        finally:
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   117
            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
   118
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
    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
   120
        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
   121
        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
   122
            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
   123
        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
   124
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
    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
   126
        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
   127
        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
   128
            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
   129
        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
   130
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
    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
   132
        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
   133
        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
   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
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
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
   137
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
    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
   139
        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
   140
        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
   141
        self.lock = threading.Lock()
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   142
        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
   143
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
    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
   145
        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
   146
            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
   147
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   148
    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
   149
        '''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
   150
        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
   151
            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
   152
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
        isolation_level = self._normalize_isolation_level(isolation_level)
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   154
        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
   155
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_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
   157
        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
   158
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
    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
   160
        '''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
   161
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
   162
        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
   163
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
   164
        '''
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
        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
   166
            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
   167
            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
   168
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
    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
   170
        '''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
   171
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
   172
        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
   173
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
   174
        '''
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
        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
   176
            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
   177
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
        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
   179
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_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
   181
        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
   182
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
    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
   184
        '''Get connection of name 'name' from pool.'''
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   185
        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
   186
        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
   187
        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
   188
            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
   189
                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
   190
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   191
            # 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
   192
            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
   193
            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
   194
                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
   195
                try:
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   196
                    conn.ping()
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   197
                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
   198
                    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
   199
                    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
   200
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
            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
   202
                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
   203
                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
   204
        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
   205
            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
   206
        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
   207
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
    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
   209
        '''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
   210
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
   211
        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
   212
        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
   213
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
   214
        '''
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
        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
   216
        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
   217
            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
   218
                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
   219
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   220
            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
   221
                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
   222
                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
   223
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   224
            # 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
   225
            try:
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   226
                conn.rollback()
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   227
            except OperationalError:
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   228
                conn.close()
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   229
                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
   230
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
            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
   232
        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
   233
            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
   234
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
    @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
   236
    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
   237
        '''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
   238
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
   239
        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
   240
        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
   241
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
   242
        '''
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
        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
   244
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
        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
   246
            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
   247
            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
   248
        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
   249
            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
   250
            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
   251
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
    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
   253
        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
   254
        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
   255
            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
   256
                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
   257
            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
   258
                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
   259
                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
   260
                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
   261
        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
   262
            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
   263
            curs.execute(ci.init_statement)
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   264
            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
   265
            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
   266
        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
   267
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
    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
   269
        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
   270
            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
   271
        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
   272
            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
   273
            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
   274
                '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
   275
                '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
   276
                '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
   277
                '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
   278
                '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
   279
                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
   280
        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
   281
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   282
    def _check_fork(self):
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   283
        '''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
   284
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   285
        If it was, clean parent's connections.
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   286
        New connections are created for children.
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   287
        Known connection credentials are inherited, but not shared.
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   288
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   289
        '''
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   290
        if self.pid == multiprocessing.current_process().pid:
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   291
            # PID has not changed
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   292
            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
   293
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   294
        # update saved PID
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   295
        self.pid = multiprocessing.current_process().pid
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   296
        # reinitialize lock
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   297
        self.lock = threading.Lock()
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   298
        # clean parent's connections
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   299
        for name in self.conn_pool:
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   300
            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
   301
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   302
    @classmethod
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   303
    def get_instance(cls):
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   304
        if not hasattr(cls, '_instance'):
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   305
            cls._instance = cls()
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   306
        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
   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
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   309
def get_instance():
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   310
    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
   311