mytoolkit/mymanager.py
author Radek Brich <radek.brich@devl.cz>
Tue, 24 Sep 2013 13:34:15 +0200
changeset 87 3ef717ee9253
parent 77 2cfef775f518
permissions -rw-r--r--
PgDiff: Add support for indexes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
#
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
# MyManager - manage database connections (MySQL version)
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
#
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
     5
# Requires: Python 2.6 / 2.7 / 3.2, MySQLdb
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
#
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
# Part of pgtoolkit
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
# http://hg.devl.cz/pgtoolkit
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
#
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    10
# Copyright (c) 2011, 2013  Radek Brich <radek.brich@devl.cz>
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
#
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
# Permission is hereby granted, free of charge, to any person obtaining a copy
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
# of this software and associated documentation files (the "Software"), to deal
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
# in the Software without restriction, including without limitation the rights
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
# copies of the Software, and to permit persons to whom the Software is
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
# furnished to do so, subject to the following conditions:
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
#
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
# The above copyright notice and this permission notice shall be included in
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
# all copies or substantial portions of the Software.
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
#
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
# THE SOFTWARE.
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
"""MySQL database connection manager
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    32
MyManager wraps MySQLdb in same manner as PgManager wraps psycopg2.
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    33
It's fully compatible so it should work as drop-in replacement for PgManager.
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    35
It adds following features over MySQLdb:
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    37
 * Save and reuse database connection parameters
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    38
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    39
 * Connection pooling
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    41
 * Easy query using the with statement
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    43
 * Dictionary rows
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
Example:
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    47
    from mytoolkit import mymanager
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    49
    dbm = mymanager.get_instance()
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    50
    dbm.create_conn(host='127.0.0.1', dbname='default')
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    52
    with dbm.cursor() as curs:
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    53
        curs.execute('SELECT now() AS now')
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    54
        row = curs.fetchone_dict()
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    55
        print(row.now)
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    57
See PgManager docs for more information.
18
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    58
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
"""
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
from contextlib import contextmanager
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    62
from collections import OrderedDict
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
import logging
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
import threading
74
d4306261ddfb Fix MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    65
import multiprocessing
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
import MySQLdb
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
import MySQLdb.cursors
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
from MySQLdb import DatabaseError, IntegrityError, OperationalError
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    72
from pgtoolkit.pgmanager import RowDict
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    73
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    74
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    75
log_sql = logging.getLogger("mymanager_sql")
75
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    76
log_sql.addHandler(logging.NullHandler())
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    77
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
    78
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
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
    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
    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
    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 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
    85
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    86
    def __init__(self, name, isolation_level=None,
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    87
                 init_statement=None, pool_size=1, **kw):
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    88
        self.name = name  # connection name is logged with SQL queries
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
        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
    90
        self.init_statement = init_statement
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
    91
        self.pool_size = pool_size
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
        self.parameters = kw
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    93
        self.adjust_parameters()
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    94
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    95
    def adjust_parameters(self):
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    96
        '''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
    97
        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
    98
        res = dict()
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
    99
        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
   100
            if k in m:
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   101
                k = m[k]
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   102
            res[k] = v
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   103
        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
   104
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
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
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
   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
    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
   109
        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
   110
            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
   111
        finally:
75
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   112
            self._log_query(query, args)
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
   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
    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
   115
        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
   116
            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
   117
        finally:
75
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   118
            self._log_query(query, args)
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
   119
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
    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
   121
        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
   122
        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
   123
            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
   124
        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
   125
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
    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
   127
        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
   128
        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
   129
            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
   130
        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
   131
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
    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
   133
        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
   134
        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
   135
75
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   136
    def mogrify(self, query, args):
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   137
        """Get query with substituted args as it will be send to server."""
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   138
        if isinstance(query, bytes):
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   139
            query = query.decode()
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   140
        if args is not None:
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   141
            db = self._get_db()
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   142
            query = query % db.literal(args)
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   143
        return query
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   144
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   145
    def _log_query(self, query, args):
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   146
        name = self.connection.name if hasattr(self.connection, 'name') else '-'
77
2cfef775f518 PgManager, MyManager: Change log level of SQL queries to debug. Fix MyManager: Wrong query logged when exception occurs.
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   147
        query = self.mogrify(query, args)
75
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   148
        if isinstance(query, bytes):
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   149
            db = self._get_db()
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   150
            charset = db.character_set_name()
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   151
            query = query.decode(charset)
77
2cfef775f518 PgManager, MyManager: Change log level of SQL queries to debug. Fix MyManager: Wrong query logged when exception occurs.
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   152
        log_sql.debug('[%s] %s' % (name, query))
75
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   153
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
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
   156
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
    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
   158
        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
   159
        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
   160
        self.lock = threading.Lock()
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   161
        self.pid = multiprocessing.current_process().pid  # forking check
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
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
    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
   164
        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
   165
            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
   166
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 15
diff changeset
   167
    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
   168
        '''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
   169
        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
   170
            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
   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
        isolation_level = self._normalize_isolation_level(isolation_level)
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   173
        ci = ConnectionInfo(name, isolation_level, **kw)
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   174
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
        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
   176
        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
   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 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
   179
        '''Close all connections of given name.
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
   180
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
   181
        Connection credentials are still saved.
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
   182
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
   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
        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
   185
            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
   186
            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
   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
    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
   189
        '''Destroy connection.
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
   190
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
        Counterpart of create_conn.
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
   192
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
   193
        '''
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
        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
   195
            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
   196
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
        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
   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
        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
   200
        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
   201
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
    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
   203
        '''Get connection of name 'name' from pool.'''
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   204
        self._check_fork()
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   205
        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
   206
        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
   207
            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
   208
                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
   209
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   210
            # connection from pool
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   211
            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
   212
            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
   213
                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
   214
                try:
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   215
                    conn.ping()
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   216
                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
   217
                    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
   218
                    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
   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
            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
   221
                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
   222
                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
   223
        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
   224
            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
   225
        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
   226
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
    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
   228
        '''Put connection back to pool.
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
   229
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   230
        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
   231
        otherwise things become broken.
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
   232
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
   233
        '''
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   234
        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
   235
        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
   236
            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
   237
                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
   238
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   239
            if len(self.conn_pool[name]) >= self.conn_known[name].pool_size:
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   240
                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
   241
                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
   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
            # 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
   244
            try:
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   245
                conn.rollback()
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   246
            except OperationalError:
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   247
                conn.close()
a9e12b7cc207 Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   248
                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
   249
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.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
   251
        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
   252
            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
   253
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
    @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
   255
    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
   256
        '''Cursor context.
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
   257
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
   258
        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
   259
        and returns cursor for that connection.
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
   260
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
   261
        '''
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
        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
   263
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
        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
   265
            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
   266
            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
   267
        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
   268
            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
   269
            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
   270
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
    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
   272
        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
   273
        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
   274
            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
   275
                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
   276
            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
   277
                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
   278
                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
   279
                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
   280
        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
   281
            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
   282
            curs.execute(ci.init_statement)
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   283
            curs.connection.commit()
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   284
            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
   285
        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
   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
    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
   288
        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
   289
            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
   290
        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
   291
            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
   292
            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
   293
                '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
   294
                '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
   295
                '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
   296
                '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
   297
                '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
   298
                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
   299
        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
   300
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   301
    def _check_fork(self):
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   302
        '''Check if process was forked (PID has changed).
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   303
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   304
        If it was, clean parent's connections.
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   305
        New connections are created for children.
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   306
        Known connection credentials are inherited, but not shared.
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   307
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   308
        '''
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   309
        if self.pid == multiprocessing.current_process().pid:
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   310
            # PID has not changed
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   311
            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
   312
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   313
        # update saved PID
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   314
        self.pid = multiprocessing.current_process().pid
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   315
        # reinitialize lock
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   316
        self.lock = threading.Lock()
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   317
        # clean parent's connections
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   318
        for name in self.conn_pool:
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   319
            self.conn_pool[name] = []
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   320
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   321
    @classmethod
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   322
    def get_instance(cls):
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   323
        if not hasattr(cls, '_instance'):
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   324
            cls._instance = cls()
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   325
        return cls._instance
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   326
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   327
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   328
def get_instance():
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   329
    return MyManager.get_instance()
15
93450b43e627 Add MyManager - replica of PgManager for MySQLdb. Intentionally not inherited from common base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   330