summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/expression.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-03-27 17:18:53 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2010-03-27 17:18:53 -0400
commit36047e9bb28501477b1403059087cccc120be2b6 (patch)
tree1438816820a5de5587cbf21a2d4ca89b173ef833 /lib/sqlalchemy/sql/expression.py
parent3cd2c4661f1522353be983a309dc947c2a2a28bb (diff)
downloadsqlalchemy-36047e9bb28501477b1403059087cccc120be2b6.tar.gz
- Added with_hint() method to Query() construct. This calls
directly down to select().with_hint() and also accepts entities as well as tables and aliases. See with_hint() in the SQL section below. [ticket:921] - Added with_hint() method to select() construct. Specify a table/alias, hint text, and optional dialect name, and "hints" will be rendered in the appropriate place in the statement. Works for Oracle, Sybase, MySQL. [ticket:921]
Diffstat (limited to 'lib/sqlalchemy/sql/expression.py')
-rw-r--r--lib/sqlalchemy/sql/expression.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index 1e02ba96a..3aaa06fd6 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -3557,6 +3557,7 @@ class Select(_SelectBaseMixin, FromClause):
__visit_name__ = 'select'
_prefixes = ()
+ _hints = util.frozendict()
def __init__(self,
columns,
@@ -3659,7 +3660,34 @@ class Select(_SelectBaseMixin, FromClause):
"""Return the displayed list of FromClause elements."""
return self._get_display_froms()
-
+
+ @_generative
+ def with_hint(self, selectable, text, dialect_name=None):
+ """Add an indexing hint for the given selectable to this :class:`Select`.
+
+ The text of the hint is written specific to a specific backend, and
+ typically uses Python string substitution syntax to render the name
+ of the table or alias, such as for Oracle::
+
+ select([mytable]).with_hint(mytable, "+ index(%(name)s ix_mytable)")
+
+ Would render SQL as::
+
+ select /*+ index(mytable ix_mytable) */ ... from mytable
+
+ The ``dialect_name`` option will limit the rendering of a particular hint
+ to a particular backend. Such as, to add hints for both Oracle and
+ Sybase simultaneously::
+
+ select([mytable]).\
+ with_hint(mytable, "+ index(%(name)s ix_mytable)", 'oracle').\
+ with_hint(mytable, "WITH INDEX ix_mytable", 'sybase')
+
+ """
+ if not dialect_name:
+ dialect_name = '*'
+ self._hints = self._hints.union({(selectable, dialect_name):text})
+
@property
def type(self):
raise exc.InvalidRequestError("Select objects don't have a type. "