commit-gnue
[Top][All Lists]
Advanced

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

gnue common/src/GDataObjects.py common/src/dbdr...


From: Jan Ischebeck
Subject: gnue common/src/GDataObjects.py common/src/dbdr...
Date: Thu, 03 Oct 2002 10:35:49 -0400

CVSROOT:        /cvsroot/gnue
Module name:    gnue
Changes by:     Jan Ischebeck <address@hidden>  02/10/03 10:35:48

Modified files:
        common/src     : GDataObjects.py 
        common/src/dbdrivers/_dbsig: DBdriver.py 
        common/src/dbdrivers/_pgsql: DBdriver.py 
Added files:
        designer/src/schema/wizards: WriteSchema.py 

Log message:
        add preliminary schema write support to common
        add schema write wizard to designer

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue/common/src/GDataObjects.py.diff?tr1=1.54&tr2=1.55&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue/common/src/dbdrivers/_dbsig/DBdriver.py.diff?tr1=1.54&tr2=1.55&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue/common/src/dbdrivers/_pgsql/DBdriver.py.diff?tr1=1.22&tr2=1.23&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue/designer/src/schema/wizards/WriteSchema.py?rev=1.1

Patches:
Index: gnue/common/src/GDataObjects.py
diff -c gnue/common/src/GDataObjects.py:1.54 
gnue/common/src/GDataObjects.py:1.55
*** gnue/common/src/GDataObjects.py:1.54        Sat Sep 28 01:47:11 2002
--- gnue/common/src/GDataObjects.py     Thu Oct  3 10:35:47 2002
***************
*** 75,80 ****
--- 75,90 ----
    # Generic error reading from the database connection
    pass
  
+ class DataTypeNotAvailable(Error):
+   # Raised when a datatype is not supported by an database
+   # during writing a schema definition to the database
+   pass
+ 
+ class NoWriteSchemaSupport(Error):
+   # Raised when a database adapter doesn't support 
+   # writing Schema to datasource
+   pass
+ 
  
  
  ###########################################################
***************
*** 236,241 ****
--- 246,254 ----
    def getSchemaByName(self, name, type=None):
      return None
  
+   # write Schema to Database
+   def writeSchema(self,obj,overwrite=0):
+     raise NoWriteSchemaSupport,"This database adapter doesn't have 'schema 
writing' support"
  
    # Called when new record master in master/detail is queried
    def _masterRecordChanged(self, master):
Index: gnue/common/src/dbdrivers/_dbsig/DBdriver.py
diff -c gnue/common/src/dbdrivers/_dbsig/DBdriver.py:1.54 
gnue/common/src/dbdrivers/_dbsig/DBdriver.py:1.55
*** gnue/common/src/dbdrivers/_dbsig/DBdriver.py:1.54   Tue Sep 10 11:15:22 2002
--- gnue/common/src/dbdrivers/_dbsig/DBdriver.py        Thu Oct  3 10:35:48 2002
***************
*** 200,205 ****
--- 200,207 ----
         '__iseq':          (2,   2, '(%s IS %s)',             None     ),
         '__isne':          (2,   2, '(%s IS NOT %s)',         None     )}
  
+   schema2nativeTypes={}
+ 
    def __init__(self, strictQueryCount=1):
      GDataObjects.DataObject.__init__(self)
  
***************
*** 271,277 ****
        # pull a record count for the upcomming query
        if self._strictQueryCount:
          recordCount = self._getQueryCount(conditions)
! 
        cursor.arraysize = self.cache
        cursor.execute(self._buildQuery(conditions))
  
--- 273,279 ----
        # pull a record count for the upcomming query
        if self._strictQueryCount:
          recordCount = self._getQueryCount(conditions)
!         
        cursor.arraysize = self.cache
        cursor.execute(self._buildQuery(conditions))
  
***************
*** 388,393 ****
--- 390,505 ----
    # this is usually not necessary (MySQL is one of few DBs that must force)
    def _beginTransaction(self):
      pass
