读取Sql文件并返回可执行的sql语句

<?php
namespace util;

/**
* 读取Sql文件并返回可执行的sql语句
*/
class Sql
{
/**
* 从sql文件获取纯sql语句
* @param string $sql_file sql文件路径
* @param bool $string 如果为真,则只返回一条sql语句,默认以数组形式返回
* @param array $replace 替换前缀,如:['my_' => 'me_'],表示将表前缀"my_"替换成"me_"
* 这种前缀替换方法不一定准确,比如正常内容内有跟前缀相同的字符,也会被替换
* @return mixed
*/
public static function getSqlFromFile($sql_file = '', $string = false, $replace = [])
{
if (!file_exists($sql_file)) {
return false;
}

// 读取sql文件内容
$handle = self::read_file($sql_file);

// 分割语句
$handle = self::parseSql($handle, $string, $replace);

return $handle;
}

/**
* 分割sql语句
* @param string $content sql内容
* @param bool $string 如果为真,则只返回一条sql语句,默认以数组形式返回
* @param array $replace 替换前缀,如:['my_' => 'me_'],表示将表前缀my_替换成me_
* @return array|string 除去注释之后的sql语句数组或一条语句
*/
public static function parseSql($content = '', $string = false, $replace = [])
{
// 被替换的前缀
$from = '';
// 要替换的前缀
$to = '';

// 替换表前缀
if (!empty($replace)) {
$to = current($replace);
$from = current(array_flip($replace));
}

if ($content != '') {
// 纯sql内容
$pure_sql = [];

// 多行注释标记
$comment = false;

// 按行分割,兼容多个平台
$content = str_replace(["\r\n", "\r"], "\n", $content);
$content = explode("\n", trim($content));

// 循环处理每一行
foreach ($content as $key => $line) {
// 跳过空行
if ($line == '') {
continue;
}

// 跳过以#或者--开头的单行注释
if (preg_match("/^(#|--)/", $line)) {
continue;
}

// 跳过以/**/包裹起来的单行注释
if (preg_match("/^\/\*(.*?)\*\//", $line)) {
continue;
}

// 多行注释开始
if (substr($line, 0, 2) == '/*') {
$comment = true;
continue;
}

// 多行注释结束
if (substr($line, -2) == '*/') {
$comment = false;
continue;
}

// 多行注释没有结束,继续跳过
if ($comment) {
continue;
}

// 替换表前缀
if ($from != '') {
$line = str_replace('`'.$from, '`'.$to, $line);
}

// sql语句
array_push($pure_sql, $line);
}

// 只返回一条语句
if ($string) {
return implode("",$pure_sql);
}

// 以数组形式返回sql语句
$pure_sql = implode("\n",$pure_sql);
$pure_sql = explode(";\n", $pure_sql);
return $pure_sql;
} else {
return $string == true ? '' : [];
}
}

/**
* 读取文件内容
* @param $filename 文件名
* @return string 文件内容
*/
public static function read_file($filename) {
$content = '';
if(function_exists('file_get_contents')) {
@$content = file_get_contents($filename);
} else {
if(@$fp = fopen($filename, 'r')) {
@$content = fread($fp, filesize($filename));
@fclose($fp);
}
}
return $content;
}
}


相关推荐

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3

  • Thinkphp5.1路径常量

    1 配置文件位置根目录/config/template.php2 配置文件内容&lt;?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reser

  • Thinkphp5.0路径常量

    1 配置文件位置根目录/application/模块名/config.php2 配置文件内容&lt;?php//配置文件return [ // 后台视图输出字符串内容替换 'view_replace_str' =&gt; [ '__PUBLIC__' =&gt; '/', '__STATIC__' =&gt; '/static', '__CONSOLE__' =&gt; '/static/console', '__CONSOLE_CSS__' =&gt; '/static/console/css', '__CONSOLE_IMAGES__' =&gt; '/static/console/ima

  • wp站点防止别人进行DDOS攻击

    1 简介wp站点防止别人进行DDOS攻击。2 配置位置位置:根目录/wp-config.php3 配置内容在【根目录/wp-config.php】文件的开头添加如下代码:if(strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false){ $protocol = $_SERVER['SERVER_PROTOCOL'] ?? ''; if(!in_array($protocol, ['HTTP/1.1', 'HTTP/2', 'HTTP/2.0', 'HTTP/3'], true)){ $protocol = 'HTTP/1.0'; } hea

  • 只读属性

    1 只读属性简介只读属性的声明方式类似于普通属性,但需要使用 readonly 关键字。2 只读属性例子class Point { public readonly float $x; public readonly float $y; public function __construct(float $x, float $y) { $this-&gt;x = $x; $this-&gt;y = $y; }}$point = new Point(3.5, 2.8);echo $point-&gt;x; // 输出: 3.5echo $point-&gt;y; // 输出: 2.8// 下面的尝