def add_classification_comment(select_languoid: sa.sql.Select, kind: str, *, label: str, bib_suffix: str = '_cr') -> sa.sql.Select: comment = aliased(ClassificationComment, name=f'cc_{kind}') label = label.format(kind=kind) return (select_languoid.add_columns(comment.comment.label(label)) .outerjoin(comment, sa.and_(comment.kind == kind, comment.languoid_id == Languoid.id)))
def add_identifier(select_languoid: sa.sql.Select, site_name: str, *, label: str) -> sa.sql.Select: identifier = aliased(Identifier, name=f'ident_{site_name}') site = aliased(IdentifierSite, name=f'ident_{site_name}_site') label = label.format(site_name=site_name) return (select_languoid.add_columns(identifier.identifier.label(label)) .outerjoin(sa.join(identifier, site, identifier.site_id == site.id), sa.and_(site.name == site_name, identifier.languoid_id == Languoid.id)))
def add_endangermentsource(select_languoid: sa.sql.Select, *, label: str, bib_suffix: str = '_e') -> sa.sql.Select: bibfile = aliased(Bibfile, name=f'bibfile{bib_suffix}') bibitem = aliased(Bibitem, name=f'bibitem{bib_suffix}') endangermentsource = (EndangermentSource.printf(bibfile, bibitem) .label(label)) return (select_languoid.add_columns(endangermentsource) .outerjoin(sa.join(Endangerment, EndangermentSource)) .outerjoin(sa.join(bibitem, bibfile)))
def add_columns( stmt: sa.sql.Select, columns: abc.Iterable[Union[sa.Column, sa.sql.ColumnElement]] ) -> sa.sql.Select: """ Add columns to an SQL Select statement """ if SA_13: for col in columns: stmt.append_column(col) else: stmt = stmt.add_columns(*columns) return stmt
def add_model_columns(select_languoid: sa.sql.Select, model, *, add_outerjoin=None, label: str = '{name}', ignore: str = 'id') -> sa.sql.Select: columns = model.__table__.columns if ignore: ignore_suffix = f'_{ignore}' columns = [c for c in columns if c.name != ignore and not c.name.endswith(ignore_suffix)] columns = [c.label(label.format(name=c.name)) for c in columns] select_languoid = select_languoid.add_columns(*columns) if add_outerjoin is not None: select_languoid = select_languoid.outerjoin(add_outerjoin) return select_languoid
def add_classification_refs(select_languoid: sa.sql.Select, kind: str, *, label: str, bib_suffix: str = '_cr') -> sa.sql.Select: ref = aliased(ClassificationRef, name=f'cr_{kind}') bibfile = aliased(Bibfile, name=f'bibfile{bib_suffix}_{kind}') bibitem = aliased(Bibitem, name=f'bibitem{bib_suffix}_{kind}') label = label.format(kind=kind) ref = (select(ref.printf(bibfile, bibitem)) .select_from(ref) .filter_by(languoid_id=Languoid.id) .correlate(Languoid) .filter_by(kind=kind) .join(ref.bibitem.of_type(bibitem)) .join(bibitem.bibfile.of_type(bibfile)) .order_by(ref.ord) .alias(f'lang_cref_{kind}')) refs = select(group_concat(ref.c.printf).label(label)).label(label) return select_languoid.add_columns(refs)