|
| 1 | +# SQL Plugin |
| 2 | + |
| 3 | +This plugin allows you to retrieve or insert data from/to a database. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +``` |
| 8 | +composer require php-etl/sql-plugin |
| 9 | +``` |
| 10 | + |
| 11 | +## Basic usage |
| 12 | + |
| 13 | +### Building an extractor |
| 14 | +```yaml |
| 15 | +sql: |
| 16 | + extractor: |
| 17 | + query: 'SELECT * FROM table1' |
| 18 | + connection: |
| 19 | + dsn: 'mysql:host=127.0.0.1;port=3306;dbname=kiboko' |
| 20 | + username: username |
| 21 | + password: password |
| 22 | +``` |
| 23 | +### Building a lookup |
| 24 | +
|
| 25 | +```yaml |
| 26 | +sql: |
| 27 | + lookup: |
| 28 | + 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 |
| 33 | + merge: |
| 34 | + map: |
| 35 | + - field: '[options]' |
| 36 | + expression: 'lookup["name"]' |
| 37 | + |
| 38 | +``` |
| 39 | + |
| 40 | +### Building a loader |
| 41 | +```yaml |
| 42 | +sql: |
| 43 | + loader: |
| 44 | + 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 |
| 49 | + |
| 50 | +``` |
| 51 | + |
| 52 | +## Advanced Usage : using params in your queries |
| 53 | + |
| 54 | +Thanks to the SQL plugin, it is possible to write your queries with parameters. |
| 55 | + |
| 56 | +### With params |
| 57 | + |
| 58 | +If you write a prepared statement using named parameters (`:param`), your parameter key in the configuration will be |
| 59 | +the name of your parameter without the `:` : |
| 60 | + |
| 61 | +```yaml |
| 62 | +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"]' |
| 72 | + # ... |
| 73 | +``` |
| 74 | + |
| 75 | +If you are using a prepared statement using interrogative markers (`?`), your parameter key in the |
| 76 | +configuration will be its position (starting from 1) : |
| 77 | + |
| 78 | +```yaml |
| 79 | +sql: |
| 80 | + # ... |
| 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 | + # ... |
| 90 | +``` |
| 91 | + |
| 92 | +## See more |
| 93 | +If you want to see complete configurations, please go to the [examples](/examples) folder. |
0 commit comments