Skip to content

Commit

Permalink
cosmos support for table names containing hyphens
Browse files Browse the repository at this point in the history
  • Loading branch information
rog555 committed Jul 31, 2023
1 parent c54abc3 commit a9b3a17
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

## [v0.0.6] - 2023-07-31

- cosmos support for table names containing hyphens

## [v0.0.5] - 2023-07-20

- patch/update support
Expand Down
4 changes: 2 additions & 2 deletions abnosql/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ def delete_item(table, partition_key, id_key, database, config):

@click.command()
@click.argument('table')
@click.argument('partition-key')
@click.option('--partition-key', '-p')
@click.option('--id-key', '-k')
@click.option('--filters', '-f')
@click.option('--database', '-d')
@click.option('--config', '-c')
def query(table, partition_key, id_key, filters, database, config):
tb = _table(table, get_config(config), database=database)
dump(tb.query(
key=get_key(partition_key, id_key),
key=get_key(partition_key, id_key) if partition_key else None,
filters=parse_dict_arg(filters)
)['items'])

Expand Down
9 changes: 7 additions & 2 deletions abnosql/plugins/table/cosmos.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import functools
import logging
import os

import typing as t

import pluggy # type: ignore
Expand Down Expand Up @@ -203,14 +205,16 @@ def query(
f'@{k}': v
for k, v in filters.items()
}
# cosmos doesnt like hyphens in table names
table_alias = 'c' if '-' in self.name else self.name
parameters.update({
f'@{k}': v
for k, v in key.items()
})
statement = f'SELECT * FROM {self.name}'
statement = f'SELECT * FROM {table_alias}'
op = 'WHERE'
for param in parameters.keys():
statement += f' {op} {self.name}.{param[1:]} = {param}'
statement += f' {op} {table_alias}.{param[1:]} = {param}'
op = 'AND'

resp = self.query_sql(
Expand Down Expand Up @@ -258,6 +262,7 @@ def _get_param(var, val):
and ' LIMIT ' not in statement.upper()
):
kwargs['query'] += f' OFFSET {next} LIMIT {limit}'
logging.debug(f'query_sql() table: {self.name}, kwargs: {kwargs}')
container = self._container(self.name)
items = list(container.query_items(**kwargs))
headers = container.client_connection.last_response_headers
Expand Down
4 changes: 4 additions & 0 deletions abnosql/plugins/table/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
import functools
import json
import logging
import os
import typing as t

Expand Down Expand Up @@ -255,8 +256,10 @@ def query(
kwargs['Limit'] = limit
response = None
if key is not None:
logging.debug(f'query() table: {self.name}, query kwargs: {kwargs}')
response = self.table.query(**kwargs)
else:
logging.debug(f'query() table: {self.name}, scan kwargs: {kwargs}')
response = self.table.scan(**kwargs)
items = response.get('Items', [])
items = kms_process_query_items(self.config, items)
Expand Down Expand Up @@ -291,6 +294,7 @@ def query_sql(
if len(params):
kwargs['Parameters'] = params

logging.debug(f'query_sql() table: {self.name}, kwargs: {kwargs}')
response = client.execute_statement(**kwargs)
items = []
_items = response.get('Items', [])
Expand Down
2 changes: 1 addition & 1 deletion abnosql/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.0.5'
__version__ = '0.0.6'


if __name__ == '__main__':
Expand Down

0 comments on commit a9b3a17

Please sign in to comment.