changeset 502:2b3974b0a584

switch instance with bare connection number prefix
author catherine.devlin@gmail.com
date Tue, 21 Sep 2010 15:26:24 -0400
parents af846fd968cd
children 0b584c8238b4
files sqlpython/connections.py sqlpython/sqlpyPlus.py
diffstat 2 files changed, 46 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpython/connections.py	Wed Sep 15 17:28:25 2010 -0400
+++ b/sqlpython/connections.py	Tue Sep 21 15:26:24 2010 -0400
@@ -202,7 +202,20 @@
         identifier = self.sql_format_wildcards(identifier)
         result = {'owner': '%', 'type': '%', 'name': '%'}
         result.update(dict(self.ls_parser.parseString(identifier)))
-        return result 
+        return result
+    dotsplitter = re.compile(r'([a-zA-Z$_#]+)([./]([a-zA-Z$_#]+))?')
+    def parse_identifier(self, identifier):
+        object_type = None
+        for otype in self.gerald_types:
+            stripped = re.search('$' + otype + '\b(.*)', identifier, re.IGNORECASE)
+            if stripped:
+                identifier = stripped.groups(0)
+                object_type = otype
+                break
+        match = self.dotsplitter.search(identifier)
+        return {'object_type': object_type, 'name1': match.group(1), 'name2': match.group(3)}
+            
+                
     def comparison_operator(self, target):
         if ('%' in target) or ('_' in target):
             operator = 'LIKE'
@@ -213,19 +226,15 @@
         return target.replace('*', '%').replace('?', '_')
     def objects(self, target, opts):
         identifier = self.parse_identifier(target)
-        clauses = []
+        clauses = {'sort': 'ASC'}
         if (identifier['owner'] == '%') and (not opts.all):
             identifier['owner'] = self.username
         for col in ('owner', 'type', 'name'):
             operator = self.comparison_operator(identifier[col])
-            clause = '%s %s' % (operator, self.bindSyntax(col))
-            clauses.append(clause)
+            clauses[col] = '%s %s' % (operator, self.bindSyntax(col))
         if hasattr(opts, 'reverse') and opts.reverse:
-            sort_direction = 'DESC'
-        else:
-            sort_direction = 'ASC'
-        clauses.append(sort_direction)
-        qry = self.all_object_qry % tuple(clauses)
+            clauses['sort'] = 'DESC'
+        qry = self.all_object_qry % clauses
         binds = (('owner', identifier['owner']), ('type', identifier['type']), ('name', identifier['name']))
         curs = self.connection.cursor()
         curs.execute(qry, self.bindVariables(binds)) 
@@ -311,6 +320,7 @@
     rdbms = 'postgres'
     default_port = 5432
     case = str.lower
+    object_type_case = str.upper 
     def set_defaults(self):
         self.port = os.getenv('PGPORT') or self.default_port
         self.database = os.getenv('ORACLE_SID')
@@ -327,10 +337,10 @@
         return dict((b[0], b[1].lower()) for b in binds)
     all_object_qry = """SELECT table_schema, table_type, table_name
                         FROM   information_schema.tables
-                        WHERE  ( (table_schema %s) OR (table_schema = 'public') )
-                        AND    table_type %s
-                        AND    table_name %s
-                        ORDER BY table_schema, table_type, table_name %s"""
+                        WHERE  ( (table_schema %(owner)s) OR (table_schema = 'public') )
+                        AND    table_type %(type)s
+                        AND    table_name %(name)s
+                        ORDER BY table_schema, table_type, table_name %(sort)s"""
     column_qry = """SELECT c.table_schema, t.table_type, c.table_name, c.column_name      
                     FROM   information_schema.columns c
                     JOIN   information_schema.tables t ON (c.table_schema = t.table_schema
@@ -350,6 +360,9 @@
     connection_parser = re.compile('(?P<username>[^/\s@]*)(/(?P<password>[^/\s@]*))?(@((?P<hostname>[^/\s:]*)(:(?P<port>\d{1,4}))?/)?(?P<database>[^/\s:]*))?(\s+as\s+(?P<mode>sys(dba|oper)))?',
                                      re.IGNORECASE)
     case = str.upper
+    database_types = ['DATABASE LINK', 'DIRECTORY', 'FUNCTION', 'INDEX', 'JOB', 
+                      'MATERIALIZED VIEW', 'PACKAGE', 'PROCEDURE', 'SEQUENCE',
+                      'SYNONYM', 'TABLE', 'TRIGGER', 'TYPE', 'VIEW']
     def uri(self):
         if self.hostname:
             uri = '%s://%s:%s@%s:%s/%s' % (self.rdbms, self.username, self.password,
@@ -382,10 +395,17 @@
                                   dsn = self.dsn, mode = self.mode)    
     all_object_qry = """SELECT owner, object_type, object_name 
                         FROM   all_objects 
-                        WHERE  owner %s
-                        AND    object_type %s
-                        AND    object_name %s
-                        ORDER BY owner, object_type, object_name %s"""
+                        WHERE  ( owner %(owner)s
+                        AND      object_type %(type)s
+                        AND      object_name %(name)s )
+                        OR
+                               ( (owner, object_name)
+                                 IN 
+                                    ( SELECT table_owner, table_name
+                                      FROM   all_synonyms
+                                      WHERE  synonym_name %(name)s )
+                               )
+                        ORDER BY owner, object_type, object_name %(sort)s"""
     column_qry = """SELECT atc.owner, ao.object_type, atc.table_name, atc.column_name      
                     FROM   all_tab_columns atc
                     JOIN   all_objects ao ON (atc.table_name = ao.object_name AND atc.owner = ao.owner)
@@ -401,7 +421,7 @@
         'Puts a tuple of (name, value) pairs into the bind format desired by cx_Oracle'
         return dict((b[0], b[1].upper()) for b in binds)
     gerald_types = {'TABLE': gerald.oracle_schema.Table,
-                    'VIEW': gerald.postgres_schema.View}
+                    'VIEW': gerald.oracle_schema.View}
 
                 
 if __name__ == '__main__':
--- a/sqlpython/sqlpyPlus.py	Wed Sep 15 17:28:25 2010 -0400
+++ b/sqlpython/sqlpyPlus.py	Tue Sep 21 15:26:24 2010 -0400
@@ -731,6 +731,14 @@
         if self.scan:
             raw = self.ampersand_substitution(raw, regexpr=self.singleampre, isglobal=False)
         return raw
+    def postparse(self, parseResult):
+        if (not parseResult.command):
+            try:
+                connection_number = int(parseResult.instance_number)
+                parseResult = self.parser.parseString('connect %d' % connection_number)
+            except (TypeError, ValueError):
+                return parseResult
+        return parseResult
     
     rowlimitPattern = pyparsing.Word(pyparsing.nums)('rowlimit')
     terminators = '; \\C \\t \\i \\p \\l \\L \\b \\r'.split() + output_templates.keys()