Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

Changeset 416

Show
Ignore:
Timestamp:
02/16/07 23:59:01
Author:
fumanchu
Message:

Updates in sync with geniusql 11.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/storage/db.py

    r415 r416  
    6666         
    6767        name = allOptions.pop('name') 
    68         self.db = self.databaseclass(name, **allOptions) 
     68        self.db = self.databaseclass(**allOptions) 
     69        self.schema = self.db.schema(name) 
    6970         
    7071        def logger(msg): 
     
    9091            wt1 = self.tablejoin(t1) 
    9192        else: 
    92             wt1 = self.db[t1.__name__] 
     93            wt1 = self.schema[t1.__name__] 
    9394         
    9495        if isinstance(t2, dejavu.UnitJoin): 
    9596            wt2 = self.tablejoin(t2) 
    9697        else: 
    97             wt2 = self.db[t2.__name__] 
     98            wt2 = self.schema[t2.__name__] 
    9899         
    99100        uj = geniusql.Join(wt1, wt2, join.leftbiased) 
     
    108109            relation = self.tablejoin(classes) 
    109110        else: 
    110             relation = self.db[classes.__name__] 
     111            relation = self.schema[classes.__name__] 
    111112        return self.db.select(relation, attrs, restriction, distinct) 
    112113     
     
    169170         
    170171        # Grab the new ID. This is threadsafe because reserve has a mutex. 
    171         newids = self.db[cls.__name__].insert(**unit._properties) 
     172        newids = self.schema[cls.__name__].insert(**unit._properties) 
    172173        for k, v in newids.iteritems(): 
    173174            setattr(unit, k, v) 
     
    178179        """ 
    179180        cls = unit.__class__ 
    180         t = self.db[cls.__name__] 
     181        t = self.schema[cls.__name__] 
    181182        if not unit.sequencer.valid_id(unit.identity()): 
    182183            # Examine all existing IDs and grant the "next" one. 
     
    188189        """Update storage from unit's data (if unit.dirty()).""" 
    189190        if unit.dirty() or forceSave: 
    190             self.db[unit.__class__.__name__].save(**unit._properties) 
     191            self.schema[unit.__class__.__name__].save(**unit._properties) 
    191192            unit.cleanse() 
    192193     
    193194    def destroy(self, unit): 
    194195        """Delete the unit.""" 
    195         table = self.db[unit.__class__.__name__] 
     196        table = self.schema[unit.__class__.__name__] 
    196197        table.delete(**unit._properties) 
    197198     
     
    251252        props = [] 
    252253        for cls in classes: 
    253             t = self.db[cls.__name__] 
     254            t = self.schema[cls.__name__] 
    254255            attrs = [] 
    255256            for key in cls.properties: 
     
    325326     
    326327    def create_database(self): 
    327         self.db.create_database() 
     328        self.schema.create_database() 
    328329     
    329330    def drop_database(self): 
    330         self.db.drop_database() 
     331        self.schema.drop_database() 
    331332     
    332333    def _make_table(self, cls): 
    333334        """Create and return a Table object for the given class.""" 
    334         db = self.db 
    335         clsname = cls.__name__ 
    336          
    337         t = db.table(clsname) 
     335        t = self.schema.table(cls.__name__) 
    338336         
    339337        indices = cls.indices() 
     
    352350    def create_storage(self, cls): 
    353351        """Create storage for the given class.""" 
    354         # Attach to self.db, which should call CREATE TABLE. 
    355         self.db[cls.__name__] = self._make_table(cls) 
     352        # Attach to self.schema, which should call CREATE TABLE. 
     353        self.schema[cls.__name__] = self._make_table(cls) 
    356354     
    357355    def _make_column(self, cls, key): 
    358356        prop = getattr(cls, key) 
    359         col = self.db.column(prop.type, default=prop.default, hints=prop.hints) 
     357        col = self.schema.column(prop.type, default=prop.default, hints=prop.hints) 
    360358        if key in cls.identifiers: 
    361359            col.key = True 
     
    366364     
    367365    def has_storage(self, cls): 
    368         return cls.__name__ in self.db 
     366        return cls.__name__ in self.schema 
    369367     
    370368    def drop_storage(self, cls): 
    371         del self.db[cls.__name__] 
     369        del self.schema[cls.__name__] 
    372370     
    373371    def rename_storage(self, oldname, newname): 
    374         self.db.rename(oldname, newname) 
     372        self.schema.rename(oldname, newname) 
    375373     
    376374    def add_property(self, cls, name): 
    377375        if not self.has_property(cls, name): 
    378             table = self.db[cls.__name__] 
     376            table = self.schema[cls.__name__] 
    379377            table[name] = self._make_column(cls, name) 
    380378     
    381379    def has_property(self, cls, name): 
    382         return name in self.db[cls.__name__] 
     380        return name in self.schema[cls.__name__] 
    383381     
    384382    def drop_property(self, cls, name): 
    385383        if self.has_property(cls, name): 
    386             del self.db[cls.__name__][name] 
     384            del self.schema[cls.__name__][name] 
    387385     
    388386    def rename_property(self, cls, oldname, newname): 
    389         t = self.db[cls.__name__] 
    390          
    391         # Sometimes, a schema will change a code model first, and then 
    392         # change the database afterward. So it's possible that the 
     387        t = self.schema[cls.__name__] 
     388         
     389        # Sometimes, a Dejavu Schema will change a code model first, and 
     390        # then change the database afterward. So it's possible that the 
    393391        # column we're trying to rename hasn't been loaded, because the 
    394392        # model layer no longer references it. So if table[oldname] 
     
    398396            t[oldname] 
    399397        except KeyError: 
    400             c = [x for x in self.db._get_columns(t.name) 
    401                  if x.name == self.db._column_name(t.name, oldname)] 
     398            c = [x for x in self.schema._get_columns(t.name) 
     399                 if x.name == self.schema._column_name(t.name, oldname)] 
    402400            if not c: 
    403401                raise KeyError("Rename failed. Old column %r not found in %r." 
     
    414412     
    415413    def add_index(self, cls, name): 
    416         self.db[cls.__name__].add_index(name) 
     414        self.schema[cls.__name__].add_index(name) 
    417415     
    418416    def has_index(self, cls, name): 
    419         return name in self.db[cls.__name__].indices 
     417        return name in self.schema[cls.__name__].indices 
    420418     
    421419    def drop_index(self, cls, name): 
    422         del self.db[cls.__name__].indices[name] 
     420        del self.schema[cls.__name__].indices[name] 
    423421     
    424422    auto_discover = True 
     
    445443            for cls in classes: 
    446444                clsname = cls.__name__ 
    447                 if clsname in self.db
     445                if clsname in self.schema
    448446                    # If our consumer-side key is already present, skip this cls. 
    449447                    # This allows arena.storage() to auto-sync class by class 
     
    454452                 
    455453                # Use the superclass call to avoid DROP/CREATE TABLE 
    456                 dict.__setitem__(self.db, clsname, t) 
     454                dict.__setitem__(self.schema, clsname, t) 
    457455     
    458456    def sync(self, classes, warn=False): 
     
    468466            clsname = cls.__name__ 
    469467             
    470             if clsname in self.db
     468            if clsname in self.schema
    471469                # If our consumer-side key is already present, skip this cls. 
    472470                # This allows arena.storage() to auto-sync class by class 
     
    475473             
    476474            # Try to find a matching Table object using the DB-side key. 
    477             tablename = self.db.table_name(clsname) 
     475            tablename = self.schema.table_name(clsname) 
    478476            try: 
    479477                # Do we already have a map? 
    480                 table = self.db[tablename] 
     478                table = self.schema[tablename] 
    481479            except KeyError: 
    482480                # Can we create a map? Discover the DB table and try again. 
    483481                try: 
    484                     table = self.db.discover(tablename) 
     482                    table = self.schema.discover(tablename) 
    485483                except geniusql.errors.MappingError: 
    486484                    msg = "%s: no such table %r." % (clsname, tablename) 
     
    491489                        raise geniusql.errors.MappingError(msg) 
    492490             
    493             self.db.alias(table.name, clsname) 
     491            self.schema.alias(table.name, clsname) 
    494492             
    495493            # Match Column objects with class properties. 
     
    497495            indices = cls.indices() 
    498496            for ckey in cls.properties: 
    499                 colname = self.db._column_name(table.name, ckey) 
     497                colname = self.schema._column_name(table.name, ckey) 
    500498                try: 
    501499                    col = dbcols[colname] 
     
    520518                # so platform-specific, we match attributes rather than names. 
    521519                if ckey in indices: 
    522                     for idx in table.indices.itervalues(): 
     520                    for ikey, idx in table.indices.iteritems(): 
    523521                        if idx.colname == colname: 
    524                             a = self.db.table_name("i" + clsname + ckey) 
    525                             table.indices.alias(idx.name, a) 
     522                            a = self.schema.table_name("i" + clsname + ckey) 
     523                            table.indices.alias(ikey, a) 
    526524                            break 
    527525                    else: 
     
    559557              ] 
    560558     
    561     def __init__(self, db): 
    562         self.db = db 
     559    def __init__(self, schema): 
     560        self.schema = schema 
    563561        self.ignore = self.ignore[:] 
    564562     
    565563    def all_classes(self): 
    566564        """Return a list of new classes for all tables in the Database.""" 
    567         self.db.discover_all() 
     565        self.schema.discover_all() 
    568566         
    569567        classes = [] 
    570568         
    571         ignore = dict.fromkeys([self.db.table_name(x) for x in self.ignore] 
     569        ignore = dict.fromkeys([self.schema.table_name(x) for x in self.ignore] 
    572570                               + self.ignore).keys() 
    573571         
    574572        seen = {} 
    575         for key, table in self.db.items(): 
     573        for key, table in self.schema.items(): 
    576574            if key not in ignore and table.name not in seen: 
    577575                cls = self.make_class(key) 
     
    582580    def make_class(self, tablename, newclassname=None): 
    583581        """Create a Unit class automatically from the named table.""" 
    584         if tablename not in self.db
    585             self.db.discover(tablename) 
    586         table = self.db[tablename] 
     582        if tablename not in self.schema
     583            self.schema.discover(tablename) 
     584        table = self.schema[tablename] 
    587585         
    588586        class AutoUnitClass(dejavu.Unit): 
     
    593591            newclassname = table.name 
    594592            # The key is probably better than the table.name. Try it. 
    595             for key, t in self.db.iteritems(): 
     593            for key, t in self.schema.iteritems(): 
    596594                if t.name == newclassname: 
    597595                    newclassname = key 
     
    600598         
    601599        for cname, c in table.iteritems(): 
    602             ptype = self.db.python_type(c.dbtype) 
     600            ptype = self.schema.db.python_type(c.dbtype) 
    603601            if ptype == int and c.hints.get('bytes') in (1, '1'): 
    604602                # This is probably a bool 
     
    616614    def all_source(self): 
    617615        """Return a list of strings, of Unit source code for all tables.""" 
    618         self.db.discover_all() 
     616        self.schema.discover_all() 
    619617         
    620618        allcode = [] 
    621619         
    622         ignore = dict.fromkeys([self.db.table_name(x) for x in self.ignore] 
     620        ignore = dict.fromkeys([self.schema.table_name(x) for x in self.ignore] 
    623621                               + self.ignore).keys() 
    624622         
    625623        seen = {} 
    626         for key, table in self.db.items(): 
     624        for key, table in self.schema.items(): 
    627625            if key not in ignore and table.name not in seen: 
    628626                code = self.make_source(key) 
     
    633631    def make_source(self, tablename, newclassname=None): 
    634632        """Create source code for a Unit class from the named table.""" 
    635         if tablename not in self.db
    636             self.db.discover(tablename) 
    637         table = self.db[tablename] 
     633        if tablename not in self.schema
     634            self.schema.discover(tablename) 
     635        table = self.schema[tablename] 
    638636         
    639637        code = [] 
     
    642640            newclassname = table.name 
    643641            # The key is probably better than the table.name. Try it. 
    644             for key, t in self.db.iteritems(): 
     642            for key, t in self.schema.iteritems(): 
    645643                if t.name == newclassname: 
    646644                    newclassname = key 
     
    652650        sequencer = None 
    653651        for cname, c in table.iteritems(): 
    654             ptype = self.db.python_type(c.dbtype) 
     652            ptype = self.schema.db.python_type(c.dbtype) 
    655653            if ptype == int and c.hints.get('bytes') in (0, '0', 1, '1'): 
    656654                # This is probably a bool 
  • trunk/test/test_storemsaccess.py

    r415 r416  
    4646                 
    4747                for c, p in [('Exhibit', 'Acreage'), ('Zoo', 'Admission')]: 
    48                     dbtype = sm.db[c][p].dbtype 
     48                    dbtype = sm.schema[c][p].dbtype 
    4949                    if fta == "CurrencyAdapter": 
    5050                        if dbtype != "CURRENCY" and not dbtype.startswith("WCHAR"): 
  • trunk/test/zoo_fixture.py

    r415 r416  
    910910            return 
    911911         
    912         zootable = s.db['Zoo'] 
     912        zootable = s.schema['Zoo'] 
    913913        cols = zootable 
    914914        self.assertEqual(len(cols), 6) 
     
    13491349         
    13501350        # Clear out all mappings and re-discover 
    1351         dict.clear(s.db
    1352         s.db.discover_all() 
     1351        dict.clear(s.schema
     1352        s.schema.discover_all() 
    13531353         
    13541354        from dejavu.storage import db 
    1355         self.modeler = db.Modeler(s.db
     1355        self.modeler = db.Modeler(s.schema
    13561356     
    13571357    def test_make_classes(self): 
     
    13611361         
    13621362        for cls in (Zoo, Animal): 
    1363             tkey = self.modeler.db.table_name(cls.__name__) 
     1363            tkey = self.modeler.schema.table_name(cls.__name__) 
    13641364             
    13651365            uc = self.modeler.make_class(tkey, cls.__name__) 
     
    13761376             
    13771377            for pname in cls.properties: 
    1378                 cname = self.modeler.db._column_name(tkey, pname) 
     1378                cname = self.modeler.schema._column_name(tkey, pname) 
    13791379                copy = getattr(uc, cname) 
    13801380                orig = getattr(cls, pname) 
     
    14001400            return 
    14011401         
    1402         tkey = self.modeler.db.table_name('Exhibit') 
     1402        tkey = self.modeler.schema.table_name('Exhibit') 
    14031403        source = self.modeler.make_source(tkey, 'Exhibit') 
    14041404         
     
    14071407            self.fail("%r does not start with %r" % (source, classline)) 
    14081408         
    1409         clsname = self.modeler.db.__class__.__name__ 
     1409        clsname = self.modeler.schema.__class__.__name__ 
    14101410        if "SQLite" in clsname: 
    14111411            # SQLite's internal types are teh suck. 
     
    14221422             
    14231423            self.assertIn(source, "    ZooID = UnitProperty(int") 
    1424             if "Firebird" in self.modeler.db.__class__.__name__
     1424            if "Firebird" in clsname
    14251425                # Firebird doesn't have a bool datatype 
    14261426                self.assertIn(source, "    PettingAllowed = UnitProperty(int")