gggordon/php-sql-replacer

在 `SQL` 文件中替换值,特别是序列化的 PHP 值。适用于数据库迁移/克隆。

2.0.0 2020-10-19 00:57 UTC

This package is auto-updated.

Last update: 2024-09-19 11:31:05 UTC


README

Build Status Scrutinizer Codecov License Latest Stable Version Latest Unstable Version

PHP SQL Replacer

一个用于在 SQL 文件中替换字符串/值的工具,尤其是用于替换序列化的 php 值。适用于数据库迁移/克隆。

安装

composer require gggordon/php-sql-replacer

用法

命令行用法

./bin/php-sql-replacer --input-file-path="./original.sql" --output-file-path="./updated.sql" --match="Original Text" --replace="New Text"

Required Options:
--input-file-path   : Path of input file
--output-file-path  : Path of output file
--match             : Exact string to look for
--replace           : String to replace match with

使用代码

如果您已经将内容存储为字符串,您可以按照以下示例替换内容。

<?php

require_once dirname(__FILE__) . '/vendor/autoload.php';

$contents ="insert into test (column1, column2) values ('I like trading','Another Value')";

$replacer = new \PhpSqlReplacer\PhpSqlReplacer();
$updatedContents = $replacer->replaceValue(
    $contents, 
    "trading", 
    "butter"
);

echo $updatedContents;

以获得以下输出

insert into test (column1, column2) values ('I like butter','Another Value')

或者

如果您正在从文件中提取内容

<?php

require_once dirname(__FILE__) . '/vendor/autoload.php';

$replacer = new \PhpSqlReplacer\PhpSqlReplacer();
$updatedContents = $replacer->replaceValueFromFile("/path/to/original_file.sql", "trading", "butter", "/path/to/output_file.sql");

echo $updatedContents;

以获得以下输出

insert into test (column1, column2) values ('I like butter','Another Value')

并在 /path/to/output_file.sql 创建新文件。

测试

使用 phpunit 进行测试

phpunit

文档

使用 phpdox 生成文档

phpdox

许可证

MIT 许可证

gggordon