def run(is_init=False) -> None: C(output_response, get_response(validate_req_action("open")), read_request)() if is_init: init_db() while True: try: C(output_response, get_response(validate_req(is_init)), read_request)() except EOFError: close_db_conn() break
def get_or_create(cls, _fields: QueryFields=None, **kwargs: QueryParam) -> SQLQuery: return C( join_queries(cls.get(_fields, **kwargs)), append_to_query("ON CONFLICT DO NOTHING"), lambda: cls.create(**kwargs), )()
def get_response(validate: Callable[[RequestType], RequestType], request: RequestType) -> ResponseType: try: return C(process_request, validate)(request) except psycopg2.Error as e: execute_sql_query(SQLQuery("ROLLBACK;")) return ResponseType({"status": "ERROR", "debug": type(e).__name__}) except (exs.InternalException, AssertionError) as e: return ResponseType({"status": "ERROR", "debug": str(e)})
def get_conds(cls, _conds: List[str]=None, **fields: QueryParam) -> str: conds = C( " AND ".join, P(chain, _conds or []), map('='.join), map(lambda k: (k, cls._val_holder(k))), filter(lambda k: fields[k] is not None), fields.keys )() return f" WHERE {conds}" if conds else ""
def get(cls, _fields: QueryFields=None, **kwargs: QueryParam) -> SQLQuery: return C( append_to_query("LIMIT 1"), lambda k: cls.list(_fields, **k), )(kwargs)
def _vals_holders(cls, **kwargs) -> str: return C(', '.join, map(cls._val_holder), kwargs.keys)()
def _columns(**kwargs: QueryParam) -> str: return C(', '.join, kwargs.keys)()