-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Accenture/develop
Add CASE WHEN clauses
- Loading branch information
Showing
14 changed files
with
205 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"clause_name": "CASE", | ||
"methods": [ | ||
{ | ||
"name": "case", | ||
"docstring_summary": "Concatenate a CASE clause to the query, to compare a single expression against multiple values or to express multiple conditional statements.", | ||
"args": { | ||
"when_then_mapping": { | ||
"type": "Dict[str, Union[List[str], str]]", | ||
"description": "A dictionary such that a value represents a literal expression (or a list of expressions) whose result will be compared to test_expression if given, else is evaluated to a BOOLEAN, and it's key represents the literal expression returned as output if the value matches test_expression if giver, or if the expression evaluates to TRUE." | ||
}, | ||
"default_result": { | ||
"type": "str", | ||
"description": "The expression to return if no value matches the test expression if given, or if all expressions evaluated as FALSE." | ||
}, | ||
"results_ref": { | ||
"type": "str", | ||
"description": "The reference name of the resulted returned values, plus any other desired reference names to return.", | ||
"default": "None" | ||
}, | ||
"test_expression": { | ||
"type": "str", | ||
"description": "An expression to test the cases against. For example, 'n.name'. Optional.", | ||
"default": "None" | ||
} | ||
} | ||
} | ||
], | ||
"successors": [ | ||
"QueryStartAvailable", | ||
"Unwind", | ||
"Where", | ||
"Set", | ||
"Remove", | ||
"CaseWhen", | ||
"Return", | ||
"Limit", | ||
"Skip", | ||
"OrderBy", | ||
"Union" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
"Return", | ||
"Limit", | ||
"Skip", | ||
"OrderBy" | ||
"OrderBy", | ||
"Case" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
|
||
|
||
def case_when(self, filters: dict, on_true: str, on_false: str, ref_name: str, comparison_operator: str = '"', boolean_operator: str = 'AND', **kwargs): | ||
filt = ' CASE WHEN ' + Properties(filters).to_str(comparison_operator, boolean_operator, **kwargs) | ||
filt += f' THEN {on_true} ELSE {on_false} END as {ref_name}' | ||
return CaseWhenAvailable(self.query + filt) | ||
from typing import Dict, List, Union | ||
|
||
|
||
def case(when_then_mapping: Dict[str, Union[List[str], str]], default_result: str, results_ref: str, test_expression: str = None): | ||
ret = " CASE" | ||
if test_expression is not None: | ||
ret += f" {test_expression}" | ||
for then, when in when_then_mapping.items(): | ||
if type(when) is not list: | ||
when = [when] | ||
ret += f" WHEN {', '.join(when)} THEN {then}" | ||
ret += f" ELSE {default_result} END" | ||
if results_ref is not None: | ||
ret += f" AS {results_ref}" | ||
return CaseAvailable(self.query + ret) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
|
||
def case_when(self, filters: dict, on_true: str, on_false: str, ref_name: str, comparison_operator: str = '"', boolean_operator: str = 'AND', **kwargs): | ||
filt = ' CASE WHEN ' + Properties(filters).to_str(comparison_operator, boolean_operator, **kwargs) | ||
filt += f' THEN {on_true} ELSE {on_false} END AS {ref_name}' | ||
return CaseWhenAvailable(self.query + filt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""Version information for Cymple""" | ||
|
||
__version__: str = "0.11.0" | ||
__version__: str = "0.12.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.