Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_sql(
self, with_alias=False, with_namespace=False, quote_char=None, **kwargs
):
if self.table and (with_namespace or self.table.alias):
namespace = self.table.alias or getattr(self.table, "_table_name")
return "{}.*".format(format_quotes(namespace, quote_char))
return "*"
def _orderby_sql(self, quote_char=None, **kwargs):
"""
Produces the ORDER BY part of the query. This is a list of fields and possibly their directionality, ASC or
DESC. The clauses are stored in the query under self._orderbys as a list of tuples containing the field and
directionality (which can be None).
If an order by field is used in the select clause, determined by a matching , then the ORDER BY clause will use
the alias, otherwise the field will be rendered as SQL.
"""
clauses = []
selected_aliases = {s.alias for s in self.base_query._selects}
for field, directionality in self._orderbys:
term = (
format_quotes(field.alias, quote_char)
if field.alias and field.alias in selected_aliases
else field.get_sql(quote_char=quote_char, **kwargs)
)
clauses.append(
"{term} {orient}".format(term=term, orient=directionality.value)
if directionality is not None
else term
)
return " ORDER BY {orderby}".format(orderby=",".join(clauses))
):
"""
Produces the ORDER BY part of the query. This is a list of fields and possibly their directionality, ASC or
DESC. The clauses are stored in the query under self._orderbys as a list of tuples containing the field and
directionality (which can be None).
If an order by field is used in the select clause,
determined by a matching, and the orderby_alias
is set True then the ORDER BY clause will use
the alias, otherwise the field will be rendered as SQL.
"""
clauses = []
selected_aliases = {s.alias for s in self._selects}
for field, directionality in self._orderbys:
term = (
format_quotes(field.alias, alias_quote_char or quote_char)
if orderby_alias and field.alias and field.alias in selected_aliases
else field.get_sql(
quote_char=quote_char, alias_quote_char=alias_quote_char, **kwargs
)
)
clauses.append(
"{term} {orient}".format(term=term, orient=directionality.value)
if directionality is not None
else term
)
return " ORDER BY {orderby}".format(orderby=",".join(clauses))
def get_value_sql(self, **kwargs):
quote_char = kwargs.get("secondary_quote_char") or ""
# FIXME escape values
if isinstance(self.value, Term):
return self.value.get_sql(**kwargs)
if isinstance(self.value, Enum):
return self.value.value
if isinstance(self.value, date):
value = self.value.isoformat()
return format_quotes(value, quote_char)
if isinstance(self.value, basestring):
value = self.value.replace(quote_char, quote_char * 2)
return format_quotes(value, quote_char)
if isinstance(self.value, bool):
return str.lower(str(self.value))
if self.value is None:
return "null"
return str(self.value)
def get_sql(self, **kwargs):
quote_char = kwargs.get("quote_char")
# FIXME escape
table_sql = format_quotes(self._table_name, quote_char)
if self._schema is not None:
table_sql = "{schema}.{table}".format(
schema=self._schema.get_sql(**kwargs), table=table_sql
)
return format_alias_sql(table_sql, self.alias, **kwargs)
def get_sql(
self,
with_alias=False,
with_namespace=False,
quote_char=None,
secondary_quote_char="'",
**kwargs
):
field_sql = format_quotes(self.name, quote_char)
# Need to add namespace if the table has an alias
if self.table and (with_namespace or self.table.alias):
field_sql = "{namespace}.{name}".format(
namespace=format_quotes(
self.table.alias or self.table._table_name, quote_char
),
name=field_sql,
)
field_alias = getattr(self, "alias", None)
if with_alias:
return format_alias_sql(
field_sql, field_alias, quote_char=quote_char, **kwargs
)
return field_sql
):
"""
Produces the GROUP BY part of the query. This is a list of fields. The clauses are stored in the query under
self._groupbys as a list fields.
If an groupby field is used in the select clause,
determined by a matching alias, and the groupby_alias is set True
then the GROUP BY clause will use the alias,
otherwise the entire field will be rendered as SQL.
"""
clauses = []
selected_aliases = {s.alias for s in self._selects}
for field in self._groupbys:
if groupby_alias and field.alias and field.alias in selected_aliases:
clauses.append(
format_quotes(field.alias, alias_quote_char or quote_char)
)
else:
clauses.append(
field.get_sql(
quote_char=quote_char,
alias_quote_char=alias_quote_char,
**kwargs
)
)
sql = " GROUP BY {groupby}".format(groupby=",".join(clauses))
if self._with_totals:
return sql + " WITH TOTALS"
return sql
def get_sql(self, **kwargs):
quote_char = kwargs.get("quote_char")
column_sql = "{name}{type}".format(
name=format_quotes(self.name, quote_char),
type=" {}".format(self.type) if self.type else "",
)
return column_sql
def get_sql(self, quote_char=None, **kwargs):
# FIXME escape
schema_sql = format_quotes(self._name, quote_char)
if self._parent is not None:
return "{parent}.{schema}".format(
parent=self._parent.get_sql(quote_char=quote_char, **kwargs),
schema=schema_sql,
)
return schema_sql