mytoolkit/mymanager.py
author Radek Brich <radek.brich@devl.cz>
Wed, 26 Sep 2012 23:32:02 +0200
changeset 47 bb8c729ae6ce
parent 39 0cef3540b69f
child 49 08e4dfe1b0cb
permissions -rw-r--r--
PgDiff: add partial support for SQL patch.
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.
18
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    70
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
    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
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
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
    74
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
    75
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
    76
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
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
    78
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
    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
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
    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 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
    84
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
    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
    86
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
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
    89
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
    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
    91
        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
    92
        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
    93
        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
    94
        self.parameters = kw
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    95
        self.adjust_parameters()
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    96
    
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    97
    def adjust_parameters(self):
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    98
        '''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
    99
        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
   100
        res = dict()
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   101
        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
   102
            if k in m:
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   103
                k = m[k]
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   104
            res[k] = v
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   105
        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
   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
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
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
   109
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
    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
   111
        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
   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
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
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
   115
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
    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
   117
        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
   118
            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
   119
        finally:
39
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
   120
            log.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
   121
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
    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
   123
        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
   124
            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
   125
        finally:
39
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
   126
            log.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
   127
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
    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
   129
        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
   130
        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
   131
            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
   132
        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
   133
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
    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
   135
        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
   136
        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
   137
            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
   138
        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
   139
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
    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
   141
        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
   142
        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
   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
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
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
   146
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
    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
   148
        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
   149
        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
   150
        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
   151
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
    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
   153
        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
   154
            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
   155
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   156
    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
   157
        '''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
   158
        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
   159
            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
   160
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
        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
   162
        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
   163
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
        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
   165
        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
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
    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
   168
        '''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
   169
        
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
        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
   171
        
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
        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
   174
            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
   175
            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
   176
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
    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
   178
        '''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
   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
        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
   181
        
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   182
        '''
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
        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
   184
            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
   185
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.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
   187
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
        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
   189
        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
   190
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
    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
   192
        '''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
   193
        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
   194
        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
   195
            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
   196
                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
   197
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   198
            conn = None
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   199
            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
   200
                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
   201
                try:
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   202
                    conn.ping()
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   203
                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
   204
                    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
   205
                    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
   206
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   207
            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
   208
                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
   209
                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
   210
        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
   211
            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
   212
        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
   213
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   214
    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
   215
        '''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
   216
        
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
        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
   218
        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
   219
        
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
        '''
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
        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
   222
        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
   223
            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
   224
                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
   225
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
            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
   227
                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
   228
                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
   229
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   230
            # 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
   231
            try:
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   232
                conn.rollback()
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   233
            except OperationalError:
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   234
                conn.close()
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   235
                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
   236
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
            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
   238
        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
   239
            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
   240
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
    @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
   242
    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
   243
        '''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
   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
        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
   246
        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
   247
        
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
        '''
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
        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
   250
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   251
        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
   252
            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
   253
            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
   254
        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
   255
            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
   256
            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
   257
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
    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
   259
        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
   260
        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
   261
            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
   262
                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
   263
            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
   264
                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
   265
                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
   266
                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
   267
        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
   268
            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
   269
            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
   270
            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
   271
        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
   272
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
    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
   274
        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
   275
            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
   276
        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
   277
            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
   278
            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
   279
                '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
   280
                '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
   281
                '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
   282
                '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
   283
                '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
   284
                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
   285
        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
   286
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
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
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
   289
    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
   290
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
   291
    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
   292
        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
   293
            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
   294
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
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
   297
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
   298
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
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 = 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
   301
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
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
   304
    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
   305
    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
   306
        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
   307
    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
   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