Skip to content

Commit f6a1304

Browse files
authored
Merge pull request #1 from php-etl/feature/initilizer-queries
Updated plugin to integrate initializer and de-initializer requests
2 parents 6fa02fc + 0b9c2db commit f6a1304

33 files changed

+3084
-1129
lines changed

README.md

Lines changed: 94 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,67 @@
11
# SQL Plugin
22

3-
This plugin allows you to retrieve or insert data from/to a database.
3+
## What is it ?
4+
5+
The SQL plugin allows you to write your own SQL queries and use them into the Pipeline stack.
6+
7+
SQL, Structured Query Language, is a language for manipulating databases.
48

59
## Installation
610

7-
```
11+
```shell
812
composer require php-etl/sql-plugin
913
```
1014

11-
## Basic usage
15+
## Usage
16+
17+
### Database connection
18+
The SQL plugin uses the PDO extension and relies on its interface to access databases using
19+
the `dsn`, `username` and `password` parameters.
20+
21+
This connection must be present in any case, whether it be when defining the extractor,
22+
loader or lookup.
23+
24+
```yaml
25+
connection:
26+
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=kiboko'
27+
username: username
28+
password: password
29+
```
30+
31+
It is possible to specify options at the time of this connection using `options`. Currently, it is only possible to
32+
specify if the database connection should be persistent.
33+
34+
```yaml
35+
connection:
36+
# ...
37+
options:
38+
persistent: true
39+
```
1240

1341
### Building an extractor
1442
```yaml
1543
sql:
1644
extractor:
1745
query: 'SELECT * FROM table1'
18-
connection:
19-
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=kiboko'
20-
username: username
21-
password: password
46+
connection:
47+
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=kiboko'
48+
username: username
49+
password: password
2250
```
2351
### Building a lookup
2452

2553
```yaml
2654
sql:
2755
lookup:
2856
query: 'SELECT * FROM table2 WHERE bar = foo'
29-
connection:
30-
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=kiboko'
31-
username: username
32-
password: password
3357
merge:
3458
map:
3559
- field: '[options]'
3660
expression: 'lookup["name"]'
61+
connection:
62+
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=kiboko'
63+
username: username
64+
password: password
3765
3866
```
3967

@@ -42,33 +70,33 @@ sql:
4270
sql:
4371
loader:
4472
query: 'INSERT INTO table1 VALUES (bar, foo, barfoo)'
45-
connection:
46-
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=kiboko'
47-
username: username
48-
password: password
73+
connection:
74+
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=kiboko'
75+
username: username
76+
password: password
4977
5078
```
5179

52-
## Advanced Usage : using params in your queries
80+
## Advanced Usage
5381

54-
Thanks to the SQL plugin, it is possible to write your queries with parameters.
82+
### Using params in your queries
5583

56-
### With params
84+
Thanks to the SQL plugin, it is possible to write your queries with parameters.
5785

5886
If you write a prepared statement using named parameters (`:param`), your parameter key in the configuration will be
5987
the name of your parameter without the `:` :
6088

6189
```yaml
6290
sql:
63-
# ...
64-
query: 'INSERT INTO table1 VALUES (:value1, :value2, :value3)'
65-
params:
66-
- key: value1
67-
value: '@=input["value1"]'
68-
- key: value2
69-
value: '@=input["value3"]'
70-
- key: value3
71-
value: '@=input["value3"]'
91+
loader:
92+
query: 'INSERT INTO table1 VALUES (:value1, :value2, :value3)'
93+
parameters:
94+
- key: value1
95+
value: '@=input["value1"]'
96+
- key: value2
97+
value: '@=input["value3"]'
98+
- key: value3
99+
value: '@=input["value3"]'
72100
# ...
73101
```
74102

@@ -77,16 +105,46 @@ configuration will be its position (starting from 1) :
77105

78106
```yaml
79107
sql:
108+
loader:
109+
query: 'INSERT INTO table1 VALUES (?, ?, ?)'
110+
parameters:
111+
- key: 1
112+
value: '@=input["value1"]'
113+
- key: 2
114+
value: '@=input["value3"]'
115+
- key: 3
116+
value: '@=input["value3"]'
80117
# ...
81-
query: 'INSERT INTO table1 VALUES (?, ?, ?)'
82-
params:
83-
- key: 1
84-
value: '@=input["value1"]'
85-
- key: 2
86-
value: '@=input["value3"]'
87-
- key: 3
88-
value: '@=input["value3"]'
89-
# ...
118+
```
119+
120+
### Creating before and after queries
121+
122+
In some cases, you may need to run queries in order to best prepare for the execution of your pipeline.
123+
124+
#### Before queries
125+
Before queries will be executed before performing the query written in the configuration. Often, these are
126+
queries that set up the database.
127+
128+
```yaml
129+
sql:
130+
before:
131+
queries:
132+
- 'CREATE TABLE foo (id INTEGER NOT NULL, value VARCHAR(255) NOT NULL)'
133+
- 'INSERT INTO foo (id, value) VALUES (1, "Lorem ipsum dolor")'
134+
- 'INSERT INTO foo (id, value) VALUES (2, "Sit amet consecutir")'
135+
# ...
136+
```
137+
138+
#### After queries
139+
After queries will be executed after performing the query written in the configuration. Often, these are
140+
queries that clean up the database.
141+
142+
```yaml
143+
sql:
144+
after:
145+
queries:
146+
- 'DROP TABLE foo'
147+
# ...
90148
```
91149

92150
## See more

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
"php-etl/satellite-toolbox": "^0.1.0",
2121
"php-etl/fast-map-plugin": "^0.4.0"
2222
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^9.0",
25+
"php-etl/phpunit-extension": "^0.2.0",
26+
"adlawson/vfs": "^0.12.1",
27+
"phpstan/phpstan": "^0.12.93",
28+
"php-etl/sql-flow": "^0.1.0"
29+
},
2330
"autoload": {
2431
"psr-4": {
2532
"Kiboko\\Plugin\\SQL\\": "src/"
@@ -40,12 +47,5 @@
4047
},
4148
"config": {
4249
"bin-dir": "bin"
43-
},
44-
"require-dev": {
45-
"symfony/console": "^5.2",
46-
"symfony/yaml": "^5.2",
47-
"phpunit/phpunit": "^9.0",
48-
"php-etl/phpunit-extension": "^0.2.0",
49-
"adlawson/vfs": "^0.12.1"
5050
}
5151
}

0 commit comments

Comments
 (0)