asika/sql-splitter

This is a class that can split BIG SQL file or string as iterator so that can help us save memory when importing SQL to database.

1.0.0 2020-01-09 04:11 UTC

This package is auto-updated.

Last update: 2024-10-11 10:30:35 UTC


README

This is a class that can split BIG SQL file or string as iterator so that can help us save memory when importing SQL to database.

Install

composer require asika/sql-splitter

Usage

use Asika\SqlSplitter\SqlSplitter;

$it = SqlSplitter::splitFromFile(__DIR__ . '/path/to/db.sql');

// Loop iterator
foreach ($it as $query) {
    if (trim($query) !== '') {
      $db->prepare($query)->execute();
    }
}

// Or just convert to array
$queries = iterator_to_array($it);

// Available methods
SqlSplitter::splitSqlString('...');
SqlSplitter::splitFromFile('path/to/fil.sql');
SqlSplitter::splitFromPsr7Stream(new Stream('zip://file.zip#backup.sql'));
SqlSplitter::splitFromStream(fopen('s3://...', 'r'));

To use PSR-7 Stream, you must install psr/http-message