|
1 | | -# sql Integration Pack |
| 1 | +# SQL Integration Pack |
| 2 | +Query, Insert, Update, and Delete information from PostgreSQL, SQLite, MsSQL, MySQL, Oracle, Firebird, and Sybase Databases |
2 | 3 |
|
3 | | -## Configuration |
4 | | -TODO: Describe configuration |
| 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. |
5 | 6 |
|
| 7 | +#### [MySQL](https://pypi.org/project/mysqlclient/) |
| 8 | +``` shell |
| 9 | +yum install mysql-devel |
| 10 | +``` |
6 | 11 |
|
7 | | -# Sensors |
| 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 | +``` |
8 | 18 |
|
9 | | -## Example Sensor |
10 | | -TODO: Describe sensor |
| 19 | +## Quick Start |
11 | 20 |
|
| 21 | +1. Install the pack |
12 | 22 |
|
13 | | -# Actions |
| 23 | + ``` shell |
| 24 | + st2 pack install sql |
| 25 | + ``` |
14 | 26 |
|
15 | | -## example |
16 | | -TODO: Describe action |
| 27 | +2. Execute an action (example: query) |
| 28 | + |
| 29 | + ``` shell |
| 30 | + st2 run sql.query host=test_serve.domain.tld username=test_user password=test_password database=test_database drivername=postgresql query="select * from test;" |
| 31 | + ``` |
| 32 | + |
| 33 | +## Configuration and Connecting to Databases |
| 34 | +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) |
| 35 | + |
| 36 | +Copy the example configuration in [sql.yaml.example](./sql.yaml.example) |
| 37 | +to `/opt/stackstorm/configs/sql.yaml` and edit as required. |
| 38 | + |
| 39 | +It can contain an array of one or more sets of SQL connection parameters, like this: |
| 40 | + |
| 41 | +``` yaml |
| 42 | +--- |
| 43 | +connections: |
| 44 | + postgresql: |
| 45 | + host: postgresql_db.domain.tld |
| 46 | + |
| 47 | + password: Password |
| 48 | + database: TestDatabase |
| 49 | + port: 5432 |
| 50 | + drivername: postgresql |
| 51 | + mysql: |
| 52 | + host: mysql_db.domain.tld |
| 53 | + |
| 54 | + password: NewPassword |
| 55 | + database: TestDatabase |
| 56 | + drivername: mysql |
| 57 | + sqlite: |
| 58 | + database: /path/to/db.sqlite |
| 59 | + drivername: sqlite |
| 60 | +``` |
| 61 | + |
| 62 | +Each entry should contain |
| 63 | + |
| 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. |
| 70 | + |
| 71 | +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;"` |
| 73 | + |
| 74 | +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. |
| 75 | + |
| 76 | +**Note** : When modifying the configuration in `/opt/stackstorm/configs/` please remember to tell StackStorm to load these new values by running `st2ctl reload --register-configs` |
| 77 | + |
| 78 | +# Usage |
| 79 | + |
| 80 | +## Actions |
| 81 | + |
| 82 | +| Action | Description | |
| 83 | +|--------|-------------| |
| 84 | +| query | Generic query action to get inforamtion from the database. | |
| 85 | +| insert | Insert data into a database table. Insert data is passed as an object. | |
| 86 | +| insert_bulk | Bulk insert data into a database table. Insert data is passed as an array of objects. | |
| 87 | +| update | Update data in a database table. | |
| 88 | +| delete | Delete data from a database table. | |
| 89 | + |
| 90 | +## Where statements |
| 91 | + |
| 92 | +The Update and Delete actions give the option to include where data into the query. This only works for AND statements. |
| 93 | + |
| 94 | +Example: |
| 95 | +``` |
| 96 | +where = { |
| 97 | + 'column_1': 'value_1', |
| 98 | + 'column_2': 'value_2' |
| 99 | +} |
| 100 | +
|
| 101 | +Produces the statement: |
| 102 | +WHERE column_1 == 'value_1' AND column_2 == 'value_2' |
| 103 | +``` |
| 104 | + |
| 105 | +For more complicated queries please use the query action. |
0 commit comments