Skip to content

Commit

Permalink
Rename interpolated to parameterized
Browse files Browse the repository at this point in the history
  • Loading branch information
surister committed Jun 14, 2024
1 parent 60982b9 commit 373d682
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
12 changes: 6 additions & 6 deletions cratedb_sqlparse_py/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ from cratedb_sqlparse import sqlparse
stmt = sqlparse("SELECT A, B FROM doc.tbl12")

print(stmt.metadata)
# Metadata(schema='doc', table_name='tbl12', interpolated_properties={}, with_properties={})
# Metadata(schema='doc', table_name='tbl12', parameterized_properties={}, with_properties={})
```

#### Query properties.
Expand All @@ -137,12 +137,12 @@ stmt = sqlparse("""
""")[0]

print(stmt.metadata)
# Metadata(schema='doc', table_name='tbl12', interpolated_properties={}, with_properties={'allocation.max_retries': '5', 'blocks.metadata': 'false'})
# Metadata(schema='doc', table_name='tbl12', parameterized_properties={}, with_properties={'allocation.max_retries': '5', 'blocks.metadata': 'false'})
```

#### Interpolated properties.
#### Parameterized properties.

Interpolated properties are properties without a real defined value, marked with a dollar string, `metadata.interpolated_properties`
Parameterized properties are properties without a real defined value, marked with a dollar string, `metadata.parameterized_properties`

```python
from cratedb_sqlparse import sqlparse
Expand All @@ -155,10 +155,10 @@ stmt = sqlparse("""
""")[0]

print(stmt.metadata)
# Metadata(schema='doc', table_name='tbl12', interpolated_properties={'blocks.metadata': '$1'}, with_properties={'allocation.max_retries': '5', 'blocks.metadata': '$1'})
# Metadata(schema='doc', table_name='tbl12', parameterized_properties={'blocks.metadata': '$1'}, with_properties={'allocation.max_retries': '5', 'blocks.metadata': '$1'})
```

In this case, `blocks.metadata` will be in `with_properties` and `interpolated_properties` as well.
In this case, `blocks.metadata` will be in `with_properties` and `parameterized_properties` as well.

For values to be picked up they need to start with a dollar `'$'` and be preceded by integers, e.g. `'$1'`, `'$123'` -
`'$123abc'` would not be valid.
Expand Down
8 changes: 4 additions & 4 deletions cratedb_sqlparse_py/cratedb_sqlparse/AstBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def visitGenericProperties(self, ctx: SqlBaseParser.GenericPropertiesContext):
node_properties = ctx.genericProperty()

properties = {}
interpolated_properties = {}
parameterized_properties = {}

for property_ in node_properties:
key = self.get_text(property_.ident())
Expand All @@ -56,12 +56,12 @@ def visitGenericProperties(self, ctx: SqlBaseParser.GenericPropertiesContext):
properties[key] = value

if value and value[0] == "$":
# It might be a interpolated value, e.g. '$1'
# It might be a parameterized value, e.g. '$1'
if value[1:].isdigit():
interpolated_properties[key] = value
parameterized_properties[key] = value

self.stmt.metadata.with_properties = properties
self.stmt.metadata.interpolated_properties = interpolated_properties
self.stmt.metadata.parameterized_properties = parameterized_properties

def get_text(self, node) -> t.Optional[str]:
"""Gets the text representation of the node or None if it doesn't have one"""
Expand Down
2 changes: 1 addition & 1 deletion cratedb_sqlparse_py/cratedb_sqlparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Metadata:

schema: str = None
table_name: str = None
interpolated_properties: dict = dataclasses.field(default_factory=dict)
parameterized_properties: dict = dataclasses.field(default_factory=dict)
with_properties: dict = dataclasses.field(default_factory=dict)


Expand Down
1 change: 1 addition & 0 deletions cratedb_sqlparse_py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ line-length = 120
branch = false
omit = [
"tests/*",
"cratedb_sqlparse/generated_parser/*"
]
source = ["cratedb_sqlparse"]

Expand Down
4 changes: 2 additions & 2 deletions cratedb_sqlparse_py/tests/test_enricher.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_table_with_properties():
assert stmt.metadata.with_properties["key3"] == "true"


def test_with_with_interpolated_properties():
def test_with_with_parameterized_properties():
from cratedb_sqlparse import sqlparse

query = "CREATE TABLE tbl (A TEXT) WITH ('key' = $1, 'key2' = '$2')"
Expand All @@ -69,7 +69,7 @@ def test_with_with_interpolated_properties():
keys = ["key", "key2"]

# Has all the keys.
assert all(x in stmt.metadata.interpolated_properties for x in keys)
assert all(x in stmt.metadata.parameterized_properties for x in keys)
assert all(x in stmt.metadata.with_properties for x in keys)
assert stmt.metadata.with_properties["key"] == "$1"
assert stmt.metadata.with_properties["key2"] == "$2"

0 comments on commit 373d682

Please sign in to comment.