Пример #1
0
 def test_sql(self):
     expected = {'sql_stmts': [{'content': [{'type': 'word', 'value': 'select'}, {'type': 'space', 'value': ' '},
                                            {'type': 'word', 'value': '1'}, {'type': 'space', 'value': ' '}],
                                'parameters': []}, {
                                   'content': [{'type': 'space', 'value': ' '}, {'type': 'word', 'value': 'select'},
                                               {'type': 'space', 'value': ' '}, {'type': 'word', 'value': '1'}],
                                   'parameters': []}]}
     self.assertDictEqual(parser(lexer("select 1 --sql-- select 1")), expected)
Пример #2
0
 def test_parameters(self):
     expected = {'sql_stmts': [{'content': [{'type': 'space', 'value': ' '}, {'type': 'newline', 'value': '\n'},
                                            {'type': 'space', 'value': ' '}, {'type': 'word', 'value': 'select'},
                                            {'type': 'space', 'value': ' '},
                                            {'type': 'parameter', 'value': '{{$query.id}}', 'name': '$query.id'},
                                            {'type': 'space', 'value': ' '}],
                                'parameters': [{'name': '$query.id', 'type': 'integer'}]}],
                 'parameters': {'$query.id': {'name': '$query.id', 'type': 'integer'}}}
     self.assertDictEqual(parser(lexer("""--($query.id integer)-- \n select {{$query.id}} """)), expected)
Пример #3
0
 def test_brace(self):
     expected = {'sql_stmts': [{'content': [{'type': 'word', 'value': 'select'}, {'type': 'space', 'value': ' '},
                                            {'type': 'brace', 'value': '(', 'group': 1, 'nullable_parameter': 'a'},
                                            {'type': 'parameter', 'name': 'a', 'value': '{{a}} is null',
                                             'nullable': True}, {'type': 'space', 'value': ' '},
                                            {'type': 'word', 'value': 'or'}, {'type': 'space', 'value': ' '},
                                            {'type': 'brace', 'value': '(', 'group': 2},
                                            {'type': 'word', 'value': '1'}, {'type': 'space', 'value': ' '},
                                            {'type': 'word', 'value': '='}, {'type': 'space', 'value': ' '},
                                            {'type': 'word', 'value': '1'},
                                            {'type': 'brace', 'value': ')', 'group': 2},
                                            {'type': 'brace', 'value': ')', 'group': 1}],
                                'parameters': [{"name": 'a'}], 'nullable': ['a']}]}
     self.assertDictEqual(parser(lexer("select ({{a}} is null or (1 = 1))")), expected)
Пример #4
0
def _build_branch(branch, map_by_files, content_reader, payload_model, output_model, model, bag):
    _properties_str, _type_str, _partition_by_str = "properties", "type", "partition_by"
    _output_type_str, _use_parent_rows_str = "output_type", "use_parent_rows"
    _parameters_str, _twig_str, _parent_rows_str = "parameters", "twig", "parent_rows"
    _cache_str = "cache"

    path, method = branch["path"], branch["method"]
    content = content_reader.get_sql(method, path)
    branch_map = {}

    if _properties_str not in payload_model:
        payload_model[_properties_str] = {}

    branch["input_type"] = payload_model[_type_str]
    input_properties = payload_model[_properties_str]

    output_properties = None
    if output_model:
        if _type_str in output_model:
            branch[_output_type_str] = output_model[_type_str]
        else:
            branch[_output_type_str] = "array"

        if _properties_str in output_model:
            output_properties = output_model[_properties_str]

        if _parent_rows_str in output_model:
            branch[_use_parent_rows_str] = output_model[_parent_rows_str]

        if _cache_str in output_model:
            if _use_parent_rows_str in branch and branch[_use_parent_rows_str]:
                raise Exception("cache and use_parent_rows can't be true at a same time")

            branch[_cache_str] = output_model[_cache_str]

        if _partition_by_str in output_model:
            branch[_partition_by_str] = output_model[_partition_by_str]

        if output_properties:
            for k in output_properties:
                v = output_properties[k]
                if type(v) == dict and _type_str in v:
                    _type = v[_type_str]
                    if _type == "object" or _type == "array":
                        branch_map[k] = {}
    else:
        branch[_output_type_str] = "array"
        branch[_use_parent_rows_str] = False

    if content:

        ast = parser(lexer(content), method)
        if "sql_stmts" not in ast:
            return

        if "parameters" in ast:
            branch["parameters"] = ast["parameters"]

        for k, v in branch["parameters"].items():
            if k[0] == "$" and k.find("$parent") == -1:
                _expand_parameter(model, k, v)
            else:
                _expand_parameter(payload_model, k, v)

        branch["twigs"] = ast["sql_stmts"]

        bag["connections"] = ["db"]
        for twig in branch["twigs"]:
            if "connection" not in twig:
                twig["connection"] = "db"
            bag["connections"].append(twig["connection"])

    lower_branch_map = _to_lower_keys(branch_map)
    for k in map_by_files:
        if k not in lower_branch_map:
            branch_map[k] = map_by_files[k]

    branches = []
    for sub_branch_name in branch_map:
        sub_branch_map = branch_map[sub_branch_name]
        sub_branch_method = ".".join([method, sub_branch_name]).lower()
        sub_branch = {
            "name": sub_branch_name,
            "method": sub_branch_method,
            "path": path
        }

        sub_branch_output_model = None

        if sub_branch_name not in input_properties:
            input_properties[sub_branch_name] = {
                "type": "object",
                "properties": {}
            }
        sub_branch_payload_model = input_properties[sub_branch_name]

        if output_properties and sub_branch_name in output_properties:
            sub_branch_output_model = output_properties[sub_branch_name]

        sub_branch_payload_model["$parent"] = payload_model

        _build_branch(sub_branch, sub_branch_map, content_reader, sub_branch_payload_model, sub_branch_output_model,
                      model, bag)

        del sub_branch_payload_model["$parent"]

        if _use_parent_rows_str in sub_branch and sub_branch[_use_parent_rows_str]:
            if _partition_by_str not in branch or not branch[_partition_by_str]:
                raise Exception("parent's _partition_by is can't be empty when child wanted to use parent rows")

        branches.append(sub_branch)

    if branches:
        branch["branches"] = branches
Пример #5
0
 def test_simple_sql(self):
     sql = " select  "
     output = parser(lexer(sql))
     expected = {"sql_stmts": [{"content": [{"type": "space", "value": " "}, {"type": "word", "value": "select"},
                                            {"type": "space", "value": "  "}], "parameters": []}]}
     self.assertDictEqual(output, expected)
Пример #6
0
 def test_string(self):
     expected = {'sql_stmts': [{'content': [{'type': 'word', 'value': 'select'}, {'type': 'space', 'value': ' '},
                                            {'type': 'string', 'value': '"\'dasas\'"'}], 'parameters': []}]}
     self.assertDictEqual(parser(lexer("select \"'dasas'\"")), expected)
Пример #7
0
 def test_dash(self):
     self.assertDictEqual(parser(lexer("--($query.id integer)--")),
                          {'parameters': {'$query.id': {'name': '$query.id', 'type': 'integer'}}})
Пример #8
0
 def test_empty(self):
     self.assertIsNone(parser(None))