예제 #1
0
    def _group_sql(self,
                   quote_char=None,
                   alias_quote_char=None,
                   groupby_alias=True,
                   **kwargs):
        """
        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
예제 #2
0
    def _orderby_sql(self,
                     quote_char=None,
                     alias_quote_char=None,
                     orderby_alias=True,
                     **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, 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))
예제 #3
0
    def get_sql(self, **kwargs: Any) -> str:
        query = super().get_sql(**kwargs)

        if self._drop_target_kind != "DICTIONARY" and self._cluster_name is not None:
            query += " ON CLUSTER " + format_quotes(self._cluster_name, super().QUOTE_CHAR)

        return query
예제 #4
0
파일: custom.py 프로젝트: erpnext-tm/frappe
 def get_sql(self, quote_char: Optional[str] = None, **kwargs: Any) -> str:
     return format_alias_sql(
         format_quotes(self.value,
                       kwargs.get("secondary_quote_char") or ""),
         self.alias or self.value,
         quote_char=quote_char,
         **kwargs,
     )
예제 #5
0
    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 "*"
예제 #6
0
    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
예제 #7
0
    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
예제 #8
0
파일: queries.py 프로젝트: x-malet/pypika
    def get_sql(self, quote_char=None, **kwargs):
        # 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(quote_char=quote_char, **kwargs),
                        table=table_sql)
        return alias_sql(table_sql, self.alias, quote_char)
예제 #9
0
파일: queries.py 프로젝트: x-malet/pypika
    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
예제 #10
0
    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)
예제 #11
0
파일: terms.py 프로젝트: alla-levina/pypika
    def get_value_sql(self, **kwargs: Any) -> str:
        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, str):
            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)
예제 #12
0
    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)
예제 #13
0
    def get_sql(self, **kwargs: Any) -> str:
        with_alias = kwargs.pop("with_alias", False)
        with_namespace = kwargs.pop("with_namespace", False)
        quote_char = kwargs.pop("quote_char", None)

        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):
            table_name = self.table.get_table_name()
            field_sql = "{namespace}.{name}".format(
                  namespace=format_quotes(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
예제 #14
0
파일: terms.py 프로젝트: jlndnn/pypika
    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 not with_alias or field_alias is None:
            return field_sql

        return alias_sql(field_sql, field_alias, quote_char)
예제 #15
0
    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):
            table_name = self.table.get_table_name()
            field_sql = "{namespace}.{name}".format(
                namespace=format_quotes(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
예제 #16
0
파일: terms.py 프로젝트: alla-levina/pypika
    def get_sql(self, with_alias: bool = False, with_namespace: bool = False, quote_char: Optional[str] = None, **kwargs: Any) -> str:
        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 "*"
예제 #17
0
 def get_sql(self, quote_char=None, secondary_quote_char="'", **kwargs):
     return format_quotes(self.name, quote_char)
예제 #18
0
파일: terms.py 프로젝트: alla-levina/pypika
 def get_sql(self, secondary_quote_char: str = "'", **kwargs: Any) -> str:
     sql = format_quotes(self._recursive_get_sql(self.value), secondary_quote_char)
     return format_alias_sql(sql, self.alias, **kwargs)
예제 #19
0
파일: terms.py 프로젝트: alla-levina/pypika
 def _get_str_sql(value: str, quote_char: str = '"', **kwargs: Any) -> str:
     return format_quotes(value, quote_char)
예제 #20
0
 def get_sql(self, secondary_quote_char: str = "'", **kwargs) -> str:
     formatted_timestamp = format_quotes(self.timestamp.isoformat(),
                                         secondary_quote_char)
     return f"TIMESTAMP {formatted_timestamp}"
예제 #21
0
 def get_sql(self, secondary_quote_char="'", **kwargs):
     return format_quotes(self._recursive_get_sql(self.value),
                          secondary_quote_char)
예제 #22
0
 def _get_str_sql(value, quote_char='"', **kwargs):
     return format_quotes(value, quote_char)
예제 #23
0
 def get_value_sql(self, **kwargs):
     quote_char = kwargs.get("secondary_quote_char") or ""
     value = self.value.replace(quote_char, quote_char * 2)
     return format_quotes(value, quote_char)
예제 #24
0
 def get_sql(self, quote_char=None, **kwargs):
     return format_quotes(self.name, quote_char)
예제 #25
0
파일: terms.py 프로젝트: alla-levina/pypika
 def get_sql(self, quote_char: Optional[str] = None, **kwargs: Any) -> str:
     return format_quotes(self.name, quote_char)