Skip to content

mitszo/sa-dump-engine

Repository files navigation

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="[email protected]", 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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages