changeset 382:fe0051d7f934

further gerald experiments
author catherine@cordelia
date Wed, 02 Sep 2009 10:11:26 -0400
parents a70adadca4d8
children 18330ebc1cc4
files sqlpython/schemagroup.py sqlpython/sqlpython.py
diffstat 2 files changed, 51 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sqlpython/schemagroup.py	Wed Sep 02 10:11:26 2009 -0400
@@ -0,0 +1,50 @@
+import gerald, re
+
+def schemagroup(rdbms, connstr, connection, user):
+    gerald_connstring = connstr.split('/?mode=')[0].replace('//','/')
+    if rdbms == 'oracle':
+        childtype = OracleSchemaGroup            
+    grp = childtype(gerald_connstring, connection, user)
+    return grp
+    
+class SchemaGroup(object):
+    def __init__(self, gerald_connstring, connection, user):
+        self.connstring = gerald_connstring
+        self.connection = connection
+        self.cursor = connection.cursor()
+        self.user = user    
+    def refresh(self):
+        now = self.schemas[self.user].time()
+        for schemaname in self.all_schemanames():
+            if ((schemaname not in self.schemas) or 
+                (self.schemas[schemaname].age() < now) 
+                ):
+                self.schemas[schemaname] = self.childtype(schemaname,
+                                                          self)
+
+
+class OracleSchema(gerald.OracleSchema):
+    def __init__(self, schemaname, parent_group):
+        self.parent_group = parent_group
+        gerald.OracleSchema.__init__(self, schemaname, 
+                                     self.parent_group.connstring)
+        self.refreshed = self.time()
+    def time(self):
+        self.parent_group.cursor.execute('SELECT sysdate FROM dual')
+        return self.parent_group.cursor.fetchone()[0]
+    def age(self):
+        self.parent_group.cursor.execute('''SELECT max(last_ddl_time) 
+                                            FROM   all_objects
+                                            WHERE  owner = :owner''',
+                                            {'owner': self.name})
+        return self.parent_group.cursor.fetchone()[0]            
+                
+class OracleSchemaGroup(SchemaGroup):         
+    childtype = OracleSchema
+    def __init__(self, connection_string, connection, user):
+        SchemaGroup.__init__(self, connection_string, connection, user)
+        self.schemas = {user: OracleSchema(user, self)}
+    def all_schemanames(self):
+        self.cursor.execute('SELECT DISTINCT owner FROM all_objects')
+        return [r[0] for r in self.cursor.fetchall()]        
+        
\ No newline at end of file
--- a/sqlpython/sqlpython.py	Fri Aug 28 10:47:59 2009 -0400
+++ b/sqlpython/sqlpython.py	Wed Sep 02 10:11:26 2009 -0400
@@ -8,7 +8,7 @@
 # Best used with the companion modules sqlpyPlus and mysqlpy 
 # See also http://twiki.cern.ch/twiki/bin/view/PSSGroup/SqlPython
 
-import cmd2,getpass,binascii,cx_Oracle,re,os
+import cmd2, getpass, binascii, cx_Oracle, re, os, platform
 import sqlalchemy, pyparsing, schemagroup
 __version__ = '1.7.0'