Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

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

Changeset 373

Show
Ignore:
Timestamp:
01/01/07 01:10:14
Author:
fumanchu
Message:

Doc updates for new auto* methods.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/doc/index.html

    r372 r373  
    6464        </ul> 
    6565    </li> 
     66    <li>Automatic Unit Classes</li> 
    6667    </ul> 
    6768</li> 
  • trunk/doc/modeling.html

    r372 r373  
    869869<h3>Automatic Unit Classes</h3> 
    870870<p>When you create your first Dejavu model, you might be forming it to 
    871 match some existing database schema. If so, Dejavu 1.5 has two new tools 
     871match some existing database schema. If so, Dejavu 1.5 has three new tools 
    872872to help you (all database Storage Managers have these).</p> 
     873 
     874<p>The <tt class='def'>autotable(tablename)</tt> method finds a database 
     875table of the given name and returns a Table object which you can then 
     876inspect on your own, or pass to <tt>autoclass</tt> or <tt>autosource</tt> 
     877(see next).</p> 
    873878 
    874879<p>The <tt class='def'>autoclass(table, newclassname=None)</tt> method 
    875880takes an existing Table <i>object</i> (not just the name) and returns 
    876 a subclass of Unit which models that table: 
     881a subclass of Unit which models that table. By default, the new class 
     882will have the same name as the database table; supply the 
     883'newclassname' argument to use a different name (for example, 
     884to capitalize the class name). 
    877885</p> 
    878886 
    879887<pre> 
    880 sm = arena.add_store('legacydb', 'mysql', 
    881                      {"host": "localhost", "db": "existing_db", 
    882                       "user": "root", "passwd": "xxxx", 
    883                       }) 
    884 dbtables = sm.db._get_tables() 
    885 t = sm.db[""] 
    886             uc = s.autoclass(t, cls.__name__) 
    887             self.assert_(not issubclass(uc, cls)) 
    888             self.assertEqual(uc.__name__, cls.__name__) 
    889             for pname in uc.properties: 
    890                 copy = getattr(uc, pname) 
    891                 orig = getattr(cls, pname) 
    892                 self.assertEqual(copy.key, orig.key) 
    893                 # self.assertEqual(copy.type, orig.type) 
    894                 self.assertEqual(copy.default, orig.default, 
    895                                  "%s.%s default %s != copy %s" 
    896                                  % (cls.__name__, pname, 
    897                                     `orig.default`, `copy.default`)) 
    898                  
    899                 for k, v in orig.hints.iteritems(): 
    900                     if isinstance(v, (int, long)): 
    901                         v2 = copy.hints.get(k) 
    902                         if v2 != 0 and v2 < v: 
    903                             self.fail("%s.%s hint[%s] %s not >= %s" % 
    904                                       (cls.__name__, pname, k, v2, v)) 
    905                     else: 
    906                         self.assertEqual(copy.hints[k], v) 
    907  
     888>>> sm = arena.add_store('legacydb', 'mysql', 
     889                         {"host": "localhost", "db": "existing_db", 
     890                          "user": "root", "passwd": "xxxx", 
     891                          }) 
     892>>> t = sm.autotable("zoo") 
     893>>> Zoo = s.autoclass(t, "Zoo") 
     894>>> Zoo 
     895&lt;class 'dejavu.storage.db.Zoo'> 
     896>>> Zoo.properties 
     897['id', 'name', 'admission', 'founded', 'lastescape', 'opens'] 
    908898</pre> 
    909899 
    910900<p>The <tt class='def'>autosource(table, newclassname=None)</tt> does 
    911901the same thing as <tt>autoclass</tt>, but returns a string that contains 
    912 valid Python code to generate the requested Unit class.</p> 
     902valid Python code to generate the requested Unit class:</p> 
     903 
     904<pre> 
     905>>> t = sm.autotable("exhibit") 
     906>>> print s.autosource(t, "Exhibit") 
     907class Exhibit(Unit): 
     908    pettingallowed = UnitProperty(bool) 
     909    animals = UnitProperty(str) 
     910    name = UnitProperty(str) 
     911    zooid = UnitProperty(int) 
     912    acreage = UnitProperty(decimal.Decimal) 
     913    creators = UnitProperty(str) 
     914    # Remove the default 'ID' property. 
     915    ID = None 
     916    identifiers = ('name', 'zooid') 
     917    sequencer = UnitSequencer() 
     918</pre> 
    913919 
    914920<hr />