Skip to content

Commit 8b2c907

Browse files
authored
Merge pull request #3 from EncoreTechnologies/update/use_builtin_connectors
Use built in packages with overrides
2 parents 21c9a2f + 54892f5 commit 8b2c907

File tree

9 files changed

+172
-82
lines changed

9 files changed

+172
-82
lines changed

README.md

Lines changed: 156 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
# SQL Integration Pack
2-
Query, Insert, Update, and Delete information from PostgreSQL, SQLite, MsSQL, MySQL, Oracle, Firebird, and Sybase Databases
3-
4-
## Pre-Requisites
5-
This pack is set up to provide funcationality for the above databases. For MySQL and MsSQL we need to install 2 system packages.
6-
7-
#### [MySQL](https://pypi.org/project/mysqlclient/)
8-
``` shell
9-
yum install mysql-devel
10-
```
1+
[![Build Status](https://circleci.com/gh/StackStorm-Exchange/stackstorm-sql.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/StackStorm-Exchange/stackstorm-sql) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
112

12-
#### [MsSQL](https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017)
13-
``` shell
14-
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
15-
yum install msodbcsql17
16-
yum install unixODBC-devel
17-
```
3+
# SQL Integration Pack
4+
Query, Insert, Update, and Delete information from PostgreSQL, MsSQL, MySQL, Oracle, Firebird Databases. Additional databases can be added by installing the pre-requisite packes and passing the driver name as documented at [SQLAlchemy Dialects Doc](https://docs.sqlalchemy.org/en/latest/dialects/index.html).
185

196
## Quick Start
207

@@ -30,6 +17,17 @@ yum install unixODBC-devel
3017
st2 run sql.query host=test_serve.domain.tld username=test_user password=test_password database=test_database drivername=postgresql query="select * from test;"
3118
```
3219

20+
## Included Drivers
21+
This pack is already set up to connect to the databases listed above. Additional databases can be connected to but pre-requisite packages will need to be installed before the drivers will work. [SQLAlchemy Dialects Doc](https://docs.sqlalchemy.org/en/latest/dialects/index.html).
22+
23+
You can pass any of the following driver names without any additional packages needing installed.
24+
* `postgresql` - PostgreSQL databases
25+
* `mssql` - Microsoft SQL Server databases
26+
* `mysql` - MySQL/MariaDB databases
27+
* `oracle` - Oracle databases
28+
* `firebird` - Firebird databases
29+
30+
3331
## Configuration and Connecting to Databases
3432
Connecting to different types of databases is shown below. Connecting to different databases is done in the same manor except with sqlite where all you need to pass is the path to the database in the database option. This is show below. For more information about connections please refer to [SQLAlchemy Connection Docs](https://docs.sqlalchemy.org/en/latest/core/engines.html)
3533

@@ -59,17 +57,20 @@ connections:
5957
drivername: sqlite
6058
```
6159

62-
Each entry should contain
60+
Each entry should contain:
6361

64-
* ``host`` - Database hostname
65-
* ``username`` - Username to authenticate to DB
66-
* ``password`` - Password for DB authentication
67-
* ``database`` - Database to use
68-
* ``port`` - Port to connect to database on. If Default leave blank
69-
* ``drivername`` - The type of database that is being connected to.
62+
* `host` - Database hostname
63+
* `username` - Username to authenticate to DB
64+
* `password` - Password for DB authentication
65+
* `database` - Database to use
66+
* `port` - Port to connect to database on. If Default leave blank
67+
* `drivername` - The type of database that is being connected to.
7068

7169
When running actions, you can pass in the name of a connection, e.g.
72-
`st2 run sql.query connection="postgresql" query="select * from test;"`
70+
71+
``` shell
72+
st2 run sql.query connection="postgresql" query="SELECT * FROM test;"
73+
```
7374

7475
Alternatively, when running an action, you can pass in the host, username, password, database, port, drivername parameters. These parameters can also be used for overrides if you wish to use the configs as well.
7576

@@ -87,19 +88,143 @@ Alternatively, when running an action, you can pass in the host, username, passw
8788
| update | Update data in a database table. |
8889
| delete | Delete data from a database table. |
8990

91+
### Action Example - sql.query
92+
93+
`sql.query` can run any SQL query against a database. This can be used for simple `SELECT`
94+
statements:
95+
96+
```shell
97+
st2 run sql.query connection="postgresql" query="SELECT * FROM test;"
98+
```
99+
100+
Workflow usage:
101+
102+
``` yaml
103+
insert_data:
104+
action: sql.query
105+
input:
106+
connection: postgresql
107+
query: "SELECT * FROM test;"
108+
```
109+
110+
This action is also the one to use if you have a complex SQL statement that you want to run,
111+
but there isn't another action in this pack that supports what you're trying to do.
112+
In this case, simply pass in your arbitrary SQL statement into the `query` parameter and
113+
it will be executed:
114+
115+
```shell
116+
st2 run sql.query connection="postgresql" query="SELECT * FROM test JOIN somecrazytable ON id;"
117+
```
118+
119+
### Action Example - sql.insert
120+
121+
`sql.insert` is used to insert a single record into a table:
122+
123+
```shell
124+
st2 run sql.insert connection="postgresql" table="people" data='{"name": "bob", "phone": "1234567890"}'
125+
```
126+
127+
Workflow usage:
128+
129+
``` yaml
130+
insert_data:
131+
action: sql.insert
132+
input:
133+
connection: postgresql
134+
table: "people"
135+
data:
136+
name: "bob"
137+
phone: "1234567890"
138+
```
139+
140+
### Action Example - sql.insert_bulk
141+
142+
`sql.insert_bulk` is used to insert multiple records into a table. In this case the `data`
143+
parameter expects an array of objects, where each object is a record to insert.
144+
145+
```shell
146+
st2 run sql.insert connection="postgresql" table="people" data='[{"name": "bob", "phone": "1234567890"}, {"name": "alice", "phone": "0987654321"}]'
147+
```
148+
149+
Workflow usage:
150+
151+
``` yaml
152+
bulk_insert_data:
153+
action: sql.insert_bulk
154+
input:
155+
connection: postgresql
156+
table: "people"
157+
data:
158+
- name: "bob"
159+
phone: "1234567890"
160+
- name: "alice"
161+
phone: "0987654321"
162+
```
163+
164+
### Action Example - sql.update
165+
166+
`sql.update` is used to update records in a table using simple `WHERE` clauses.
167+
If you need to run complex `WHERE` conditions, then use the `sql.query` action instead.
168+
169+
```shell
170+
st2 run sql.insert connection="postgresql" table="people" where='{"name": "bob"}' update='{"phone": "5551234"}'
171+
```
172+
173+
Workflow usage:
174+
175+
``` yaml
176+
update_data:
177+
action: sql.update
178+
input:
179+
connection: postgresql
180+
table: "people"
181+
where:
182+
name: "bob"
183+
update:
184+
phone: "5551234"
185+
```
186+
187+
### Action Example - sql.delete
188+
189+
`sql.delete` is used to delete records in a table using simple `WHERE` clauses.
190+
If you need to run complex `WHERE` conditions, then use the `sql.query` action instead.
191+
192+
```shell
193+
st2 run sql.insert connection="postgresql" table="people" where='{"name": "bob"}'
194+
```
195+
196+
Workflow usage:
197+
198+
``` yaml
199+
update_data:
200+
action: sql.update
201+
input:
202+
connection: postgresql
203+
table: "people"
204+
where:
205+
name: "bob"
206+
```
207+
90208
## Where statements
91209

92210
The Update and Delete actions give the option to include where data into the query. This only works for AND statements.
93211

94-
Example:
212+
Example (YAML for workflows):
213+
```yaml
214+
where:
215+
column_1: "value_1"
216+
column_2: "value_2"
95217
```
96-
where = {
97-
'column_1': 'value_1',
98-
'column_2': 'value_2'
99-
}
100218

101-
Produces the statement:
219+
Example (JSON for `st2` CLI):
220+
```shell
221+
where='{"column_1": "value_1", "column_2": "value_2"}'
222+
```
223+
224+
Produces the SQL `WHERE` statement:
225+
226+
```sql
102227
WHERE column_1 == 'value_1' AND column_2 == 'value_2'
103228
```
104229

105-
For more complicated queries please use the query action.
230+
For more complicated queries please use the `sql.query` action.

actions/delete.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@
3434
drivername:
3535
type: string
3636
description: "Optional override of the database_type in <connection> (required if <connection> is not specified). The type of database that is being connected to."
37-
enum:
38-
- postgresql
39-
- sqlite
40-
- mssql
41-
- mysql
42-
- oracle
43-
- firebird
44-
- sybase
4537
required: false
4638
table:
4739
type: string

actions/insert.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@
3434
drivername:
3535
type: string
3636
description: "Optional override of the database_type in <connection> (required if <connection> is not specified). The type of database that is being connected to."
37-
enum:
38-
- postgresql
39-
- sqlite
40-
- mssql
41-
- mysql
42-
- oracle
43-
- firebird
44-
- sybase
4537
required: false
4638
table:
4739
type: string

actions/insert_bulk.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@
3434
drivername:
3535
type: string
3636
description: "Optional override of the database_type in <connection> (required if <connection> is not specified). The type of database that is being connected to."
37-
enum:
38-
- postgresql
39-
- sqlite
40-
- mssql
41-
- mysql
42-
- oracle
43-
- firebird
44-
- sybase
4537
required: false
4638
table:
4739
type: string

actions/lib/base_action.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
('port', False, None),
1414
('drivername', True, "")]
1515

16+
DEFAULT_KNOWN_DRIVER_CONNECTORS = {
17+
'postgresql': 'postgresql+psycopg2',
18+
'mysql': 'mysql+pymysql',
19+
'oracle': 'oracle+cx_oracle',
20+
'mssql': 'mssql+pymssql',
21+
'firebird': 'firebird+fdb'
22+
}
23+
1624

1725
class BaseAction(Action):
1826
def __init__(self, config):
@@ -106,6 +114,11 @@ def db_connection(self, kwargs_dict):
106114
# Get the connection details from either config or from action params
107115
connection = self.resolve_connection(kwargs_dict)
108116

117+
# Update Driver with a connector
118+
default_driver = DEFAULT_KNOWN_DRIVER_CONNECTORS.get(connection['drivername'], None)
119+
if default_driver:
120+
connection['drivername'] = default_driver
121+
109122
# Format the connection string
110123
database_connection_string = URL(**connection)
111124

actions/query.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@
3434
drivername:
3535
type: string
3636
description: "Optional override of the database_type in <connection> (required if <connection> is not specified). The type of database that is being connected to."
37-
enum:
38-
- postgresql
39-
- sqlite
40-
- mssql
41-
- mysql
42-
- oracle
43-
- firebird
44-
- sybase
4537
required: false
4638
query:
4739
type: string

actions/update.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@
3434
drivername:
3535
type: string
3636
description: "Optional override of the database_type in <connection> (required if <connection> is not specified). The type of database that is being connected to."
37-
enum:
38-
- postgresql
39-
- sqlite
40-
- mssql
41-
- mysql
42-
- oracle
43-
- firebird
44-
- sybase
4537
required: false
4638
table:
4739
type: string

config.schema.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,5 @@ connection:
3535
drivername:
3636
description: "The type of database that is being connected to."
3737
type: string
38-
enum:
39-
- postgresql
40-
- sqlite
41-
- mssql
42-
- mysql
43-
- oracle
44-
- firebird
45-
- sybase
4638
required: true
4739
additionalProperties: false

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sqlalchemy
2-
psycopg2
3-
mysqlclient
4-
pyodbc
2+
psycopg2 <=2.7.5
3+
pymysql
4+
pymssql
55
cx_Oracle
66
fdb

0 commit comments

Comments
 (0)