示例#1
0
文件: terms.py 项目: x8lucas8x/pypika
    def get_sql(self, with_alias=False, **kwargs):
        if not self._cases:
            raise CaseException(
                "At least one 'when' case is required for a CASE statement.")
        if self._else is None:
            raise CaseException(
                "'Else' clause is requred for a CASE statement.")

        return 'CASE {cases} ELSE {else_clause} END{alias}'.format(
            cases=" ".join('WHEN {when} THEN {then}'.format(
                when=criterion.get_sql(**kwargs), then=term.get_sql(**kwargs))
                           for criterion, term in self._cases),
            else_clause=self._else.get_sql(**kwargs),
            alias=' \"{}\"'.format(self.alias)
            if self.alias is not None and with_alias else '')
示例#2
0
    def get_sql(self, with_alias=False, **kwargs):
        if not self._cases:
            raise CaseException(
                "At least one 'when' case is required for a CASE statement.")

        cases = " ".join('WHEN {when} THEN {then}'.format(
            when=criterion.get_sql(**kwargs), then=term.get_sql(**kwargs))
                         for criterion, term in self._cases)
        else_ = (' ELSE {}'.format(self._else.get_sql(
            **kwargs)) if self._else else '')

        case_sql = 'CASE {cases}{else_} END'.format(cases=cases, else_=else_)

        if not with_alias or self.alias is None:
            return case_sql

        return alias_sql(case_sql, self.alias, kwargs.get('quote_char'))
示例#3
0
    def get_sql(self, with_alias=False, **kwargs):
        if not self._cases:
            raise CaseException(
                "At least one 'when' case is required for a CASE statement.")

        cases = " ".join("WHEN {when} THEN {then}".format(
            when=criterion.get_sql(**kwargs), then=term.get_sql(**kwargs))
                         for criterion, term in self._cases)
        else_ = " ELSE {}".format(self._else.get_sql(
            **kwargs)) if self._else else ""

        case_sql = "CASE {cases}{else_} END".format(cases=cases, else_=else_)

        if with_alias:
            return format_alias_sql(case_sql, self.alias, **kwargs)

        return case_sql