palmiot/sql-splitter

工具,用于从文件或管道中拆分 SQL 数据库。

1.0.0 2020-10-14 00:21 UTC

This package is auto-updated.

Last update: 2024-09-14 09:31:09 UTC


README

工具,用于从文件或流管道中拆分 SQL 数据库。

安装

composer require palmiot/sql-splitter

使用方法

基本上有两个类,SqlSplitter.php 和扩展 SqlSplitterCli.php。第一个类负责执行艰苦的工作,而第二个类提供命令行接口,并允许直接从 mysqldump 读取输出流管道。

SqlSplitter 的方法

该类为每个变量提供设置器和获取器,但实际工作由以下方法完成;

$splitter->setFrom('test/fixtures/alldatabases.sql');           // Expect a path or directory of physical file..
$splitter->setTo('./databases');                                // Set the path or directory where save the splitted databases;
$splitter->setPrefix('test');                                   // Set a prefix name for all splitted databases when them save.
$splitter->setData('CREATE DATABASE ... CREATE DATABASE ...');  // Expect an string with all databases.
$splitter->setDatabase('dbname', 'CREATE DATABASE...');         // For add manualy a database.
$splitter->split();                                             // Split and localize action of databases.
$splitter->save();                                              // Save splitted databases on .sql files to output directory.
$splitter->saveJoin();                                          // Save on a single .sql file all databases localized (the result will be similar to source).
$splitter->getLog();                                            // List all exception messages if occurred.
示例
include('vendor/autoload.php'); // Using composer

use Palmiot\SqlSplitter\SqlSplitter as Splitter;

$splitter = new Splitter();
$splitter->setFrom('test/fixtures/alldatabases.sql');
$splitter->setTo('databases');
$splitter->setPrefix('test');
$splitter->split();
$splitter->save();
$splitter->saveJoin();
foreach($splitter->getLog() as $entry){
    print($entry->getMessage());
}

SqlSplitterCli

该类的工作是监听参数或参数并准备它们传递给父类(SqlSplitter),最终请求工作并以字符串格式返回结果。

__construct 可以在传递给父类之前以数组格式监听参数(见下文)。调用者是方法 run()。基本用法如下;

cli.php
include('src/SqlSplitter.php');
include('src/SqlSplitterCli.php');  // Including manually (if not use composer)

$work = (new Palmiot\SqlSplitter\SqlSplitterCli([]))->run();    // Listen and work

print($work);   // STDOUT

一旦此文件被编写,我们可以从命令行发送以下参数;

****************************************************************************************************************
************************************************* SQL Splitter *************************************************
****************************************************************************************************************
*                                                                                                              *
*      -i :: File .sql with all databases                                                                      *
*      --input :: /path/alldatabases.sql                                                                       *
*                                                                                                              *
*      -s :: Stream of databases                                                                               *
*      --stream :: The output of mysqldump directly                                                            *
*                                                                                                              *
*      -o :: Folder where will save the splitted databases                                                     *
*      --output :: /save/here/                                                                                 *
*                                                                                                              *
*      -p :: Apply a generic prefix for all splitted databases                                                 *
*      --prefix :: test                                                                                        *
*                                                                                                              *
*      -j :: Save all dump                                                                                     *
*      --join :: Also save the all databases as a file                                                         *
*                                                                                                              *
*      -l :: Display logs                                                                                      *
*      --logs :: if something goes wrong                                                                       *
*                                                                                                              *
*      Usage:                                                                                                  *
*                                                                                                              *
*      // from input file                                                                                      *
*      $ php cli.php -i alldatabases.sql -o databases -p test -j -l                                            *
*                                                                                                              *
*      // from dump directly                                                                                   *
*      $ mysqldump -P port -h ip -u user -ppass --opt --all-databases | php cli.php -s -o databases -j -l      *
*                                                                                                              *
****************************************************************************************************************