Mercurial > sqlpython
changeset 383:18330ebc1cc4
merged version change to 1.6.8
author | catherine@cordelia |
---|---|
date | Wed, 02 Sep 2009 10:12:17 -0400 |
parents | fe0051d7f934 (diff) 71d510285e80 (current diff) |
children | b9fedbfbec79 |
files | sqlpython/sqlpython.py |
diffstat | 3 files changed, 77 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sqlpython/schemagroup.py Wed Sep 02 10:12:17 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/sqlpyPlus.py Wed Sep 02 09:44:21 2009 -0400 +++ b/sqlpython/sqlpyPlus.py Wed Sep 02 10:12:17 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,32 +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' + seek = '^%s$' % (arg.replace('*', '.*').replace('?','.'). \ + replace('%', '.*')) + gerald = self.connections[self.connection_number]['gerald'] + result = [] + for (name, obj) in gerald.schema.items(): + if hasattr(obj, 'type'): + dbtype = obj.type 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)) + 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): + 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 Sep 02 09:44:21 2009 -0400 +++ b/sqlpython/sqlpython.py Wed Sep 02 10:12:17 2009 -0400 @@ -1,5 +1,5 @@ # -# SqlPython V1.6.8 +# SqlPython V1.6.7 # Author: Luca.Canali@cern.ch, Apr 2006 # Rev 2-Sep-09 @@ -98,9 +98,14 @@ def url_connect(self, arg): eng = sqlalchemy.create_engine(arg) self.conn = eng.connect().connection + user = eng.url.username or '' + rdbms = eng.url.drivername conn = {'conn': self.conn, 'prompt': self.prompt, 'dbname': eng.url.database, - 'rdbms': eng.url.drivername, 'user': eng.url.username or '', - 'eng': eng} + 'rdbms': rdbms, 'user': user, 'eng': eng, + 'schemas': schemagroup.schemagroup(rdbms, arg, + self.conn, user)} + s = conn['schemas'] + s.refresh() return conn def ora_connect(self, arg): modeval = 0