changeset 425:06713eb8954e

connect2 changes in progress
author catherine@bothari
date Mon, 25 Jan 2010 10:05:45 -0500
parents 34607fdba3eb
children 3a2199bf7c84
files sqlpython/mysqlpy.py sqlpython/sqlpyPlus.py sqlpython/sqlpython.py
diffstat 3 files changed, 93 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpython/mysqlpy.py	Fri Jan 22 16:41:20 2010 -0500
+++ b/sqlpython/mysqlpy.py	Mon Jan 25 10:05:45 2010 -0500
@@ -10,7 +10,6 @@
 
 from sqlpyPlus import *
 import sys, tempfile, optparse, unittest
-import sqlalchemy
 
 class mysqlpy(sqlpyPlus):
     '''
@@ -103,6 +102,7 @@
 
     def do_longops(self,args):
         '''Runs query_longops defined above, to display long running operations (full scans, etc)'''
+        import pdb; pdb.set_trace()
         self.onecmd(self.query_longops)
         
     def do_load(self,args):
--- a/sqlpython/sqlpyPlus.py	Fri Jan 22 16:41:20 2010 -0500
+++ b/sqlpython/sqlpyPlus.py	Mon Jan 25 10:05:45 2010 -0500
@@ -1043,7 +1043,7 @@
                     self.poutput(m.db_object.comments) 
             if hasattr(m.db_object, 'columns'):
                 cols = sorted(m.db_object.columns.values(), key=sortkey)[:rowlimit]
-                if opts.long:
+                if opts.long and hasattr(m.db_object, 'constraints'):
                     primary_key_columns = self._key_columns(m.db_object, 'Primary')
                     unique_key_columns = self._key_columns(m.db_object, 'Unique')
                     self.colnames = 'N Name Nullable Type Key Default Comments'.split()
@@ -1265,12 +1265,12 @@
         if 'condition' in cons:
             details = '(%s)' % cons['condition']
         elif 'reftable' in cons:
-            details = '(%s) in %s' % (','.join(cons['columns']), cons['reftable'])
+            details = 'columns (%s) in table "%s"' % (','.join(cons['columns']), cons['reftable'])
         elif 'columns' in cons:
             details = '(%s)' % ','.join(cons['columns'])
         else:
             details = ''
-        return '%7s %s: %s %s' % (cons['type'], cons['name'], details,
+        return '%7s key "%s": %s %s' % (cons['type'], cons['name'], details,
                                   ((not cons['enabled']) and 'DISABLED') or '')
 
 
@@ -1369,7 +1369,7 @@
         >>> s.interpret_variable_assignment(s.parsed("baz := 22"))
         (True, 'baz', 22)
         >>> s.interpret_variable_assignment(s.parsed("foo"))
-        (False, 'foo', None)    
+        (False, 'foo', None)
         '''
         arg = self.parsed(arg)
         try:
--- a/sqlpython/sqlpython.py	Fri Jan 22 16:41:20 2010 -0500
+++ b/sqlpython/sqlpython.py	Mon Jan 25 10:05:45 2010 -0500
@@ -69,7 +69,22 @@
         self.connection_number = connection_number
         self.curs = self.conn.cursor()
             
+    def successfully_connect_to_number(self, arg):
+        try:
+            connection_number = int(arg)
+        except ValueError:            
+            return False
+        try:
+            self.make_connection_current(connection_number)
+        except IndexError:
+            self.list_connections()
+            return False
+        if (self.rdbms == 'oracle') and self.serveroutput:
+            self.curs.callproc('dbms_output.enable', [])           
+        return True
+
     def successful_connection_to_number(self, arg):
+        # deprecated 
         try:
             connection_number = int(arg)
         except ValueError:            
@@ -104,7 +119,7 @@
         self.no_connection()        
             
     def url_connect(self, arg):
-        eng = sqlalchemy.create_engine(arg) 
+        eng = sqlalchemy.create_engine(arg, use_ansiquotes=True) 
         self.conn = eng.connect().connection
         user = eng.url.username or ''
         rdbms = eng.url.drivername
