commit-gnue
[Top][All Lists]
Advanced

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

[gnue] r7572 - in trunk/gnue-common/src/datasources/drivers/sapdb: . Sch


From: johannes
Subject: [gnue] r7572 - in trunk/gnue-common/src/datasources/drivers/sapdb: . Schema Schema/Creation sapdb
Date: Thu, 2 Jun 2005 03:33:39 -0500 (CDT)

Author: johannes
Date: 2005-06-02 03:33:38 -0500 (Thu, 02 Jun 2005)
New Revision: 7572

Added:
   trunk/gnue-common/src/datasources/drivers/sapdb/Behavior.py
Removed:
   trunk/gnue-common/src/datasources/drivers/sapdb/Schema/Discovery/
Modified:
   trunk/gnue-common/src/datasources/drivers/sapdb/Schema/Creation/Creation.py
   trunk/gnue-common/src/datasources/drivers/sapdb/sapdb/Connection.py
   trunk/gnue-common/src/datasources/drivers/sapdb/sapdb/__init__.py
Log:
Added Behavior implementation replacing Discovery.Introspection


Added: trunk/gnue-common/src/datasources/drivers/sapdb/Behavior.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sapdb/Behavior.py 2005-06-02 
07:11:47 UTC (rev 7571)
+++ trunk/gnue-common/src/datasources/drivers/sapdb/Behavior.py 2005-06-02 
08:33:38 UTC (rev 7572)
@@ -0,0 +1,242 @@
+# GNU Enterprise Common Library - SAP DB driver - Schema Support
+#
+# Copyright 2001-2005 Free Software Foundation
+#
+# 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.
+#
+# $Id$
+
+from gnue.common.datasources.drivers import DBSIG2
+from gnue.common.datasources import GSchema
+
+# =============================================================================
+#
+# =============================================================================
+
+class Behavior (DBSIG2.Behavior):
+  """
+  Limitations:
+  * MaxDB has no named primary keys, so we build them from "pk_<tablename>"
+  """
+
+  _RELKIND = {'TABLE'  : {'type': 'table'  , 'name': _('Tables')},
+              'VIEW'   : {'type': 'view'   , 'name': _('Views')},
+              # 'SYNONYM': {'type': 'synonym', 'name': _('Synonyms')},
+              # 'RESULT' : {'type': 'result' , 'name': _('Result Table')}
+              }
+
+  # ---------------------------------------------------------------------------
+  # Read the current connection's schema
+  # ---------------------------------------------------------------------------
+
+  def _readSchema_ (self, parent):
+    """
+    Read the connection's schema and build a GSchema object tree connected to
+    the given parent object (which is of type GSSchema).
+    """
+
+    tables = self.__readTables (parent)
+    self.__readFields (tables)
+    self.__readIndices (tables)
+    self.__readConstraints (tables)
+
+
+  # ---------------------------------------------------------------------------
+  # Read all relations of the types listed in _RELKINDS
+  # ---------------------------------------------------------------------------
+
+  def __readTables (self, parent):
+   
+    result  = {}
+    masters = {}
+
+    cmd = u"SELECT tableid, tablename, tabletype, owner FROM DOMAIN.TABLES " \
+           "WHERE TYPE <> 'SYSTEM' ORDER BY tablename"
+
+    cursor = self.__connection.makecursor (cmd)
+    try:
+      for (relid, name, kind, owner) in cursor.fetchall ():
+        if not kind in self._RELKIND:
+          continue
+        if not kind in masters:
+          masters [kind] = GSchema.GSTables (parent, **self._RELKIND [kind])
+
+        attrs = {'id'   : relid,
+                 'name' : name,
+                 'type' : kind,
+                 'owner': owner}
+        result [name] = GSchema.GSTable (masters [kind], **attrs)
+
+    finally:
+      cursor.close ()
+
+    return result
+
+
+  # ---------------------------------------------------------------------------
+  # Read all fields for the given tables
+  # ---------------------------------------------------------------------------
+
+  def __readFields (self, tables):
+
+    cmd = u'SELECT tablename, columnname, mode, datatype, len, dec, ' \
+           '  nullable, "DEFAULT", "DEFAULTFUNCTION", pos, keypos ' \
+           'FROM DOMAIN.COLUMNS ' \
+           'ORDER BY tablename, pos'
+
+    result = {}
+    cursor = self.__connection.makecursor (cmd)
+
+    try:
+      for rs in cursor.fetchall ():
+        (tname, cname, mode, nativetype, length, decimal, nullable,
+         default, defaultfunc, pos, keypos) = rs
+
+        table = tables.get (tname)
+        if table is None:
+          continue
+
+        attrs = {'id'        : "%s.%s" % (tname, cname),
+                 'name'      : cname,
+                 'nativetype': nativetype,
+                 'nullable'  : nullable == 'YES',
+                 'pos'       : pos,
+                 'keypos'    : keypos}
+
+        if nativetype in ['DATE', 'TIME', 'TIMESTAMP']:
+          attrs ['type'] = nativetype == 'TIMESTAMP' and 'date' or \
+              nativetype.lower ()
+
+        elif nativetype in ['FIXED', 'FLOAT', 'INTEGER', 'SMALLINT']:
+          attrs ['type']   = 'number'
+          attrs ['length'] = length
+
+          if nativetype == 'FIXED':
+            attrs ['precision'] = decimal
+
+        elif nativetype in ['BOOLEAN']:
+          attrs ['type'] = 'boolean'
+
+        else:
+          attrs ['type']   = 'string'
+          attrs ['length'] = length
+
+        if default is not None:
+          attrs ['defaultwith'] = 'constant'
+          attrs ['default']     = default
+
+        elif defaultfunc is not None:
+          if defaultfunc in ['DATE', 'TIME', 'TIMESTAMP']:
+            dkind = 'timestamp'
+          else:
+            dkind = 'constant'
+          attrs ['defaultwith'] = dkind
+          attrs ['defaultval']  = defaultfunc
+
+        parent = table.findChildOfType ('GSFields') or GSchema.GSFields (table)
+        result [attrs ['id']] = GSchema.GSField (parent, **attrs)
+
+    finally:
+      cursor.close ()
+
+    # Finally iterate over all tables added and build up their primary keys
+    for table in tables.values ():
+      fields = [(f.keypos, f.name) for f in \
+                  table.findChildrenOfType ('GSField', False, True) \
+                  if f.keypos is not None]
+      if fields:
+        fields.sort ()
+        pk = GSchema.GSPrimaryKey (table, name = "pk_%s" % table.name)
+        for (p, name) in fields:
+          GSchema.GSPKField (pk, name = name)
+
+    return result
+
+
+  # ---------------------------------------------------------------------------
+  # Read all indices defined for the given tables
+  # ---------------------------------------------------------------------------
+
+  def __readIndices (self, tables):
+
+    cmd = u"SELECT tablename, indexname, columnname, type " \
+           "FROM DOMAIN.INDEXCOLUMNS " \
+           "WHERE disabled = 'NO' " \
+           "ORDER BY tablename, indexname, columnno"
+
+    cursor = self.__connection.makecursor (cmd)
+    try:
+      for (tname, iname, cname, itype) in cursor.fetchall ():
+        #print "TN:", tname, "IN:", iname, "CN:", cname, "IT:", itype
+        table = tables.get (tname)
+        if table is None:
+          continue
+
+        indices = table.findChildOfType ('GSIndexes') or \
+            GSchema.GSIndexes (table)
+
+        index = None
+        for ix in indices.findChildrenOfType ('GSIndex', False, True):
+          if ix.name == iname:
+            index = ix
+            break
+
+        if index is None:
+          index = GSchema.GSIndex (indices, name = iname,
+                                            unique = itype == 'UNIQUE')
+        GSchema.GSIndexField (index, name = cname)
+
+    finally:
+      cursor.close ()
+
+
+  # ---------------------------------------------------------------------------
+  # Read all referential constraints for the tables given
+  # ---------------------------------------------------------------------------
+
+  def __readConstraints (self, tables):
+
+    cmd = u"SELECT tablename, columnname, fkeyname, reftablename, " \
+           "  refcolumnname " \
+           "FROM DOMAIN.FOREIGNKEYCOLUMNS " \
+           "ORDER BY tablename, fkeyname"
+
+    cursor = self.__connection.makecursor (cmd)
+
+    try:
+      for (tname, cname, fkname, reftable, refcol) in cursor.fetchall ():
+        table = tables.get (tname)
+        if table is None:
+          continue
+
+        master = table.findChildOfType ('GSConstraints') or \
+                                                  GSchema.GSConstraints (table)
+        cs = None
+        for item in master.findChildrenOfType ('GSConstraint', False, True):
+          if item.name == fkname:
+            cs = item
+            break
+
+        if cs is None:
+          cs = GSchema.GSConstraint (master, name = fkname, type = 
'foreignkey')
+
+        GSchema.GSConstraintField (cs, name = cname)
+        GSchema.GSConstraintRef (cs, name = refcol, table = reftable)
+
+    finally:
+      cursor.close ()


