Permalink
Browse files

Fixed #11863: added a `Model.objects.raw()` method for executing raw …

…SQL queries and yield models.

See `docs/topics/db/raw.txt` for details.

Thanks to seanoc for getting the ball rolling, and to Russ for wrapping things up.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11921 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 25ab934 commit 20ad30713e1162b26a62920d06a4beb3f3b678d9 @jacobian jacobian committed Dec 20, 2009
@@ -1,5 +1,5 @@
import django.utils.copycompat as copy
-from django.db.models.query import QuerySet, EmptyQuerySet, insert_query
+from django.db.models.query import QuerySet, EmptyQuerySet, insert_query, RawQuerySet
from django.db.models import signals
from django.db.models.fields import FieldDoesNotExist
@@ -181,6 +181,9 @@ def _insert(self, values, **kwargs):
def _update(self, values, **kwargs):
return self.get_query_set()._update(values, **kwargs)
+ def raw(self, query, params=None, *args, **kwargs):
+ return RawQuerySet(model=self.model, query=query, params=params, *args, **kwargs)
+
class ManagerDescriptor(object):
# This class ensures managers aren't accessible via model instances.
# For example, Poll.objects works, but poll_obj.objects raises AttributeError.
@@ -5,7 +5,7 @@
from django.db import connection, transaction, IntegrityError
from django.db.models.aggregates import Aggregate
from django.db.models.fields import DateField
-from django.db.models.query_utils import Q, select_related_descend, CollectedObjects, CyclicDependency, deferred_class_factory
+from django.db.models.query_utils import Q, select_related_descend, CollectedObjects, CyclicDependency, deferred_class_factory, InvalidQuery
from django.db.models import signals, sql
from django.utils.copycompat import deepcopy
@@ -287,7 +287,7 @@ def aggregate(self, *args, **kwargs):
Returns a dictionary containing the calculations (aggregation)
over the current queryset
- If args is present the expression is passed as a kwarg ussing
+ If args is present the expression is passed as a kwarg using
the Aggregate object's default alias.
"""
for arg in args:
@@ -1107,6 +1107,89 @@ def delete_objects(seen_objs):
if forced_managed:
transaction.leave_transaction_management()
+class RawQuerySet(object):
+ """
+ Provides an iterator which converts the results of raw SQL queries into
+ annotated model instances.
+ """
+ def __init__(self, query, model=None, query_obj=None, params=None, translations=None):
+ self.model = model
+ self.query = query_obj or sql.RawQuery(sql=query, connection=connection, params=params)
+ self.params = params or ()
+ self.translations = translations or {}
+
+ def __iter__(self):
+ for row in self.query:
+ yield self.transform_results(row)
+
+ def __repr__(self):
+ return "<RawQuerySet: %r>" % (self.query.sql % self.params)
+
+ @property
+ def columns(self):
+ """
+ A list of model field names in the order they'll appear in the
+ query results.
+ """
+ if not hasattr(self, '_columns'):
+ self._columns = self.query.get_columns()
+
+ # Adjust any column names which don't match field names
+ for (query_name, model_name) in self.translations.items():
+ try:
+ index = self._columns.index(query_name)
+ self._columns[index] = model_name
+ except ValueError:
+ # Ignore translations for non-existant column names
+ pass
+
+ return self._columns
+
+ @property
+ def model_fields(self):
+ """
+ A dict mapping column names to model field names.
+ """
+ if not hasattr(self, '_model_fields'):
+ self._model_fields = {}
+ for field in self.model._meta.fields:
+ name, column = field.get_attname_column()
+ self._model_fields[column] = name
+ return self._model_fields
+
+ def transform_results(self, values):
+ model_init_kwargs = {}
+ annotations = ()
+
+ # Associate fields to values
+ for pos, value in enumerate(values):
+ column = self.columns[pos]
+
+ # Separate properties from annotations
+ if column in self.model_fields.keys():
+ model_init_kwargs[self.model_fields[column]] = value
+ else:
+ annotations += (column, value),
+
+ # Construct model instance and apply annotations
+ skip = set()
+ for field in self.model._meta.fields:
+ if field.name not in model_init_kwargs.keys():
+ skip.add(field.attname)
+
+ if skip:
+ if self.model._meta.pk.attname in skip:
+ raise InvalidQuery('Raw query must include the primary key')
+ model_cls = deferred_class_factory(self.model, skip)
+ else:
+ model_cls = self.model
+
+ instance = model_cls(**model_init_kwargs)
+
+ for field, value in annotations:
+ setattr(instance, field, value)
+
+ return instance
def insert_query(model, values, return_id=False, raw_values=False):
"""
@@ -20,6 +20,13 @@ class CyclicDependency(Exception):
"""
pass
+class InvalidQuery(Exception):
+ """
+ The query passed to raw isn't a safe query to use with raw.
+ """
+ pass
+
+
class CollectedObjects(object):
"""
A container that stores keys and lists of values along with remembering the
@@ -15,15 +15,50 @@
from django.db import connection
from django.db.models import signals
from django.db.models.fields import FieldDoesNotExist
-from django.db.models.query_utils import select_related_descend
+from django.db.models.query_utils import select_related_descend, InvalidQuery
from django.db.models.sql import aggregates as base_aggregates_module
from django.db.models.sql.expressions import SQLEvaluator
from django.db.models.sql.where import WhereNode, Constraint, EverythingNode, AND, OR
from django.core.exceptions import FieldError
from datastructures import EmptyResultSet, Empty, MultiJoin
from constants import *
-__all__ = ['Query', 'BaseQuery']
+__all__ = ['Query', 'BaseQuery', 'RawQuery']
+
+class RawQuery(object):
+ """
+ A single raw SQL query
+ """
+
+ def __init__(self, sql, connection, params=None):
+ self.validate_sql(sql)
+ self.params = params or ()
+ self.sql = sql
+ self.connection = connection
+ self.cursor = None
+
+ def get_columns(self):
+ if self.cursor is None:
+ self._execute_query()
+ return [column_meta[0] for column_meta in self.cursor.description]
+
+ def validate_sql(self, sql):
+ if not sql.lower().strip().startswith('select'):
+ raise InvalidQuery('Raw queries are limited to SELECT queries. Use '
+ 'connection.cursor directly for types of queries.')
+
+ def __iter__(self):
+ # Always execute a new query for a new iterator.
+ # This could be optomized with a cache at the expense of RAM.
+ self._execute_query()
+ return self.cursor
+
+ def __repr__(self):
+ return "<RawQuery: %r>" % (self.sql % self.params)
+
+ def _execute_query(self):
+ self.cursor = self.connection.cursor()
+ self.cursor.execute(self.sql, self.params)
class BaseQuery(object):
"""
@@ -1059,14 +1059,9 @@ Falling back to raw SQL
=======================
If you find yourself needing to write an SQL query that is too complex for
-Django's database-mapper to handle, you can fall back into raw-SQL statement
-mode.
-
-The preferred way to do this is by giving your model custom methods or custom
-manager methods that execute queries. Although there's nothing in Django that
-*requires* database queries to live in the model layer, this approach keeps all
-your data-access logic in one place, which is smart from an code-organization
-standpoint. For instructions, see :ref:`topics-db-sql`.
+Django's database-mapper to handle, you can fall back on writing SQL by hand.
+Django has a couple of options for writing raw SQL queries; see
+:ref:`topics-db-sql`.
Finally, it's important to note that the Django database layer is merely an
interface to your database. You can access your database via other tools,
Oops, something went wrong.

0 comments on commit 20ad307

Please sign in to comment.