db.py 15 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1
##############################################################################
Kevin Deldycke's avatar
Kevin Deldycke committed
2
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
3 4
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
Kevin Deldycke's avatar
Kevin Deldycke committed
5
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
6 7
# Copyright (c) Digital Creations.  All rights reserved.
# Copyright (c) Nexedi SARL 2004.  All rights reserved.
Kevin Deldycke's avatar
Kevin Deldycke committed
8
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
9
# This license has been certified as Open Source(tm).
Kevin Deldycke's avatar
Kevin Deldycke committed
10
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
11 12 13
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
Kevin Deldycke's avatar
Kevin Deldycke committed
14
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
15 16
# 1. Redistributions in source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer.
Kevin Deldycke's avatar
Kevin Deldycke committed
17
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
18 19 20 21
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions, and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
Kevin Deldycke's avatar
Kevin Deldycke committed
22
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
23 24 25 26 27 28 29
# 3. Digital Creations requests that attribution be given to Zope
#    in any manner possible. Zope includes a "Powered by Zope"
#    button that is installed by default. While it is not a license
#    violation to remove this button, it is requested that the
#    attribution remain. A significant investment has been put
#    into Zope, and this effort will continue if the Zope community
#    continues to grow. This is one way to assure that growth.
Kevin Deldycke's avatar
Kevin Deldycke committed
30
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
31 32 33
# 4. All advertising materials and documentation mentioning
#    features derived from or use of this software must display
#    the following acknowledgement:
Kevin Deldycke's avatar
Kevin Deldycke committed
34
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
35 36 37
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
Kevin Deldycke's avatar
Kevin Deldycke committed
38
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
39 40 41
#    In the event that the product being advertised includes an
#    intact Zope distribution (with copyright and license included)
#    then this clause is waived.
Kevin Deldycke's avatar
Kevin Deldycke committed
42
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
43 44 45
# 5. Names associated with Zope or Digital Creations must not be used to
#    endorse or promote products derived from this software without
#    prior written permission from Digital Creations.
Kevin Deldycke's avatar
Kevin Deldycke committed
46
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
47 48
# 6. Modified redistributions of any form whatsoever must retain
#    the following acknowledgment:
Kevin Deldycke's avatar
Kevin Deldycke committed
49
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
50 51 52
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
Kevin Deldycke's avatar
Kevin Deldycke committed
53
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
54 55
#    Intact (re-)distributions of any official Zope release do not
#    require an external acknowledgement.
Kevin Deldycke's avatar
Kevin Deldycke committed
56
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
57 58 59 60 61 62
# 7. Modifications are encouraged but must be packaged separately as
#    patches to official Zope releases.  Distributions that do not
#    clearly separate the patches from the original work must be clearly
#    labeled as unofficial distributions.  Modifications which do not
#    carry the name Zope may be packaged in any form, as long as they
#    conform to all of the clauses above.
Kevin Deldycke's avatar
Kevin Deldycke committed
63 64
#
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
65
# Disclaimer
Kevin Deldycke's avatar
Kevin Deldycke committed
66
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
67 68 69 70 71 72 73 74 75 76 77 78
#   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
#   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
Kevin Deldycke's avatar
Kevin Deldycke committed
79 80
#
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
81 82 83
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations.  Specific
# attributions are listed in the accompanying credits file.
Kevin Deldycke's avatar
Kevin Deldycke committed
84
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
85 86
##############################################################################

87 88 89 90
import _mysql
import MySQLdb
from _mysql_exceptions import OperationalError, NotSupportedError
MySQLdb_version_required = (0,9,2)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
_v = getattr(_mysql, 'version_info', (0,0,0))
if _v < MySQLdb_version_required:
    raise NotSupportedError, \
       "ZMySQLDDA requires at least MySQLdb %s, %s found" % \
       (MySQLdb_version_required, _v)

