Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit (import from django-orm-extensions).

  • Loading branch information...
commit e5c901926a9972831f42c3d19ff1ff6b4765dbca 0 parents
Andrey Antukh authored
7 .gitignore
@@ -0,0 +1,7 @@
+*.pyc
+.*swp
+doc/build
+dist
+versiontools*
+build*
+*.egg*
79 README.rst
@@ -0,0 +1,79 @@
+=====================
+djorm-ext-expressions
+=====================
+
+Django by default, provides a wide range of field types and generic lookups for queries. This in many cases is more than enough. But there are cases where you need to use types defined for yourself and search operators that are not defined in django lookups and another important case is to make searches requiring the execution of some function in WHERE clause.
+
+In django, for these last two cases, it requires writing SQL statements. ``djorm-ext-expressions`` introduces the method ``manager.where()`` and some class'es (SqlExpression, SqlFunction, AND, OR, ...) to facilite sql construction for advanced cases.
+
+Simple usage
+------------
+
+Imagine some django model with postgresql integer array field. You need to find objects in the field containing a set of group numbers.
+
+**NOTE**: array field is part of django orm extensions package and is located on ``djorm-ext-pgarray`` submodule.
+
+**Example model definition**
+
+.. code-block:: python
+
+ from django.db import models
+ from djorm_expressions.models import ExpressionManager
+ from .somefiels import ArrayField
+
+ class Register(models.Model):
+ name = models.CharField(max_length=200)
+ points = ArrayField(dbtype="int")
+
+ objects = ExpressionManager()
+
+
+With this model definition, we can do this searches::
+
+ from djorm_expressions.base import SqlExpression, AND, OR
+
+ # search all register items that points field contains [2,3]
+
+ qs = Register.manager.where(
+ SqlExpression("points", "@>", [2,3])
+ )
+
+ # search all register items that points fields contains [2,3] or [5,6]
+
+ expression = OR(
+ SqlExpression("points", "@>", [2,3]),
+ SqlExpression("points", "@>", [5,6]),
+ )
+
+ qs = Register.objects.where(expression)
+
+
+Also, we can use functions to construct a expression::
+
+ from djorm_expressions.base import SqlFunction
+
+ class BitLength(SqlFunction):
+ sql_function = "bit_length"
+
+ # search all registers items that bit_length(name) > 20.
+ qs = Register.objects.where(
+ SqlExpression(BitLength("name"), ">", 20)
+ )
+
+
+I finally can redefine the behavior "SqlExpression" and make it more "object oriented"::
+
+ class ArrayExpression(object):
+ def __init__(self, field):
+ self.field = field
+
+ def contains(self, value):
+ return SqlExpression(self.field, "@>", value)
+
+ def overlap(self, value):
+ return SqlExpression(self.field, "&&", value)
+
+ # search all register items that points field contains [2,3]
+ qs = Register.objects.where(
+ ArrayExpression("points").contains([2,3])
+ )
1  djorm_expressions/__init__.py
@@ -0,0 +1 @@
+__version__ = (4, 0, 0, 'final', 0)
128 djorm_expressions/base.py
@@ -0,0 +1,128 @@
+# -*- coding: utf-8 -*-
+
+from .utils import _setup_joins_for_fields
+from .tree import AND, OR
+
+class SqlNode(object):
+ negated = False
+
+ sql_negated_template = "NOT %s"
+
+ @property
+ def field_parts(self):
+ raise NotImplementedError
+
+ def as_sql(self, qn, queryset):
+ raise NotImplementedError
+
+ def __invert__(self):
+ # TODO: use clone insetead self modification.
+ self.negated = True
+ return self
+
+
+class SqlExpression(SqlNode):
+ sql_template = "%(field)s %(operator)s %%s"
+
+ def __init__(self, field_or_func, operator, value=None, **kwargs):
+ self.operator = operator
+ self.value = value
+ self.extra = kwargs
+
+ if isinstance(field_or_func, SqlNode):
+ self.field = field_or_func.field
+ self.sql_function = field_or_func
+ else:
+ self.field = field_or_func
+ self.sql_function = None
+
+ @property
+ def field_parts(self):
+ return self.field.split("__")
+
+ def as_sql(self, qn, queryset):
+ """
+ Return the statement rendered as sql.
+ """
+
+ # setup joins if needed
+ if self.sql_function is None:
+ _setup_joins_for_fields(self.field_parts, self, queryset)
+
+ # build sql
+ params, args = {}, []
+
+ if self.operator is not None:
+ params['operator'] = self.operator
+
+ if self.sql_function is None:
+ if isinstance(self.field, basestring):
+ params['field'] = qn(self.field)
+ elif isinstance(self.field, (tuple, list)):
+ _tbl, _fld = self.field
+ params['field'] = "%s.%s" % (qn(_tbl), qn(_fld))
+ else:
+ raise ValueError("Invalid field value")
+ else:
+ params['field'], _args = self.sql_function.as_sql(qn, queryset)
+ args.extend(_args)
+
+ params.update(self.extra)
+ if self.value is not None:
+ args.extend([self.value])
+
+ template_result = self.sql_template % params
+
+ if self.negated:
+ return self.sql_negated_template % (template_result), args
+
+ return template_result, args
+
+
+class RawExpression(SqlExpression):
+ field_parts = []
+
+ def __init__(self, sqlstatement, *args):
+ self.statement = sqlstatement
+ self.params = args
+
+ def as_sql(self, qn, queryset):
+ return self.statement, self.params
+
+
+# TODO: add function(function()) feature.
+
+class SqlFunction(SqlNode):
+ sql_template = '%(function)s(%(field)s)'
+ sql_function = None
+ args = []
+
+ def __init__(self, field, *args, **kwargs):
+ self.field = field
+ self.args = args
+ self.extern_params = kwargs
+
+ @property
+ def field_parts(self):
+ return self.field.split("__")
+
+ def as_sql(self, qn, queryset):
+ """
+ Return the aggregate/annotation rendered as sql.
+ """
+
+ _setup_joins_for_fields(self.field_parts, self, queryset)
+
+ params = {}
+ if self.sql_function is not None:
+ params['function'] = self.sql_function
+ if isinstance(self.field, basestring):
+ params['field'] = qn(self.field)
+ elif isinstance(self.field, (tuple, list)):
+ _tbl, _fld = self.field
+ params['field'] = "%s.%s" % (qn(_tbl), qn(_fld))
+ else:
+ raise ValueError("Invalid field value")
+
+ params.update(self.extern_params)
+ return self.sql_template % params, self.args
66 djorm_expressions/models.py
@@ -0,0 +1,66 @@
+# -*- coding: utf-8 -*-
+
+from django.utils.datastructures import SortedDict
+from django.db.models.sql.where import ExtraWhere
+from django.db.models.query import QuerySet
+from django.db import models
+
+from .base import AND
+
+
+class ExpressionQuerySetMixin(object):
+ def annotate_functions(self, **kwargs):
+ extra_select, params = SortedDict(), []
+ clone = self._clone()
+
+ for alias, node in kwargs.iteritems():
+ _sql, _params = node.as_sql(self.quote_name, self)
+
+ extra_select[alias] = _sql
+ params.extend(_params)
+
+ clone.query.add_extra(extra_select, params, None, None, None, None)
+ return clone
+
+ def where(self, *args):
+ clone = self._clone()
+ statement = AND(*args)
+
+ _sql, _params = statement.as_sql(self.quote_name, clone)
+ if hasattr(_sql, 'to_str'):
+ _sql = _sql.to_str()
+
+ clone.query.where.add(ExtraWhere([_sql], _params), "AND")
+ return clone
+
+ def quote_name(self, name):
+ if name.startswith('"') and name.endswith('"'):
+ return name # Quoting once is enough.
+ return '"%s"' % name
+
+
+
+class ExpressionManagerMixin(object):
+ def annotate_functions(self, **kwargs):
+ return self.get_query_set().annotate_functions(**kwargs)
+
+ def where(self, *args):
+ return self.get_query_set().where(*args)
+
+
+class ExpressionQuerySet(ExpressionQuerySetMixin, QuerySet):
+ """
+ Predefined expression queryset. Usefull if you only use expresions.
+ """
+ pass
+
+
+class ExpressionManager(ExpressionManagerMixin, models.Manager):
+ """
+ Prededined expression manager what uses `ExpressionQuerySet`.
+ """
+
+ use_for_related_fields = True
+
+ def get_query_set(self):
+ return ExpressionQuerySet(model=self.model, using=self._db)
48 djorm_expressions/tests/__init__.py
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+
+from django.test import TestCase
+
+from djorm_expressions.base import RawExpression, SqlExpression, SqlFunction, AND, OR
+from .models import Person, Profile
+
+class BitLength(SqlFunction):
+ sql_function = "bit_length"
+
+
+class SqlExpressionsTests(TestCase):
+ def setUp(self):
+ Person.objects.all().delete()
+
+ def test_raw_statements_0(self):
+ expresion_instance = OR(
+ AND(
+ RawExpression("name = %s", "Foo"),
+ RawExpression("age = %s", 14),
+ ),
+ AND(
+ RawExpression("name = %s", "Bar"),
+ RawExpression("age = %s", 14),
+ )
+ )
+ sql, params = expresion_instance.as_sql(None, None)
+ self.assertEqual(sql.to_str(), "(name = %s AND age = %s) OR (name = %s AND age = %s)")
+ self.assertEqual(params, ['Foo', 14, 'Bar', 14])
+
+
+ def test_string_sample_statement(self):
+ obj = Person.objects.create(name="jose")
+
+ queryset = Person.objects.where(
+ SqlExpression(BitLength("name"), "=", 32)
+ )
+ self.assertEqual(queryset.count(), 1)
+
+ def test_join_lookup_with_expression(self):
+ person = Person.objects.create(name="jose")
+ profile = Profile.objects.create(person=person)
+
+ queryset = Profile.objects.where(
+ SqlExpression(BitLength("person__name"), "=", 32)
+ )
+ self.assertEqual(queryset.count(), 1)
+
14 djorm_expressions/tests/models.py
@@ -0,0 +1,14 @@
+# -*- coding: utf-8 -*-
+
+from django.db import models
+
+from ..models import ExpressionManager
+
+class Person(models.Model):
+ name = models.CharField(max_length=200)
+ objects = ExpressionManager()
+
+
+class Profile(models.Model):
+ person = models.ForeignKey("Person", related_name="profiles")
+ objects = ExpressionManager()
86 djorm_expressions/tree.py
@@ -0,0 +1,86 @@
+# -*- coding: utf-8 -*-
+
+from django.utils import tree
+from django.db.models.sql.datastructures import MultiJoin
+
+class CommonBaseTree(tree.Node):
+ """
+ Encapsulates filters as objects that can then be combined logically (using
+ & and |).
+ """
+ # Connection types
+ AND = 'AND'
+ OR = 'OR'
+ default = AND
+ query = None
+
+ def __init__(self, *args, **kwargs):
+ super(CommonBaseTree, self).__init__(children=list(args) + kwargs.items())
+
+ def _combine(self, other, conn):
+ if not isinstance(other, (BaseTree)):
+ raise TypeError(other)
+ obj = type(self)()
+ obj.add(self, conn)
+ obj.add(other, conn)
+ return obj
+
+ def __or__(self, other):
+ return self._combine(other, self.OR)
+
+ def __and__(self, other):
+ return self._combine(other, self.AND)
+
+ def __invert__(self):
+ obj = type(self)()
+ obj.add(self, self.AND)
+ obj.negate()
+ return obj
+
+ def set_query(self, query):
+ self.query = query
+ return self
+
+
+class RawSQL(object):
+ def __init__(self, items, connector, query=None):
+ self.items = items
+ self.connector = connector
+ self.query = query
+
+ def __str__(self):
+ connector = " %s " % (self.connector)
+ return connector.join(self.items)
+
+ def to_str(self, closure=False):
+ if closure:
+ return u"(%s)" % unicode(self)
+ return unicode(self)
+
+
+class OperatorTree(CommonBaseTree):
+ """
+ Base operator node class.
+ """
+ def as_sql(self, qn, queryset):
+ items, params = [], []
+
+ for child in self.children:
+ _sql, _params = child.as_sql(qn, queryset)
+
+ if isinstance(_sql, RawSQL):
+ _sql = _sql.to_str(True)
+
+ items.extend([_sql])
+ params.extend(_params)
+
+ sql_obj = RawSQL(items, self._connector, queryset)
+ return sql_obj, params
+
+
+class AND(OperatorTree):
+ _connector = "AND"
+
+
+class OR(OperatorTree):
+ _connector = "OR"
49 djorm_expressions/utils.py
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+
+from django.db.models.fields import FieldDoesNotExist
+
+def _setup_joins_for_fields(parts, node, queryset):
+ """
+ Method ported from django 1.4 for correct setup joins.
+ """
+
+ parts_num = len(parts)
+
+ if parts_num == 0:
+ return
+
+ if parts_num == 1:
+ node.field = (queryset.model._meta.db_table, parts[0])
+
+ field, source, opts, join_list, last, _ = queryset.query.setup_joins(
+ parts, queryset.model._meta, queryset.query.get_initial_alias(), False)
+
+ # Process the join chain to see if it can be trimmed
+ col, _, join_list = queryset.query.trim_joins(source, join_list, last, False)
+
+ # If the aggregate references a model or field that requires a join,
+ # those joins must be LEFT OUTER - empty join rows must be returned
+ # in order for zeros to be returned for those aggregates.
+ for column_alias in join_list:
+ queryset.query.promote_alias(column_alias, unconditional=True)
+
+ # this works for one level of depth
+ #lookup_model = self.query.model._meta.get_field(parts[-2]).rel.to
+ #lookup_field = lookup_model._meta.get_field(parts[-1])
+
+ if parts_num >= 2:
+ lookup_model = queryset.model
+ for counter, field_name in enumerate(parts):
+ try:
+ lookup_field = lookup_model._meta.get_field(field_name)
+ except FieldDoesNotExist:
+ parts.pop()
+ break
+
+ try:
+ lookup_model = lookup_field.rel.to
+ except AttributeError:
+ parts.pop()
+ break
+
+ node.field = (lookup_model._meta.db_table, lookup_field.attname)
34 setup.py
@@ -0,0 +1,34 @@
+from setuptools import setup, find_packages
+
+description="""
+Core module of django orm extensions package. Is a collection of third party plugins build in one unified package.
+"""
+
+setup(
+ name = "djorm-ext-expressions",
+ version = ':versiontools:djorm_expressions:',
+ url = 'https://github.com/niwibe/djorm-ext-expressions',
+ license = 'BSD',
+ platforms = ['OS Independent'],
+ description = description.strip(),
+ author = 'Andrey Antukh',
+ author_email = 'niwi@niwi.be',
+ maintainer = 'Andrey Antukh',
+ maintainer_email = 'niwi@niwi.be',
+ packages = find_packages(),
+ include_package_data = False,
+ install_requires = [],
+ setup_requires = [
+ 'versiontools >= 1.9',
+ ],
+ zip_safe = False,
+ classifiers = [
+ 'Development Status :: 4 - Beta',
+ 'Framework :: Django',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: BSD License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Topic :: Internet :: WWW/HTTP',
+ ]
+)
10 testing/runtests.py
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+
+import os, sys
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
+
+from django.core.management import call_command
+
+if __name__ == "__main__":
+ args = sys.argv[1:]
+ call_command("test", *args, verbosity=2)
30 testing/settings.py
@@ -0,0 +1,30 @@
+import os, sys
+
+sys.path.insert(0, '/home/niwi/devel/djorm-ext-core')
+
+PROJECT_ROOT = os.path.dirname(__file__)
+DEBUG = True
+TEMPLATE_DEBUG = DEBUG
+
+DATABASES = {
+ 'default': {
+ 'ENGINE': 'django.db.backends.postgresql_psycopg2',
+ 'NAME': 'test',
+ 'USER': '',
+ 'PASSWORD': '',
+ 'HOST': 'localhost',
+ 'PORT': '',
+ }
+}
+
+TIME_ZONE = 'America/Chicago'
+LANGUAGE_CODE = 'en-us'
+ADMIN_MEDIA_PREFIX = '/static/admin/'
+STATICFILES_DIRS = ()
+
+SECRET_KEY = 'di!n($kqa3)nd%ikad#kcjpkd^uw*h%*kj=*pm7$vbo6ir7h=l'
+INSTALLED_APPS = (
+ 'djorm_core',
+ 'djorm_expressions',
+ 'djorm_expressions.tests',
+)
Please sign in to comment.
Something went wrong with that request. Please try again.