|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Tabular Data Importer |
| 4 | +--- |
| 5 | + |
| 6 | +# Tabular Data |
| 7 | + |
| 8 | +<p class="message-notice">Starting with version <code>9.22.0</code></p> |
| 9 | + |
| 10 | +Since version `9.6` the package provides a common API to works with tabular data like structure. A tabular data |
| 11 | +is data organized in rows and columns. The fact that the package aim at interacting mainly with CSV does not |
| 12 | +restrict its usage to CSV document only, In fact if you can provide a tabular data structure to the package |
| 13 | +it should be able to manipulate such data with ease. Hence, the introduction of the `TabularData` interface.to allow |
| 14 | +interoperates with any tabular structure. |
| 15 | + |
| 16 | +As seen by the package a tabular data is: |
| 17 | + |
| 18 | +- a collection of similar records (preferably consistent in their size); |
| 19 | +- an optional header with unique values; |
| 20 | + |
| 21 | +This `TabularData` interface such contract by extending PHP's `IteratorAggregate` interface and by providing the |
| 22 | +`getHeader` method which returns a list of unique string (which can be empty if no header is provided). |
| 23 | + |
| 24 | +```php |
| 25 | +interface TabularData extends IteratorAggregate |
| 26 | +{ |
| 27 | + /** @return list<string> */ |
| 28 | + public function getHeader(): array; |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +## Basic Usage |
| 33 | + |
| 34 | +Once a `TabularData` implementing object is given to the `ResultSet` class it can be manipulated and inspected as if |
| 35 | +it was a CSV document. It will effectively access the full reading API provided by the package. |
| 36 | + |
| 37 | +For instance the `Reader` class implements the `TabularData` interface as such you can instantiate directly |
| 38 | +a `ResultSet` instance using the following code: |
| 39 | + |
| 40 | +```php |
| 41 | +$resultSet = ResultSet::createFromTabularData( |
| 42 | + Reader::createFromPath('path/to/file.csv') |
| 43 | +); |
| 44 | +``` |
| 45 | + |
| 46 | +## Database Importer usage |
| 47 | + |
| 48 | +A common source of tabular data are RDBMS result. From listing the content of a table to returning the result of |
| 49 | +a complex query on multiple tables with joins, RDBMS result are always express as tabular data. As such it is possible |
| 50 | +to convert them and manipulate via the package. To ease such manipulation the `ResultSet` class exposes the |
| 51 | +`ResultSet::createFromRdbms` method: |
| 52 | + |
| 53 | +```php |
| 54 | +$connection = new SQLite3( '/path/to/my/db.sqlite'); |
| 55 | +$stmt = $connection->query("SELECT * FROM users"); |
| 56 | +$stmt instanceof SQLite3Result || throw new RuntimeException('SQLite3 results not available'); |
| 57 | + |
| 58 | +$user24 = ResultSet::createFromRdbms($stmt)->nth(23); |
| 59 | +``` |
| 60 | + |
| 61 | +the `createFromRdbms` can be used with the following Database Extensions: |
| 62 | + |
| 63 | +- SQLite3 (`SQLite3Result` object) |
| 64 | +- MySQL Improved Extension (`mysqli_result` object) |
| 65 | +- PostgreSQL (`PgSql\Result` object returned by the `pg_get_result`) |
| 66 | +- PDO (`PDOStatement` object) |
| 67 | + |
| 68 | +Behind the scene the named constructor leverages the `League\Csv\RdbmsResult` class which implements the `TabularData` interface. |
| 69 | +This class is responsible from converting RDBMS results into `TabularData` instances. But you can also use the class |
| 70 | +as a standalone feature to quickly |
| 71 | + |
| 72 | +- retrieve column names from the listed Database extensions as follows: |
| 73 | + |
| 74 | +```php |
| 75 | +$connection = pg_connect("dbname=publisher"); |
| 76 | +$result = pg_query($connection, "SELECT * FROM authors"); |
| 77 | +$result !== false || throw new RuntimeException('PostgreSQL results not available'); |
| 78 | + |
| 79 | +$names = RdbmsResult::columnNames($result); |
| 80 | +//will return ['firstname', 'lastname', ...] |
| 81 | +``` |
| 82 | + |
| 83 | +- convert the result into an `Iterator` using the `records` public static method. |
| 84 | + |
| 85 | +```php |
| 86 | +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); |
| 87 | +$connection = new mysqli("localhost", "my_user", "my_password", "world"); |
| 88 | +$result = $connection->query("SELECT * FROM authors"); |
| 89 | +$result instanceOf mysqli_result || throw new RuntimeException('MySQL results not available'); |
| 90 | +foreach (RdbmsResult::records($stmt) as $record) { |
| 91 | + // returns each found record which match the processed query. |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +<p class="message-warning">The <code>PDOStatement</code> class does not support rewinding the object. |
| 96 | +To work around this limitation, the <code>RdbmsResult</code> stores the results in a |
| 97 | +<code>ArrayIterator</code> instance for cache which can lead to huge memory usage if the |
| 98 | +returned <code>PDOStatement</code> result is huge.</p> |
| 99 | + |
| 100 | +## Generic Importer Logic |
| 101 | + |
| 102 | +Implementing the `TabularData` should be straightforward, you can easily convert any structure into a `TabularData` instance |
| 103 | +using the following logic. Keep in mind that the codebase to generate an instance may vary depending on the source and the |
| 104 | +size of your data but the logic should stay the same. |
| 105 | + |
| 106 | +```php |
| 107 | +use League\Csv\ResultSet; |
| 108 | +use League\Csv\TabularData; |
| 109 | + |
| 110 | +$payload = <<<JSON |
| 111 | +[ |
| 112 | + {"id": 1, "firstname": "Jonn", "lastname": "doe", "email": "john@example.com"}, |
| 113 | + {"id": 2, "firstname": "Jane", "lastname": "doe", "email": "jane@example.com"}, |
| 114 | +] |
| 115 | +JSON; |
| 116 | + |
| 117 | +$tabularData = new class ($payload) implements TabularData { |
| 118 | + private readonly array $header; |
| 119 | + private readonly ArrayIterator $records; |
| 120 | + public function __construct(string $payload) |
| 121 | + { |
| 122 | + try { |
| 123 | + $data = json_decode($payload, true); |
| 124 | + $this->header = array_keys($data[0] ?? []); |
| 125 | + $this->records = new ArrayIterator($data); |
| 126 | + } catch (Throwable $exception) { |
| 127 | + throw new ValueError('The provided JSON payload could not be converted into a Tabular Data instance.', previous: $exception); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public function getHeader() : array |
| 132 | + { |
| 133 | + return $this->header; |
| 134 | + } |
| 135 | + |
| 136 | + public function getIterator() : Iterator |
| 137 | + { |
| 138 | + return $this->records; |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +$resultSet = ResultSet::createFromTabularData($tabularData); |
| 143 | +``` |
0 commit comments