commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gnue/common/src/dbdrivers/mysql DBdriver.py


From: Jason Cater
Subject: gnue/common/src/dbdrivers/mysql DBdriver.py
Date: Sat, 09 Feb 2002 12:50:32 -0500

CVSROOT:        /cvsroot/gnue
Module name:    gnue
Changes by:     Jason Cater <address@hidden>    02/02/09 12:50:32

Modified files:
        common/src/dbdrivers/mysql: DBdriver.py 

Log message:
        adding schema support for MySQL

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue/common/src/dbdrivers/mysql/DBdriver.py.diff?tr1=1.10&tr2=1.11&r1=text&r2=text

Patches:
Index: gnue/common/src/dbdrivers/mysql/DBdriver.py
diff -c gnue/common/src/dbdrivers/mysql/DBdriver.py:1.10 
gnue/common/src/dbdrivers/mysql/DBdriver.py:1.11
*** gnue/common/src/dbdrivers/mysql/DBdriver.py:1.10    Thu Jan 10 15:59:18 2002
--- gnue/common/src/dbdrivers/mysql/DBdriver.py Sat Feb  9 12:50:32 2002
***************
*** 1,19 ****
  #
  # This file is part of GNU Enterprise.
  #
! # GNU Enterprise is free software; you can redistribute it 
! # and/or modify it under the terms of the GNU General Public 
! # License as published by the Free Software Foundation; either 
  # version 2, or (at your option) any later version.
  #
! # GNU Enterprise is distributed in the hope that it will be 
! # useful, but WITHOUT ANY WARRANTY; without even the implied 
! # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  # PURPOSE. See the GNU General Public License for more details.
  #
! # You should have received a copy of the GNU General Public 
! # License along with program; see the file COPYING. If not, 
! # write to the Free Software Foundation, Inc., 59 Temple Place 
  # - Suite 330, Boston, MA 02111-1307, USA.
  #
  # Copyright 2000, 2001 Free Software Foundation
--- 1,19 ----
  #
  # This file is part of GNU Enterprise.
  #
! # GNU Enterprise is free software; you can redistribute it
! # and/or modify it under the terms of the GNU General Public
! # License as published by the Free Software Foundation; either
  # version 2, or (at your option) any later version.
  #
! # GNU Enterprise is distributed in the hope that it will be
! # useful, but WITHOUT ANY WARRANTY; without even the implied
! # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  # PURPOSE. See the GNU General Public License for more details.
  #
! # You should have received a copy of the GNU General Public
! # License along with program; see the file COPYING. If not,
! # write to the Free Software Foundation, Inc., 59 Temple Place
  # - Suite 330, Boston, MA 02111-1307, USA.
  #
  # Copyright 2000, 2001 Free Software Foundation
***************
*** 29,35 ****
  # (which it does NOT by default)
  
  
! from string import lower
  import sys
  from gnue.common import GDebug, GDataObjects
  from gnue.common.dbdrivers._dbsig.DBdriver \
--- 29,35 ----
  # (which it does NOT by default)
  
  
! import string
  import sys
  from gnue.common import GDebug, GDataObjects
  from gnue.common.dbdrivers._dbsig.DBdriver \
***************
*** 51,57 ****
  
  
  class MySQL_RecordSet(DBSIG_RecordSet): 
!   pass    
  
  
  class MySQL_ResultSet(DBSIG_ResultSet): 
--- 51,57 ----
  
  
  class MySQL_RecordSet(DBSIG_RecordSet): 
!   pass
  
  
  class MySQL_ResultSet(DBSIG_ResultSet): 
***************
*** 62,68 ****
  
  
  
! class MySQL_DataObject(DBSIG_DataObject): 
    def __init__(self): 
      DBSIG_DataObject.__init__(self)
      self._DatabaseError = MySQLdb.DatabaseError
--- 62,68 ----
  
  
  
! class MySQL_DataObject(DBSIG_DataObject):
    def __init__(self): 
      DBSIG_DataObject.__init__(self)
      self._DatabaseError = MySQLdb.DatabaseError
***************
*** 71,80 ****
  
    def connect(self, connectData={}): 
      GDebug.printMesg(1,"Mysql database driver initializing")
!     try: 
!       self._dataConnection = MySQLdb.connect(user=connectData['_username'], 
!                    passwd=connectData['_password'], 
!                    host=connectData['host'], 
                     db=connectData['dbname'])
      except self._DatabaseError, value:
        raise GDataObjects.LoginError, value
--- 71,80 ----
  
    def connect(self, connectData={}): 
      GDebug.printMesg(1,"Mysql database driver initializing")
!     try:
!       self._dataConnection = MySQLdb.connect(user=connectData['_username'],
!                    passwd=connectData['_password'],
!                    host=connectData['host'],
                     db=connectData['dbname'])
      except self._DatabaseError, value:
        raise GDataObjects.LoginError, value
