commit-gnue
[Top][All Lists]
Advanced

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

[gnue] r7562 - in trunk/gnue-common/src/datasources/drivers/oracle: . Ba


From: johannes
Subject: [gnue] r7562 - in trunk/gnue-common/src/datasources/drivers/oracle: . Base
Date: Wed, 1 Jun 2005 08:05:40 -0500 (CDT)

Author: johannes
Date: 2005-06-01 08:05:39 -0500 (Wed, 01 Jun 2005)
New Revision: 7562

Added:
   trunk/gnue-common/src/datasources/drivers/oracle/Behavior.py
Modified:
   trunk/gnue-common/src/datasources/drivers/oracle/Base/Connection.py
Log:
Transformed Schema.Discovery.Introspection into Behavior


Modified: trunk/gnue-common/src/datasources/drivers/oracle/Base/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/oracle/Base/Connection.py 
2005-06-01 12:28:06 UTC (rev 7561)
+++ trunk/gnue-common/src/datasources/drivers/oracle/Base/Connection.py 
2005-06-01 13:05:39 UTC (rev 7562)
@@ -25,7 +25,7 @@
 
 import os
 from gnue.common.datasources.drivers import DBSIG2
-from gnue.common.datasources.drivers.oracle.Schema.Discovery.Introspection 
import Introspection
+from gnue.common.datasources.drivers.oracle import Behavior
 
 
 # =============================================================================
@@ -41,7 +41,7 @@
   _boolean_false = 0
   _boolean_true  = 1
 
-  defaultBehavior = Introspection
+  _behavior      = Behavior.Behavior
 
 
   # ---------------------------------------------------------------------------

Added: trunk/gnue-common/src/datasources/drivers/oracle/Behavior.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/oracle/Behavior.py        
2005-06-01 12:28:06 UTC (rev 7561)
+++ trunk/gnue-common/src/datasources/drivers/oracle/Behavior.py        
2005-06-01 13:05:39 UTC (rev 7562)
@@ -0,0 +1,156 @@
+# GNU Enterprise Common Library - Oracle 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
+
+
+# =============================================================================
+# This class implements schema support for oracle drivers
+# =============================================================================
+
+class Behavior (DBSIG2.Behavior):
+  """
+  Limitations:
+
+  * does not detect primary keys, indices and constraints
+  """
+
+  _RELKIND = {
+     'user_table'  : {'type': 'usertable',   'name': _("User Tables")},
+     'user_view'   : {'type': 'userview',    'name': _("User Views")},
+     'user_synonym': {'type': 'usersynonym', 'name': _("User Synonyms")},
+     'all_table'   : {'type': 'alltable',    'name': _('System Tables')},
+     'all_view'    : {'type': 'allview',     'name': _('System Views')},
+     'all_synonym' : {'type': 'allsynonym',  'name': _('System Synonyms')}}
+
+
+  # ---------------------------------------------------------------------------
+  # 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)
+
+
+  # ---------------------------------------------------------------------------
+  # Read all relations available
+  # ---------------------------------------------------------------------------
+
+  def __readTables (self, parent):
+    
+    masters = {}
+    result  = {}
+
+    cmd = \
+      "select owner||'.'||table_name||'.'||table_type full_name, \n" + \
+      "  decode(owner,user,null,owner||'.')||table_name table_name, \n" + \
+      "  decode(owner,user,'user_','all_')||lower(table_type) table_type \n" + 
\
+      "  from all_catalog where table_type in ('%s') %s \n" \
+              % (string.join(where_type,"','"), where_user) + \
+      "  order by table_name "
+
+    cursor = self.__connection.makecursor ()
+    try:
+      for (fullname, name, kind) in cursor.fetchall ():
+        if not kind in masters:
+          masters [kind] = GSchema.GSTables (self._RELKIND [kind])
+
+        properties = {'id': fullname, 'name': name.lower (), 'kind': kind}
+        result [fullname] = GSchema.GSTable (masters [kind], **properties)
+
+    finally:
+      cursor.close ()
+
+    return result
+
+
+  # ---------------------------------------------------------------------------
+  # Read fields
+  # ---------------------------------------------------------------------------
+
+  def __readFields (self, tables):
+    
+    for (key, table) in tables.items ():
+
+      (owner, name, type) = key.split ('.')
+
+      if type == 'SYNONYM':
+        cmd = "select table_owner, table_name, " + \
+              "decode(db_link,null,'','@'||db_link) name " + \
+              "from all_synonyms " + \
+              "where owner = '%s' and synonym_name='%s'" % (owner, name)
+
+        cursor = self.__connection.makecursor (cmd)
+        try:
+          (owner, name, link) = cursor.fetchone ()
+          if link is None:
+            link = ""
+
+        finally:
+          cursor.close()
+      else:
+        link = ""
+
+      cmd = "SELECT owner||'.'||table_name||'.'||column_name||'.%s', " \
+            "  column_name, data_type, nullable, data_length, data_scale, " \
+            "  data_precision "  \
+            "FROM all_tab_columns%s " \
+            "WHERE owner = '%s' AND table_name = '%s' " \
+            "ORDER BY column_id" % (link, link, owner, name)
+
+      cursor = self.__connection.makecursor (cmd)
+
+      try:
+        for rs in cursor.fetchall ():
+          (fullname, name, nativetype, nullable, length, scale, prec) = rs
+
+          attrs = {'id'        : fullname,
+                   'name'      : name.lower (),
+                   'nativetype': nativetype,
+                   'nullable'  : nullable != 'N'}
+
+          if nativetype == 'NUMBER':
+            attrs ['precision'] = int (scale)
+            attrs ['type']      = 'number'
+            attrs ['length']    = int (prec)
+
+          elif nativetype == 'DATE':
+            attrs ['type'] = 'date'
+
+          else:
+            attrs ['type'] = 'string'
+            if int (length):
+              attrs ['length'] = int (length)
+
+        parent = table.findChildOfType ('GSFields') or GSchema.GSFields (table)
+        GSchema.GSField (parent, **attrs)
+
+      finally:
+        cursor.close ()


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





reply via email to

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