Skip to main content

A small library that enables capturing SQLAlchemy SQL statements / queries.

Project description

sqlalchemy-capture-sql

A small library that enables capturing SQLAlchemy SQL statements / queries.

Django has django.db.connection.queries connection property that enables user to display executed raw SQL queries (DEBUG mode only). Sometimes in debugging or unit testing this can serve the purpose to check and control nr. and type of sql statements executed for monitored case.

This library provides simple class that enables similar behaviour, but for SQLAlchemy. Each SQL statement is captured along with passed parameters. Internally it uses event.listens_for(engine, 'before_cursor_execute' event handler, e.g.:

@event.listens_for(engine, 'before_cursor_execute')
def capture_sa_statement_listener(...)

REMARKS:

  • There is some "heuristic" duration measurement, i.e. class measures time between 2 captures, it is not actual DB execution time.

  • Capturing type of command and table name is very simple and one should not rely on it.

Tested and developed on Python 3.7+SQLAlchemy 1.3, but I assume it should work on later and probably some previous versions.

Some very simple examples using factory-boy classes, more to come.

with with python statement:

with CaptureSqlStatements(engine_cloud) as capture_stmts:
    # put here any calls that issue sqlalchemy commands that produce some
    # sql statements execution.
    cpm = FactoryModel.create()
    # no .finish() needed
capture_stmts.pp(short=True)

standard style:

capture_stmts = CaptureSqlStatements(engine_cloud)
# put here any calls that issue sqlalchemy commands that produce some
# sql statements execution.
cpm = FactoryModel.create()
capture_stmts.finish()
capture_stmts.pp(short=True)

Both cases produces same result, it could look like this:

== NOTE: duration measures time between 2 captures, it is not actual DB execution time.

== Totally captured 5 statement(s):

  1. 0.00 SELECT FROM person ORDER BY person.id DESC
  2. 0.00 INSERT INTO company_access (alive, allow_empty_cashbag,
     <- 'True+True+False+True+True+False+False+False+False+False+False+False+T
  3. 0.00 SELECT FROM person  WHERE person.id = %(param_1)s
     <- '4'
  4. 0.00 SELECT FROM company_access  WHERE company...
     <- '3'
  5. 0.01 INSERT INTO company_person (packing_model_id, company_access_i
     <- '4+3'

-- By sql command:
    SELECT      3
    INSERT      2

-- By table (top 20):
    PERSON                    2
    COMPANY_ACCESS      2
    COMPANY_PERSON            1

-- By sql command + table (top 20):
    SELECT PERSON                    2
    INSERT COMPANY_ACCESS      1
    SELECT COMPANY_ACCESS      1
    INSERT COMPANY_PERSON            1

One can iterate all statements:

for statement in capture_stmts:
    print(statement.statement)
    print(statement.tst_started)
    print(statement.duration)    # BEWARE: not actual DB execution time, 
                                           Rounded on 2 decimal places.
    print(statement.stmt_repr)   # Dropped list of columns from SELECT
    print(statement.parameters)
    print(statement.executemany) # bool
    print(statement.sql_type)    # BEWARE: do not rely on this
    print(statement.first_table) # BEWARE: do not rely on this

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy-capture-sql-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_capture_sql-0.1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy-capture-sql-0.1.0.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy-capture-sql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d8841686abe8a60760c31f40c44a365e6e44e9edae52fff4933e4d62ce56e15a
MD5 e8e90c592f1333a6ca21614394a6f72c
BLAKE2b-256 b8df4d53f860cdd19682ea34760219ba46c95deafa95d5845a8c067e621bbc71

See more details on using hashes here.

File details

Details for the file sqlalchemy_capture_sql-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_capture_sql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57ed85776923f25c911d252d6a9a77764b6622efb79dafd0d67be12fc49e9414
MD5 d4acd3bd44e424944a5f0058af80640f
BLAKE2b-256 1636030ea64f7b149768ec4425bd03a2feea88a131937a46f23f866291fb2c78

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page