***************
*** 88,117 ****
  
  
    def _beginTransaction(self):
!     try: 
        self._dataConnection.begin()
      except: 
        pass
  
  class MySQL_DataObject_Object(MySQL_DataObject, \
        DBSIG_DataObject_Object):
  
!   def __init__(self): 
      MySQL_DataObject.__init__(self)
  
!   def _buildQuery(self, conditions={}): 
      return DBSIG_DataObject_Object._buildQuery(self, conditions)
  
  
  class MySQL_DataObject_SQL(MySQL_DataObject, \
!       DBSIG_DataObject_SQL): 
!   def __init__(self): 
!     # Call DBSIG init first because MySQL_DataObject needs to overwrite 
      # some of its values
!     DBSIG_DataObject_SQL.__init__(self) 
      MySQL_DataObject.__init__(self)
  
!   def _buildQuery(self, conditions={}): 
      return DBSIG_DataObject_SQL._buildQuery(self, conditions)
  
  
--- 88,202 ----
  
  
    def _beginTransaction(self):
!     try:
        self._dataConnection.begin()
      except: 
        pass
  
+ 
+   #
+   # Schema (metadata) functions
+   #
+ 
+   # Return a list of the types of Schema objects this driver provides
+   def getSchemaTypes(self):
+     return [('table','Table',1)]
+ 
+   # Return a list of Schema objects
+   def getSchemaList(self, type=None):
+ 
+     # TODO: This excludes any system tables and views. Should it?
+     statement = "SHOW TABLES"
+ 
+     cursor = self._dataConnection.cursor()
+     cursor.execute(statement)
+ 
+     list = []
+     for rs in cursor.fetchall():
+       list.append(GDataObjects.Schema(attrs={'id':rs[0], 'name':rs[0],
+                          'type':'table'},
+                          getChildSchema=self.__getFieldSchema))
+ 
+     cursor.close()
+     return list
+ 
+ 
+   # Find a schema object with specified name
+   def getSchemaByName(self, name, type=None):
+     statement = "DESCRIBE %s" % (name)
+ 
+     cursor = self._dataConnection.cursor()
+     cursor.execute(statement)
+ 
+     rs = cursor.fetchone()
+     if rs:
+       schema = GDataObjects.Schema(attrs={'id':name, 'name':name,
+                            'type':'table'},
+                            getChildSchema=self.__getFieldSchema)
+     else:
+       schema = None
+ 
+     cursor.close()
+     return schema
+ 
+ 
+   # Get fields for a table
+   def __getFieldSchema(self, parent):
+ 
+     statement = "DESCRIBE %s" % parent.id
+ 
+     cursor = self._dataConnection.cursor()
+     cursor.execute(statement)
+ 
+     list = []
+     for rs in cursor.fetchall():
+ 
+       nativetype = string.split(string.replace(rs[1],')',''),'(')
+ 
+ 
+       attrs={'id': "%s.%s" % (parent.id, rs[0]), 'name': rs[0],
+              'type':'field', 'nativetype': nativetype[0],
+              'required': rs[2] != 'YES'}
+ 
+       if nativetype[0] in ('int','integer','bigint','mediumint',
+                            'smallint','tinyint','float','real',
+                            'double','decimal'):
+         attrs['datatype']='number'
+       elif nativetype[0] in ('date','time','timestamp','datetime'):
+         attrs['datatype']='date'
+       else:
+         attrs['datatype']='text'
+ 
+       if len(nativetype) == 2:
+         attrs['length'] = int(nativetype[1])
+ 
+       list.append(GDataObjects.Schema(attrs=attrs))
+ 
+     cursor.close()
+     return list
+ 
+ 
+ 
+ 
  class MySQL_DataObject_Object(MySQL_DataObject, \
        DBSIG_DataObject_Object):
  
!   def __init__(self):
      MySQL_DataObject.__init__(self)
  
!   def _buildQuery(self, conditions={}):
      return DBSIG_DataObject_Object._buildQuery(self, conditions)
  
  
  class MySQL_DataObject_SQL(MySQL_DataObject, \
!       DBSIG_DataObject_SQL):
!   def __init__(self):
!     # Call DBSIG init first because MySQL_DataObject needs to overwrite
      # some of its values
!     DBSIG_DataObject_SQL.__init__(self)
      MySQL_DataObject.__init__(self)
  
!   def _buildQuery(self, conditions={}):
      return DBSIG_DataObject_SQL._buildQuery(self, conditions)
  
  
***************
*** 147,153 ****
  #
  #  All datasouce "types" and corresponding DataObject class
  # 
! supportedDataObjects = { 
    'object': MySQL_DataObject_Object,
    'sql':    MySQL_DataObject_SQL
  }
--- 232,238 ----
  #
  #  All datasouce "types" and corresponding DataObject class
  # 
! supportedDataObjects = {
    'object': MySQL_DataObject_Object,
    'sql':    MySQL_DataObject_SQL
  }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]