From 4b5dc8e71ac897110aee5354e173e1f50f709601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez=20Moreno?= Date: Tue, 9 Oct 2018 10:25:56 +0200 Subject: [PATCH] Distinct --- sqlquerybuilder/__init__.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/sqlquerybuilder/__init__.py b/sqlquerybuilder/__init__.py index a0faa09..ad39871 100644 --- a/sqlquerybuilder/__init__.py +++ b/sqlquerybuilder/__init__.py @@ -241,6 +241,7 @@ class SQLQuery(object): self._limits = None self._sql = sql self._nolock = False + self._distinct = False def has_filters(self,): return self._order_by or self._group_by or self._joins\ @@ -272,6 +273,11 @@ class SQLQuery(object): clone._nolock = enabled return clone + def distinct(self, enabled=True): + clone = self._clone() + clone._distinct = enabled + return clone + def filter(self, *args, **kwargs): clone = self._clone() clone._filters &= self._q(*args, **kwargs) @@ -374,6 +380,11 @@ class SQLCompiler(object): return " WITH (NOLOCK)" return "" + def get_distinct(self,): + if self._distinct: + return " DISTINCT " + return "" + def get_limits(self,): if self._limits and self.sql_mode != "SQL_SERVER": offset = self._limits.start @@ -397,7 +408,10 @@ class SQLCompiler(object): else: table = self.get_table() - sql = ["SELECT", self.get_top(), self.get_columns(), + sql = ["SELECT", + self.get_distinct(), + self.get_top(), + self.get_columns(), "FROM", table, self.get_nolock(), self.get_joins(), self.get_where(),