from MySQLdb.converters import conversions
from MySQLdb.constants import FIELD_TYPE, CR, CLIENT
from Shared.DC.ZRDB.TM import TM
from DateTime import DateTime
from zLOG import LOG, ERROR, INFO

import string, sys
from string import strip, split, find, upper, rfind
from time import time
107
from thread import get_ident, allocate_lock
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

hosed_connection = (
    CR.SERVER_GONE_ERROR,
    CR.SERVER_LOST
    )

key_types = {
    "PRI": "PRIMARY KEY",
    "MUL": "INDEX",
    "UNI": "UNIQUE",
    }

field_icons = "bin", "date", "datetime", "float", "int", "text", "time"

icon_xlate = {
    "varchar": "text", "char": "text",
    "enum": "what", "set": "what",
    "double": "float", "numeric": "float",
    "blob": "bin", "mediumblob": "bin", "longblob": "bin",
    "tinytext": "text", "mediumtext": "text",
    "longtext": "text", "timestamp": "datetime",
    "decimal": "float", "smallint": "int",
    "mediumint": "int", "bigint": "int",
    }

type_xlate = {
    "double": "float", "numeric": "float",
    "decimal": "float", "smallint": "int",
    "mediumint": "int", "bigint": "int",
    "int": "int", "float": "float",
    "timestamp": "datetime", "datetime": "datetime",
    "time": "datetime",
    }

def _mysql_timestamp_converter(s):
  if len(s) < 14:
    s = s + "0"*(14-len(s))
  parts = map(int, (s[:4],s[4:6],s[6:8],
                    s[8:10],s[10:12],s[12:14]))
  return DateTime("%04d-%02d-%02d %02d:%02d:%02d" % tuple(parts))

def DateTime_or_None(s):
    try: return DateTime(s)
    except: return None

def int_or_long(s):
    try: return int(s)
    except: return long(s)

157
class ThreadedDeferredDB:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
158 159 160 161 162
    """
        An experimental MySQL DA which implements deferred execution
        of SQL code in order to reduce locks and provide better behaviour
        with MyISAM non transactional tables
    """
Kevin Deldycke's avatar
Kevin Deldycke committed
163

164 165 166 167 168 169 170
    conv=conversions.copy()
    conv[FIELD_TYPE.LONG] = int_or_long
    conv[FIELD_TYPE.DATETIME] = DateTime_or_None
    conv[FIELD_TYPE.DATE] = DateTime_or_None
    conv[FIELD_TYPE.DECIMAL] = float
    del conv[FIELD_TYPE.TIME]

Jean-Paul Smets's avatar
Jean-Paul Smets committed
171
    def __init__(self,connection):
172 173 174 175 176 177 178 179 180 181 182 183
        """
          Parse the connection string.
          Initiate a trial connection with the database to check
          transactionality once instead of once per DeferredDB instance.
        """
        self._connection = connection
        self._kw_args = self._parse_connection_string(connection)
        self._db_pool = {}
        self._db_lock = allocate_lock()
        connection = MySQLdb.connect(**self._kw_args)
        transactional = connection.server_capabilities & CLIENT.TRANSACTIONS
        connection.close()
