changeset 433:439621c917c4

changes to match refactored cmd2
author catherine@dellzilla
date Tue, 26 Jan 2010 19:10:52 -0500
parents 26b09e1481e7
children 8e822859e3a4
files sqlpython/connections.py sqlpython/sqlpyPlus.py
diffstat 2 files changed, 29 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpython/connections.py	Tue Jan 26 12:43:13 2010 -0500
+++ b/sqlpython/connections.py	Tue Jan 26 19:10:52 2010 -0500
@@ -10,16 +10,16 @@
     def __init__(self, name, dbobj):
         self.fullname = name
         self.dbobj = dbobj
-        self.type = str(type(self.dbobj)).split()[-1].lower()
+        self.type = str(type(self.dbobj)).split('.')[-1].lower().strip("'>")
         self.path = '%s/%s' % (self.type, self.fullname)
         #self.type_path = '%s/' % self.dbobj.type
         (self.owner, self.unqualified_name) = self.fullname.split('.')
         self.owner = self.owner.lower()
     def match_pattern(self, pattern, specific_owner=None):
-        return ( pattern.match(fullname) or 
+        return ( pattern.match(self.fullname) or 
                   pattern.match(self.type) or 
                   ((not specific_owner) and pattern.match(self.unqualified_name)) or
-                  ((self.owner == specific_owner.lower()) and pattern.match(self.unqualified_name)) )
+                  (specific_owner and (self.owner == specific_owner.lower()) and pattern.match(self.unqualified_name)) )
         
 class GeraldPlaceholder(object):
     current = False
@@ -36,6 +36,9 @@
             self.parse_connect_arg(arg, opts)
         self.connection = self.new_connection()
         self.gerald = GeraldPlaceholder()
+        self.discover_metadata()
+        
+    def discover_metadata(self):
         self.metadata_discovery_thread = MetadataDiscoveryThread(self)
         self.metadata_discovery_thread.start()
     
@@ -71,15 +74,7 @@
     
     def gerald_uri(self):
         return self.uri.split('?mode=')[0]
-        
-    def discover_schemas(self):
-        self.gerald = self.gerald_class(self.username, self.gerald_uri())
-        self.gerald.descriptions = {}
-        for (name, obj) in self.gerald.schema.items():
-            self.gerald.descriptions[name] = ObjectDescriptor(name, obj)            
-        self.gerald.current = True
-        self.gerald.complete = True
-    
+           
     def set_instance_number(self, instance_number):
         self.instance_number = instance_number
         self.prompt = "%d:%s@%s> " % (self.instance_number, self.username, self.db_name)        
@@ -170,12 +165,14 @@
         threading.Thread.__init__(self)
         self.db_instance = db_instance
     def run(self):
-        self.db_instance.gerald = self.db_instance.gerald_class(self.db_instance.username, self.db_instance.gerald_uri())
-        self.db_instance.gerald.descriptions = {}
-        for (name, obj) in self.db_instance.gerald.schema.items():
-            self.db_instance.gerald.descriptions[name] = ObjectDescriptor(name, obj)            
-        self.db_instance.gerald.current = True
-        self.db_instance.gerald.complete = True
+        self.db_instance.gerald.current = False
+        newgerald = self.db_instance.gerald_class(self.db_instance.username, self.db_instance.gerald_uri())
+        newgerald.descriptions = {}
+        for (name, obj) in newgerald.schema.items():
+            newgerald.descriptions[name] = ObjectDescriptor(name, obj)            
+        newgerald.current = True
+        newgerald.complete = True
+        self.db_instance.gerald = newgerald
 
 rdbms_types = {'oracle': OracleDatabaseInstance, 'mysql': MySQLDatabaseInstance, 'postgres': PostgresDatabaseInstance}
                   
