__init__.py :  » Web-Server » SkunkWEB » skunkweb-3.4.4 » pylibs » MySQL » 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 Server » SkunkWEB 
SkunkWEB » skunkweb 3.4.4 » pylibs » MySQL » __init__.py
# Time-stamp: <03/04/23 17:57:46 smulloni>

########################################################################
#  Copyright (C) 2001 Andrew T. Csillag <drew_csillag@geocities.com>
#  
#      You may distribute under the terms of either the GNU General
#      Public License or the SkunkWeb License, as specified in the
#      README file.
########################################################################

import sys
import MySQLdb
from SkunkExcept import *

# The connect params dictionary 
_users = {}

# actual connections
_connections = {}

# an optional connection test; should be callable, take a connection
# as an argument, and return a boolean indicating whether the
# connection is valid
connection_test = None

# a reasonable test query
def SimpleQueryTest(conn):
    try:
        conn.ping()
    except MySQLdb.DatabaseError:
        return 0
    else:
        return 1

def initUser(connUser, connParams):
    """
    Prior to using the module, you must call this function 
    to add a user and associate it with a connect string. 
    The function can be called more than once.
    """
    if not _users.has_key(connUser):
        _users[connUser] = connParams

def cleanUser(connUser):
    """
    Destroys the database connection defined by connUser.
    If there is no database connection open for connUser,
    a SkunkStandardError is thrown.
    """
    if not _users.has_key(connUser) :
        raise SkunkStandardError, 'user %s is not initialized' % (connUser)

    del _users[connUser]
    if _connections.has_key(connUser):
        _connections[connUser].close()
        del _connections[connUser]

def getConnection(connUser):
    """
    Returns a database connection as defined by 
    connUser. If this module already has an open
    connection for connUser, it returns it; otherwise,
    it creates a new connection, stores it, and returns it.
    """
    try:
        connectParams = _users[connUser]
    except KeyError:
        raise SkunkStandardError, 'user %s is not initialized' % (connUser)
    
    if not _connections.has_key(connUser):
        db = _connections[connUser] = _real_connect(connUser, connectParams)
    else:
        db=_connections[connUser]
        # hook for testing the connection before returning it from the cache
        if connection_test and not connection_test(db):
            # there is apparently a bug in some versions of MySQLdb
            # that causes connections not to be closed properly
            # when garbage collected.  This attempts to resolve that
            # problem when possible.
            try:
                db.close()
            except:
                pass
            # these next are redundant
            del db
            del _connections[connUser]
            
            db=_real_connect(connUser, connectParams)
            _connections[connUser]=db
    return db

def _real_connect(connUser, connParams):
    return MySQLdb.connect(**connParams)
    
def _initStuff():
    #here so that these will be imported ahead of time so that
    #userModuleCleanup won't clobber them
    import MySQLdb
    from MySQLdb import connections,converters,cursors,sets,times
    from MySQLdb import constants,_mysql
    from MySQLdb.constants import CLIENT,CR,ER,FIELD_TYPE,FLAG,REFRESH
    
_initStuff()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.