summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2005-12-30 05:58:45 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2005-12-30 05:58:45 +0000
commit5ceef4809d2eeb5030eb25668064b0a4a6262eba (patch)
tree1e0053ff3ab865d7884324d81caa450d443cc96b /lib/sqlalchemy/sql.py
parent24bb21e8db0470443378057f2a1444de498c1e56 (diff)
downloadsqlalchemy-5ceef4809d2eeb5030eb25668064b0a4a6262eba.tar.gz
changes related to mapping against arbitrary selects, selects with labels or functions:
testfunction has a more complete test (needs an assert tho); added new labels, synonymous with column key, to "select" statements that are subqueries with use_labels=False, since SQLite wants them - this also impacts the names of the columns attached to the select object in the case that the key and name dont match, since it is now the key, not the name; aliases generate random names if name is None (need some way to make them more predictable to help plan caching); select statements have a rowid column of None, since there isnt really a "rowid"...at least cant figure out what it would be yet; mapper creates an alias if given a select to map against, since Postgres wants it; mapper checks if it has pks for a given table before saving/deleting, skips it otherwise; mapper will not try to order by rowid if table doesnt have a rowid (since select statements dont have rowids...)
Diffstat (limited to 'lib/sqlalchemy/sql.py')
-rw-r--r--lib/sqlalchemy/sql.py28
1 files changed, 19 insertions, 9 deletions
diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py
index b0e86259a..7db60ffb9 100644
--- a/lib/sqlalchemy/sql.py
+++ b/lib/sqlalchemy/sql.py
@@ -20,7 +20,7 @@
import sqlalchemy.schema as schema
import sqlalchemy.util as util
import sqlalchemy.types as types
-import string, re
+import string, re, random
__all__ = ['text', 'column', 'func', 'select', 'update', 'insert', 'delete', 'join', 'and_', 'or_', 'not_', 'union', 'union_all', 'desc', 'asc', 'outerjoin', 'alias', 'subquery', 'literal', 'bindparam', 'exists']
@@ -497,7 +497,7 @@ class FromClause(Selectable):
return Join(self, right, *args, **kwargs)
def outerjoin(self, right, *args, **kwargs):
return Join(self, right, isouter = True, *args, **kwargs)
- def alias(self, name):
+ def alias(self, name=None):
return Alias(self, name)
@@ -751,11 +751,17 @@ class Alias(FromClause):
self._columns = util.OrderedProperties()
self.foreign_keys = []
if alias is None:
- alias = id(self)
+ n = getattr(selectable, 'name')
+ if n is None:
+ n = 'anon'
+ alias = n + "_" + hex(random.randint(0, 65535))[2:]
self.name = alias
self.id = self.name
self.count = 0
- self.rowid_column = self.selectable.rowid_column._make_proxy(self)
+ if self.selectable.rowid_column is not None:
+ self.rowid_column = self.selectable.rowid_column._make_proxy(self)
+ else:
+ self.rowid_column = None
for co in selectable.columns:
co._make_proxy(self)
@@ -930,7 +936,7 @@ class TableImpl(FromClause):
return Join(self.table, right, *args, **kwargs)
def outerjoin(self, right, *args, **kwargs):
return Join(self.table, right, isouter = True, *args, **kwargs)
- def alias(self, name):
+ def alias(self, name=None):
return Alias(self.table, name)
def select(self, whereclause = None, **params):
return select([self.table], whereclause, **params)
@@ -1082,16 +1088,20 @@ class Select(SelectBaseMixin, FromClause):
for f in column._get_from_objects():
f.accept_visitor(self._correlator)
- if self.rowid_column is None and hasattr(f, 'rowid_column') and f.rowid_column is not None:
- self.rowid_column = f.rowid_column._make_proxy(self)
column._process_from_dict(self._froms, False)
if column.is_selectable():
+ # if its a column unit, add it to our exported
+ # list of columns. this is where "columns"
+ # attribute of the select object gets populated.
+ # notice we are overriding the names of the column
+ # with either its label or its key, since one or the other
+ # is used when selecting from a select statement (i.e. a subquery)
for co in column.columns:
if self.use_labels:
- co._make_proxy(self, name = co._label)
+ co._make_proxy(self, name=co._label)
else:
- co._make_proxy(self)
+ co._make_proxy(self, name=co.key)
def _get_col_by_original(self, column):
if self.use_labels: