NEW: __in lookup
This commit is contained in:
3
setup.py
3
setup.py
@@ -1,6 +1,7 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
from distutils.core import setup
|
from distutils.core import setup
|
||||||
from setuptools import find_packages
|
from setuptools import find_packages
|
||||||
|
import sqlquerybuilder
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='sqlquerybuilder',
|
name='sqlquerybuilder',
|
||||||
@@ -8,6 +9,7 @@ setup(
|
|||||||
author='José Sánchez Moreno',
|
author='José Sánchez Moreno',
|
||||||
author_email='jose@o2w.es',
|
author_email='jose@o2w.es',
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
|
test_suite="tests",
|
||||||
license='MIT',
|
license='MIT',
|
||||||
description=u'SQL Query Builder inspired on django ORM Syntax',
|
description=u'SQL Query Builder inspired on django ORM Syntax',
|
||||||
long_description=open('README.rst').read(),
|
long_description=open('README.rst').read(),
|
||||||
@@ -22,6 +24,7 @@ setup(
|
|||||||
'Operating System :: Microsoft :: Windows',
|
'Operating System :: Microsoft :: Windows',
|
||||||
'Operating System :: POSIX',
|
'Operating System :: POSIX',
|
||||||
'Programming Language :: Python',
|
'Programming Language :: Python',
|
||||||
|
'Programming Language :: SQL',
|
||||||
],
|
],
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import copy
|
import copy
|
||||||
|
import collections
|
||||||
|
|
||||||
VERSION = "0.0.3"
|
VERSION = "0.0.3"
|
||||||
|
|
||||||
@@ -76,10 +77,9 @@ class F(object):
|
|||||||
|
|
||||||
class Q(QMixin):
|
class Q(QMixin):
|
||||||
lookup_types = [
|
lookup_types = [
|
||||||
'iexact', 'contains', 'icontains',
|
'icontains', 'istartswith', 'iendswith',
|
||||||
'startswith', 'istartswith', 'endswith', 'iendswith', 'year',
|
'year', 'month', 'day', 'week_day', 'hour', 'minute', 'second',
|
||||||
'month', 'day', 'week_day', 'hour', 'minute', 'second',
|
'isnull', 'in']
|
||||||
'isnull', 'search', 'regex', 'iregex']
|
|
||||||
|
|
||||||
op_map = {
|
op_map = {
|
||||||
'lte': '<=',
|
'lte': '<=',
|
||||||
@@ -101,7 +101,7 @@ class Q(QMixin):
|
|||||||
|
|
||||||
def _get_value(self, value):
|
def _get_value(self, value):
|
||||||
if isinstance(value, int) or isinstance(value, float):
|
if isinstance(value, int) or isinstance(value, float):
|
||||||
return value
|
return str(value)
|
||||||
|
|
||||||
if isinstance(value, datetime.datetime):
|
if isinstance(value, datetime.datetime):
|
||||||
return "'%s'" % value.strftime("%Y-%m-%d %H:%M:%S")
|
return "'%s'" % value.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
@@ -109,6 +109,9 @@ class Q(QMixin):
|
|||||||
if isinstance(value, datetime.date):
|
if isinstance(value, datetime.date):
|
||||||
return "'%s'" % value.strftime("%Y-%m-%d")
|
return "'%s'" % value.strftime("%Y-%m-%d")
|
||||||
|
|
||||||
|
if isinstance(value, list) or isinstance(value, set):
|
||||||
|
return ", ".join([self._get_value(item) for item in value])
|
||||||
|
|
||||||
if isinstance(value, F):
|
if isinstance(value, F):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@@ -132,6 +135,9 @@ class Q(QMixin):
|
|||||||
if lookup == "istartwith":
|
if lookup == "istartwith":
|
||||||
return "{0} like '{1}%'".format(column, value)
|
return "{0} like '{1}%'".format(column, value)
|
||||||
|
|
||||||
|
if lookup == "in" and value:
|
||||||
|
return "{0} in ({1})".format(column, self._get_value(value))
|
||||||
|
|
||||||
if lookup == 'isnull':
|
if lookup == 'isnull':
|
||||||
op = ""
|
op = ""
|
||||||
if not value:
|
if not value:
|
||||||
|
|||||||
@@ -61,5 +61,17 @@ class TestSqlBuilder(unittest.TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(sql), "SELECT name, date, tlf , count(*) as total FROM users WHERE ((name='jhon') AND NOT (DATEPART('year', date)<=1977))")
|
str(sql), "SELECT name, date, tlf , count(*) as total FROM users WHERE ((name='jhon') AND NOT (DATEPART('year', date)<=1977))")
|
||||||
|
|
||||||
|
def test_in(self,):
|
||||||
|
sql = Queryset("users")
|
||||||
|
sql = sql.filter(name__in=["jose", "andres"])
|
||||||
|
self.assertEqual(
|
||||||
|
str(sql), "SELECT * FROM users WHERE (name in ('jose', 'andres'))")
|
||||||
|
|
||||||
|
sql = Queryset("users")
|
||||||
|
sql = sql.filter(year__in=[2012, 2014, "jose"])
|
||||||
|
self.assertEqual(
|
||||||
|
str(sql), "SELECT * FROM users WHERE (year in (2012, 2014, 'jose'))")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user