Skip to content

Commit 46cca71

Browse files
committed
Added lots of examples to the README
1 parent e6026bb commit 46cca71

File tree

1 file changed

+141
-20
lines changed

1 file changed

+141
-20
lines changed

README.md

Lines changed: 141 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
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)
2+
13
# SQL Integration Pack
24
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).
35

4-
## Included Drivers
5-
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).
6-
7-
You can pass any of the following driver names without any additional packages needing installed.
8-
```
9-
postgresql
10-
mssql
11-
mysql
12-
oracle
13-
firebird
14-
```
15-
166
## Quick Start
177

188
1. Install the pack
@@ -27,6 +17,17 @@ firebird
2717
st2 run sql.query host=test_serve.domain.tld username=test_user password=test_password database=test_database drivername=postgresql query="select * from test;"
2818
```
2919

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+
3031
## Configuration and Connecting to Databases
3132
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)
3233

@@ -56,17 +57,20 @@ connections:
5657
drivername: sqlite
5758
```
5859

59-
Each entry should contain
60+
Each entry should contain:
6061

61-
* ``host`` - Database hostname
62-
* ``username`` - Username to authenticate to DB
63-
* ``password`` - Password for DB authentication
64-
* ``database`` - Database to use
65-
* ``port`` - Port to connect to database on. If Default leave blank
66-
* ``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.
6768

6869
When running actions, you can pass in the name of a connection, e.g.
69-
`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+
```
7074

7175
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.
7276

@@ -84,6 +88,123 @@ Alternatively, when running an action, you can pass in the host, username, passw
8488
| update | Update data in a database table. |
8589
| delete | Delete data from a database table. |
8690

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+
87208
## Where statements
88209

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

0 commit comments

Comments
 (0)