Skip to main content

a mock engine to output SQL statements generated by SQLAlchemy

Project description

sa-dump-engine

This module provides a mock engine to output SQL statements generated by SQLAlchemy.

Installation

You can install this library using pip:

pip install sa-dump-engine

or

pip install git+https://github.com/mitszo/sa-dump-engine.git

Usage

Here's a simple example of how to use the library:

from sa_dump_engine import create_dump_engine

# create a mock engine to dump SQL statements with dialect_name like 'sqlite', 'mysql', 'postgresql'
engine = create_dump_engine(dialect_name, literal_binds=False)

# do some with dump-engine, then you can get SQL statement
# to get CREATE TABLE statement
metadata_obj.create_all(engine)

# to get INSERT Statement
conn = engine.connect()
conn.execute(
    insert(user).
    values(user_name="user1", email="user1@example.com", nickname="user1")
)

then, you'll get the SQL statement like below:

CREATE TABLE user (
        user_id INTEGER NOT NULL,
        user_name VARCHAR(16) NOT NULL,
        email_address VARCHAR(60),
        nickname VARCHAR(50) NOT NULL,
        PRIMARY KEY (user_id)
);

CREATE TABLE user_prefs (
        pref_id INTEGER NOT NULL,
        user_id INTEGER NOT NULL,
        pref_name VARCHAR(40) NOT NULL,
        pref_value VARCHAR(100),
        PRIMARY KEY (pref_id),
        FOREIGN KEY(user_id) REFERENCES user (user_id)
);

INSERT INTO user (user_name, email_address, nickname) VALUES (?, ?, ?);

If you want to ouput the SQL statement to a file, you can call create_dump_engine with output parameter like below:

with open('output.sql', 'w') as f:
    engine = create_dump_engine(dialect_name, output=f)
    metadata_obj.create_all(engine)

Dependencies

This library requires the following Python packages:

  • SQLAlchemy

see pyproject.toml for more details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

  • @mitszo

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

sa_dump_engine-0.3.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sa_dump_engine-0.3.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file sa_dump_engine-0.3.0.tar.gz.

File metadata

  • Download URL: sa_dump_engine-0.3.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for sa_dump_engine-0.3.0.tar.gz
Algorithm Hash digest
SHA256 187e48ed1904c5d6c3b0b645282b226a76c328c338ecb217d4c698ab86e66da0
MD5 4bb2f9bbafcae9399cd6a011a1d5bcc6
BLAKE2b-256 0b0bb7bcaf83f1083d2baa5984327ece9e42a39559b3acffc211b55e5a0cffa1

See more details on using hashes here.

File details

Details for the file sa_dump_engine-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: sa_dump_engine-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for sa_dump_engine-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8540d722558a04822f3a5ae46e8232d65293fddb8e981ed004d262595ab12e9d
MD5 b10c69707581a37d2e2a631f6477ba5f
BLAKE2b-256 586bf4eeece1921ac3955d21a83388a852fb26e1259bbb6e8444e1886713d9e1

See more details on using hashes here.

Supported by

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