From: Mike Bayer Date: Sat, 27 May 2006 16:59:07 +0000 (+0000) Subject: doc updates, added 'save' method to assignmapper X-Git-Tag: rel_0_2_0~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=acd4981f56a7ad6b3d6f1b6a3e1439027ff1abe9;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git doc updates, added 'save' method to assignmapper --- diff --git a/doc/build/content/plugins.txt b/doc/build/content/plugins.txt index 1502d4c013..cb1707758a 100644 --- a/doc/build/content/plugins.txt +++ b/doc/build/content/plugins.txt @@ -84,7 +84,7 @@ The `objectstore` is an instance of `SessionContext`, available in the `sqlalche #### Attaching Mappers to their Class {@name=attaching} -With `get_session()` handling the details of providing a `Session` in all cases, the `assign_mapper` function provides some of the functionality of `Query` and `Session` directly off the mapped instances themselves. This is a "monkeypatch" function that creates a primary mapper, attaches the mapper to the class, and also the methods `get, get_by, select, select_by, selectone, selectfirst, commit, expire, refresh, expunge` and `delete`: +With `get_session()` handling the details of providing a `Session` in all cases, the `assign_mapper` function provides some of the functionality of `Query` and `Session` directly off the mapped instances themselves. This is a "monkeypatch" function that creates a primary mapper, attaches the mapper to the class, and also the methods `get`, `select`, `select_by`, `selectone`, `get_by`, `join_to`, `join_via`, `flush`, `delete`, `expire`, `refresh`, `expunge`, `merge`, `update`, `save`, and `save_or_update`: {python} # "assign" a mapper to the User class/users table @@ -101,6 +101,8 @@ With `get_session()` handling the details of providing a `Session` in all cases, # flush the changes on a specific object myotheruser.flush() +A more generic version of `assign_mapper` that works with any `SessionContext` is available in the [plugins_assignmapper](rel:plugins_assignmapper) plugin. + #### Engine Strategy Set to threadlocal By Default {@name=engine} The `threadlocal` mod also establishes `threadlocal` as the default *strategy* when calling the `create_engine()` function. This strategy is specified by the `strategy` keyword argument to `create_engine()` and can still be overridden to be "`plain`" or "`threadlocal`" explicitly. @@ -181,7 +183,24 @@ The construction of each `Session` instance can be customized by providing a "cr # get the session corresponding to "scope2", bound to engine "someengine": session = ctx.current +### assignmapper + +**Author:** Mike Bayer + +This is a generic version of the `assign_mapper` function present in the [plugins_threadlocal](rel:plugins_threadlocal) mod. It requires an explicit [plugins_sessioncontext](rel:plugins_sessioncontext). + {python} + import sqlalchemy + from sqlalchemy.ext.sessioncontext import SessionContext + from sqlalchemy.ext.assignmapper import assign_mapper + + # session context + ctx = SessionContext(sqlalchemy.create_session) + + # assign mapper to class MyClass using table 'sometable', getting + # Sessions from 'ctx'. + assign_mapper(ctx, MyClass, sometable) + ### ActiveMapper **Author:** Jonathan LaCour @@ -243,11 +262,11 @@ More discussion on ActiveMapper can be found at [Jonathan LaCour's Blog](http:// **Author:** Jonathan Ellis -SqlSoup creates mapped classes on the fly from tables. It is essentially a nicer version of the "row data gateway" pattern. +SqlSoup creates mapped classes on the fly from tables, which are automatically reflected from the database based on name. It is essentially a nicer version of the "row data gateway" pattern. {python} >>> from sqlalchemy.ext.sqlsoup import SqlSoup - >>> soup = SqlSoup('sqlite://filename=:memory:') + >>> soup = SqlSoup('sqlite:///') >>> users = soup.users.select() >>> users.sort() diff --git a/doc/build/content/unitofwork.txt b/doc/build/content/unitofwork.txt index 0e0a534e2e..3bc1c4e967 100644 --- a/doc/build/content/unitofwork.txt +++ b/doc/build/content/unitofwork.txt @@ -216,7 +216,7 @@ It also can be called with a list of objects; in this form, the flush operation {python} # saves only user1 and address2. all other modified # objects remain present in the session. - session.flush(user1, address2) + session.flush([user1, address2]) This second form of flush should be used carefully as it will not necessarily locate other dependent objects within the session, whose database representation may have foreign constraint relationships with the objects being operated upon. diff --git a/lib/sqlalchemy/ext/assignmapper.py b/lib/sqlalchemy/ext/assignmapper.py index 07ba95a694..f7b093e9de 100644 --- a/lib/sqlalchemy/ext/assignmapper.py +++ b/lib/sqlalchemy/ext/assignmapper.py @@ -33,5 +33,5 @@ def assign_mapper(ctx, class_, *args, **kwargs): class_.mapper = m for name in ['get', 'select', 'select_by', 'selectone', 'get_by', 'join_to', 'join_via']: monkeypatch_query_method(ctx, class_, name) - for name in ['flush', 'delete', 'expire', 'refresh', 'expunge', 'merge', 'update', 'save_or_update']: + for name in ['flush', 'delete', 'expire', 'refresh', 'expunge', 'merge', 'save', 'update', 'save_or_update']: monkeypatch_objectstore_method(ctx, class_, name)