MySQLObjectStore.py :  » Web-Frameworks » Webware » Webware-1.0.2 » MiddleKit » Run » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » MiddleKit » Run » MySQLObjectStore.py
import new

import MySQLdb
from MySQLdb import Warning

from SQLObjectStore import SQLObjectStore

try: # for Python < 2.3
  True, False
except NameError:
  True, False = 1, 0


class MySQLObjectStore(SQLObjectStore):
  """MySQLObjectStore implements an object store backed by a MySQL database.

  MySQL notes:
    * MySQL home page: http://www.mysql.com.
    * MySQL version this was developed and tested with: 3.22.34 & 3.23.27
    * The platforms developed and tested with include Linux (Mandrake 7.1) and Windows ME.
    * The MySQL-Python DB API 2.0 module used under the hood is MySQLdb by Andy Dustman.
      http://dustman.net/andy/python/MySQLdb/
    * Newer versions of MySQLdb have autocommit switched off by default

  The connection arguments passed to __init__ are:
    - host
    - user
    - passwd
    - port
    - unix_socket
    - client_flag
    - autocommit

  You wouldn't use the 'db' argument, since that is determined by the model.

  See the MySQLdb docs or the DB API 2.0 docs for more information.
    http://www.python.org/topics/database/DatabaseAPI-2.0.html

  """

  def __init__(self, **kwargs):
    try:
      self._autocommit = kwargs['autocommit']
      del kwargs['autocommit']
    except KeyError:
      self._autocommit = False
    SQLObjectStore.__init__(self, **kwargs)

  def newConnection(self):
    kwargs = self._dbArgs.copy()
    self.augmentDatabaseArgs(kwargs)
    conn = self.dbapiModule().connect(**kwargs)
    if self._autocommit:
      # MySQLdb 1.2.0 and later disables autocommit by default
      try:
        conn.autocommit(True)
      except AttributeError:
        pass
    return conn

  def connect(self):
    SQLObjectStore.connect(self)
    if self._autocommit:
      # Since our autocommit patch above does not get applied to pooled
      # connections, we have to monkey-patch the pool connection method
      try:
        pool = self._pool
        connection = pool.connection
      except AttributeError:
        pass
      else:
        def newConnection(self):
          conn = self._normalConnection()
          try:
            conn.autocommit(True)
          except AttributeError:
            pass
          return conn
        pool._normalConnection = connection
        pool._autocommit = self._autocommit
        pool.connection = new.instancemethod(
          newConnection, pool, pool.__class__)

  def retrieveLastInsertId(self, conn, cur):
    try:
      # MySQLdb module 1.2.0 and later
      id = conn.insert_id()
    except AttributeError:
      # MySQLdb module 1.0.0 and earlier
      id = cur.insert_id()
    # The above is more efficient than this:
    # conn, cur = self.executeSQL('select last_insert_id();', conn)
    # id = cur.fetchone()[0]
    return id

  def dbapiModule(self):
    return MySQLdb

  def augmentDatabaseArgs(self, args, pool=0):
    if not args.get('db'):
      args['db'] = self._model.sqlDatabaseName()

  def _executeSQL(self, cur, sql):
    try:
      cur.execute(sql)
    except MySQLdb.Warning, e:
      if not self.setting('IgnoreSQLWarnings', False):
        raise

# Mixins

class StringAttr:

  def sqlForNonNone(self, value):
    """MySQL provides a quoting function for string -- this method uses it."""
    return "'" + MySQLdb.escape_string(value) + "'"
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.