Changeset 416
- Timestamp:
- 02/16/07 23:59:01
- Files:
-
- trunk/storage/db.py (modified) (27 diffs)
- trunk/test/test_storemsaccess.py (modified) (1 diff)
- trunk/test/zoo_fixture.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/storage/db.py
r415 r416 66 66 67 67 name = allOptions.pop('name') 68 self.db = self.databaseclass(name, **allOptions) 68 self.db = self.databaseclass(**allOptions) 69 self.schema = self.db.schema(name) 69 70 70 71 def logger(msg): … … 90 91 wt1 = self.tablejoin(t1) 91 92 else: 92 wt1 = self. db[t1.__name__]93 wt1 = self.schema[t1.__name__] 93 94 94 95 if isinstance(t2, dejavu.UnitJoin): 95 96 wt2 = self.tablejoin(t2) 96 97 else: 97 wt2 = self. db[t2.__name__]98 wt2 = self.schema[t2.__name__] 98 99 99 100 uj = geniusql.Join(wt1, wt2, join.leftbiased) … … 108 109 relation = self.tablejoin(classes) 109 110 else: 110 relation = self. db[classes.__name__]111 relation = self.schema[classes.__name__] 111 112 return self.db.select(relation, attrs, restriction, distinct) 112 113 … … 169 170 170 171 # 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) 172 173 for k, v in newids.iteritems(): 173 174 setattr(unit, k, v) … … 178 179 """ 179 180 cls = unit.__class__ 180 t = self. db[cls.__name__]181 t = self.schema[cls.__name__] 181 182 if not unit.sequencer.valid_id(unit.identity()): 182 183 # Examine all existing IDs and grant the "next" one. … … 188 189 """Update storage from unit's data (if unit.dirty()).""" 189 190 if unit.dirty() or forceSave: 190 self. db[unit.__class__.__name__].save(**unit._properties)191 self.schema[unit.__class__.__name__].save(**unit._properties) 191 192 unit.cleanse() 192 193 193 194 def destroy(self, unit): 194 195 """Delete the unit.""" 195 table = self. db[unit.__class__.__name__]196 table = self.schema[unit.__class__.__name__] 196 197 table.delete(**unit._properties) 197 198 … … 251 252 props = [] 252 253 for cls in classes: 253 t = self. db[cls.__name__]254 t = self.schema[cls.__name__] 254 255 attrs = [] 255 256 for key in cls.properties: … … 325 326 326 327 def create_database(self): 327 self. db.create_database()328 self.schema.create_database() 328 329 329 330 def drop_database(self): 330 self. db.drop_database()331 self.schema.drop_database() 331 332 332 333 def _make_table(self, cls): 333 334 """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__) 338 336 339 337 indices = cls.indices() … … 352 350 def create_storage(self, cls): 353 351 """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) 356 354 357 355 def _make_column(self, cls, key): 358 356 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) 360 358 if key in cls.identifiers: 361 359 col.key = True … … 366 364 367 365 def has_storage(self, cls): 368 return cls.__name__ in self. db366 return cls.__name__ in self.schema 369 367 370 368 def drop_storage(self, cls): 371 del self. db[cls.__name__]369 del self.schema[cls.__name__] 372 370 373 371 def rename_storage(self, oldname, newname): 374 self. db.rename(oldname, newname)372 self.schema.rename(oldname, newname) 375 373 376 374 def add_property(self, cls, name): 377 375 if not self.has_property(cls, name): 378 table = self. db[cls.__name__]376 table = self.schema[cls.__name__] 379 377 table[name] = self._make_column(cls, name) 380 378 381 379 def has_property(self, cls, name): 382 return name in self. db[cls.__name__]380 return name in self.schema[cls.__name__] 383 381 384 382 def drop_property(self, cls, name): 385 383 if self.has_property(cls, name): 386 del self. db[cls.__name__][name]384 del self.schema[cls.__name__][name] 387 385 388 386 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 then392 # change the database afterward. So it's possible that the387 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 393 391 # column we're trying to rename hasn't been loaded, because the 394 392 # model layer no longer references it. So if table[oldname] … … 398 396 t[oldname] 399 397 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)] 402 400 if not c: 403 401 raise KeyError("Rename failed. Old column %r not found in %r." … … 414 412 415 413 def add_index(self, cls, name): 416 self. db[cls.__name__].add_index(name)414 self.schema[cls.__name__].add_index(name) 417 415 418 416 def has_index(self, cls, name): 419 return name in self. db[cls.__name__].indices417 return name in self.schema[cls.__name__].indices 420 418 421 419 def drop_index(self, cls, name): 422 del self. db[cls.__name__].indices[name]420 del self.schema[cls.__name__].indices[name] 423 421 424 422 auto_discover = True … … 445 443 for cls in classes: 446 444 clsname = cls.__name__ 447 if clsname in self. db:445 if clsname in self.schema: 448 446 # If our consumer-side key is already present, skip this cls. 449 447 # This allows arena.storage() to auto-sync class by class … … 454 452 455 453 # Use the superclass call to avoid DROP/CREATE TABLE 456 dict.__setitem__(self. db, clsname, t)454 dict.__setitem__(self.schema, clsname, t) 457 455 458 456 def sync(self, classes, warn=False): … … 468 466 clsname = cls.__name__ 469 467 470 if clsname in self. db:468 if clsname in self.schema: 471 469 # If our consumer-side key is already present, skip this cls. 472 470 # This allows arena.storage() to auto-sync class by class … … 475 473 476 474 # 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) 478 476 try: 479 477 # Do we already have a map? 480 table = self. db[tablename]478 table = self.schema[tablename] 481 479 except KeyError: 482 480 # Can we create a map? Discover the DB table and try again. 483 481 try: 484 table = self. db.discover(tablename)482 table = self.schema.discover(tablename) 485 483 except geniusql.errors.MappingError: 486 484 msg = "%s: no such table %r." % (clsname, tablename) … … 491 489 raise geniusql.errors.MappingError(msg) 492 490 493 self. db.alias(table.name, clsname)491 self.schema.alias(table.name, clsname) 494 492 495 493 # Match Column objects with class properties. … … 497 495 indices = cls.indices() 498 496 for ckey in cls.properties: 499 colname = self. db._column_name(table.name, ckey)497 colname = self.schema._column_name(table.name, ckey) 500 498 try: 501 499 col = dbcols[colname] … … 520 518 # so platform-specific, we match attributes rather than names. 521 519 if ckey in indices: 522 for i dx in table.indices.itervalues():520 for ikey, idx in table.indices.iteritems(): 523 521 if idx.colname == colname: 524 a = self. db.table_name("i" + clsname + ckey)525 table.indices.alias(i dx.name, a)522 a = self.schema.table_name("i" + clsname + ckey) 523 table.indices.alias(ikey, a) 526 524 break 527 525 else: … … 559 557 ] 560 558 561 def __init__(self, db):562 self. db = db559 def __init__(self, schema): 560 self.schema = schema 563 561 self.ignore = self.ignore[:] 564 562 565 563 def all_classes(self): 566 564 """Return a list of new classes for all tables in the Database.""" 567 self. db.discover_all()565 self.schema.discover_all() 568 566 569 567 classes = [] 570 568 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] 572 570 + self.ignore).keys() 573 571 574 572 seen = {} 575 for key, table in self. db.items():573 for key, table in self.schema.items(): 576 574 if key not in ignore and table.name not in seen: 577 575 cls = self.make_class(key) … … 582 580 def make_class(self, tablename, newclassname=None): 583 581 """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] 587 585 588 586 class AutoUnitClass(dejavu.Unit): … … 593 591 newclassname = table.name 594 592 # 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(): 596 594 if t.name == newclassname: 597 595 newclassname = key … … 600 598 601 599 for cname, c in table.iteritems(): 602 ptype = self. db.python_type(c.dbtype)600 ptype = self.schema.db.python_type(c.dbtype) 603 601 if ptype == int and c.hints.get('bytes') in (1, '1'): 604 602 # This is probably a bool … … 616 614 def all_source(self): 617 615 """Return a list of strings, of Unit source code for all tables.""" 618 self. db.discover_all()616 self.schema.discover_all() 619 617 620 618 allcode = [] 621 619 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] 623 621 + self.ignore).keys() 624 622 625 623 seen = {} 626 for key, table in self. db.items():624 for key, table in self.schema.items(): 627 625 if key not in ignore and table.name not in seen: 628 626 code = self.make_source(key) … … 633 631 def make_source(self, tablename, newclassname=None): 634 632 """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] 638 636 639 637 code = [] … … 642 640 newclassname = table.name 643 641 # 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(): 645 643 if t.name == newclassname: 646 644 newclassname = key … … 652 650 sequencer = None 653 651 for cname, c in table.iteritems(): 654 ptype = self. db.python_type(c.dbtype)652 ptype = self.schema.db.python_type(c.dbtype) 655 653 if ptype == int and c.hints.get('bytes') in (0, '0', 1, '1'): 656 654 # This is probably a bool trunk/test/test_storemsaccess.py
r415 r416 46 46 47 47 for c, p in [('Exhibit', 'Acreage'), ('Zoo', 'Admission')]: 48 dbtype = sm. db[c][p].dbtype48 dbtype = sm.schema[c][p].dbtype 49 49 if fta == "CurrencyAdapter": 50 50 if dbtype != "CURRENCY" and not dbtype.startswith("WCHAR"): trunk/test/zoo_fixture.py
r415 r416 910 910 return 911 911 912 zootable = s. db['Zoo']912 zootable = s.schema['Zoo'] 913 913 cols = zootable 914 914 self.assertEqual(len(cols), 6) … … 1349 1349 1350 1350 # 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() 1353 1353 1354 1354 from dejavu.storage import db 1355 self.modeler = db.Modeler(s. db)1355 self.modeler = db.Modeler(s.schema) 1356 1356 1357 1357 def test_make_classes(self): … … 1361 1361 1362 1362 for cls in (Zoo, Animal): 1363 tkey = self.modeler. db.table_name(cls.__name__)1363 tkey = self.modeler.schema.table_name(cls.__name__) 1364 1364 1365 1365 uc = self.modeler.make_class(tkey, cls.__name__) … … 1376 1376 1377 1377 for pname in cls.properties: 1378 cname = self.modeler. db._column_name(tkey, pname)1378 cname = self.modeler.schema._column_name(tkey, pname) 1379 1379 copy = getattr(uc, cname) 1380 1380 orig = getattr(cls, pname) … … 1400 1400 return 1401 1401 1402 tkey = self.modeler. db.table_name('Exhibit')1402 tkey = self.modeler.schema.table_name('Exhibit') 1403 1403 source = self.modeler.make_source(tkey, 'Exhibit') 1404 1404 … … 1407 1407 self.fail("%r does not start with %r" % (source, classline)) 1408 1408 1409 clsname = self.modeler. db.__class__.__name__1409 clsname = self.modeler.schema.__class__.__name__ 1410 1410 if "SQLite" in clsname: 1411 1411 # SQLite's internal types are teh suck. … … 1422 1422 1423 1423 self.assertIn(source, " ZooID = UnitProperty(int") 1424 if "Firebird" in self.modeler.db.__class__.__name__:1424 if "Firebird" in clsname: 1425 1425 # Firebird doesn't have a bool datatype 1426 1426 self.assertIn(source, " PettingAllowed = UnitProperty(int")
