summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-08-22 18:41:46 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2012-08-22 18:41:46 -0400
commit8f5a31441aed9d223e67d211472445e574fc521f (patch)
tree5f95780d99cf6c77ffd569d34d709514fa98263c /lib/sqlalchemy
parent477dd0f774f1c2f2f3873924ac0606bf499e0061 (diff)
downloadsqlalchemy-8f5a31441aed9d223e67d211472445e574fc521f.tar.gz
- [bug] Fixed cextension bug whereby the
"ambiguous column error" would fail to function properly if the given index were a Column object and not a string. Note there are still some column-targeting issues here which are fixed in 0.8. [ticket:2553] - find more cases where column targeting is being inaccurate, add more information to result_map to better differentiate "ambiguous" results from "present" or "not present". In particular, result_map is sensitive to dupes, even though no error is raised; the conflicting columns are added to the "obj" member of the tuple so that the two are both directly accessible in the result proxy - handwringing over the damn "name fallback" thing in results. can't really make it perfect yet - fix up oracle returning clause. not sure why its guarding against labels, remove that for now and see what the bot says.
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/cextension/resultproxy.c13
-rw-r--r--lib/sqlalchemy/dialects/mssql/base.py14
-rw-r--r--lib/sqlalchemy/dialects/oracle/base.py8
-rw-r--r--lib/sqlalchemy/engine/result.py2
-rw-r--r--lib/sqlalchemy/sql/compiler.py64
-rw-r--r--lib/sqlalchemy/sql/expression.py3
6 files changed, 60 insertions, 44 deletions
diff --git a/lib/sqlalchemy/cextension/resultproxy.c b/lib/sqlalchemy/cextension/resultproxy.c
index 8c89baa25..76c785f85 100644
--- a/lib/sqlalchemy/cextension/resultproxy.c
+++ b/lib/sqlalchemy/cextension/resultproxy.c
@@ -244,7 +244,7 @@ BaseRowProxy_subscript(BaseRowProxy *self, PyObject *key)
PyObject *processors, *values;
PyObject *processor, *value, *processed_value;
PyObject *row, *record, *result, *indexobject;
- PyObject *exc_module, *exception;
+ PyObject *exc_module, *exception, *cstr_obj;
char *cstr_key;
long index;
int key_fallback = 0;
@@ -301,9 +301,16 @@ BaseRowProxy_subscript(BaseRowProxy *self, PyObject *key)
if (exception == NULL)
return NULL;
- cstr_key = PyString_AsString(key);
- if (cstr_key == NULL)
+ // wow. this seems quite excessive.
+ cstr_obj = PyObject_Str(key);
+ if (cstr_obj == NULL)
return NULL;
+ cstr_key = PyString_AsString(cstr_obj);
+ if (cstr_key == NULL) {
+ Py_DECREF(cstr_obj);
+ return NULL;
+ }
+ Py_DECREF(cstr_obj);
PyErr_Format(exception,
"Ambiguous column name '%.200s' in result set! "
diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py
index 0dd610788..bccd585d0 100644
--- a/lib/sqlalchemy/dialects/mssql/base.py
+++ b/lib/sqlalchemy/dialects/mssql/base.py
@@ -859,15 +859,15 @@ class MSSQLCompiler(compiler.SQLCompiler):
t, column)
if add_to_result_map is not None:
- self.result_map[column.name
- if self.dialect.case_sensitive
- else column.name.lower()] = \
- (column.name, (column, ) + add_to_result_map,
- column.type)
+ add_to_result_map(
+ column.name,
+ column.name,
+ (column, ),
+ column.type
+ )
return super(MSSQLCompiler, self).\
- visit_column(converted,
- result_map=None, **kwargs)
+ visit_column(converted, **kwargs)
return super(MSSQLCompiler, self).visit_column(
column, add_to_result_map=add_to_result_map, **kwargs)
diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py
index 88bc15bcc..eb1d75caa 100644
--- a/lib/sqlalchemy/dialects/oracle/base.py
+++ b/lib/sqlalchemy/dialects/oracle/base.py
@@ -508,12 +508,14 @@ class OracleCompiler(compiler.SQLCompiler):
columnlist = list(expression._select_iterables(returning_cols))
- # within_columns_clause =False so that labels (foo AS bar) don't render
- columns = [self.process(c, within_columns_clause=False, result_map=self.result_map) for c in columnlist]
+ columns = [
+ self._label_select_column(None, c, True, False, {})
+ for c in columnlist
+ ]
binds = [create_out_param(c, i) for i, c in enumerate(columnlist)]
- return 'RETURNING ' + ', '.join(columns) + " INTO " + ", ".join(binds)
+ return 'RETURNING ' + ', '.join(columns) + " INTO " + ", ".join(binds)
def _TODO_visit_compound_select(self, select):
"""Need to determine how to get ``LIMIT``/``OFFSET`` into a ``UNION`` for Oracle."""
diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py
index 91d161348..a9bb248b3 100644
--- a/lib/sqlalchemy/engine/result.py
+++ b/lib/sqlalchemy/engine/result.py
@@ -232,7 +232,7 @@ class ResultMetaData(object):
# unambiguous.
primary_keymap[name
if self.case_sensitive
- else name.lower()] = (processor, obj, None)
+ else name.lower()] = rec = (processor, obj, None)
self.keys.append(colname)
if obj:
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index fd9718f1f..ee96c8c81 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -375,7 +375,7 @@ class SQLCompiler(engine.Compiled):
return "(" + grouping.element._compiler_dispatch(self, **kwargs) + ")"
def visit_label(self, label,
- add_to_result_map = None,
+ add_to_result_map=None,
within_label_clause=False,
within_columns_clause=False, **kw):
# only render labels within the columns clause
@@ -388,17 +388,12 @@ class SQLCompiler(engine.Compiled):
labelname = label.name
if add_to_result_map is not None:
- self.result_map[
- labelname
- if self.dialect.case_sensitive
- else labelname.lower()
- ] = (
- label.name,
- (label, label.element, labelname, ) +
- label._alt_names +
- add_to_result_map,
- label.type,
- )
+ add_to_result_map(
+ labelname,
+ label.name,
+ (label, label.element, labelname, ) + label._alt_names,
+ label.type
+ )
return label.element._compiler_dispatch(self,
within_columns_clause=True,
@@ -423,15 +418,12 @@ class SQLCompiler(engine.Compiled):
name = self._truncated_identifier("colident", name)
if add_to_result_map is not None:
- self.result_map[
- name
- if self.dialect.case_sensitive
- else name.lower()
- ] = (
- orig_name,
- (column, name, column.key) + add_to_result_map,
- column.type
- )
+ add_to_result_map(
+ name,
+ orig_name,
+ (column, name, column.key),
+ column.type
+ )
if is_literal:
name = self.escape_literal_column(name)
@@ -557,11 +549,9 @@ class SQLCompiler(engine.Compiled):
def visit_function(self, func, add_to_result_map=None, **kwargs):
if add_to_result_map is not None:
- self.result_map[
- func.name
- if self.dialect.case_sensitive
- else func.name.lower()
- ] = (func.name, add_to_result_map, func.type)
+ add_to_result_map(
+ func.name, func.name, (), func.type
+ )
disp = getattr(self, "visit_%s_func" % func.name.lower(), None)
if disp:
@@ -932,6 +922,20 @@ class SQLCompiler(engine.Compiled):
else:
return alias.original._compiler_dispatch(self, **kwargs)
+ def _add_to_result_map(self, keyname, name, objects, type_):
+ if not self.dialect.case_sensitive:
+ keyname = keyname.lower()
+
+ if keyname in self.result_map:
+ # conflicting keyname, just double up the list
+ # of objects. this will cause an "ambiguous name"
+ # error if an attempt is made by the result set to
+ # access.
+ e_name, e_obj, e_type = self.result_map[keyname]
+ self.result_map[keyname] = e_name, e_obj + objects, e_type
+ else:
+ self.result_map[keyname] = name, objects, type_
+
def _label_select_column(self, select, column, populate_result_map,
asfrom, column_clause_args):
"""produce labeled columns present in a select()."""
@@ -939,13 +943,16 @@ class SQLCompiler(engine.Compiled):
if column.type._has_column_expression:
col_expr = column.type.column_expression(column)
if populate_result_map:
- add_to_result_map = (column, )
+ add_to_result_map = lambda keyname, name, objects, type_: \
+ self._add_to_result_map(
+ keyname, name,
+ objects + (column,), type_)
else:
add_to_result_map = None
else:
col_expr = column
if populate_result_map:
- add_to_result_map = ()
+ add_to_result_map = self._add_to_result_map
else:
add_to_result_map = None
@@ -993,7 +1000,6 @@ class SQLCompiler(engine.Compiled):
**column_clause_args
)
-
def format_from_hint_text(self, sqltext, table, hint, iscrud):
hinttext = self.get_from_hint_text(table, hint)
if hinttext:
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index ce03c9c52..cbc3b47ad 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -4325,8 +4325,9 @@ class ColumnClause(Immutable, ColumnElement):
self.is_literal = is_literal
def _compare_name_for_result(self, other):
+ # TODO: this still isn't 100% correct
if self.table is not None and hasattr(other, 'proxy_set'):
- return other.proxy_set.intersection(self.proxy_set)
+ return self.proxy_set.intersection(other.proxy_set)
else:
return super(ColumnClause, self).\
_compare_name_for_result(other)