Skip to content

Commit

Permalink
204 refactored BaseQueryBuilder metadata transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
adp-atea committed Nov 10, 2023
1 parent e8d612f commit db1bd6a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
13 changes: 7 additions & 6 deletions spinta/backends/postgresql/commands/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from spinta.exceptions import UnknownMethod
from spinta.exceptions import FieldNotInResource
from spinta.components import Model, Property, Action, Page, UrlParams
from spinta.ufuncs.basequerybuilder.components import BaseQueryBuilder, QueryPage, merge_with_page_sort, merge_with_page_limit, merge_with_page_selected_list
from spinta.ufuncs.basequerybuilder.components import BaseQueryBuilder, QueryPage, merge_with_page_sort, \
merge_with_page_limit, merge_with_page_selected_list, QueryParams
from spinta.utils.data import take
from spinta.types.datatype import DataType, ExternalRef, Inherit, BackRef, Time, ArrayBackRef
from spinta.types.datatype import Array
Expand All @@ -45,7 +46,7 @@
class PgQueryBuilder(BaseQueryBuilder):
backend: PostgreSQL

def init(self, params: UrlParams, backend: PostgreSQL, table: sa.Table):
def init(self, backend: PostgreSQL, table: sa.Table, params: QueryParams = None):
result = self(
backend=backend,
table=table,
Expand All @@ -58,9 +59,9 @@ def init(self, params: UrlParams, backend: PostgreSQL, table: sa.Table):
offset=None,
aggregate=False,
page=QueryPage(),
group_by=[]
group_by=[],
)
result.parse_params(params)
result.init_query_params(params)
return result

def build(self, where):
Expand Down Expand Up @@ -478,7 +479,7 @@ def select(env, dtype):
def select(env, dtype):
default_langs = env.context.get('config').languages
table = env.backend.get_table(env.model)
column = env.backend.get_column(table, dtype.prop, langs=env.lang_priority, default_langs=default_langs)
column = env.backend.get_column(table, dtype.prop, langs=env.query_params.lang_priority, default_langs=default_langs)
return Selected(column, dtype.prop)