184 185 186 187 188 189 190
        if self._try_transactions == '-':
            transactional = 0
        elif not transactional and self._try_transactions == '+':
            raise NotSupportedError, "transactions not supported by this server"
        self._use_TM = self._transactions = transactional
        if self._mysql_lock:
            self._use_TM = 1
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    def _parse_connection_string(self, connection):
        kwargs = {'conv': self.conv}
        items = split(connection)
        self._use_TM = None
        if not items: return kwargs
        lockreq, items = items[0], items[1:]
        if lockreq[0] == "*":
            self._mysql_lock = lockreq[1:]
            db_host, items = items[0], items[1:]
            self._use_TM = 1
        else:
            self._mysql_lock = None
            db_host = lockreq
        if '@' in db_host:
            db, host = split(db_host,'@',1)
            kwargs['db'] = db
            if ':' in host:
                host, port = split(host,':',1)
                kwargs['port'] = int(port)
            kwargs['host'] = host
        else:
            kwargs['db'] = db_host
        if kwargs['db'] and kwargs['db'][0] in ('+', '-'):
            self._try_transactions = kwargs['db'][0]
            kwargs['db'] = kwargs['db'][1:]
        else:
            self._try_transactions = None
        if not kwargs['db']:
            del kwargs['db']
        if not items: return kwargs
        kwargs['user'], items = items[0], items[1:]
        if not items: return kwargs
        kwargs['passwd'], items = items[0], items[1:]
        if not items: return kwargs
        kwargs['unix_socket'], items = items[0], items[1:]
        return kwargs

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    def _pool_set(self, key, value):
      self._db_lock.acquire()
      try:
        self._db_pool[key] = value
      finally:
        self._db_lock.release()

    def _pool_get(self, key):
      self._db_lock.acquire()
      try:
        return self._db_pool.get(key)
      finally:
        self._db_lock.release()

    def _access_db(self, method_id, args, kw):
        """
          Generic method to call pooled objects' methods.
          When the current thread had never issued any call, create a DeferredDB
          instance.
        """
        ident = get_ident()
        db = self._pool_get(ident)
        if db is None:
            db = DeferredDB(kw_args=self._kw_args, use_TM=self._use_TM,
                            mysql_lock=self._mysql_lock,
                            transactions=self._transactions)
            self._pool_set(ident, db)
        return getattr(db, method_id)(*args, **kw)

    def tables(self, *args, **kw):
        return self._access_db(method_id='tables', args=args, kw=kw)

    def columns(self, *args, **kw):
        return self._access_db(method_id='columns', args=args, kw=kw)

    def query(self, *args, **kw):
        return self._access_db(method_id='query', args=args, kw=kw)

    def string_literal(self, *args, **kw):
        return self._access_db(method_id='string_literal', args=args, kw=kw)


class DeferredDB(TM):
    """
        An experimental MySQL DA which implements deferred execution
        of SQL code in order to reduce locks and provide better behaviour
        with MyISAM non transactional tables
    """

    defs={
        FIELD_TYPE.CHAR: "i", FIELD_TYPE.DATE: "d",
        FIELD_TYPE.DATETIME: "d", FIELD_TYPE.DECIMAL: "n",
        FIELD_TYPE.DOUBLE: "n", FIELD_TYPE.FLOAT: "n", FIELD_TYPE.INT24: "i",
        FIELD_TYPE.LONG: "i", FIELD_TYPE.LONGLONG: "l",
        FIELD_TYPE.SHORT: "i", FIELD_TYPE.TIMESTAMP: "d",
        FIELD_TYPE.TINY: "i", FIELD_TYPE.YEAR: "i",
        }

    _p_oid=_p_changed=_registered=None

    def __init__(self, kw_args, use_TM, mysql_lock, transactions):
        self._kw_args = kw_args
        self._mysql_lock = mysql_lock
        self._use_TM = use_TM
        self._transactions = transactions
        self._forceReconnection()
        self._sql_string_list = []

    def __del__(self):
        self.db.close()

    def _forceReconnection(self):
        db = MySQLdb.connect(**self._kw_args)
        self.db = db

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    def tables(self, rdb=0,
               _care=('TABLE', 'VIEW')):
        r=[]
        a=r.append
        result = self._query("SHOW TABLES")
        row = result.fetch_row(1)
        while row:
            a({'TABLE_NAME': row[0][0], 'TABLE_TYPE': 'TABLE'})
            row = result.fetch_row(1)
        return r

    def columns(self, table_name):
        from string import join
        try:
            c = self._query('SHOW COLUMNS FROM %s' % table_name)
        except:
            return ()
        r=[]
        for Field, Type, Null, Key, Default, Extra in c.fetch_row(0):
            info = {}
            field_default = Default and "DEFAULT %s"%Default or ''
            if Default: info['Default'] = Default
            if '(' in Type:
                end = rfind(Type,')')
                short_type, size = split(Type[:end],'(',1)
                if short_type not in ('set','enum'):
                    if ',' in size:
                        info['Scale'], info['Precision'] = \
                                       map(int, split(size,',',1))
                    else:
                        info['Scale'] = int(size)
            else:
                short_type = Type
            if short_type in field_icons:
                info['Icon'] = short_type
            else:
                info['Icon'] = icon_xlate.get(short_type, "what")
            info['Name'] = Field
            info['Type'] = type_xlate.get(short_type,'string')
            info['Extra'] = Extra,
            info['Description'] = join([Type, field_default, Extra or '',
                                        key_types.get(Key, Key or ''),
                                        Null != 'YES' and 'NOT NULL' or '']),
            info['Nullable'] = (Null == 'YES') and 1 or 0
            if Key:
                info['Index'] = 1
            if Key == 'PRI':
                info['PrimaryKey'] = 1
                info['Unique'] = 1
            elif Key == 'UNI':
                info['Unique'] = 1
            r.append(info)
        return r

    def _query(self, query, force_reconnect=False):
      """
        Send a to MySQL server.
        It reconnects automaticaly if needed and the following conditions are
        met:
         - It has not just tried to reconnect (ie, this function will not
           attemp to connect twice per call).
         - This conection is not transactionnal and has set not MySQL locks,
           because they are bound to the connection. This check can be
           overridden by passing force_reconnect with True value.
      """
      try:
