Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 71ff5e8bd0 | |||
| 61b783e447 | |||
| 8dfdd8a26d |
@@ -105,8 +105,8 @@ class Q(QMixin):
|
|||||||
_mode = "MYSQL"
|
_mode = "MYSQL"
|
||||||
|
|
||||||
lookup_types = [
|
lookup_types = [
|
||||||
'icontains', 'istartswith', 'iendswith',
|
'icontains', 'istartswith', 'iendswith',
|
||||||
'contains', 'startswith', 'endswith',
|
'contains', 'startswith', 'endswith',
|
||||||
'year', 'month', 'day', 'week_day', 'hour', 'minute', 'second',
|
'year', 'month', 'day', 'week_day', 'hour', 'minute', 'second',
|
||||||
'isnull', 'in']
|
'isnull', 'in']
|
||||||
|
|
||||||
@@ -241,7 +241,6 @@ class SQLQuery(object):
|
|||||||
self._limits = None
|
self._limits = None
|
||||||
self._sql = sql
|
self._sql = sql
|
||||||
self._nolock = False
|
self._nolock = False
|
||||||
self._distinct = False
|
|
||||||
|
|
||||||
def has_filters(self,):
|
def has_filters(self,):
|
||||||
return self._order_by or self._group_by or self._joins\
|
return self._order_by or self._group_by or self._joins\
|
||||||
@@ -273,11 +272,6 @@ class SQLQuery(object):
|
|||||||
clone._nolock = enabled
|
clone._nolock = enabled
|
||||||
return clone
|
return clone
|
||||||
|
|
||||||
def distinct(self, enabled=True):
|
|
||||||
clone = self._clone()
|
|
||||||
clone._distinct = enabled
|
|
||||||
return clone
|
|
||||||
|
|
||||||
def filter(self, *args, **kwargs):
|
def filter(self, *args, **kwargs):
|
||||||
clone = self._clone()
|
clone = self._clone()
|
||||||
clone._filters &= self._q(*args, **kwargs)
|
clone._filters &= self._q(*args, **kwargs)
|
||||||
@@ -298,6 +292,9 @@ class SQLQuery(object):
|
|||||||
clone._group_by = args
|
clone._group_by = args
|
||||||
return clone
|
return clone
|
||||||
|
|
||||||
|
def first(self):
|
||||||
|
return self[:1]
|
||||||
|
|
||||||
def join(self, table, on="", how="inner join"):
|
def join(self, table, on="", how="inner join"):
|
||||||
clone = self._clone()
|
clone = self._clone()
|
||||||
if on:
|
if on:
|
||||||
@@ -380,11 +377,6 @@ class SQLCompiler(object):
|
|||||||
return " WITH (NOLOCK)"
|
return " WITH (NOLOCK)"
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def get_distinct(self,):
|
|
||||||
if self._distinct:
|
|
||||||
return " DISTINCT "
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def get_limits(self,):
|
def get_limits(self,):
|
||||||
if self._limits and self.sql_mode != "SQL_SERVER":
|
if self._limits and self.sql_mode != "SQL_SERVER":
|
||||||
offset = self._limits.start
|
offset = self._limits.start
|
||||||
@@ -408,10 +400,7 @@ class SQLCompiler(object):
|
|||||||
else:
|
else:
|
||||||
table = self.get_table()
|
table = self.get_table()
|
||||||
|
|
||||||
sql = ["SELECT",
|
sql = ["SELECT", self.get_top(), self.get_columns(),
|
||||||
self.get_distinct(),
|
|
||||||
self.get_top(),
|
|
||||||
self.get_columns(),
|
|
||||||
"FROM", table,
|
"FROM", table,
|
||||||
self.get_nolock(),
|
self.get_nolock(),
|
||||||
self.get_joins(), self.get_where(),
|
self.get_joins(), self.get_where(),
|
||||||
@@ -437,6 +426,31 @@ class SQLCompiler(object):
|
|||||||
self.get_group_by(),
|
self.get_group_by(),
|
||||||
self.get_limits(), ") as tbl_paginated WHERE ", conds]
|
self.get_limits(), ") as tbl_paginated WHERE ", conds]
|
||||||
|
|
||||||
|
if self.sql_mode == "ACCESS" and self._limits and \
|
||||||
|
self._limits.start is not None and self._limits.stop is not None:
|
||||||
|
conds = []
|
||||||
|
|
||||||
|
if self._limits.start is not None:
|
||||||
|
conds.append("row_number > %s" % self._limits.start)
|
||||||
|
|
||||||
|
if self._limits.stop is not None:
|
||||||
|
conds.append("row_number <= %s" % self._limits.stop)
|
||||||
|
|
||||||
|
conds = " AND ".join(conds)
|
||||||
|
count = "(select count(*) FROM {table} as t2 WHERE t2.{id} <= {table}.{id}) as row_number".format(table=table,
|
||||||
|
id=self._order_by[0])
|
||||||
|
|
||||||
|
return ["SELECT * FROM (", "SELECT", self.get_columns(), ",",
|
||||||
|
count,
|
||||||
|
# "FROM",
|
||||||
|
# table,
|
||||||
|
self.get_joins(),
|
||||||
|
self.get_where(),
|
||||||
|
self.get_group_by(),
|
||||||
|
" FROM ",
|
||||||
|
table,
|
||||||
|
") WHERE ", conds]
|
||||||
|
|
||||||
return sql
|
return sql
|
||||||
|
|
||||||
def _compile(self):
|
def _compile(self):
|
||||||
@@ -447,7 +461,7 @@ class SQLCompiler(object):
|
|||||||
|
|
||||||
__str__ = __repr__
|
__str__ = __repr__
|
||||||
|
|
||||||
@property
|
@ property
|
||||||
def sql(self,):
|
def sql(self,):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
||||||
@@ -461,6 +475,6 @@ class Queryset(SQLCompiler, SQLQuery):
|
|||||||
|
|
||||||
class SQLModel(object):
|
class SQLModel(object):
|
||||||
|
|
||||||
@classproperty
|
@ classproperty
|
||||||
def objects(cls):
|
def objects(cls):
|
||||||
return Queryset(cls.table, getattr(cls, 'sql_mode', None))
|
return Queryset(cls.table, getattr(cls, 'sql_mode', None))
|
||||||
|
|||||||
Reference in New Issue
Block a user