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
812composer 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
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
5886If you write a prepared statement using named parameters (`:param`), your parameter key in the configuration will be
5987the name of your parameter without the `:` :
6088
6189` ` ` yaml
6290sql:
63- # ...
64- query : ' INSERT INTO table1 VALUES (:value1, :value2, :value3)'
65- parameters :
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
79107sql:
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- parameters :
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
0 commit comments