PageRenderTime 41ms CodeModel.GetById 21ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/lib/python/indra/ipc/mysql_pool.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 81 lines | 72 code | 0 blank | 9 comment | 0 complexity | 4e3248aa9e6b13326c9d559c36bfd6f3 MD5 | raw file
 1"""\
 2@file mysql_pool.py
 3@brief Thin wrapper around eventlet.db_pool that chooses MySQLdb and Tpool.
 4
 5$LicenseInfo:firstyear=2007&license=mit$
 6
 7Copyright (c) 2007-2009, Linden Research, Inc.
 8
 9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26$/LicenseInfo$
27"""
28
29import MySQLdb
30from eventlet import db_pool
31
32class DatabaseConnector(db_pool.DatabaseConnector):
33    def __init__(self, credentials, *args, **kwargs):
34        super(DatabaseConnector, self).__init__(MySQLdb, credentials,
35                                                conn_pool=db_pool.ConnectionPool,
36                                                *args, **kwargs)
37
38    # get is extended relative to eventlet.db_pool to accept a port argument
39    def get(self, host, dbname, port=3306):
40        key = (host, dbname, port)
41        if key not in self._databases:
42            new_kwargs = self._kwargs.copy()
43            new_kwargs['db'] = dbname
44            new_kwargs['host'] = host
45            new_kwargs['port'] = port
46            new_kwargs.update(self.credentials_for(host))
47            dbpool = ConnectionPool(*self._args, **new_kwargs)
48            self._databases[key] = dbpool
49
50        return self._databases[key]
51
52class ConnectionPool(db_pool.TpooledConnectionPool):
53    """A pool which gives out saranwrapped MySQLdb connections from a pool
54    """
55
56    def __init__(self, *args, **kwargs):
57        super(ConnectionPool, self).__init__(MySQLdb, *args, **kwargs)
58
59    def get(self):
60        conn = super(ConnectionPool, self).get()
61        # annotate the connection object with the details on the
62        # connection; this is used elsewhere to check that you haven't
63        # suddenly changed databases in midstream while making a
64        # series of queries on a connection.
65        arg_names = ['host','user','passwd','db','port','unix_socket','conv','connect_timeout',
66         'compress', 'named_pipe', 'init_command', 'read_default_file', 'read_default_group',
67         'cursorclass', 'use_unicode', 'charset', 'sql_mode', 'client_flag', 'ssl',
68         'local_infile']
69        # you could have constructed this connectionpool with a mix of
70        # keyword and non-keyword arguments, but we want to annotate
71        # the connection object with a dict so it's easy to check
72        # against so here we are converting the list of non-keyword
73        # arguments (in self._args) into a dict of keyword arguments,
74        # and merging that with the actual keyword arguments
75        # (self._kwargs).  The arg_names variable lists the
76        # constructor arguments for MySQLdb Connection objects.
77        converted_kwargs = dict([ (arg_names[i], arg) for i, arg in enumerate(self._args) ])
78        converted_kwargs.update(self._kwargs)
79        conn.connection_parameters = converted_kwargs
80        return conn
81