Skip to content

Commit 6544a36

Browse files
author
patched.codes[bot]
committed
Patched patchwork/steps/CallSQL/README.md
1 parent ace2e70 commit 6544a36

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

patchwork/steps/CallSQL/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# CallSQL Module Documentation
2+
3+
## Overview
4+
5+
The CallSQL module is part of the Patchwork system, designed to interact with SQL databases by performing specific SQL queries. This module is ideal for users who need to automate database queries and handle SQL interactions programmatically. The module consists of a core execution script `CallSQL.py`, an initialization file `__init__.py`, and type definitions in `typed.py`.
6+
7+
## Core Components
8+
9+
### CallSQL.py
10+
11+
This is the main script for executing SQL queries. It uses the SQLAlchemy library to establish a connection to the database and execute the SQL command provided by the user. It handles the following tasks:
12+
13+
1. **Connection Setup**: Uses SQLAlchemy to build the connection to the database based on input parameters, including the database dialect, driver, username, password, host, and port.
14+
2. **Query Execution**: Renders SQL templates with Mustache syntax and executes them.
15+
3. **Error Handling**: Captures and logs any SQL execution errors, returning failed status if required.
16+
17+
#### Inputs
18+
19+
This module takes the following inputs through a dictionary structure:
20+
21+
- **`db_dialect`**: (Required) The dialect of the database (e.g., `'postgresql'`).
22+
- **`db_query`**: (Required) The SQL query or template to execute.
23+
- **`db_driver`**: The specific database driver to use.
24+
- **`db_username`**: The username for database connection.
25+
- **`db_password`**: The password for database connection.
26+
- **`db_host`**: The server host; defaults to `'localhost'`.
27+
- **`db_port`**: The connection port; defaults to `5432`.
28+
- **`db_name`**: The name of the database to connect to.
29+
- **`db_params`**: Additional parameters for the connection in dictionary format.
30+
- **`db_driver_args`**: Additional driver-specific arguments.
31+
- **`db_query_template_values`**: Dictionary of values to render SQL templates.
32+
33+
#### Outputs
34+
35+
- **`results`**: A dictionary containing a list of query result rows, with each row being a dictionary of column names and values.
36+
37+
### typed.py
38+
39+
This file contains the type definitions for the inputs and outputs, ensuring type safety and clarity for users.
40+
41+
#### `CallSQLInputs`
42+
43+
Defines both required and optional parameters required to execute a SQL query within the CallSQL module.
44+
45+
#### `CallSQLOutputs`
46+
47+
Defines the structure of the query results, which are returned as a list of dictionaries.
48+
49+
## Example Usage
50+
51+
To use the CallSQL functionality, create an instance of the `CallSQL` class with the relevant parameters. Here's a basic example:
52+
53+
```python
54+
inputs = {
55+
"db_dialect": "postgresql",
56+
"db_query": "SELECT * FROM my_table WHERE id = :id",
57+
"db_username": "user",
58+
"db_password": "pass",
59+
"db_host": "localhost",
60+
"db_port": 5432,
61+
"db_name": "my_database",
62+
"db_query_template_values": {"id": 1}
63+
}
64+
65+
call_sql_step = CallSQL(inputs)
66+
results = call_sql_step.run()
67+
```
68+
69+
This example initializes the CallSQL step with necessary parameters and executes the query, returning the results for further processing.
70+
71+
## Conclusion
72+
73+
The CallSQL module provides a robust way to execute SQL statements with dynamic query capabilities through Mustache templating. It is designed for flexibility and ease of use in SQL database operations within the Patchwork ecosystem.

0 commit comments

Comments
 (0)