Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
stout-yeoman committed Dec 6, 2023
1 parent d1c6bca commit 8d179e1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion rqlalchemy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-


from rqlalchemy.query import RQLSelectError
from rqlalchemy.query import RQLSelect
from rqlalchemy.query import RQLSelectError

__title__ = "rqlalchemy"
__version__ = "0.4.5"
Expand Down
48 changes: 29 additions & 19 deletions rqlalchemy/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,43 @@
import operator
from copy import deepcopy
from functools import reduce
from typing import Any, Callable, Dict, NamedTuple, Optional, Sequence, Union, List
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import NamedTuple
from typing import Optional
from typing import Sequence
from typing import Union

import sqlalchemy
from pyrql import RQLSyntaxError
from pyrql import parse
from pyrql import unparse
from sqlalchemy import ColumnElement, Row, RowMapping, and_
from sqlalchemy import func
from sqlalchemy import not_
from sqlalchemy import or_
from sqlalchemy import ColumnElement
from sqlalchemy import Row
from sqlalchemy import RowMapping
from sqlalchemy import Select
from sqlalchemy.orm import Session
from sqlalchemy import func
from sqlalchemy import sql
from sqlalchemy.sql import _typing
from sqlalchemy.sql import elements
from sqlalchemy.inspection import inspect
from sqlalchemy.orm.exc import MultipleResultsFound
from sqlalchemy.orm import Session
from sqlalchemy.orm import decl_api
from sqlalchemy.orm.exc import MultipleResultsFound
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.sql import _typing
from sqlalchemy.sql import elements

ArgsType = List[Any]
BinaryOperator = Callable[[Any, Any], Any]


class PaginatedResults(NamedTuple):
page: Any
total: int
previous_page: Optional[str] = None
next_page: Optional[str] = None


class RQLSelectError(Exception):
pass

Expand Down Expand Up @@ -116,7 +124,7 @@ def rql(self, query: str = "", limit: Optional[int] = None) -> "RQLSelect":
select_ = select_.distinct()

return select_

def rql_all(self, session: Session) -> Sequence[Union[Union[Row, RowMapping], Any]]:
"""
Executes the sql expression differently based on which clauses included:
Expand Down Expand Up @@ -157,7 +165,7 @@ def rql_all(self, session: Session) -> Sequence[Union[Union[Row, RowMapping], An
return [row._asdict() for row in session.execute(query)]

return session.scalars(self).all()

def rql_paginate(self, session: Session) -> PaginatedResults:
"""
Convenience function for pagination. Returns:
Expand Down Expand Up @@ -190,7 +198,9 @@ def rql_paginate(self, session: Session) -> PaginatedResults:
else:
previous_page = None

return PaginatedResults(page=page, total=total, previous_page=previous_page, next_page=next_page)
return PaginatedResults(
page=page, total=total, previous_page=previous_page, next_page=next_page
)

def rql_expr_replace(self, replacement: Dict[str, Any]) -> str:
"""Replace any nodes matching the replacement name
Expand Down Expand Up @@ -228,7 +238,6 @@ def _rql_walk(self, node: Dict[str, Any]) -> None:
if node:
self._rql_where_clause = self._rql_apply(node)


def _rql_apply(self, node: Dict[str, Any]) -> Any:
if isinstance(node, dict):
name = node["name"]
Expand Down Expand Up @@ -277,10 +286,10 @@ def _rql_attr(self, attr):
raise NotImplementedError

def _rql_value(self, value: Any) -> Any:
if isinstance(value, dict):
value = self._rql_apply(value)
if isinstance(value, dict):
value = self._rql_apply(value)

return value
return value

def _rql_compare(self, args: ArgsType, op: BinaryOperator) -> elements.BinaryExpression:
attr, value = args
Expand Down Expand Up @@ -328,7 +337,7 @@ def _rql_like(self, args: ArgsType) -> elements.BinaryExpression:
def _rql_limit(self, args: ArgsType) -> None:
args = [self._rql_value(v) for v in args]

self._rql_limit_clause = min(args[0], self._rql_max_limit or float("inf"))
self._rql_limit_clause = min(args[0], self._rql_max_limit or float("inf"))

if len(args) == 2:
self._rql_offset_clause = args[1]
Expand Down Expand Up @@ -427,7 +436,8 @@ def _rql_aggregate(self, args: ArgsType) -> None:
self._rql_group_by_clause = attributes
self._rql_select_clause = attributes + aggregations


def select(*entities: _typing._ColumnsClauseArgument[Any], **__kw: Any) -> RQLSelect:
if __kw:
raise _typing._no_kw()
return RQLSelect(*entities)
return RQLSelect(*entities)

0 comments on commit 8d179e1

Please sign in to comment.