mytoolkit/mymanager.py
author Radek Brich <radek.brich@devl.cz>
Mon, 21 Nov 2011 10:47:24 +0100
changeset 17 f768a3529ee7
parent 16 cb7e13711a99
child 18 a9e12b7cc207
permissions -rw-r--r--
ToolBase - implement 'database' config option.
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
#
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
# Requires: Python 2.6, 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
     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
#
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
# Copyright (c) 2011  Radek Brich <radek.brich@devl.cz>
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
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
MyManager wraps MySQLdb connect function, adding following features:
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
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
 * Manage database connection parameters - link connection 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
    35
   to an unique identifier, retrieve connection object by this identifier
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
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
 * Connection pooling - connections with same identifier are pooled and reused
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
 * Easy query using the with statement - retrieve cursor directly by 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
    40
   identifier, don't worry about 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
    41
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
 * Dict rows - cursor has additional methods like fetchall_dict(), which
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
   returns dict row instead of ordinary list-like 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
    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
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
import 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
    48
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
db = mymanager.get_instance()
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
db.create_conn(host='127.0.0.1', db='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
    51
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
with db.cursor() as 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
    53
    curs.execute('SELECT now() AS now')
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
    row = curs.fetchone_dict()
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
    print row.now
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
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
First, we have obtained MyManager instance. This is like calling
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
MyManager(), although in our example the instance is global. That means
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
getting the instance in another module brings us all the defined 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
    60
etc.
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
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
On next line we have created connection named 'default' (this name can be left out).
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
The with statement obtains connection (actually connects to database when needed),
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
then returns cursor for this connection. At the end of with 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
    65
the connection is returned to the pool or closed (depending on number of 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
    66
in pool and on setting of keep_open parameter).
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
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
The row returned by fetchone_dict() is special dict object, which can be accessed
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
using item or attribute access, that is row['now'] or row.now.
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
"""
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
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
from contextlib import 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
    73
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
    74
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
    75
import select
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
import socket
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
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
    79
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
    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
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
    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
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
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
    85
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
    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
    87
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
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
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
    90
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
    def __init__(self, isolation_level=None, init_statement=None, keep_open=1, **kw):
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
        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
    93
        self.init_statement = 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
    94
        self.keep_open = keep_open
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
        self.parameters = kw
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    96
        self.adjust_parameters()
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    97
    
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    98
    def adjust_parameters(self):
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    99
        '''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
   100
        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
   101
        res = dict()
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   102
        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
   103
            if k in m:
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   104
                k = m[k]
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   105
            res[k] = v
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   106
        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
   107
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
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
class RowDict(dict):
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
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
    def __getattr__(self, key):
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
        return self[key]
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
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
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
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
   116
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
    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
   118
        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
   119
            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
   120
        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
   121
            log.debug(self._executed)
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
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
    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
   124
        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
   125
            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
   126
        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
   127
            log.debug(self._executed)
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
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
    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
   130
        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
   131
        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
   132
            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
   133
        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
   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
    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
   136
        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
   137
        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
   138
            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
   139
        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
   140
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
    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
   142
        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
   143
        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
   144
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
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
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
   147
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
    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
   149
        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
   150
        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
   151
        self.lock = threading.Lock()
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
    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
   154
        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
   155
            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
   156
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   157
    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
   158
        '''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
   159
        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
   160
            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
   161
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
        isolation_level = self._normalize_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
   163
        ci = ConnectionInfo(isolation_level, **kw)
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
        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
   166
        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
   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 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
   169
        '''Close all connections of given 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
   170
        
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
        Connection credentials are still saved.
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
        
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
        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
   175
            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
   176
            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
   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
    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
   179
        '''Destroy 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
   180
        
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
        Counterpart of create_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
   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
        '''
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
        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
   185
            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
   186
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
        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
   188
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
        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
   190
        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
   191
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
    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
   193
        '''Get 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
   194
        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
   195
        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
   196
            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
   197
                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
   198
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
            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
   201
                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
   202
                if conn.closed:
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 = 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
   204
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
            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
   206
                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
   207
                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
   208
        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
   209
            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
   210
        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
   211
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
    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
   213
        '''Put connection back to 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
   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
        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
   216
        otherwise things become broken.
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
        
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
        '''
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
        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
   220
        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
   221
            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
   222
                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
   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
            if len(self.conn_pool[name]) >= self.conn_known[name].keep_open:
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   225
                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
   226
                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
   227
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   228
            # connection returned to the pool must not be in transaction
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
            conn.rollback()
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.
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
        
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.
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
        '''
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':
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
                conn.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
   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)
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
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
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   282
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
   283
    NullHandler = logging.NullHandler
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
except AttributeError:
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   285
    class NullHandler(logging.Handler):
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   286
        def emit(self, record):
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   287
            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
   288
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   289
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   290
log = logging.getLogger("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
   291
log.addHandler(NullHandler())
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
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
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   294
instance = 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
   295
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   296
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   297
def get_instance():
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   298
    global instance
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   299
    if instance 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
   300
        instance = 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
   301
    return instance
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   302
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   303