changeset 377:2bbf953ef231

basic ls works under postgres
author catherine@cordelia
date Wed, 12 Aug 2009 18:31:27 -0400
parents c0df8426dcbd
children 05d2de3e7ea8
files sqlpython/sqlpyPlus.py sqlpython/sqlpython.py
diffstat 2 files changed, 20 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpython/sqlpyPlus.py	Wed Aug 12 17:03:24 2009 -0400
+++ b/sqlpython/sqlpyPlus.py	Wed Aug 12 18:31:27 2009 -0400
@@ -1429,7 +1429,7 @@
         'WINDOW',               
         'WINDOW GROUP',         
         'XML SCHEMA')
-    
+        
     @options([make_option('-l', '--long', action='store_true', help='long descriptions'),
               make_option('-a', '--all', action='store_true', help="all schemas' objects"),
               make_option('-t', '--timesort', action='store_true', help="Sort by last_ddl_time"),              
@@ -1439,52 +1439,25 @@
         Lists objects as through they were in an {object_type}/{object_name} UNIX
         directory structure.  `*` and `%` may be used as wildcards.
         '''
-        clauses = {'owner': '', 'moreColumns': '',
-                   'source': metaqueries['ls'][self.rdbms],
-                   'where': self.ls_where_clause(arg, opts)}
-        if opts.long:
-            clauses['moreColumns'] = ', status, last_ddl_time'
-        if opts.all:
-            clauses['owner'] = "owner || '.' ||"
-
-        # 'Normal' sort order is DATE DESC (maybe), object type ASC, object name ASC
-        sortdirection = (hasattr(opts, 'reverse') and opts.reverse and 'DESC') or 'ASC'
-        orderby = 'object_type %s, object_name %s' % (sortdirection, sortdirection)
-        if hasattr(opts, 'timesort') and opts.timesort:
-            if hasattr(opts, 'reverse') and opts.reverse:
-                direction = 'DESC'
-            else:
-                direction = 'ASC'
-            orderby = 'last_ddl_time %s, %s' % (direction, orderby)
-        clauses['orderby'] = orderby    
-        statement = '''
-            SELECT object_type || '/' || %(owner)s object_name AS name %(moreColumns)s 
-            FROM   (%(source)s) source
-            %(where)s
-            ORDER BY %(orderby)s;''' % clauses
-        self.do_select(self.parsed(statement, 
-                                   terminator=arg.parsed.terminator or ';', 
-                                   suffix=arg.parsed.suffix))
-
-        
-    @options([make_option('-l', '--long', action='store_true', help='long descriptions'),
-              make_option('-a', '--all', action='store_true', help="all schemas' objects"),
-              make_option('-t', '--timesort', action='store_true', help="Sort by last_ddl_time"),              
-              make_option('-r', '--reverse', action='store_true', help="Reverse order while sorting")])            
-    def do_ls(self, arg, opts):
         seek = '^%s$' % (arg.replace('*', '.*').replace('?','.'). \
                          replace('%', '.*'))
         gerald = self.connections[self.connection_number]['gerald']
+        result = []
         for (name, obj) in gerald.schema.items():
-            dbtype = str(type(obj)).rstrip("'>").split('.')[-1]
-            if dbtype == 'CodeObject':
+            if hasattr(obj, 'type'):
                 dbtype = obj.type
+            else:
+                dbtype = str(type(obj)).rstrip("'>").split('.')[-1]
             descriptor = '%s/%s' % (dbtype, name)
+            descriptor = descriptor.upper()
             if (not arg) or \
                re.search(seek, descriptor, re.IGNORECASE) or \
                re.search(seek, name, re.IGNORECASE) or \
                re.search(seek, dbtype, re.IGNORECASE):
-                self.poutput(descriptor)
+                result.append(descriptor)
+                # if opts.long: status, last_ddl_time
+        result.sort(reverse=bool(opts.reverse))
+        self.poutput('\n'.join(result))
         
     @options([make_option('-i', '--ignore-case', dest='ignorecase', action='store_true', help='Case-insensitive search')])        
     def do_grep(self, arg, opts):
--- a/sqlpython/sqlpython.py	Wed Aug 12 17:03:24 2009 -0400
+++ b/sqlpython/sqlpython.py	Wed Aug 12 18:31:27 2009 -0400
@@ -101,6 +101,16 @@
         conn  = {'conn': self.conn, 'prompt': self.prompt, 'dbname': eng.url.database,
                  'rdbms': eng.url.drivername, 'user': eng.url.username or '', 
                  'eng': eng}
+        if eng.url.drivername == 'oracle':
+            conn['gerald'] = gerald.OracleSchema(eng.url.username, 
+                                    arg.split('/?mode=')[0].replace('//','/'))
+        elif eng.url.drivername == 'mysql':
+            conn['gerald'] = gerald.MySQLSchema(eng.url.username, 
+                                                arg.replace('//','/'))
+        elif eng.url.drivername == 'postgres':
+            conn['gerald'] = gerald.PostgresSchema('public', 
+                                                   arg.replace('//','/'))
+            
         return conn
     def ora_connect(self, arg):
         modeval = 0
@@ -138,10 +148,6 @@
             modeval = cx_Oracle.SYSOPER
         result = self.url_connect('oracle://%s:%s@%s/?mode=%d' % (orauser, orapass, oraserv, modeval))
         result['dbname'] = oraserv
-        result['gerald'] = gerald.OracleSchema('schema', 
-                                               'oracle:/%s:%s@%s' % (orauser,
-                                                                     orapass,
-                                                                     oraserv))
         return result
     
     connection_modes = {re.compile(' AS SYSDBA', re.IGNORECASE): cx_Oracle.SYSDBA,