@@ -177,6 +192,75 @@
         if mode:
             url = '%s/?mode=%d' % mode
         return url
+
+    @cmd2.options([cmd2.make_option('-a', '--add', action='store_true', 
+                                    help='add connection (keep current connection)'),
+                   cmd2.make_option('-c', '--close', action='store_true', 
+                                    help='close connection {N} (or current)'),
+                   cmd2.make_option('-C', '--closeall', action='store_true', 
+                                    help='close all connections'),
+                   cmd2.make_option('--postgres', action='store_true', help='Connect to postgreSQL: `sqlpython --postgres [DBNAME [USERNAME]]`'),
+                   cmd2.make_option('--oracle', action='store_true', help='Connect to an Oracle database'),
+                   cmd2.make_option('--mysql', action='store_true', help='Connect to a MySQL database'),                   
+                   cmd2.make_option('-r', '--rdbms', type='string', 
+                                    help='Type of database to connect to (oracle, postgres, mysql)'),
+                   cmd2.make_option('-H', '--host', type='string', 
+                                    help='Host to connect to (postgresql only)'),                                  
+                   cmd2.make_option('-p', '--port', type='int', 
+                                    help='Port to connect to (postgresql only)'),                                  
+                   cmd2.make_option('-d', '--database', type='string', 
+                                    help='Database name to connect to'),
+                   cmd2.make_option('-U', '--username', type='string', 
+                                    help='Database user name to connect as')
+                   ])
+    def do_connect2(self, arg, opts):
+ 
+        '''Opens the DB connection'''
+        if opts.closeall:
+            self.closeall()
+            return 
+        if opts.close:
+            if not arg:
+                arg = self.connection_number
+            self.disconnect(arg)
+            return 
+        if (not arg) and (not opts.postgres):
+            self.list_connections()
+            return 
+        if self.successfully_connect_to_number(arg):
+            return
+        match = re.search("(postgres|oracle|mysql|sqlite|mssql):/(.*$)")
+        if match:
+            (username, password, host, port, dbName
+             ) = gerald.utilities.dburi.Connection().parse_uri(match.group(2))
+            rdbms = match.group(1)
+            
+        
+        try:
+            connect_info = self.url_connect(arg)
+        except sqlalchemy.exc.ArgumentError, e:
+            url = self.connect_url(arg, opts)
+            connect_info = self.url_connect(url)
+        except Exception, e:
+            self.perror(str(e))
+            self.perror(r'URL connection format: rdbms://username:password@host/database')
+            return
+        if opts.add or (self.connection_number is None):
+            try:
+                self.connection_number = max(self.connections.keys()) + 1
+            except ValueError:
+                self.connection_number = 0
+        connect_info['prompt'] = '%d:%s@%s> ' % (self.connection_number, connect_info['user'], connect_info['dbname'])
+        self.connections[self.connection_number] = connect_info
+        self.make_connection_current(self.connection_number)
+        self.curs = self.conn.cursor()
+        if (self.rdbms == 'oracle') and self.serveroutput:
+            self.curs.callproc('dbms_output.enable', [])
+        #if (self.rdbms == 'mysql'):
+        #    self.curs.execute('SET SQL_MODE=ANSI')
+        #    # this dies... if only we could set sql_mode when making the connection
+        return 
+    
     
     @cmd2.options([cmd2.make_option('-a', '--add', action='store_true', 
                                     help='add connection (keep current connection)'),
@@ -238,8 +322,9 @@
         self.curs = self.conn.cursor()
         if (self.rdbms == 'oracle') and self.serveroutput:
             self.curs.callproc('dbms_output.enable', [])
-        if (self.rdbms == 'mysql'):
-            self.curs.execute('SET SQL_MODE=ANSI')
+        #if (self.rdbms == 'mysql'):
+        #    self.curs.execute('SET SQL_MODE=ANSI')
+        #    # this dies... if only we could set sql_mode when making the connection
         return 
     
     def postparsing_precmd(self, statement):