--- a/sqlpython/sqlpyPlus.py	Tue Jan 26 12:43:13 2010 -0500
+++ b/sqlpython/sqlpyPlus.py	Tue Jan 26 19:10:52 2010 -0500
@@ -971,10 +971,11 @@
         Args specify which objects to store, same format as `ls`.'''
         self._vc(arg, opts, 'git')        
         
-    all_users_option = make_option('-a', action='store_const', dest="scope",
+    all_users_option = make_option('-a', '--all', action='store_const', dest="scope",
                                          default={'col':'', 'view':'user', 'schemas':'user', 'firstcol': ''}, 
                                          const={'col':', owner', 'view':'all', 'schemas':'all', 'firstcol': 'owner, '},
-                                         help='Describe all objects (not just my own)')                
+                                         )     
+    all_users_option = make_option('-a', '--all', action='store_true', help='Describe all objects (not just my own)')
     @options([all_users_option,
               make_option('-c', '--col', action='store_true', help='find column'),
              ])                    
@@ -1051,12 +1052,12 @@
                     self.rows = [(col['name'], (col['nullable'] and 'NULL') or 'NOT NULL', self._col_type_descriptor(col)) 
                                  for col in cols]
                 self.coltypes = [str] * len(self.colnames)
-                self.poutput(self.tabular_output(arg.parsed.terminator, self.tblname) + '\n\n')
+                self.poutput('%s\n\n' % self.tabular_output(arg.parsed.terminator, self.tblname))
             elif hasattr(descrip.dbobj, 'increment_by'):
                 self.colnames = 'name min_value max_value increment_by'.split()
                 self.coltypes = [str, int, int, int]
                 self.rows = [(getattr(descrip.dbobj, p) for p in self.colnames)]
-                self.poutput(self.tabular_output(arg.parsed.terminator, self.tblname) + '\n\n')
+                self.poutput('%s\n\n' % self.tabular_output(arg.parsed.terminator, self.tblname))
             elif hasattr(descrip.dbobj, 'source'):
                 end_heading = re.compile(r'\bDECLARE|BEGIN\b', re.IGNORECASE)
                 for (index, (ln, line)) in enumerate(descrip.dbobj.source):
@@ -1210,8 +1211,8 @@
         self.do_ls("%s/%s%s%s" % (type, str(arg), arg.parsed.terminator, arg.parsed.suffix), opts)
 
     standard_options = [
+              all_users_option,
               make_option('-l', '--long', action='store_true', help='long descriptions'),
-              make_option('-a', '--all', action='store_true', help="all schemas' objects"),
               make_option('-i', '--immediate', action='store_true', help="force immediate refresh of metadata"),
               #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")]
@@ -1513,33 +1514,21 @@
         return result.replace('\\*','.*').replace('\\?','.')
         
     def _regex_form_of_search_pattern(self, s, exact=False):
+        if not s:
+            return re.compile('.*')
         seekpatt = r'[/\\]?%s[/\\]?' % (
             s.replace('*', '.*').replace('?','.').replace('%', '.*'))        
         if exact:
             seekpatt = '^%s$' % seekpatt
         return re.compile(seekpatt, re.IGNORECASE)
 
-    @options([make_option('-a', '--all', action='store_true', help="all schemas' objects"),
-              make_option('-i', '--immediate', action='store_true', help="Wait until refresh is done"),
-              make_option('-c', '--check', action='store_true', help="Don't refresh, just check refresh status")])
-    def do_refresh(self, arg, opts):
+    def do_refresh(self, arg):
         '''Refreshes metadata for the specified schema; only required
-           if table structures, etc. have changed. (sqlpython will check
-           for new objects, and will not waste labor if no objects have
-           been created or modified in a schema.)'''
-        (username, schemas) = self.metadata()
-        if opts.check:
-            self.poutput(schemas.refresh_times(arg))
-            return
-        if opts.all:
-            if opts.immediate:
-                self.perror("Don't combine --all and --immediate.  It will take too long.")
-                return
-            schemas.refresh()
-        elif arg:
-            schemas.refresh_one(arg)
+           if table structures, etc. have changed.'''
+        if self.current_instance.gerald.complete and self.current_instance.gerald.current:
+            self.current_instance.discover_metadata()
         else:
-            schemas.refresh_one(username)
+            self.pfeedback('Metadata discovery is already underway.')       
         
     def _print_gerald_status_warning(self, gerald_schema):
         if not gerald_schema.complete:
@@ -1575,7 +1564,7 @@
         '''
         opts.exact = True
         (username, schemas) = self.metadata()
-        result = [descrip.fullname for descrip in self._matching_database_objects(arg, opts)]
+        result = [descrip.path for descrip in self._matching_database_objects(arg, opts)]
         if result:
             result.sort(reverse=bool(opts.reverse))
             self.poutput('\n'.join(result))