+ 
+   
+   def _buildFieldDefinition(self,field):
+     try:
+       sql="%s %s" % (field.name,
+                      self.schema2nativeTypes[field.type])
+     except:
+       raise GDataObjects.DataTypeNotAvailable, \
+             "Datatype '%s' is not supported by database"
+     
+     if hasattr(field,"size"):
+       sql=sql+"(%s)" % field.size
+          
+     #   if hasattr(field,"precision"):
+     #   if hasattr(field,"scale"):
+ 
+     if not field.nullable:
+       sql=sql+" NOT NULL"
+       
+     if hasattr(field,"default"):
+       sql=sql+" DEFAULT %s" % self._toSqlString(field.default)
+ 
+     return sql
+ 
+   def _buildTableDefinition(self,tbl):  
+       
+     sql="CREATE TABLE %s (" % tbl.name
+ 
+ 
+     # add fields to table
+ 
+     fields=tbl.findChildOfType("GSFields")
+ 
+     delim=""
+     
+     for field in fields._children:
+       
+        sql=sql+delim+"%s" % self._buildFieldDefinition(field)
+        delim=","
+ 
+     # define primary key
+ 
+     pk=tbl.findChildOfType("GSPrimaryKey")
+ 
+     if (pk!=None) and len(pk._children):
+       
+       sql=sql+", primary key %s (" % pk.name
+       
+       delim=""
+     
+       for pkfield in pk._children:
+         
+         sql=sql+delim+pkfield.name
+         
+         delim=","
+ 
+       sql=sql+") "
+       
+ 
+     # close definition
+ 
+     sql=sql+");"
+ 
+     GDebug.printMesg(1,"SQL Statement: %s" % sql)
+ 
+     return sql
+   
+   def writeTable(self,tbl,overwrite):
+     
+     sql = self._buildTableDefinition(tbl)
+     
+     try:
+       cursor = self._dataConnection.cursor()
+       cursor.execute(sql)
+       cursor.close()
+     except:
+       # test it is an error because of an existing table
+       # directly raise an error, if it is an access rights problem
+       if not overwrite:
+         return "Could not write table %s to database." % tbl.name
+       else:
+         cursor = self._dataConnection.cursor()
+         # drop table
+         cursor.execute("drop table %s" % tbl.name)
+         # create table
+         cursor.execute(sql)
+         cursor.close()
+ 
+     
+   # write Schema to Database
+   def writeSchema(self,obj,overwrite=0):
+     if obj._type=="GSTable":
+       return self.writeTable(obj,overwrite)
+       
+     elif obj._type=="GSView":
+       return self.writeView(obj,overwrite)
+       
+     else:
+       # do the same for all children
+       result=[]
+       for child in obj._children:
+         result.append(self.writeSchema(child))
+ 
+       if len(result)==0:
+         return None
+       elif len(result)==1:
+         return result[0]
+       else:
+         return result
+ 
  
  
  class DBSIG_DataObject_Object:
Index: gnue/common/src/dbdrivers/_pgsql/DBdriver.py
diff -c gnue/common/src/dbdrivers/_pgsql/DBdriver.py:1.22 
gnue/common/src/dbdrivers/_pgsql/DBdriver.py:1.23
*** gnue/common/src/dbdrivers/_pgsql/DBdriver.py:1.22   Mon Jul 22 00:08:50 2002
--- gnue/common/src/dbdrivers/_pgsql/DBdriver.py        Thu Oct  3 10:35:48 2002
***************
*** 89,94 ****
--- 89,109 ----
      self._recordSetClass = PGSQL_RecordSet
  
  class PGSQL_DataObject(DBSIG_DataObject):
+ 
+   schema2nativeTypes={"auto":"int4",
+                       "int":"int",
+                       "number":"int",
+                       "float":"float",
+                       "decimal":"decimal",
+                       "varchar":"varchar",
+                       "char":"char",
+                       "blob":"text",
+                       "text":"text",
+                       "longtext":"text",
+                       "date":"date",
+                       "datetime":"datetime",
+                       "timestamp":"timestamp"}
+ 
    def __init__(self, pgdriver=None, pgresultset=None):
      DBSIG_DataObject.__init__(self)
      self._connectString = 'host=%s dbname=%s user=%s password=%s port=%s'
***************
*** 283,289 ****
  
      cursor.close()
      return list
! 
  #
  #  Extensions to Trigger Namespaces
  #
--- 298,304 ----
  
      cursor.close()
      return list
!    
  #
  #  Extensions to Trigger Namespaces
  #




reply via email to

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