Expand Down Expand Up @@ -530,7 +531,7 @@ def select(env, dtype, leaf):
@ufunc.resolver(PgQueryBuilder, Ref)
def select(env, dtype):
uri = dtype.model.uri_prop
if env.prioritize_uri and uri is not None:
if env.query_params.prioritize_uri and uri is not None:
fpr = ForeignProperty(None, dtype.prop, dtype.model.properties['_id'])
table = env.get_joined_table(fpr)
column = table.c[uri.place]
Expand Down
6 changes: 3 additions & 3 deletions spinta/backends/postgresql/commands/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from spinta.exceptions import NotFoundError
from spinta.exceptions import ItemDoesNotExist
from spinta.backends.postgresql.components import PostgreSQL
from spinta.ufuncs.basequerybuilder.components import get_page_values
from spinta.ufuncs.basequerybuilder.components import get_page_values, QueryParams
from spinta.ufuncs.helpers import merge_formulas
from spinta.utils.nestedstruct import flat_dicts_to_nested
from spinta.backends.postgresql.commands.query import PgQueryBuilder
Expand Down Expand Up @@ -43,7 +43,7 @@ def getall(
*,
query: Expr = None,
default_expand: bool = True,
params: UrlParams = None,
params: QueryParams = None,
**kwargs
) -> Iterator[ObjectData]:
assert isinstance(query, (Expr, type(None))), query
Expand All @@ -57,7 +57,7 @@ def getall(
builder = PgQueryBuilder(context)
builder.update(model=model)
table = backend.get_table(model)
env = builder.init(params, backend, table)
env = builder.init(backend, table, params)
expr = env.resolve(query)
where = env.execute(expr)
qry = env.build(where)
Expand Down
13 changes: 8 additions & 5 deletions spinta/commands/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
from spinta.exceptions import ItemDoesNotExist
from spinta.types.datatype import DataType
from spinta.typing import ObjectData
from spinta.ufuncs.basequerybuilder.components import get_allowed_page_property_types
from spinta.ufuncs.basequerybuilder.components import get_allowed_page_property_types, QueryParams, \
update_query_with_url_params
from spinta.ufuncs.basequerybuilder.ufuncs import add_page_expr
from spinta.utils.data import take
from spinta.utils.encoding import encode_page_values
Expand Down Expand Up @@ -57,6 +58,8 @@ async def getall(
expr = urlparams_to_expr(params, add_count=False)
else:
expr = urlparams_to_expr(params)
query_params = QueryParams()
update_query_with_url_params(query_params, params)
accesslog: AccessLog = context.get('accesslog')
accesslog.request(
# XXX: Read operations does not have a transaction, but it
Expand All @@ -70,12 +73,12 @@ async def getall(
rows = []
else:
if is_page_enabled:
rows = get_page(context, model, backend, copy_page, expr, params.limit, default_expand=False, params=params)
rows = get_page(context, model, backend, copy_page, expr, params.limit, default_expand=False, params=query_params)
else:
if backend.support_expand:
rows = commands.getall(context, model, backend, params=params, query=expr, default_expand=False)
rows = commands.getall(context, model, backend, params=query_params, query=expr, default_expand=False)
else:
rows = commands.getall(context, model, backend, params=params, query=expr)
rows = commands.getall(context, model, backend, params=query_params, query=expr)

if params.count:
# XXX: Quick and dirty hack. Functions should be handled properly.
Expand Down Expand Up @@ -202,7 +205,7 @@ def get_page(
expr: Expr,
limit: Optional[int] = None,
default_expand: bool = True,
params: UrlParams = None,
params: QueryParams = None,
) -> Iterator[ObjectData]:
config = context.get('config')
page_size = config.push_page_size
Expand Down
10 changes: 5 additions & 5 deletions spinta/datasets/backends/sql/commands/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from spinta.types.file.components import FileData
from spinta.types.text.components import Text
from spinta.ufuncs.basequerybuilder.components import BaseQueryBuilder, QueryPage, merge_with_page_selected_list, \
merge_with_page_sort, merge_with_page_limit
merge_with_page_sort, merge_with_page_limit, QueryParams
from spinta.ufuncs.components import ForeignProperty
from spinta.core.ufuncs import Unresolved
from spinta.utils.data import take
Expand Down Expand Up @@ -131,7 +131,7 @@ class SqlQueryBuilder(BaseQueryBuilder):
selected: Dict[str, Selected] = None
params: ResolvedParams

def init(self, params: UrlParams, backend: Sql, table: sa.Table):
def init(self, backend: Sql, table: sa.Table, params: QueryParams = None):
result = self(
backend=backend,
table=table,
Expand All @@ -142,9 +142,9 @@ def init(self, params: UrlParams, backend: Sql, table: sa.Table):
sort=[],
limit=None,
offset=None,
page=QueryPage()
page=QueryPage(),
)
result.parse_params(params)
result.init_query_params(params)
return result

def build(self, where):
Expand Down Expand Up @@ -674,7 +674,7 @@ def select(env: SqlQueryBuilder, dtype: DataType) -> Selected:
def select(env, dtype):
default_langs = env.context.get('config').languages
table = env.backend.get_table(env.model)
column = env.backend.get_column(table, dtype.prop, langs=env.lang_priority, default_langs=default_langs)
column = env.backend.get_column(table, dtype.prop, langs=env.query_params.lang_priority, default_langs=default_langs)
return Selected(env.add_column(column), dtype.prop)


Expand Down
6 changes: 3 additions & 3 deletions spinta/datasets/backends/sql/commands/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from spinta.exceptions import ValueNotInEnum, BackendNotGiven
from spinta.types.datatype import PrimaryKey
from spinta.types.datatype import Ref
from spinta.ufuncs.basequerybuilder.components import get_page_values
from spinta.ufuncs.basequerybuilder.components import get_page_values, QueryParams
from spinta.ufuncs.helpers import merge_formulas
from spinta.utils.nestedstruct import flat_dicts_to_nested
from spinta.utils.schema import NA
Expand Down Expand Up @@ -77,7 +77,7 @@ def getall(
backend: Sql,
*,
query: Expr = None,
params: UrlParams = None,
params: QueryParams = None,
**kwargs
) -> Iterator[ObjectData]:
conn = context.get(f'transaction.{backend.name}')
Expand All @@ -91,7 +91,7 @@ def getall(
for model_params in iterparams(context, model):
table = model.external.name.format(**model_params)
table = backend.get_table(model, table)
env = builder.init(params, backend, table)
env = builder.init(backend, table, params)
env.update(params=model_params)
expr = env.resolve(query)
where = env.execute(expr)
Expand Down
4 changes: 2 additions & 2 deletions spinta/manifests/tabular/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ def _string_datatype_handler(reader: PropertyReader, row: dict):
)
)
if row['ref']:
pass
new_data['enum'] = row['ref']
if dataset or row['source']:
new_data['external'] = {
'name': row['source'],
Expand Down Expand Up @@ -829,7 +829,7 @@ def _text_datatype_handler(reader: PropertyReader, row: dict):
)
)
if row['ref']:
pass
new_data['enum'] = row['ref']
if dataset or row['source']:
new_data['external'] = {
'name': row['source'],
Expand Down
21 changes: 15 additions & 6 deletions spinta/ufuncs/basequerybuilder/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ def __init__(self):
self.page_ = Page()


class QueryParams:
prioritize_uri: bool = False
lang_priority: List[str] = None


class BaseQueryBuilder(Env):
page: QueryPage
expand: List[Property] = None
prioritize_uri: bool = False
lang_priority: List[str] = None
query_params: QueryParams = None

def parse_params(self, params: UrlParams):
if params is not None:
self.lang_priority = params.accept_langs
self.prioritize_uri = params.fmt.prioritize_uri
def init_query_params(self, params: QueryParams):
if params is None:
params = QueryParams()
self.query_params = params


class LoadBuilder(Env):
Expand Down Expand Up @@ -136,3 +140,8 @@ def merge_with_page_limit(limit: int, page: QueryPage):
return limit
return page.size
return limit


def update_query_with_url_params(query_params: QueryParams, url_params: UrlParams):
query_params.prioritize_uri = url_params.fmt.prioritize_uri
query_params.lang_priority = url_params.accept_langs

0 comments on commit db1bd6a

Please sign in to comment.