Property changes on: trunk/gnue-common/src/datasources/drivers/sapdb/Behavior.py
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: 
trunk/gnue-common/src/datasources/drivers/sapdb/Schema/Creation/Creation.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sapdb/Schema/Creation/Creation.py 
2005-06-02 07:11:47 UTC (rev 7571)
+++ trunk/gnue-common/src/datasources/drivers/sapdb/Schema/Creation/Creation.py 
2005-06-02 08:33:38 UTC (rev 7572)
@@ -52,7 +52,7 @@
 
     # Import here so epydoc can import this module even if sapdb is not
     # installed
-    import sapdb.dbm
+    import sdb.dbm
 
     host     = self.connection.parameters.get ('host', 'localhost')
     dbname   = self.connection.parameters.get ('dbname', None)
@@ -65,10 +65,10 @@
     res    = BasicLoginHandler ().askLogin (title, fields, {})
 
     try:
-      session = sapdb.dbm.DBM (host, '', '',
+      session = sdb.dbm.DBM (host, '', '',
                               "%s,%s" % (res ['_username'], res ['_password']))
 
-    except sapdb.dbm.CommunicationError, err:
+    except sdb.dbm.CommunicationError, err:
       raise errors.AdminError, \
           u_("Unable to establish session: %s") % errors.getException () [2]
 

Modified: trunk/gnue-common/src/datasources/drivers/sapdb/sapdb/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sapdb/sapdb/Connection.py 
2005-06-02 07:11:47 UTC (rev 7571)
+++ trunk/gnue-common/src/datasources/drivers/sapdb/sapdb/Connection.py 
2005-06-02 08:33:38 UTC (rev 7572)
@@ -25,7 +25,7 @@
 
 from gnue.common.datasources.drivers import DBSIG2
 from gnue.common.datasources.drivers.sapdb.Schema.Creation.Creation import 
Creation
-from gnue.common.datasources.drivers.sapdb.Schema.Discovery.Introspection 
import Introspection
+from gnue.common.datasources.drivers.sapdb import Behavior
 
 
 # =============================================================================
@@ -37,12 +37,12 @@
   Connection class for MaxDB and SAP-DB databases.
   """
 
-  _drivername = 'sapdb.dbapi'
+  _drivername = 'sdb.dbapi'
 
   _named_as_sequence = True
 
-  defaultBehavior = Introspection
-  defaultCreator  = Creation
+  _behavior      = Behavior.Behavior
+  defaultCreator = Creation
 
 
   # ---------------------------------------------------------------------------
@@ -59,7 +59,7 @@
               connectData.get ('host', '')]
 
     # keyword arguments
-    kwargs = {'autocommit': 'off'}
+    kwargs = {}
       
     for gnueName, sapdbName in [('sqlmode'   , 'sqlmode'),
                                 ('timeout'   , 'timeout'),

Modified: trunk/gnue-common/src/datasources/drivers/sapdb/sapdb/__init__.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sapdb/sapdb/__init__.py   
2005-06-02 07:11:47 UTC (rev 7571)
+++ trunk/gnue-common/src/datasources/drivers/sapdb/sapdb/__init__.py   
2005-06-02 08:33:38 UTC (rev 7572)
@@ -39,6 +39,6 @@
 def __initplugin__ ():
   from gnue.common.datasources import GConnections
   try:
-    import sapdb.dbapi
+    import sdb.dbapi
   except ImportError:
     raise GConnections.DependencyError, ('sapdb', None)





reply via email to

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