Skip to content

Commit

Permalink
Database: Add polyfills from rdflib-sqlalchemy and Singer/Meltano
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Jan 16, 2024
1 parent aa96b63 commit 6191433
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/sqlalchemy_cratedb/polyfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ def polyfill_refresh_after_dml_session(session: sa.orm.Session):
listen(session, "after_flush", do_flush)


def polyfill_refresh_after_dml_engine(engine: sa.engine.Engine):
def receive_after_execute(
conn: sa.engine.Connection, clauseelement, multiparams, params, execution_options, result
):
"""
Run a `REFRESH TABLE ...` command after each DML operation (INSERT, UPDATE, DELETE).
This is used by CrateDB's Singer/Meltano and `rdflib-sqlalchemy` adapters.
"""

if isinstance(clauseelement, (sa.sql.Insert, sa.sql.Update, sa.sql.Delete)):
if not isinstance(clauseelement.table, sa.sql.Join):
full_table_name = f'"{clauseelement.table.name}"'
if clauseelement.table.schema is not None:
full_table_name = f'"{clauseelement.table.schema}".' + full_table_name
conn.execute(sa.text(f'REFRESH TABLE {full_table_name};'))

sa.event.listen(engine, "after_execute", receive_after_execute)


def do_flush(session, flush_context):
"""
SQLAlchemy event handler for the 'after_flush' event,
Expand Down

0 comments on commit 6191433

Please sign in to comment.