370
        self.db.query(query)
371 372 373 374 375 376 377
      except OperationalError, m:
        if ((not force_reconnect) and \
            (self._mysql_lock or self._transactions)) or \
           m[0] not in hosed_connection:
          raise
        # Hm. maybe the db is hosed.  Let's restart it.
        self._forceReconnection()
378 379
        self.db.query(query)
      return self.db.store_result()
380 381 382 383 384 385 386

    def query(self,query_string, max_rows=1000):
        self._use_TM and self._register()
        for qs in filter(None, map(strip,split(query_string, '\0'))):
            qtype = upper(split(qs, None, 1)[0])
            if qtype == "SELECT":
                raise NotSupportedError, "can not SELECT in deferred connections"
387
            self._sql_string_list.append(qs)
388 389 390 391

        return (),()

    def string_literal(self, s):
392
      return self.db.string_literal(s)
393

Jean-Paul Smets's avatar
Jean-Paul Smets committed
394
    def _begin(self, *ignored):
395 396 397
        # The Deferred DB instance is sometimes used for several
        # transactions, so it is required to clear the sql_string_list
        # each time a transaction starts
398 399
        self._sql_string_list = []
        self._transaction_begun = True
Kevin Deldycke's avatar
Kevin Deldycke committed
400

Jean-Paul Smets's avatar
Jean-Paul Smets committed
401
    def _finish(self, *ignored):
402
        if not self._transaction_begun:
403
            return
404
        self._transaction_begun = False
405 406 407 408 409 410
        # Ping the database to reconnect if connection was lost.
        self._query("SELECT 1", force_reconnect=True)
        if self._transactions:
            self._query("BEGIN")
        if self._mysql_lock:
            self._query("SELECT GET_LOCK('%s',0)" % self._mysql_lock)
411
        for qs in self._sql_string_list:
412 413 414 415 416
            self._query(qs)
        if self._mysql_lock:
            self._query("SELECT RELEASE_LOCK('%s')" % self._mysql_lock)
        if self._transactions:
            self._query("COMMIT")
Jean-Paul Smets's avatar
Jean-Paul Smets committed
417 418

    def _abort(self, *ignored):
419
        self._transaction_begun = False
420