]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- added 'object_session' as classlevel method to Session
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 9 Aug 2007 19:51:36 +0000 (19:51 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 9 Aug 2007 19:51:36 +0000 (19:51 +0000)
- moved 'identity_key' to be a classmethod on Session
- some docstrings
- merged r3229 from 0.3 branch to unconditonally quote schemaname in PG-reflected default
- name fixes in dynamic unit test

CHANGES
doc/build/content/session.txt
lib/sqlalchemy/databases/postgres.py
lib/sqlalchemy/orm/scoping.py
lib/sqlalchemy/orm/session.py
test/orm/dynamic.py

diff --git a/CHANGES b/CHANGES
index 700db631e9ff3cf2607c23dcd3c5e30b2db12ef3..d1d55cd44915beee142f00c0a3c2bbb6edba5533 100644 (file)
--- a/CHANGES
+++ b/CHANGES
 - mssql
     - added support for TIME columns (simulated using DATETIME) [ticket:679]
     - index names are now quoted when dropping from reflected tables [ticket:684]
+- postgres
+    - when reflecting tables from alternate schemas, the "default" placed upon
+      the primary key, i.e. usually a sequence name, has the "schema" name
+      unconditionally quoted, so that schema names which need quoting are fine.
+      its slightly unnecessary for schema names which don't need quoting
+      but not harmful.
       
 0.3.10
 - general
index 7fb998ab994220ae5934da369a4b35b429fe19ab..938815c4ced00d3676474f83e9f187190e8d9e5c 100644 (file)
@@ -1,4 +1,4 @@
-Using the Session {@name=unitofwork}
+    Using the Session {@name=unitofwork}
 ============
 
 The [Mapper](rel:advdatamapping) is the entrypoint to the configurational API of the SQLAlchemy object relational mapper.  But the primary object one works with when using the ORM is the [Session](rel:docstrings_sqlalchemy.orm.session_Session).
@@ -156,9 +156,10 @@ Knowing these states is important, since the `Session` tries to be strict about
 
 * How can I get the `Session` for a certain object ?
 
+    Use the `object_session()` classmethod available on `Session`:
+    
         {python}
-        from sqlalchemy.orm import object_session
-        session = object_session(someobject)
+        session = Session.object_session(someobject)
 
 * Is the session threadsafe ?
 
index d3467105b379911f9f7a0e8cc8ad47286d4f2cd2..8f04eca26c3372e9e3db1b13da8a12a3f1aa2b0f 100644 (file)
@@ -447,7 +447,9 @@ class PGDialect(ansisql.ANSIDialect):
                     # the default is related to a Sequence
                     sch = table.schema
                     if '.' not in match.group(2) and sch is not None:
-                        default = match.group(1) + sch + '.' + match.group(2) + match.group(3)
+                        # unconditionally quote the schema name.  this could
+                        # later be enhanced to obey quoting rules / "quote schema"
+                        default = match.group(1) + ('"%s"' % sch) + '.' + match.group(2) + match.group(3)
                 colargs.append(schema.PassiveDefault(sql.text(default)))
             table.append_column(schema.Column(name, coltype, nullable=nullable, *colargs))
 
index fc2fba87bec314e93f2e527fb07842542c81e4f4..86050e9756a7a39704ed003a44394783948ca979 100644 (file)
@@ -81,7 +81,7 @@ def clslevel(name):
     def do(cls, *args,**kwargs):
         return getattr(Session, name)(*args, **kwargs)
     return classmethod(do)
-for prop in ('close_all',):
+for prop in ('close_all','object_session', 'identity_key'):
     setattr(ScopedSession, prop, clslevel(prop))
     
 class _ScopedExt(MapperExtension):
index e7268dd98682689afa230da792aa79adccb60c1f..ab12d4c14e1f2a9cac44eeb1c6a644ef76a43bd0 100644 (file)
@@ -502,12 +502,6 @@ class Session(object):
         self.uow = unitofwork.UnitOfWork(weak_identity_map=self.weak_identity_map)
         self.uow.echo = echo
 
-    def mapper(self, class_, entity_name=None):
-        """Given a ``Class``, return the primary ``Mapper`` responsible for
-        persisting it."""
-
-        return _class_mapper(class_, entity_name = entity_name)
-
     def bind_mapper(self, mapper, bind, entity_name=None):
         """Bind the given `mapper` or `class` to the given ``Engine`` or ``Connection``.
 
@@ -801,7 +795,7 @@ class Session(object):
         finally:
             _recursive.remove(mapper)
             
-    def identity_key(self, *args, **kwargs):
+    def identity_key(cls, *args, **kwargs):
         """Get an identity key.
 
         Valid call signatures:
@@ -863,7 +857,14 @@ class Session(object):
                 % ", ".join(kwargs.keys()))
         mapper = _object_mapper(instance)
         return mapper.identity_key_from_instance(instance)
-
+    identity_key = classmethod(identity_key)
+    
+    def object_session(cls, obj):
+        """return the ``Session`` to which the given object belongs."""
+        
+        return object_session(obj)
+    object_session = classmethod(object_session)
+    
     def _save_impl(self, object, **kwargs):
         if hasattr(object, '_instance_key'):
             if not self.identity_map.has_key(object._instance_key):
@@ -942,15 +943,25 @@ class Session(object):
         return getattr(obj, '_sa_session_id', None) == self.hash_key
 
     def __contains__(self, obj):
+        """return True if the given object is associated with this session.
+        
+        The instance may be pending or persistent within the Session for a
+        result of True.
+        """
+        
         return self._is_attached(obj) and (obj in self.uow.new or self.identity_map.has_key(obj._instance_key))
 
     def __iter__(self):
+        """return an iterator of all objects which are pending or persistent within this Session."""
+        
         return iter(list(self.uow.new) + self.uow.identity_map.values())
 
     def _get(self, key):
         return self.identity_map[key]
 
     def has_key(self, key):
+        """return True if the given identity key is present within this Session's identity map."""
+        
         return self.identity_map.has_key(key)
 
     dirty = property(lambda s:s.uow.locate_dirty(),
index ac676465309bbec14932c6f051bdfdf441d41d14..3cca2f7f1c9e501324c6265280a06913f9ea97d9 100644 (file)
@@ -144,8 +144,8 @@ def create_backref_test(autoflush, saveuser):
             sess.flush()
         self.assert_(list(u.addresses) == [])
 
-    test_backref.__name__ = "test_%s%s" % (
-        (autoflush and "autoflush" or ""),
+    test_backref.__name__ = "test%s%s" % (
+        (autoflush and "_autoflush" or ""),
         (saveuser and "_saveuser" or "_savead"),
     )
     setattr(FlushTest, test_backref.__name__, test_backref)