]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
(no commit message)
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 24 Sep 2005 03:04:09 +0000 (03:04 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 24 Sep 2005 03:04:09 +0000 (03:04 +0000)
lib/sqlalchemy/pool.py
lib/sqlalchemy/schema.py

index cf31ecd9bae54bb42231024bd2c27e01c428e2d3..d181f39c3023081496dd892c0087535135e79e48 100644 (file)
@@ -40,6 +40,8 @@ def manage(module, **params):
 
 def clear_managers():
     """removes all current DBAPI2 managers.  all pools and connections are disposed."""
+    for manager in proxies.values():
+        manager.close()
     proxies.clear()
 
     
@@ -154,6 +156,13 @@ class DBProxy:
         self.poolclass = poolclass
         self.pools = {}
 
+    def close(self):
+        for key in self.pools.keys():
+            del self.pools[key]
+
+    def __del__(self):
+        self.close()
+            
     def get_pool(self, *args, **params):
         key = self._serialize(*args, **params)
         try:
index ebd660828db575f8a0f06b9f85970c29f4e4c7d7..59042eeb82565a541733d441f0a2f50b024e877d 100644 (file)
@@ -61,7 +61,7 @@ class TableSingleton(type):
         except KeyError:
             if kwargs.get('mustexist', False):
                 raise "Table '%s' not defined" % name
-            table = type.__call__(self, name, engine, *args, **kwargs)
+            table = type.__call__(self, name, engine, *args)
             engine.tables[name] = table
             # load column definitions from the database if 'autoload' is defined
             # we do it after the table is in the singleton dictionary to support
@@ -76,7 +76,7 @@ class Table(SchemaItem):
     """represents a relational database table."""
     __metaclass__ = TableSingleton
     
-    def __init__(self, name, engine, *args, **kwargs):
+    def __init__(self, name, engine, *args):
         self.name = name
         self.columns = OrderedProperties()
         self.c = self.columns
@@ -96,7 +96,7 @@ class Table(SchemaItem):
 
     def append_item(self, item):
         self._init_items(item)
-
+        
     def _set_parent(self, schema):
         schema.tables[self.name] = self
         self.schema = schema
@@ -106,42 +106,51 @@ class Table(SchemaItem):
             c.accept_visitor(visitor)
         return visitor.visit_table(self)
 
+    def toengine(self, engine):
+        """returns a singleton instance of this Table with a different engine"""
+        try:
+            return engine.tables[self.name]
+        except:
+            for c in self.columns:
+                args.append(c.copy())
+            return Table(self.name, engine, *args)
+
 class Column(SchemaItem):
     """represents a column in a database table."""
-    def __init__(self, name, type, key = None, primary_key = False, foreign_key = None, sequence = None, nullable = True):
+    def __init__(self, name, type, *args, **kwargs):
         self.name = name
         self.type = type
-        self.sequence = sequence
-        self.foreign_key = foreign_key
-        self.key = key or name
-        self.primary_key = primary_key
-        if primary_key:
-            nullable = False
-        self.nullable = nullable
+        self.args = args
+        self.key = kwargs.get('key', name)
+        self.primary_key = kwargs.get('primary_key', False)
+        self.nullable = kwargs.get('nullable', not self.primary_key)
+        self.foreign_key = None
+        self.sequence = None
         self._orig = None
         
     original = property(lambda s: s._orig or s)
+    engine = property(lambda s: s.table.engine)
     
+        
     def _set_parent(self, table):
         table.columns[self.key] = self
         if self.primary_key:
             table.primary_keys.append(self)
         self.table = table
-        self.engine = table.engine
-        self.type = self.engine.type_descriptor(self.type)
-        self._impl = self.engine.columnimpl(self)
+        if self.table.engine is not None:
+            self.type = self.table.engine.type_descriptor(self.type)
+            
+        self._impl = self.table.engine.columnimpl(self)
+
+        self._init_items(*self.args)
+        self.args = None
+
+    def copy(self):
+        """creates a copy of this Column, unitialized"""
+        return Column(self.name, self.type, key = self.key, primary_key = self.primary_key, foreign_key = self.foreign_key.copy(), sequence = self.sequence)
         
-        if self.foreign_key is not None:
-            self._init_items(self.foreign_key)
-#            table.foreign_keys[self.foreign_key.column.key] = self.foreign_key
-
-    def set_foreign_key(self, fk):
-        self.foreign_key = fk
-        self._init_items(self.foreign_key)
-#        self.table.foreign_keys[self.foreign_key.column.key] = self.foreign_key
-    
     def _make_proxy(self, selectable, name = None):
-        """creates a copy of this Column for use in a new selectable unit"""
+        """creates a copy of this Column, initialized the way this Column is"""
         # using copy.copy(c) seems to add a full second to the select.py unittest package
         #c = copy.copy(self)
         #if name is not None:
@@ -150,7 +159,6 @@ class Column(SchemaItem):
         # TODO: do we want the same foreign_key object here ?  
         c = Column(name or self.name, self.type, key = name or self.key, primary_key = self.primary_key, foreign_key = self.foreign_key, sequence = self.sequence)
         c.table = selectable
-        c.engine = self.engine
         c._orig = self.original
         selectable.columns[c.key] = c
         c._impl = self.engine.columnimpl(c)
@@ -172,6 +180,12 @@ class ForeignKey(SchemaItem):
         self._colspec = column
         self._column = None
 
+    def copy(self):
+        if isinstance(self._colspec, str):
+            return ForeignKey(self._colspec)
+        else:
+            return ForeignKey("%s.%s" % (self._colspec.table.name, self._colspec.column.key))
+        
     def _init_column(self):
         # ForeignKey inits its remote column as late as possible, so tables can
         # be defined without dependencies
@@ -198,6 +212,7 @@ class ForeignKey(SchemaItem):
     
     def _set_parent(self, column):
         self.parent = column
+        self.parent.foreign_key = self
         
 class Sequence(SchemaItem):
     """represents a sequence, which applies to Oracle and Postgres databases."""
@@ -225,4 +240,5 @@ class SchemaVisitor(object):
         def visit_index(self, index):pass
         def visit_sequence(self, sequence):pass
 
-        
+            
+            
\ No newline at end of file