|
| 1 | +# CallSQL Module Documentation |
| 2 | + |
| 3 | +The `CallSQL` module is designed to facilitate interaction with a SQL database by executing SQL queries and fetching results. This functionality is particularly useful for integrating database operations within a larger data processing or ETL pipeline. |
| 4 | + |
| 5 | +## Components |
| 6 | + |
| 7 | +The `CallSQL` module consists of three primary components: |
| 8 | + |
| 9 | +1. **CallSQL.py**: Contains the main logic for executing a SQL query and fetching results. |
| 10 | +2. **\_\_init\_\_.py**: Initializes the module (empty in this case, can be used for package-level imports or initial setup). |
| 11 | +3. **typed.py**: Defines input and output data structures using typed dictionaries. |
| 12 | + |
| 13 | +## File: CallSQL.py |
| 14 | + |
| 15 | +### Description |
| 16 | + |
| 17 | +The `CallSQL` class inherits from the `Step` base class and is responsible for running a SQL query against a specified database. It uses SQLAlchemy to manage database connections and execute queries. |
| 18 | + |
| 19 | +### Inputs |
| 20 | + |
| 21 | +- **db_query**: (str) The SQL query to be executed. |
| 22 | +- **db_dialect**: (str) The type of SQL dialect (e.g., 'postgresql', 'mysql'). |
| 23 | +- **db_driver**: (Optional[str]) The specific driver for the database dialect. |
| 24 | +- **db_username**: (Optional[str]) Username for database authentication. |
| 25 | +- **db_password**: (Optional[str]) Password for database authentication. |
| 26 | +- **db_host**: (Optional[str]) Database host, default is 'localhost'. |
| 27 | +- **db_port**: (Optional[int]) Port number for database connection, default is 5432. |
| 28 | +- **db_database**: (Optional[str]) Name of the database to connect to. |
| 29 | +- **db_params**: (Optional[dict[str, Any]]) Additional parameters for the database connection. |
| 30 | +- **db_driver_args**: (Optional[dict[str, Any]]) Custom arguments for the database driver. |
| 31 | +- **db_query_template_values**: (Optional[dict[str, Any]]) Values to render into the SQL query using Mustache templating. |
| 32 | + |
| 33 | +### Outputs |
| 34 | + |
| 35 | +- **results**: (list[dict[str, Any]]) A list of rows returned by the executed query, each represented as a dictionary. |
| 36 | + |
| 37 | +## File: \_\_init\_\_.py |
| 38 | + |
| 39 | +### Description |
| 40 | + |
| 41 | +This file is included to initialize the package. It's empty, but serves as a placeholder for future expansions or to include package-level imports. |
| 42 | + |
| 43 | +## File: typed.py |
| 44 | + |
| 45 | +### Description |
| 46 | + |
| 47 | +Defines typed dictionaries to ensure structured input and output data handling: |
| 48 | + |
| 49 | +### Data Structures |
| 50 | + |
| 51 | +- **__RequiredCallSQLInputs**: Contains required fields for executing the SQL query (`db_dialect`, `db_query`). |
| 52 | +- **CallSQLInputs**: Extends `__RequiredCallSQLInputs` with optional fields like database connection details (`db_username`, `db_password`, etc.). |
| 53 | +- **CallSQLOutputs**: Defines the structure of the output data, containing the `results` list. |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +To use the `CallSQL` module, you need to provide a dictionary of inputs detailing the database connection parameters and the SQL query. The `run` method will execute the query and return the result set as a list of dictionaries. |
| 58 | + |
| 59 | +### Example |
| 60 | + |
| 61 | +```python |
| 62 | +inputs = { |
| 63 | + "db_query": "SELECT * FROM users WHERE age > :age", |
| 64 | + "db_dialect": "postgresql", |
| 65 | + "db_username": "user", |
| 66 | + "db_password": "password", |
| 67 | + "db_host": "localhost", |
| 68 | + "db_port": 5432, |
| 69 | + "db_database": "example_db", |
| 70 | + "db_query_template_values": { |
| 71 | + "age": 21 |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +sql_step = CallSQL(inputs) |
| 76 | +results = sql_step.run() |
| 77 | +print(results) |
| 78 | +``` |
| 79 | + |
| 80 | +This code sets up the required inputs, runs a SQL query to select users older than 21, and prints the results. |
0 commit comments