adodb_odbc.py :  » Database » ADOdb » adodb-220 » adodb » 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 » Database » ADOdb 
ADOdb » adodb 220 » adodb » adodb_odbc.py
########################################################################
# Vers 2.10 16 July 2008, (c)2004-2008 John Lim (jlim#natsoft.com) All Rights Reserved
# Released under a BSD-style license. See LICENSE.txt.
# Download: http://adodb.sourceforge.net/#pydownload
########################################################################

import adodb
import odbc,time,re,datetime

#Thread Safety= ? undefined
#Param Style  = ? undefined
try:
    True, False
except NameError:
    # Maintain compatibility with Python 2.2
    True, False = 1, 0
        
class adodb_odbc(adodb.ADOConnection):
    databaseType = 'odbc'
    databaseProvider = 'odbc'
    hasRowCount = False
    replaceQuote = "''"
        
    def __init__(self):
        pass

    def Module(self):
        return odbc
    
    #host=host1 user=user1 password=secret port=4341    
    def _connect(self,host=None,user=None,password=None,database=None):
        if user == None and password == None and database == None:
            dsn = host
        else:
            dsn = 'dsn='+self.addq(host)
            if (user != None): dsn += '; uid='+self.addq(user)
            if (password != None): dsn += '; pwd='+self.addq(password)
            if (database != None): dsn += '; database='+self.addq(database)
        
        self._conn = odbc.odbc(dsn)
        self._conn.setautocommit(1)

    def _newcursor(self,rs):
        return cursor_odbc(rs,self)

    
    hasTop = 'TOP'
    _retop = None
    
    def SelectLimit(self,sql,limit,offset=-1,params=None):
        if self._retop == None:
            self._retop = re.compile(r'(^\s*select\s+(distinctrow|distinct)?)',re.IGNORECASE)
            
        sql = self._retop.sub('\\1 '+self.hasTop+' '+str(limit)+' ',sql)
        return self.Execute(sql)

    def BeginTrans(self):
        if self._autocommit:
            self._autocommit = False
        self._conn.setautocommit(0)

    def RollbackTrans(self):
        self._conn.rollback()
        self._autocommit = True
        
    def CommitTrans(self):
        self._conn.commit()
        self._autocommit = True

    # (1997, 12, 19, 1, 51, 53)
    def Date(self,d):
        return datetime.datetime(long(d[0]),long(d[1]),long(d[2]))
    
    def TimeStamp(self,d):
        d = time.localtime(d)
        return datetime.datetime(long(d[0]),long(d[1]),long(d[2]),long(d[3]),long(d[4]),long(d[5]))

    def MetaColumns(self, table):
        rs = self.Execute("select * from "+table)
        arr = []
        cnt = 0
        for ff in rs.fields:
            f = rs.FetchField(cnt)
            arr.append((f[0], self.MetaType(f[1]), f[2]))
            cnt += 1
        return arr
            
class cursor_odbc(adodb.ADOCursor):
    def __init__(self,rs,conn):
        adodb.ADOCursor.__init__(self,rs,conn,norowcount=True)
            
if __name__ == '__main__':
    db = adodb_odbc()
    db.Connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\\inetpub\\adodb\\northwind.mdb;Uid=Admin;Pwd=;")
    adodb.Test(db)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.