Changeset 161:25c77b296b92 in livinglogic.python.orasql

Show
Ignore:
Timestamp:
08/11/06 14:29:57 (13 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Tags:
rel-1-10
Message:

Rename Definition to Object and drop the "Definition" part of the name
from all subclasses. Rename methods accordingly.

Files:
7 modified

Legend:

Unmodified
Added
Removed
  • NEWS.xml

    r160 r161  
    88 
    99<item>All other subclasses of <class>Definition</class> have been renamed 
    10 to remove the <z>Definition</z> for the name to reduce typing.</item> 
     10to remove the <z>Definition</z> for the name to reduce typing. (Methods 
     11have been renamed accordingly too.</item> 
     12 
     13<item><function>oramerge.main</function> and <function>oradiff.main</function> 
     14now accept option arrays as arguments.</item> 
    1115 
    1216<item><filename>oradiff.py</filename> has finally been fixed.</item> 
  • src/ll/orasql/__init__.py

    r160 r161  
    2727<ulist> 
    2828<item>It allows calling procedures with keyword arguments (via the 
    29 <pyref class="ProcedureDefinition"><class>ProcedureDefinition</class></pyref> class).</item> 
     29<pyref class="Procedure"><class>Procedure</class></pyref> class).</item> 
    3030<item>Query results will be put into <pyref class="Record"><class>Record</class></pyref> 
    3131objects, where database fields are accessible as object attributes.</item> 
     
    8383 
    8484class ConflictError(ValueError): 
    85     def __init__(self, definition, message): 
    86         self.definition = definition 
     85    def __init__(self, object, message): 
     86        self.object = object 
    8787        self.message = message 
    8888 
    8989    def __str__(self): 
    90         return "conflict in %r: %s" % self.definition 
     90        return "conflict in %r: %s" % (self.object, self.message) 
    9191 
    9292 
     
    168168 
    169169    def __iter__(self): 
    170         return self.class_.iterdefinitions(self.connection, self.schema) 
     170        return self.class_.iterobjects(self.connection, self.schema) 
    171171 
    172172 
     
    219219            yield ipipe.XMode(self, "objectsflatall", "all objects (unordered)", "all objects in all schemas (unordered)") 
    220220        elif mode == "objectscreateuser": 
    221             for item in self.iterdefinitions("create", "user"): 
     221            for item in self.iterobjects("create", "user"): 
    222222                yield item 
    223223        elif mode == "objectsdropuser": 
    224             for item in self.iterdefinitions("drop", "user"): 
     224            for item in self.iterobjects("drop", "user"): 
    225225                yield item 
    226226        elif mode == "objectsflatuser": 
    227             for item in self.iterdefinitions("flat", "user"): 
     227            for item in self.iterobjects("flat", "user"): 
    228228                yield item 
    229229        elif mode == "objectscreateall": 
    230             for item in self.iterdefinitions("create", "all"): 
     230            for item in self.iterobjects("create", "all"): 
    231231                yield item 
    232232        elif mode == "objectsdropall": 
    233             for item in self.iterdefinitions("drop", "all"): 
     233            for item in self.iterobjects("drop", "all"): 
    234234                yield item 
    235235        elif mode == "objectsflatall": 
    236             for item in self.iterdefinitions("flat", "all"): 
     236            for item in self.iterobjects("flat", "all"): 
    237237                yield item 
    238238        else: 
     
    242242    def iterschema(self, schema="user"): 
    243243        """ 
    244         <par>Generator that returns the number of different <class>Definition</class> 
    245         objects for this database.</par> 
     244        <par>Generator that returns the number of different object types for this 
     245        database.</par> 
    246246         
    247247        <par>For the meaning of <arg>user</arg> see 
    248         <pyref method="iterdefinitions"><method>iterdefinitions</method></pyref>.</par> 
     248        <pyref method="iterobjects"><method>iterobjects</method></pyref>.</par> 
    249249        """ 
    250250        cursor = self.cursor() 
     
    254254            cursor.execute("select object_type as type, count(*) as count from user_objects group by object_type") 
    255255        for row in cursor.fetchall(): 
    256             class_ = Definition.name2type.get(row.type.lower(), None) 
     256            class_ = Object.name2type.get(row.type.lower(), None) 
    257257            if class_ is not None: 
    258258                yield _AllTypes(self, class_, schema, row.count) 
    259259 
    260     def iterdefinitions(self, mode="create", schema="user"): 
    261         """ 
    262         <par>Generator that yields the definition of all (or the current users) 
    263         sequences, tables, primary keys, foreign keys, comments, unique constraints, 
    264         indexes, views, functions, procedures, packages and types in a 
    265         specified order.</par> 
     260    def iterobjects(self, mode="create", schema="user"): 
     261        """ 
     262        <par>Generator that yields the sequences, tables, primary keys, 
     263        foreign keys, comments, unique constraints, indexes, views, functions, 
     264        procedures, packages and types in the current users schema (or all users 
     265        schemas) in a specified order.</par> 
    266266        <par><arg>mode</arg> specifies the order in which objects will be yielded:</par> 
    267267        <dlist> 
     
    272272        <term><lit>"flat"</lit></term><item>Unordered.</item> 
    273273        </dlist> 
    274         <par><arg>schema</arg> specifies from which schema definitions should be 
     274        <par><arg>schema</arg> specifies from which schema objects should be 
    275275        yielded:</par> 
    276276        <dlist> 
    277         <term><lit>"user"</lit></term><item>Only definitions belonging to the current 
    278         user (and those definitions these depend on) will be yielded.</item> 
    279         <term><lit>"all"</lit></term><item>All definitions from all users will be 
     277        <term><lit>"user"</lit></term><item>Only objects belonging to the current 
     278        user (and those objects these depend on) will be yielded.</item> 
     279        <term><lit>"all"</lit></term><item>All objects from all users will be 
    280280        yielded.</item> 
    281281        </dlist> 
     
    291291        cursor = self.cursor() 
    292292 
    293         def do(definition): 
     293        def do(obj): 
    294294            if mode == "create": 
    295                 for subdefinition in definition.iterreferencesall(self, done): 
    296                     yield subdefinition 
     295                for subobj in obj.iterreferencesall(self, done): 
     296                    yield subobj 
    297297            elif mode == "drop": 
    298                 for subdefinition in definition.iterreferencedbyall(self, done): 
    299                     yield subdefinition 
     298                for subobj in obj.iterreferencedbyall(self, done): 
     299                    yield subobj 
    300300            else: 
    301                 if definition not in done: 
    302                     done.add(definition) 
    303                     yield definition 
     301                if obj not in done: 
     302                    done.add(obj) 
     303                    yield obj 
    304304 
    305305        def dosequences(): 
     
    310310                cursor.execute("select null as sequence_owner, sequence_name from user_sequences") 
    311311            for rec in cursor.fetchall(): 
    312                 for definition in do(SequenceDefinition(rec.sequence_name, rec.sequence_owner, self)): 
    313                     yield definition 
     312                for obj in do(Sequence(rec.sequence_name, rec.sequence_owner, self)): 
     313                    yield obj 
    314314 
    315315        def dotables(): 
     
    319319                cursor.execute("select null as owner, table_name from user_tables") 
    320320            for rec in cursor.fetchall(): 
    321                 tabledefinition = TableDefinition(rec.table_name, rec.owner, self) 
     321                obj = Table(rec.table_name, rec.owner, self) 
    322322                if mode == "create" or mode == "flat": 
    323                     for definition in do(tabledefinition): 
    324                         yield definition 
     323                    for subobj in do(obj): 
     324                        yield subobj 
    325325     
    326                 if not tabledefinition.ismview(self): 
     326                if not obj.ismview(self): 
    327327                    # Primary key 
    328328                    if schema == "all": 
     
    331331                        cursor.execute("select null as owner, constraint_name from user_constraints where constraint_type='P' and table_name=:name", name=rec.table_name) 
    332332                    for rec2 in cursor.fetchall(): 
    333                         for definition in do(PKDefinition(rec2.constraint_name, rec2.owner, self)): 
    334                             yield definition 
     333                        for subobj in do(PrimaryKey(rec2.constraint_name, rec2.owner, self)): 
     334                            yield subobj 
    335335     
    336336                    # Comments 
     
    341341                    for rec2 in cursor.fetchall(): 
    342342                        # No dependency checks neccessary, but use do anyway 
    343                         for definition in do(CommentDefinition("%s.%s" % (rec.table_name, rec2.column_name), rec.owner, self)): 
    344                             yield definition 
     343                        for subobj in do(Comment("%s.%s" % (rec.table_name, rec2.column_name), rec.owner, self)): 
     344                            yield subobj 
    345345 
    346346                if mode == "drop": 
    347                     for definition in do(tabledefinition): 
    348                         yield definition 
     347                    for subobj in do(obj): 
     348                        yield subobj 
    349349 
    350350        def doconstraints(): 
     
    353353            else: 
    354354                cursor.execute("select constraint_type, null as owner, constraint_name from user_constraints where constraint_type in ('R', 'U') order by table_name, constraint_type, constraint_name") 
    355             types = {"U": UniqueDefinition, "R": FKDefinition} 
     355            types = {"U": UniqueConstraint, "R": ForeignKey} 
    356356            for rec in cursor.fetchall(): 
    357                 for definition in do(types[rec.constraint_type](rec.constraint_name, rec.owner, self)): 
    358                     yield definition 
     357                for subobj in do(types[rec.constraint_type](rec.constraint_name, rec.owner, self)): 
     358                    yield subobj 
    359359 
    360360        def doindexes(): 
     
    364364                cursor.execute("select null as owner, index_name from user_indexes where index_type in ('NORMAL', 'FUNCTION-BASED NORMAL') order by table_name, index_name") 
    365365            for rec in cursor.fetchall(): 
    366                 for definition in do(IndexDefinition(rec.index_name, rec.owner, self)): 
    367                     yield definition 
     366                for subobj in do(Index(rec.index_name, rec.owner, self)): 
     367                    yield subobj 
    368368 
    369369        def dosynonyms(): 
     
    373373                cursor.execute("select null as owner, synonym_name from user_synonyms") 
    374374            for rec in cursor.fetchall(): 
    375                 for definition in do(SynonymDefinition(rec.synonym_name, rec.owner, self)): 
    376                     yield definition 
     375                for subobj in do(Synonym(rec.synonym_name, rec.owner, self)): 
     376                    yield subobj 
    377377 
    378378        def doviews(): 
     
    382382                cursor.execute("select null as owner, view_name from user_views") 
    383383            for rec in cursor.fetchall(): 
    384                 for definition in do(ViewDefinition(rec.view_name, rec.owner, self)): 
    385                     yield definition 
     384                for subobj in do(View(rec.view_name, rec.owner, self)): 
     385                    yield subobj 
    386386 
    387387        def domviews(): 
     
    391391                cursor.execute("select null as owner, mview_name from user_mviews") 
    392392            for rec in cursor.fetchall(): 
    393                 for definition in do(MaterializedViewDefinition(rec.mview_name, rec.owner, self)): 
    394                     yield definition 
     393                for subobj in do(MaterializedView(rec.mview_name, rec.owner, self)): 
     394                    yield subobj 
    395395 
    396396        def docode(): 
    397             for type in (FunctionDefinition, ProcedureDefinition, PackageDefinition, PackageBodyDefinition, TypeDefinition, TriggerDefinition, JavaSourceDefinition): 
     397            for type in (Function, Procedure, Package, PackageBody, Type, Trigger, JavaSource): 
    398398                if schema == "all": 
    399399                    cursor.execute("select decode(owner, user, null, owner) as owner, object_name from all_objects where lower(object_type)=lower(:type)", type=type.type) 
     
    401401                    cursor.execute("select null as owner, object_name from user_objects where lower(object_type)=lower(:type)", type=type.type) 
    402402                for rec in cursor.fetchall(): 
    403                     for definition in do(type(rec.object_name, rec.owner, self)): 
    404                         yield definition 
     403                    for subobj in do(type(rec.object_name, rec.owner, self)): 
     404                        yield subobj 
    405405 
    406406        funcs = (dosequences, dotables, doconstraints, doindexes, dosynonyms, doviews, domviews, docode) 
     
    409409 
    410410        for func in funcs: 
    411             for definition in func(): 
    412                 yield definition 
     411            for obj in func(): 
     412                yield obj 
    413413 
    414414 
     
    497497    """ 
    498498    Mixin class that provides methods for determining creation and modification 
    499     dates for definitions. 
     499    dates for objects. 
    500500    """ 
    501501    def cdate(self, connection=None): 
     
    519519    """ 
    520520    Mixin class that provides methods returning the create and drop statements 
    521     for various definitions. 
     521    for various objects. 
    522522    """ 
    523523    def createddl(self, connection=None, term=True): 
     
    567567 
    568568 
    569 class Definition(object): 
     569class Object(object): 
    570570    """ 
    571571    <par>The base class for all Python classes modelling schema objects in the 
     
    573573 
    574574    <par>Subclasses are: 
    575     <pyref class="SequenceDefinition"><class>SequenceDefinition</class></pyref>, 
    576     <pyref class="TableDefinition"><class>TableDefinition</class></pyref>, 
    577     <pyref class="PKDefinition"><class>PKDefinition</class></pyref>, 
    578     <pyref class="CommentDefinition"><class>CommentDefinition</class></pyref>, 
    579     <pyref class="FKDefinition"><class>FKDefinition</class></pyref>, 
    580     <pyref class="IndexDefinition"><class>IndexDefinition</class></pyref>, 
    581     <pyref class="UniqueDefinition"><class>UniqueDefinition</class></pyref>, 
    582     <pyref class="SynonymDefinition"><class>SynonymDefinition</class></pyref>, 
    583     <pyref class="ViewDefinition"><class>ViewDefinition</class></pyref>, 
    584     <pyref class="MaterializedViewDefinition"><class>MaterializedViewDefinition</class></pyref>, 
    585     <pyref class="LibraryDefinition"><class>LibraryDefinition</class></pyref>, 
    586     <pyref class="FunctionDefinition"><class>FunctionDefinition</class></pyref>, 
    587     <pyref class="PackageDefinition"><class>PackageDefinition</class></pyref>, 
    588     <pyref class="TypeDefinition"><class>TypeDefinition</class></pyref>, 
    589     <pyref class="TriggerDefinition"><class>TriggerDefinition</class></pyref>, 
    590     <pyref class="JavaSourceDefinition"><class>JavaSourceDefinition</class></pyref> and 
    591     <pyref class="ColumnDefinition"><class>ColumnDefinition</class></pyref>. 
     575    <pyref class="Sequence"><class>Sequence</class></pyref>, 
     576    <pyref class="Table"><class>Table</class></pyref>, 
     577    <pyref class="PrimaryKey"><class>PrimaryKey</class></pyref>, 
     578    <pyref class="Comment"><class>Comment</class></pyref>, 
     579    <pyref class="ForeignKey"><class>ForeignKey</class></pyref>, 
     580    <pyref class="Index"><class>Index</class></pyref>, 
     581    <pyref class="Unique"><class>Unique</class></pyref>, 
     582    <pyref class="Synonym"><class>Synonym</class></pyref>, 
     583    <pyref class="View"><class>View</class></pyref>, 
     584    <pyref class="MaterializedView"><class>MaterializedView</class></pyref>, 
     585    <pyref class="Library"><class>Library</class></pyref>, 
     586    <pyref class="Function"><class>Function</class></pyref>, 
     587    <pyref class="Package"><class>Package</class></pyref>, 
     588    <pyref class="Type"><class>Type</class></pyref>, 
     589    <pyref class="Trigger"><class>Trigger</class></pyref>, 
     590    <pyref class="JavaSource"><class>JavaSource</class></pyref> and 
     591    <pyref class="Column"><class>Column</class></pyref>. 
    592592    </par> 
    593593    """ 
     
    597597        def __new__(mcl, name, bases, dict): 
    598598            typename = None 
    599             if "type" in dict and name != "Definition": 
     599            if "type" in dict and name != "Object": 
    600600                typename = dict["type"] 
    601601            cls = type.__new__(mcl, name, bases, dict) 
    602602            if typename is not None: 
    603                 Definition.name2type[typename] = cls 
     603                Object.name2type[typename] = cls 
    604604            return cls 
    605605 
     
    657657    def iterreferences(self, connection=None): 
    658658        """ 
    659         <par>Definitions directly used by <self/>.</par> 
     659        <par>Objects directly used by <self/>.</par> 
    660660 
    661661        <par>If <arg>connection</arg> is not <lit>None</lit> it will be used as 
     
    668668        for rec in cursor: 
    669669            try: 
    670                 type = Definition.name2type[rec.referenced_type.lower()] 
     670                type = Object.name2type[rec.referenced_type.lower()] 
    671671            except KeyError: 
    672672                pass # FIXME: Issue a warning? 
     
    676676    def iterreferencesall(self, connection=None, done=None): 
    677677        """ 
    678         <par>All definitions used by <self/> (recursively).</par> 
     678        <par>All objects used by <self/> (recursively).</par> 
    679679 
    680680        <par>For the meaning of <arg>connection</arg> see 
     
    687687        if self not in done: 
    688688            done.add(self) 
    689             for definition in self.iterreferences(connection): 
    690                 for subdefinition in definition.iterreferencesall(connection, done): 
    691                     yield subdefinition 
     689            for obj in self.iterreferences(connection): 
     690                for subobj in obj.iterreferencesall(connection, done): 
     691                    yield subobj 
    692692            yield self 
    693693 
    694694    def iterreferencedby(self, connection=None): 
    695695        """ 
    696         <par>Definitions using <self/>.</par> 
     696        <par>Objects using <self/>.</par> 
    697697 
    698698        <par>For the meaning of <arg>connection</arg> see 
     
    703703        for rec in cursor: 
    704704            try: 
    705                 type = Definition.name2type[rec.type.lower()] 
     705                type = Object.name2type[rec.type.lower()] 
    706706            except KeyError: 
    707707                pass # FIXME: Issue a warning? 
     
    711711    def iterreferencedbyall(self, connection=None, done=None): 
    712712        """ 
    713         <par>All definitions depending on <self/> (recursively).</par> 
     713        <par>All objects depending on <self/> (recursively).</par> 
    714714 
    715715        <par>For the meaning of <arg>connection</arg> see 
     
    722722        if self not in done: 
    723723            done.add(self) 
    724             for definition in self.iterreferencedby(connection): 
    725                 for subdefinition in definition.iterreferencedbyall(connection, done): 
    726                     yield subdefinition 
     724            for obj in self.iterreferencedby(connection): 
     725                for subobj in obj.iterreferencedbyall(connection, done): 
     726                    yield subobj 
    727727            yield self 
    728728 
     
    745745 
    746746    @classmethod 
    747     def iterdefinitions(cls, connection, schema="user"): 
    748         """ 
    749         <par>Generator that yields the definition of all objects of this class in the database 
    750         schema of <arg>cursor</arg>.</par> 
     747    def iterobjects(cls, connection, schema="user"): 
     748        """ 
     749        <par>Generator that yields all objects of this type in the database schema 
     750        of <arg>cursor</arg>.</par> 
    751751        """ 
    752752        cursor = connection.cursor() 
     
    820820 
    821821 
    822 class SequenceDefinition(MixinNormalDates, Definition): 
     822class Sequence(MixinNormalDates, Object): 
    823823    """ 
    824824    Models a sequence in the database. 
     
    906906 
    907907    def __xattrs__(self, mode): 
    908         for attr in super(SequenceDefinition, self).__xattrs__(mode): 
     908        for attr in super(Sequence, self).__xattrs__(mode): 
    909909            yield attr 
    910910            if attr == "-createddl()": 
     
    954954 
    955955 
    956 class TableDefinition(MixinNormalDates, Definition): 
     956class Table(MixinNormalDates, Object): 
    957957    """ 
    958958    Models a table in the database. 
     
    10021002        rec = cursor.fetchone() 
    10031003        if rec is not None: 
    1004             rec = MaterializedViewDefinition(self.name, self.owner, connection) 
     1004            rec = MaterializedView(self.name, self.owner, connection) 
    10051005        return rec 
    10061006 
     
    10121012 
    10131013    @classmethod 
    1014     def iterdefinitions(cls, connection, schema="user"): 
     1014    def iterobjects(cls, connection, schema="user"): 
    10151015        cursor = connection.cursor() 
    10161016        if schema == "all": 
     
    10221022    def itercolumns(self, connection=None): 
    10231023        """ 
    1024         <par>Generator that yields the definition of all columns of the 
    1025         <class>TableDefinition</class> <self/>.</par> 
     1024        <par>Generator that yields all column objects of the <class>Table</class> <self/>.</par> 
    10261025        """ 
    10271026        (connection, cursor) = self.getcursor(connection) 
     
    10291028 
    10301029        for rec in cursor.fetchall(): 
    1031             yield ColumnDefinition("%s.%s" % (self.name, rec.column_name), self.owner, connection) 
     1030            yield Column("%s.%s" % (self.name, rec.column_name), self.owner, connection) 
    10321031 
    10331032    def iterrecords(self, connection=None): 
     
    10421041    def itercomments(self, connection=None): 
    10431042        """ 
    1044         Generator that yields all <pyref class="CommentDefinition">column comments</pyref> of the table <self/>. 
     1043        Generator that yields all <pyref class="Comment">column comments</pyref> of the table <self/>. 
    10451044        """ 
    10461045        (connection, cursor) = self.getcursor(connection) 
    10471046        cursor.execute("select column_name from all_tab_columns where owner=nvl(:owner, user) and table_name=:name order by column_id", owner=self.owner, name=self.name) 
    10481047        for rec in cursor.fetchall(): 
    1049             yield CommentDefinition("%s.%s" % (self.name, rec.column_name), self.owner, connection) 
     1048            yield Comment("%s.%s" % (self.name, rec.column_name), self.owner, connection) 
    10501049 
    10511050    def iterconstraints(self, connection=None): 
    10521051        """ 
    1053         Generator that yields all <pyref class="ConstraintDefinition">constraints</pyref> for this table. 
     1052        Generator that yields all <pyref class="Constraint">constraints</pyref> for this table. 
    10541053        """ 
    10551054        (connection, cursor) = self.getcursor(connection) 
    10561055        # Primary and unique key(s) 
    10571056        cursor.execute("select decode(owner, user, null, owner) as owner, constraint_type, constraint_name from all_constraints where constraint_type in ('P', 'U', 'R') and owner=nvl(:owner, user) and table_name=:name", owner=self.owner, name=self.name) 
    1058         types = {"P": PKDefinition, "U": UniqueDefinition, "R": FKDefinition} 
     1057        types = {"P": PrimaryKey, "U": UniqueConstraint, "R": ForeignKey} 
    10591058        for rec in cursor: 
    10601059            yield types[rec.constraint_type](rec.constraint_name, rec.owner, connection) 
     
    10641063        # A table doesn't depend on anything ... 
    10651064        if self.ismview(connection): 
    1066             # ... unless it was created by a materialized view, in which case it depends on this view 
    1067             yield MaterializedViewDefinition(self.name, self.owner, connection) 
     1065            # ... unless it was created by a materialized view, in which case it depends on the view 
     1066            yield MaterializedView(self.name, self.owner, connection) 
    10681067 
    10691068    def iterreferencedby(self, connection=None): 
    10701069        if not self.ismview(connection): 
    1071             for definition in self.itercomments(connection): 
    1072                 yield definition 
    1073             for definition in self.iterconstraints(connection): 
    1074                 yield definition 
    1075         for definition in super(TableDefinition, self).iterreferencedby(connection): 
     1070            for obj in self.itercomments(connection): 
     1071                yield obj 
     1072            for obj in self.iterconstraints(connection): 
     1073                yield obj 
     1074        for obj in super(Table, self).iterreferencedby(connection): 
    10761075            # skip the materialized view 
    1077             if not isinstance(definition, MaterializedViewDefinition) or definition.name != self.name or definition.owner != self.owner: 
    1078                 yield definition 
     1076            if not isinstance(obj, MaterializedView) or obj.name != self.name or obj.owner != self.owner: 
     1077                yield obj 
    10791078 
    10801079    def __xiter__(self, mode): 
     
    11191118    if hasattr(ipipe, "Descriptor"): 
    11201119        def __xattrs__(self, mode): 
    1121             for attr in super(TableDefinition, self).__xattrs__(mode): 
     1120            for attr in super(Table, self).__xattrs__(mode): 
    11221121                yield attr 
    11231122            if mode=="detail": 
     
    11291128    else: 
    11301129        def __xattrs__(self, mode): 
    1131             for attr in super(TableDefinition, self).__xattrs__(mode): 
     1130            for attr in super(Table, self).__xattrs__(mode): 
    11321131                yield attr 
    11331132            if mode=="detail": 
     
    11351134 
    11361135 
    1137 class ConstraintDefinition(Definition): 
     1136class Constraint(Object): 
    11381137    """ 
    11391138    Base class of all constraints (primary key constraints, foreign key constraints 
     
    11421141 
    11431142 
    1144 class PKDefinition(ConstraintDefinition): 
     1143class PrimaryKey(Constraint): 
    11451144    """ 
    11461145    Models a primary key constraint in the database. 
     
    11971196        cursor.execute("select decode(owner, user, null, owner) as owner, constraint_name from all_constraints where constraint_type='R' and r_owner=nvl(:owner, user) and r_constraint_name=:name", owner=self.owner, name=self.name) 
    11981197        for rec in cursor.fetchall(): 
    1199             yield FKDefinition(rec.constraint_name, rec.owner, connection) 
     1198            yield ForeignKey(rec.constraint_name, rec.owner, connection) 
    12001199 
    12011200        cursor.execute("select decode(owner, user, null, owner) as owner, index_name from all_indexes where owner=nvl(:owner, user) and index_name=:name", owner=self.owner, name=self.name) 
    12021201        rec = cursor.fetchone() # Ist there an index for this constraint? 
    12031202        if rec is not None: 
    1204             yield IndexDefinition(rec.index_name, rec.owner, connection) 
     1203            yield Index(rec.index_name, rec.owner, connection) 
    12051204 
    12061205    def iterreferences(self, connection=None): 
     
    12081207        cursor.execute("select decode(owner, user, null, owner) as owner, table_name from all_constraints where constraint_type='P' and owner=nvl(:owner, user) and constraint_name=:name", owner=self.owner, name=self.name) 
    12091208        for rec in cursor.fetchall(): 
    1210             yield TableDefinition(rec.table_name, rec.owner, connection) 
     1209            yield Table(rec.table_name, rec.owner, connection) 
    12111210 
    12121211    def table(self, connection=None): 
    12131212        """ 
    1214         Return the <pyref class="TableDefinition"><class>TableDefinition</class></pyref> <self/> belongs to. 
     1213        Return the <pyref class="Table"><class>Table</class></pyref> <self/> belongs to. 
    12151214        """ 
    12161215        (connection, cursor) = self.getcursor(connection) 
    12171216        cursor.execute("select table_name from all_constraints where constraint_type='P' and owner=nvl(:owner, user) and constraint_name=:name", owner=self.owner, name=self.name) 
    12181217        rec = cursor.fetchone() 
    1219         return TableDefinition(rec.table_name, self.owner, connection) 
     1218        return Table(rec.table_name, self.owner, connection) 
    12201219 
    12211220    def __xattrs__(self, mode): 
    1222         for attr in super(PKDefinition, self).__xattrs__(mode): 
     1221        for attr in super(PrimaryKey, self).__xattrs__(mode): 
    12231222            yield attr 
    12241223        if mode == "detail": 
     
    12261225 
    12271226 
    1228 class CommentDefinition(Definition): 
     1227class Comment(Object): 
    12291228    """ 
    12301229    Models a column comment in the database. 
     
    12631262    def iterreferences(self, connection=None): 
    12641263        connection = self.getconnection(connection) 
    1265         yield TableDefinition(self.name.split(".")[0], self.owner, connection) 
     1264        yield Table(self.name.split(".")[0], self.owner, connection) 
    12661265 
    12671266    def iterreferencedby(self, connection=None): 
     
    12701269 
    12711270 
    1272 class FKDefinition(ConstraintDefinition): 
     1271class ForeignKey(Constraint): 
    12731272    """ 
    12741273    Models a foreign key constraint in the database. 
     
    13471346    def table(self, connection=None): 
    13481347        """ 
    1349         Return the <pyref class="TableDefinition"><class>TableDefinition</class></pyref> <self/> belongs to. 
     1348        Return the <pyref class="Table"><class>Table</class></pyref> <self/> belongs to. 
    13501349        """ 
    13511350        (connection, cursor) = self.getcursor(connection) 
    13521351        cursor.execute("select table_name from all_constraints where constraint_type='R' and owner=nvl(:owner, user) and constraint_name=:name", owner=self.owner, name=self.name) 
    13531352        rec = cursor.fetchone() 
    1354         return TableDefinition(rec.table_name, self.owner, connection) 
     1353        return Table(rec.table_name, self.owner, connection) 
    13551354 
    13561355    def pk(self, connection=None): 
    13571356        """ 
    1358         Return the <pyref class="PKDefinition">primary key</pyref> referenced by <self/>. 
     1357        Return the <pyref class="PrimaryKey">primary key</pyref> referenced by <self/>. 
    13591358        """ 
    13601359        (connection, cursor) = self.getcursor(connection) 
    13611360        cursor.execute("select decode(r_owner, user, null, r_owner) as r_owner, r_constraint_name from all_constraints where constraint_type='R' and owner=nvl(:owner, user) and constraint_name=:name", owner=self.owner, name=self.name) 
    13621361        rec = cursor.fetchone() 
    1363         return PKDefinition(rec.r_constraint_name, rec.r_owner, connection) 
     1362        return PrimaryKey(rec.r_constraint_name, rec.r_owner, connection) 
    13641363 
    13651364    def __xattrs__(self, mode): 
    1366         for attr in super(FKDefinition, self).__xattrs__(mode): 
     1365        for attr in super(ForeignKey, self).__xattrs__(mode): 
    13671366            yield attr 
    13681367        if mode == "detail": 
     
    13711370 
    13721371 
    1373 class IndexDefinition(MixinNormalDates, Definition): 
     1372class Index(MixinNormalDates, Object): 
    13741373    """ 
    13751374    Models an index in the database. 
     
    14111410    def constraint(self, connection=None): 
    14121411        """ 
    1413         If this index is generated by a constraint, return it's definition otherwise return <lit>None</lit>. 
     1412        If this index is generated by a constraint, return the constraint otherwise return <lit>None</lit>. 
    14141413        """ 
    14151414        (connection, cursor) = self.getcursor(connection) 
     
    14171416        rec = cursor.fetchone() 
    14181417        if rec is not None: 
    1419             rec = {"U": UniqueDefinition, "P": PKDefinition}[rec.constraint_type](self.name, self.owner, connection) 
     1418            rec = {"U": UniqueConstraint, "P": PrimaryKey}[rec.constraint_type](self.name, self.owner, connection) 
    14201419        return rec 
    14211420 
     
    14321431            yield constraint 
    14331432        else: 
    1434             for definition in super(IndexDefinition, self).iterreferences(connection): 
    1435                 yield definition 
     1433            for obj in super(Index, self).iterreferences(connection): 
     1434                yield obj 
    14361435 
    14371436    def __xattrs__(self, mode): 
    1438         for attr in super(IndexDefinition, self).__xattrs__(mode): 
     1437        for attr in super(Index, self).__xattrs__(mode): 
    14391438            yield attr 
    14401439        if mode == "detail": 
     
    14431442    def table(self, connection=None): 
    14441443        """ 
    1445         Return the <pyref class="TableDefinition"><class>TableDefinition</class></pyref> <self/> belongs to. 
     1444        Return the <pyref class="Table"><class>Table</class></pyref> <self/> belongs to. 
    14461445        """ 
    14471446        (connection, cursor) = self.getcursor(connection) 
    14481447        cursor.execute("select table_name from all_indexes where table_owner=nvl(:owner, user) and index_name=:name", owner=self.owner, name=self.name) 
    14491448        rec = cursor.fetchone() 
    1450         return TableDefinition(rec.table_name, self.owner, connection) 
     1449        return Table(rec.table_name, self.owner, connection) 
    14511450 
    14521451    def __xattrs__(self, mode): 
    1453         for attr in super(UniqueDefinition, self).__xattrs__(mode): 
     1452        for attr in super(UniqueConstraint, self).__xattrs__(mode): 
    14541453            yield attr 
    14551454        if mode == "detail": 
     
    14581457 
    14591458 
    1460 class UniqueDefinition(ConstraintDefinition): 
     1459class UniqueConstraint(Constraint): 
    14611460    """ 
    14621461    Models a unique constraint in the database. 
     
    15161515        cursor.execute("select decode(owner, user, null, owner) as owner, constraint_name from all_constraints where constraint_type='R' and r_owner=nvl(:owner, user) and r_constraint_name=:name", owner=self.owner, name=self.name) 
    15171516        for rec in cursor: 
    1518             yield FKDefinition(rec.constraint_name, rec.owner, connection) 
     1517            yield ForeignKey(rec.constraint_name, rec.owner, connection) 
    15191518 
    15201519        cursor.execute("select decode(owner, user, null, owner) as owner, index_name from all_indexes where owner=nvl(:owner, user) and index_name=:name", owner=self.owner, name=self.name) 
    15211520        rec = cursor.fetchone() # Ist there an index for this constraint? 
    15221521        if rec is not None: 
    1523             yield IndexDefinition(rec.index_name, rec.owner, connection) 
     1522            yield Index(rec.index_name, rec.owner, connection) 
    15241523 
    15251524    def iterreferences(self, connection=None): 
     
    15271526        cursor.execute("select decode(owner, user, null, owner) as owner, table_name from all_constraints where constraint_type='U' and owner=nvl(:owner, user) and constraint_name=:name", owner=self.owner, name=self.name) 
    15281527        for rec in cursor.fetchall(): 
    1529             yield TableDefinition(rec.table_name, rec.owner, connection) 
     1528            yield Table(rec.table_name, rec.owner, connection) 
    15301529 
    15311530    def table(self, connection=None): 
    15321531        """ 
    1533         Return the <pyref class="TableDefinition"><class>TableDefinition</class></pyref> <self/> belongs to. 
     1532        Return the <pyref class="Table"><class>Table</class></pyref> <self/> belongs to. 
    15341533        """ 
    15351534        (connection, cursor) = self.getcursor(connection) 
    15361535        cursor.execute("select table_name from all_constraints where constraint_type='U' and owner=nvl(:owner, user) and constraint_name=:name", owner=self.owner, name=self.name) 
    15371536        rec = cursor.fetchone() 
    1538         return TableDefinition(rec.table_name, self.owner, connection) 
     1537        return Table(rec.table_name, self.owner, connection) 
    15391538 
    15401539    def __xattrs__(self, mode): 
    1541         for attr in super(UniqueDefinition, self).__xattrs__(mode): 
     1540        for attr in super(UniqueConstraint, self).__xattrs__(mode): 
    15421541            yield attr 
    15431542        if mode == "detail": 
     
    15451544 
    15461545 
    1547 class SynonymDefinition(Definition): 
     1546class Synonym(Object): 
    15481547    """ 
    15491548    Models a synonym in the database. 
     
    16011600 
    16021601 
    1603 class ViewDefinition(MixinNormalDates, Definition): 
     1602class View(MixinNormalDates, Object): 
    16041603    """ 
    16051604    Models a view in the database. 
     
    16671666 
    16681667 
    1669 class MaterializedViewDefinition(ViewDefinition): 
     1668class MaterializedView(View): 
    16701669    """ 
    16711670    Models a meterialized view in the database. 
     
    16971696    def iterreferences(self, connection=None): 
    16981697        # skip the table 
    1699         for definition in super(MaterializedViewDefinition, self).iterreferences(connection): 
    1700             if not isinstance(definition, TableDefinition) or definition.name != self.name or definition.owner != self.owner: 
    1701                 yield definition 
     1698        for obj in super(MaterializedView, self).iterreferences(connection): 
     1699            if not isinstance(obj, Table) or obj.name != self.name or obj.owner != self.owner: 
     1700                yield obj 
    17021701 
    17031702    def iterreferencedby(self, connection=None): 
    17041703        connection = self.getconnection(connection) 
    1705         yield TableDefinition(self.name, self.owner, connection) 
    1706  
    1707  
    1708 class LibraryDefinition(Definition): 
     1704        yield Table(self.name, self.owner, connection) 
     1705 
     1706 
     1707class Library(Object): 
    17091708    """ 
    17101709    Models a library in the database. 
     
    17341733 
    17351734 
    1736 class FunctionDefinition(MixinNormalDates, MixinCodeDDL, Definition): 
     1735class Function(MixinNormalDates, MixinCodeDDL, Object): 
    17371736    """ 
    17381737    Models a function in the database. 
     
    17411740 
    17421741 
    1743 class ArgumentInfo(object): 
    1744     """ 
    1745     <class>ArgumentInfo</class> objects hold information about the arguments 
     1742class Argument(object): 
     1743    """ 
     1744    <class>Argument</class> objects hold information about the arguments 
    17461745    of a stored procedure. 
    17471746    """ 
     
    17601759 
    17611760 
    1762 class ProcedureDefinition(MixinNormalDates, MixinCodeDDL, Definition): 
    1763     """ 
    1764     Models a procedure  in the database. A <class>ProcedureDefinition</class> 
    1765     object can be used as a wrapper for calling the procedure with keyword arguments. 
     1761class Procedure(MixinNormalDates, MixinCodeDDL, Object): 
     1762    """ 
     1763    Models a procedure  in the database. A <class>Procedure</class> object can be 
     1764    used as a wrapper for calling the procedure with keyword arguments. 
    17661765    """ 
    17671766 
     
    17771776     
    17781777    def __init__(self, name, owner=None, connection=None, encoding=None): 
    1779         Definition.__init__(self, name, owner, connection) 
     1778        Object.__init__(self, name, owner, connection) 
    17801779        self.encoding = encoding 
    17811780        self._argsbypos = None 
     
    17881787            cursor.execute("select lower(argument_name) as name, lower(in_out) as in_out, lower(data_type) as datatype from user_arguments where lower(object_name)=lower(:name) and data_level=0 order by sequence", name=self.name) 
    17891788            for (i, record) in enumerate(cursor): 
    1790                 arginfo = ArgumentInfo(record.name, i, record.datatype, "in" in record.in_out, "out" in record.in_out) 
     1789                arginfo = Argument(record.name, i, record.datatype, "in" in record.in_out, "out" in record.in_out) 
    17911790                self._argsbypos.append(arginfo) 
    17921791                self._argsbyname[arginfo.name] = arginfo 
     
    18591858 
    18601859    def __xattrs__(self, mode="default"): 
    1861         for attr in Definition.__xattrs__(self, mode): 
     1860        for attr in Object.__xattrs__(self, mode): 
    18621861            yield attr 
    18631862        if mode == "detail": 
     
    18651864 
    18661865 
    1867 class PackageDefinition(MixinNormalDates, MixinCodeDDL, Definition): 
     1866class Package(MixinNormalDates, MixinCodeDDL, Object): 
    18681867    """ 
    18691868    Models a package in the database. 
     
    18721871 
    18731872 
    1874 class PackageBodyDefinition(MixinNormalDates, MixinCodeDDL, Definition): 
     1873class PackageBody(MixinNormalDates, MixinCodeDDL, Object): 
    18751874    """ 
    18761875    Models a package body in the database. 
     
    18791878 
    18801879 
    1881 class TypeDefinition(MixinNormalDates, MixinCodeDDL, Definition): 
     1880class Type(MixinNormalDates, MixinCodeDDL, Object): 
    18821881    """ 
    18831882    Models a type definition in the database. 
     
    18861885 
    18871886 
    1888 class TriggerDefinition(MixinNormalDates, MixinCodeDDL, Definition): 
     1887class Trigger(MixinNormalDates, MixinCodeDDL, Object): 
    18891888    """ 
    18901889    Models a trigger in the database. 
     
    18931892 
    18941893 
    1895 class JavaSourceDefinition(MixinNormalDates, Definition): 
     1894class JavaSource(MixinNormalDates, Object): 
    18961895    """ 
    18971896    Models Java source code in the database. 
     
    19191918 
    19201919 
    1921 class ColumnDefinition(Definition): 
     1920class Column(Object): 
    19221921    """ 
    19231922    Models a single column of a table in the database. This is used to output 
     
    20422041        connection = self.getconnection(connection) 
    20432042        name = self.name.split(".") 
    2044         yield TableDefinition(name[0], self.owner, connection) 
     2043        yield Table(name[0], self.owner, connection) 
    20452044 
    20462045    def iterreferencedby(self, connection=None): 
  • src/ll/orasql/scripts/oracreate.py

    r146 r161  
    1818s4error = astyle.Style.fromenv("LL_ORASQL_REPRANSI_ERROR", "red:black") 
    1919s4connectstring = astyle.Style.fromenv("LL_ORASQL_REPRANSI_CONNECTSTRING", "yellow:black") 
    20 s4definition = astyle.Style.fromenv("LL_ORASQL_REPRANSI_DEFINITION", "green:black") 
     20s4object = astyle.Style.fromenv("LL_ORASQL_REPRANSI_OBJECT", "green:black") 
    2121 
    2222 
     
    5858        cs2 = s4connectstring(connection2.connectstring()) 
    5959 
    60     def keep(definition): 
    61         if definition.owner is not None: 
     60    def keep(obj): 
     61        if obj.owner is not None: 
    6262            return False 
    6363        if options.keepjunk: 
    6464            return True 
    65         # output pk, fks etc. only when they belong to table we *do* output 
    66         if isinstance(definition, (orasql.ConstraintDefinition, orasql.IndexDefinition)): 
    67             definition = definition.table() 
    68         if "$" in definition.name or definition.name.startswith("SYS_EXPORT_SCHEMA_"): 
     65        # output pk, fks etc. only when they belong to table we do output 
     66        if isinstance(obj, (orasql.Constraint, orasql.Index)): 
     67            obj = obj.table() 
     68        if "$" in obj.name or obj.name.startswith("SYS_EXPORT_SCHEMA_"): 
    6969            return False 
    7070        return True 
    7171 
    72     for (i, definition) in enumerate(connection.iterdefinitions(mode="create", schema="user")): 
    73         keepdef = keep(definition) 
     72    for (i, obj) in enumerate(connection.iterobjects(mode="create", schema="user")): 
     73        keepobj = keep(obj) 
    7474        if options.verbose: 
    7575            if options.execute: 
     
    7777            else: 
    7878                msg = astyle.style_default("oracreate.py: ", cs1, " fetching #%d" % (i+1)) 
    79             msg = astyle.style_default(msg, " ", s4definition(repr(definition))) 
    80             if not keepdef: 
     79            msg = astyle.style_default(msg, " ", s4object(repr(obj))) 
     80            if not keepobj: 
    8181                msg = astyle.style_default(msg, " ", s4warning("(skipped)")) 
    8282            stderr.writeln(msg) 
    8383 
    84         if keepdef: 
    85             if isinstance(definition, orasql.SequenceDefinition) and options.seqcopy: 
    86                 ddl = definition.createddlcopy(connection, term) 
     84        if keepobj: 
     85            if isinstance(obj, orasql.Sequence) and options.seqcopy: 
     86                ddl = obj.createddlcopy(connection, term) 
    8787            else: 
    88                 ddl = definition.createddl(connection, term) 
     88                ddl = obj.createddl(connection, term) 
    8989            if ddl: 
    9090                if options.execute: 
  • src/ll/orasql/scripts/oradiff.py

    r159 r161  
    2424s4pos = astyle.Style.fromenv("LL_ORASQL_REPRANSI_POS", "black:black:bold") 
    2525s4connectstring = astyle.Style.fromenv("LL_ORASQL_REPRANSI_CONNECTSTRING", "yellow:black") 
    26 s4definition = astyle.Style.fromenv("LL_ORASQL_REPRANSI_DEFINITION", "green:black") 
     26s4object = astyle.Style.fromenv("LL_ORASQL_REPRANSI_OBJECT", "green:black") 
    2727 
    2828 
     
    3131 
    3232 
    33 def df(definition): 
    34     return s4definition(repr(definition)) 
     33def df(obj): 
     34    return s4object(repr(obj)) 
    3535 
    3636 
     
    3939 
    4040 
    41 def gettimestamp(definition, connection, format): 
     41def gettimestamp(obj, connection, format): 
    4242    try: 
    43         timestamp = definition.udate(connection) 
     43        timestamp = obj.udate(connection) 
    4444    except orasql.SQLObjectNotFoundError: 
    4545        return "doesn't exist" 
     
    8383 
    8484 
    85 def showudiff(out, definition, ddl1, ddl2, connection1, connection2, context=3, timeformat="%c"): 
     85def showudiff(out, obj, ddl1, ddl2, connection1, connection2, context=3, timeformat="%c"): 
    8686    def header(prefix, style, connection): 
    87         return style("%s %r in %s: %s" % (prefix, definition, connection.connectstring(), gettimestamp(definition, connection, timeformat))) 
     87        return style("%s %r in %s: %s" % (prefix, obj, connection.connectstring(), gettimestamp(obj, connection, timeformat))) 
    8888 
    8989    started = False 
     
    140140        objects = set() 
    141141 
    142         def keep(definition): 
    143             if definition.owner is not None: 
     142        def keep(obj): 
     143            if obj.owner is not None: 
    144144                return False 
    145145            if options.keepjunk: 
    146146                return True 
    147             if "$" in definition.name or definition.name.startswith("SYS_EXPORT_SCHEMA_"): 
     147            if "$" in obj.name or obj.name.startswith("SYS_EXPORT_SCHEMA_"): 
    148148                return False 
    149149            return True 
    150150     
    151         for (i, definition) in enumerate(connection.iterdefinitions(mode="flat", schema="user")): 
    152             keepdef = keep(definition) 
     151        for (i, obj) in enumerate(connection.iterobjects(mode="flat", schema="user")): 
     152            keepdef = keep(obj) 
    153153            if options.verbose: 
    154                 msg = astyle.style_default("oradiff.py: ", cs(connection), ": fetching #%d " % (i+1), df(definition)) 
     154                msg = astyle.style_default("oradiff.py: ", cs(connection), ": fetching #%d " % (i+1), df(obj)) 
    155155                if not keepdef: 
    156156                    msg += " (skipped)" 
    157157                stderr.writeln(msg) 
    158158            if keepdef: 
    159                 objects.add(definition) 
     159                objects.add(obj) 
    160160        return objects 
    161161 
     
    164164 
    165165    onlyin1 = objects1 - objects2 
    166     for (i, definition) in enumerate(onlyin1): 
     166    for (i, obj) in enumerate(onlyin1): 
    167167        if options.verbose: 
    168             stderr.writeln("oradiff.py: only in ", cs(connection1), " #%d/%d " % (i+1, len(onlyin1)), df(definition)) 
     168            stderr.writeln("oradiff.py: only in ", cs(connection1), " #%d/%d " % (i+1, len(onlyin1)), df(obj)) 
    169169        if options.mode == "brief": 
    170             stdout.writeln(df(definition), ": only in ", cs(connection1)) 
     170            stdout.writeln(df(obj), ": only in ", cs(connection1)) 
    171171        elif options.mode == "full": 
    172             stdout.writeln(comment(df(definition), ": only in " % cs(connection1))) 
    173             ddl = definition.dropddl(connection1, term=True) 
     172            stdout.writeln(comment(df(obj), ": only in " % cs(connection1))) 
     173            ddl = obj.dropddl(connection1, term=True) 
    174174            if ddl: 
    175175                stdout.write(ddl) 
    176176        elif options.mode == "udiff": 
    177             ddl = getcanonicalddl(definition.createddl(connection1), options.blank) 
    178             showudiff(stdout, definition, ddl, [], connection1, connection2, options.context) 
     177            ddl = getcanonicalddl(obj.createddl(connection1), options.blank) 
     178            showudiff(stdout, obj, ddl, [], connection1, connection2, options.context) 
    179179 
    180180    onlyin2 = objects2 - objects1 
    181     for (i, definition) in enumerate(onlyin2): 
     181    for (i, obj) in enumerate(onlyin2): 
    182182        if options.verbose: 
    183             stderr.writeln("oradiff.py: only in ", cs(connection2), " #%d/%d " % (i+1, len(onlyin2)), df(definition)) 
     183            stderr.writeln("oradiff.py: only in ", cs(connection2), " #%d/%d " % (i+1, len(onlyin2)), df(obj)) 
    184184        if options.mode == "brief": 
    185             stdout.writeln(df(definition), ": only in ", cs(connection2)) 
     185            stdout.writeln(df(obj), ": only in ", cs(connection2)) 
    186186        elif options.mode == "full": 
    187             stdout.writeln(comment(df(definition), ": only in ", cs(connection2))) 
    188             ddl = definition.createddl(connection2, term=True) 
     187            stdout.writeln(comment(df(obj), ": only in ", cs(connection2))) 
     188            ddl = obj.createddl(connection2, term=True) 
    189189            if ddl: 
    190190                stdout.write(ddl) 
    191191        elif options.mode == "udiff": 
    192             ddl = getcanonicalddl(definition.createddl(connection2), options.blank) 
    193             showudiff(stdout, definition, [], ddl, connection1, connection2, options.context) 
     192            ddl = getcanonicalddl(obj.createddl(connection2), options.blank) 
     193            showudiff(stdout, obj, [], ddl, connection1, connection2, options.context) 
    194194 
    195195    common = objects1 & objects2 
    196     for (i, definition) in enumerate(common): 
     196    for (i, obj) in enumerate(common): 
    197197        if options.verbose: 
    198             stderr.writeln("oradiff.py: diffing #%d/%d " % (i+1, len(common)), df(definition)) 
    199         ddl1 = definition.createddl(connection1) 
    200         ddl2 = definition.createddl(connection2) 
     198            stderr.writeln("oradiff.py: diffing #%d/%d " % (i+1, len(common)), df(obj)) 
     199        ddl1 = obj.createddl(connection1) 
     200        ddl2 = obj.createddl(connection2) 
    201201        ddl1c = getcanonicalddl(ddl1, options.blank) 
    202202        ddl2c = getcanonicalddl(ddl2, options.blank) 
    203203        if ddl1c != ddl2c: 
    204204            if options.mode == "brief": 
    205                 stdout.writeln(df(definition), ": different") 
     205                stdout.writeln(df(obj), ": different") 
    206206            elif options.mode == "full": 
    207                 stdout.writeln(comment(df(definition), ": different")) 
    208                 stdout.write(definition.createddl(connection2)) 
     207                stdout.writeln(comment(df(obj), ": different")) 
     208                stdout.write(obj.createddl(connection2)) 
    209209            elif options.mode == "udiff": 
    210                 showudiff(stdout, definition, ddl1c, ddl2c, connection1, connection2, options.context) 
     210                showudiff(stdout, obj, ddl1c, ddl2c, connection1, connection2, options.context) 
    211211 
    212212 
  • src/ll/orasql/scripts/oradrop.py

    r141 r161  
    1818s4error = astyle.Style.fromenv("LL_ORASQL_REPRANSI_ERROR", "red:black") 
    1919s4connectstring = astyle.Style.fromenv("LL_ORASQL_REPRANSI_CONNECTSTRING", "yellow:black") 
    20 s4definition = astyle.Style.fromenv("LL_ORASQL_REPRANSI_DEFINITION", "green:black") 
     20s4object = astyle.Style.fromenv("LL_ORASQL_REPRANSI_OBJECT", "green:black") 
    2121 
    2222 
     
    5353    cs = s4connectstring(connection.connectstring()) 
    5454 
    55     def keep(definition): 
    56         if definition.owner is not None and not isinstance(definition, orasql.FKDefinition): 
     55    def keep(obj): 
     56        if obj.owner is not None and not isinstance(obj, orasql.ForeignKey): 
    5757            return False 
    5858        if options.keepjunk: 
    5959            return True 
    60         if "$" in definition.name: 
     60        if "$" in obj.name: 
    6161            return False 
    6262        return True 
    6363 
    6464    ddls = [] 
    65     for (i, definition) in enumerate(connection.iterdefinitions(mode="drop", schema="user")): 
    66         keepdef = keep(definition) 
     65    for (i, obj) in enumerate(connection.iterobjects(mode="drop", schema="user")): 
     66        keepdef = keep(obj) 
    6767        # Get DDL 
    6868        ddl = "" 
    6969        action = "skipped" 
    70         if definition.owner is not None: 
    71             if isinstance(definition, orasql.FKDefinition): 
     70        if obj.owner is not None: 
     71            if isinstance(obj, orasql.ForeignKey): 
    7272                if options.fks == "disable": 
    73                     ddl = definition.disableddl(cursor, term) 
     73                    ddl = obj.disableddl(cursor, term) 
    7474                    action = "disabled" 
    7575                elif options.fks == "drop": 
    76                     ddl = definition.dropddl(cursor, term) 
     76                    ddl = obj.dropddl(cursor, term) 
    7777                    action = None 
    7878        elif keepdef: 
    79             ddl = definition.dropddl(connection, term) 
     79            ddl = obj.dropddl(connection, term) 
    8080            action = None 
    8181 
    8282        # Progress report 
    8383        if options.verbose: 
    84             msg = astyle.style_default("oradrop.py: ", cs, ": fetching #%d " % (i+1), s4definition(repr(definition))) 
     84            msg = astyle.style_default("oradrop.py: ", cs, ": fetching #%d " % (i+1), s4object(repr(obj))) 
    8585            if action is not None: 
    8686                msg = astyle.style_default(msg, " ", s4warning("(%s)" % action)) 
     
    9090            # Print or execute DDL 
    9191            if options.execute: 
    92                 ddls.append((definition, ddl)) 
     92                ddls.append((obj, ddl)) 
    9393            else: 
    9494                stdout.write(ddl) 
     
    9797    if options.execute: 
    9898        cursor = connection.cursor() 
    99         for (i, (definition, ddl)) in enumerate(ddls): 
     99        for (i, (obj, ddl)) in enumerate(ddls): 
    100100            if options.verbose: 
    101                 stderr.writeln("oradrop.py: ", cs, ": dropping #%d/%d " % (i+1, len(ddls)), s4definition(repr(definition))) 
     101                stderr.writeln("oradrop.py: ", cs, ": dropping #%d/%d " % (i+1, len(ddls)), s4object(repr(obj))) 
    102102            try: 
    103103                cursor.execute(ddl) 
  • src/ll/orasql/scripts/oramerge.py

    r155 r161  
    2626s4pos = astyle.Style.fromenv("LL_ORASQL_REPRANSI_POS", "black:black:bold") 
    2727s4connectstring = astyle.Style.fromenv("LL_ORASQL_REPRANSI_CONNECTSTRING", "yellow:black") 
    28 s4definition = astyle.Style.fromenv("LL_ORASQL_REPRANSI_DEFINITION", "green:black") 
     28s4object = astyle.Style.fromenv("LL_ORASQL_REPRANSI_OBJECT", "green:black") 
    2929 
    3030 
     
    3434 
    3535 
    36 def df(definition): 
    37     return s4definition(repr(definition)) 
     36def df(obj): 
     37    return s4object(repr(obj)) 
    3838 
    3939 
     
    4646 
    4747 
    48 def showaction(out, action, definition): 
    49     out.writeln(comment(action, " ", df(definition))) 
    50  
    51  
    52 def gettimestamp(definition, cursor, format): 
     48def showaction(out, action, obj): 
     49    out.writeln(comment(action, " ", df(obj))) 
     50 
     51 
     52def gettimestamp(obj, cursor, format): 
    5353    try: 
    54         timestamp = definition.udate(cursor) 
     54        timestamp = obj.udate(cursor) 
    5555    except orasql.SQLObjectNotFoundError: 
    5656        return "doesn't exist" 
     
    6262 
    6363 
    64 def main(): 
     64def main(args=None): 
    6565    colors = ("yes", "no", "auto") 
    6666    blanks = ("literal", "trail", "lead", "both", "collapse") 
     
    6969    p.add_option("-v", "--verbose", dest="verbose", help="Give a progress report?", default=False, action="store_true") 
    7070    p.add_option("-c", "--color", dest="color", help="Color output (%s)" % ", ".join(colors), default="auto", choices=colors) 
    71     p.add_option("-k", "--keepdollar", dest="keepdollar", help="Output objects with '$' in their name?", default=False, action="store_true") 
    72  
    73     (options, args) = p.parse_args() 
     71    p.add_option("-k", "--keepjunk", dest="keepjunk", help="Output objects with '$' in their name?", default=False, action="store_true") 
     72 
     73    (options, args) = p.parse_args(args) 
    7474    if len(args) != 3: 
    7575        p.error("incorrect number of arguments") 
     
    9292        objects = set() 
    9393 
    94         for (i, definition) in enumerate(connection.iterdefinitions(mode="flat", schema="user")): 
    95             keep = "$" not in definition.name or options.keepdollar 
     94        for (i, obj) in enumerate(connection.iterobjects(mode="flat", schema="user")): 
     95            keep = ("$" not in obj.name and not obj.name.startswith("SYS_EXPORT_SCHEMA_")) or options.keepjunk 
    9696            if options.verbose: 
    97                 msg = astyle.style_default("oramerge.py: ", cs(connection), " fetching #%d " % (i+1), df(definition)) 
     97                msg = astyle.style_default("oramerge.py: ", cs(connection), " fetching #%d " % (i+1), df(obj)) 
    9898                if not keep: 
    9999                    msg += " (skipped)" 
    100100                stderr.writeln(msg) 
    101101            if keep: 
    102                 objects.add(definition) 
     102                objects.add(obj) 
    103103        return objects 
    104104 
     
    117117 
    118118    allobjects = objects1 | objects2 | objects3 
    119     for (i, definition) in enumerate(allobjects): 
    120         in1 = definition in objects1 
    121         in2 = definition in objects2 
    122         in3 = definition in objects3 
     119    for (i, obj) in enumerate(allobjects): 
     120        in1 = obj in objects1 
     121        in2 = obj in objects2 
     122        in3 = obj in objects3 
    123123        if in1 != in2: # ignore changes from in2 to in3, because only if something changed in the sources we have to do something 
    124124            if not in1 and in2 and not in3: # added in in2 => copy it to db3 
    125                 msg = astyle.style_default(df(definition), ": added in ", cs(connection2), " => add it to ", cs(connection3)) 
     125                msg = astyle.style_default(df(obj), ": added in ", cs(connection2), " => add it to ", cs(connection3)) 
    126126                action = "create" 
    127127            elif not in1 and in2 and in3: # added in both in2 and in3 => collision? 
    128                 if definition.createddl(connection2) != definition.createddl(connection3): 
    129                     msg = astyle.style_default(df(definition), ": added in ", cs(connection2), " and ", cs(connection3), " => ", s4error("conflict")) 
     128                if obj.createddl(connection2) != obj.createddl(connection3): 
     129                    msg = astyle.style_default(df(obj), ": added in ", cs(connection2), " and ", cs(connection3), " => ", s4error("conflict")) 
    130130                    action = "collision" 
    131131                    retcode = 2 
    132132                else: 
    133                     msg = astyle.style_default(df(definition), ": added in ", cs(connection2), " and ", cs(connection3), " and identical => keep it") 
     133                    msg = astyle.style_default(df(obj), ": added in ", cs(connection2), " and ", cs(connection3), " and identical => keep it") 
    134134                    action = None 
    135135            elif in1 and not in2 and not in3: # removed in in2 and in3 => not needed 
    136                 msg = astyle.style_default(df(definition), ": removed in ", cs(connection2), " and ", cs(connection3), " => not needed") 
     136                msg = astyle.style_default(df(obj), ": removed in ", cs(connection2), " and ", cs(connection3), " => not needed") 
    137137                action = None 
    138138            elif in1 and not in2 and in3: # removed in in2 => remove in db3 
    139                 msg = astyle.style_default(df(definition), ": removed in ", cs(connection2), " => remove in ", cs(connection3)) 
     139                msg = astyle.style_default(df(obj), ": removed in ", cs(connection2), " => remove in ", cs(connection3)) 
    140140                action = "drop" 
    141141            else: 
    142142                raise ValueError("the boolean world is about to end") 
    143143        elif in1 and in2 and in3: # in all three => merge it 
    144             ddl1 = definition.createddl(connection1) 
    145             ddl2 = definition.createddl(connection2) 
    146             ddl3 = definition.createddl(connection3) 
     144            ddl1 = obj.createddl(connection1) 
     145            ddl2 = obj.createddl(connection2) 
     146            ddl3 = obj.createddl(connection3) 
    147147 
    148148            if options.verbose: 
    149                 stderr.writeln("oramerge.py: diffing #%d/%d " % (i+1, len(allobjects)), df(definition)) 
     149                stderr.writeln("oramerge.py: diffing #%d/%d " % (i+1, len(allobjects)), df(obj)) 
    150150 
    151151            if ddl1 != ddl2: # ignore changes between ddl2 and ddl3 here too 
    152152                # If it's a table, we do not output a merged "create table" statement, but the appropriate "alter table" statements 
    153                 if isinstance(definition, orasql.TableDefinition): 
    154                     fields1 = set(definition.itercolumns(connection1)) 
    155                     fields2 = set(definition.itercolumns(connection2)) 
    156                     fields3 = set(definition.itercolumns(connection3)) 
     153                if isinstance(obj, orasql.Table): 
     154                    fields1 = set(obj.itercolumns(connection1)) 
     155                    fields2 = set(obj.itercolumns(connection2)) 
     156                    fields3 = set(obj.itercolumns(connection3)) 
    157157                    for field in fields1 | fields2 | fields3: 
    158158                        if options.verbose: 
     
    187187                                    stdout.writeln(ddl) 
    188188                else: 
    189                     msg = astyle.style_default(df(definition), ": merge them") 
     189                    msg = astyle.style_default(df(obj), ": merge them") 
    190190                    action = "merge" 
    191191            else: 
    192                 msg = astyle.style_default(df(definition), ": identical") 
     192                msg = astyle.style_default(df(obj), ": identical") 
    193193                action = None 
    194194 
    195195        if action is not None: 
    196196            if action == "collision": 
    197                 showaction(stdout, action, definition) 
     197                showaction(stdout, action, obj) 
    198198                stdout.writeln(conflictmarker(7*"<", "added in ", cs(connection2), " and ", cs(connection3), " with different content")) 
    199199            elif action == "create": 
    200                 showaction(stdout, action, definition) 
    201                 stdout.writeln(definition.createddl(connection2, term=True)) 
     200                showaction(stdout, action, obj) 
     201                stdout.writeln(obj.createddl(connection2, term=True)) 
    202202            elif action == "drop": 
    203                 showaction(stdout, action, definition) 
    204                 stdout.writeln(definition.dropddl(connection3, term=True)) 
     203                showaction(stdout, action, obj) 
     204                stdout.writeln(obj.dropddl(connection3, term=True)) 
    205205            elif action == "merge": 
    206206                filename1 = tempfile.mktemp(suffix=".sql", prefix="oramerge_1_") 
     
    245245                                # diff3 seems to append a "\n" 
    246246                                if finalddl != ddl3 and (not finalddl.endswith("\n") or finalddl[:-1] != ddl3): 
    247                                     showaction(stdout, "merge", definition) 
     247                                    showaction(stdout, "merge", obj) 
    248248                                    stdout.write(finalddl) 
    249249                            elif diffretcode == 1: # conflict 
    250                                 showaction(stdout, "conflict", definition) 
     250                                showaction(stdout, "conflict", obj) 
    251251                                retcode = 2 
    252252                                for line in data.splitlines(): 
  • test/test_orasql.py

    r160 r161  
    1919 
    2020 
    21 # here all definitions are collected, so we don't need to call iterdefinitions() multiple times 
    22 deflist = [] 
    23 defdict = {} 
     21# here all objects are collected, so we don't need to call iterobjects() multiple times 
     22objlist = [] 
     23objdict = {} 
    2424 
    2525 
     
    2727    db = orasql.connect(dbname) 
    2828    # get all definitions (this tests that iterdefinition(), iterreferences() and iterreferencedby() run to completion) 
    29     module.defdict = {} 
    30     for definition in db.iterdefinitions(): 
    31         module.deflist.append(definition) 
    32         references = list(definition.iterreferences()) 
    33         referencedby = list(definition.iterreferencedby()) 
    34         module.defdict[definition] = (references, referencedby) 
     29    module.objdict = {} 
     30    for obj in db.iterobjects(): 
     31        module.objlist.append(obj) 
     32        references = list(obj.iterreferences()) 
     33        referencedby = list(obj.iterreferencedby()) 
     34        module.objdict[obj] = (references, referencedby) 
    3535 
    3636 
    3737def teardown_module(module): 
    38     del module.deflist 
    39     del module.defdict 
     38    del module.objlist 
     39    del module.objdict 
    4040 
    4141 
     
    5858 
    5959def test_referenceconsistency(): 
    60     for (definition, (references, referencedby)) in defdict.iteritems(): 
    61         for refdef in references: 
    62             # check that iterdefinitions() returned everything from this schema 
    63             assert refdef.owner is not None or refdef in defdict 
    64             # check that the referenced definition points back to this one (via referencedby) 
    65             if refdef.owner is None: 
    66                 assert definition in defdict[refdef][1] 
     60    for (obj, (references, referencedby)) in objdict.iteritems(): 
     61        for refobj in references: 
     62            # check that iterobjects() returned everything from this schema 
     63            assert refobj.owner is not None or refobj in objdict 
     64            # check that the referenced object points back to this one (via referencedby) 
     65            if refobj.owner is None: 
     66                assert obj in objdict[refobj][1] 
    6767 
    6868        # do the inverted check 
    69         for refdef in referencedby: 
    70             assert refdef.owner is not None or refdef in defdict 
    71             if refdef.owner is None: 
    72                 assert definition in defdict[refdef][0] 
     69        for refobj in referencedby: 
     70            assert refobj.owner is not None or refobj in objdict 
     71            if refobj.owner is None: 
     72                assert obj in objdict[refobj][0] 
    7373 
    7474 
    7575def test_ddl(): 
    7676    # check various ddl methods 
    77     for definition in defdict: 
    78         definition.createddl() 
    79         if isinstance(definition, orasql.SequenceDefinition): 
    80             definition.createddlcopy() 
    81         definition.dropddl() 
    82         if isinstance(definition, orasql.FKDefinition): 
    83             definition.enableddl() 
    84             definition.disableddl() 
     77    for obj in objdict: 
     78        obj.createddl() 
     79        if isinstance(obj, orasql.Sequence): 
     80            obj.createddlcopy() 
     81        obj.dropddl() 
     82        if isinstance(obj, orasql.ForeignKey): 
     83            obj.enableddl() 
     84            obj.disableddl() 
    8585 
    8686 
    8787def test_repr(): 
    8888    # check that each repr method works 
    89     for definition in defdict: 
    90         repr(definition) 
     89    for obj in objdict: 
     90        repr(obj) 
    9191 
    9292 
    9393def test_cudate(): 
    9494    # check that cdate/udate method works 
    95     for definition in defdict: 
    96         cdate = definition.cdate() 
     95    for obj in objdict: 
     96        cdate = obj.cdate() 
    9797        assert cdate is None or isinstance(cdate, datetime.datetime) 
    98         udate = definition.udate() 
     98        udate = obj.udate() 
    9999        assert udate is None or isinstance(udate, datetime.datetime) 
    100100 
    101101 
    102102def test_table_columns(): 
    103     for definition in defdict: 
    104         if isinstance(definition, orasql.TableDefinition): 
    105             for col in definition.itercolumns(): 
    106                 # comments are not output by iterdefinitions(), so we have to call iterreferences() instead of using definitions 
    107                 assert definition in col.iterreferences() 
     103    for obj in objdict: 
     104        if isinstance(obj, orasql.Table): 
     105            for col in obj.itercolumns(): 
     106                # comments are not output by iterobjects(), so we have to call iterreferences() 
     107                assert obj in col.iterreferences() 
    108108                # check various methods 
    109109                # calling modifyddl() doesn't make sense 
     
    115115 
    116116def test_table_comments(): 
    117     for definition in defdict: 
    118         if isinstance(definition, orasql.TableDefinition): 
    119             # comments are output by iterdefinitions(), but not for materialized views 
    120             if definition.ismview(): 
    121                 for com in definition.itercomments(): 
    122                     assert definition in com.iterreferences() 
     117    for obj in objdict: 
     118        if isinstance(obj, orasql.Table): 
     119            # comments are output by iterobjects(), but not for materialized views 
     120            if obj.ismview(): 
     121                for com in obj.itercomments(): 
     122                    assert obj in com.iterreferences() 
    123123            else: 
    124                 for com in definition.itercomments(): 
    125                     assert definition in defdict[com][0] 
     124                for com in obj.itercomments(): 
     125                    assert obj in objdict[com][0] 
    126126 
    127127 
    128128def test_table_constraints(): 
    129     for definition in defdict: 
    130         if isinstance(definition, orasql.TableDefinition): 
    131             for con in definition.iterconstraints(): 
    132                 assert definition in defdict[con][0] 
     129    for obj in objdict: 
     130        if isinstance(obj, orasql.Table): 
     131            for con in obj.iterconstraints(): 
     132                assert obj in objdict[con][0] 
    133133 
    134134 
    135135def test_table_records(): 
    136     for definition in defdict: 
    137         if isinstance(definition, orasql.TableDefinition): 
     136    for obj in objdict: 
     137        if isinstance(obj, orasql.Table): 
    138138            # fetch only a few records 
    139             for (i, rec) in enumerate(definition.iterrecords()): 
     139            for (i, rec) in enumerate(obj.iterrecords()): 
    140140                if i >= 5: 
    141141                    break 
     
    143143 
    144144def test_table_mview(): 
    145     for definition in defdict: 
    146         if isinstance(definition, orasql.TableDefinition): 
    147             assert (definition.mview() is not None) == definition.ismview() 
     145    for obj in objdict: 
     146        if isinstance(obj, orasql.Table): 
     147            assert (obj.mview() is not None) == obj.ismview() 
    148148 
    149149 
    150150def test_constaints(): 
    151     for definition in defdict: 
    152         if isinstance(definition, orasql.ConstraintDefinition): 
    153             definition.table() 
    154             if isinstance(definition, orasql.FKDefinition): 
    155                 definition.pk() 
     151    for obj in objdict: 
     152        if isinstance(obj, orasql.Constraint): 
     153            obj.table() 
     154            if isinstance(obj, orasql.ForeignKey): 
     155                obj.pk() 
    156156 
    157157 
    158158def test_procedure_arguments(): 
    159     for definition in defdict: 
    160         if isinstance(definition, orasql.ProcedureDefinition): 
    161             list(definition.iterarguments()) 
     159    for obj in objdict: 
     160        if isinstance(obj, orasql.Procedure): 
     161            list(obj.iterarguments()) 
    162162 
    163163 
    164164def test_createorder(): 
    165     # check that the default output order of iterdefinitions() (i.e. create order) works 
     165    # check that the default output order of iterobjects() (i.e. create order) works 
    166166    done = set() 
    167     for definition in deflist: 
    168         for refdef in defdict[definition][0]: 
    169             print definition, refdef 
    170             assert refdef in done 
    171         done.add(definition) 
     167    for obj in objlist: 
     168        for refobj in objdict[obj][0]: 
     169            print obj, refobj 
     170            assert refobj in done 
     171        done.add(obj) 
    172172 
    173173 
     
    213213 
    214214 
     215@withbitbucket 
     216def test_scripts_oramerge(): 
     217    # Test oramerge (not really: we will not get any differences) 
     218    args = "--color yes --verbose %s %s %s" % (dbname, dbname, dbname) 
     219    oramerge.main(args.split()) 
     220 
     221 
    215222def test_callprocedure(): 
    216223    db = orasql.connect(dbname) 
    217     proc = orasql.ProcedureDefinition("orasql_test") 
     224    proc = orasql.Procedure("orasql_test") 
    218225    result = proc(db.cursor(), p_in=42, p_inout=17) 
    219226    assert result.p_in == 42