|
@@ -702,108 +702,87 @@ function DedeSearchAPIURL($action, $parms=array()) |
|
|
|
|
|
|
|
|
return $finalUrl; |
|
|
return $finalUrl; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function ConvertMysqlToSqlite($mysqlQuery) { |
|
|
function ConvertMysqlToSqlite($mysqlQuery) { |
|
|
// 移除 CHARACTER SET 和 COLLATE |
|
|
|
|
|
|
|
|
//移除CHARACTER SET和COLLATE |
|
|
$query = preg_replace('/character set \S+/i', '', $mysqlQuery); |
|
|
$query = preg_replace('/character set \S+/i', '', $mysqlQuery); |
|
|
$query = preg_replace('/collate \S+/i', '', $query); |
|
|
$query = preg_replace('/collate \S+/i', '', $query); |
|
|
|
|
|
|
|
|
// 移除 unsigned 关键字 |
|
|
|
|
|
|
|
|
//移除unsigned关键字 |
|
|
$query = str_replace('unsigned', '', $query); |
|
|
$query = str_replace('unsigned', '', $query); |
|
|
|
|
|
|
|
|
// 替换 MySQL 的整数类型为 SQLite 的 INTEGER |
|
|
|
|
|
|
|
|
//替换MySQL的整数类型为SQLite的INTEGER |
|
|
$query = preg_replace('/\b(TINY|SMALL|MEDIUM|BIG)?INT\(\d+\)/i', 'INTEGER', $query); |
|
|
$query = preg_replace('/\b(TINY|SMALL|MEDIUM|BIG)?INT\(\d+\)/i', 'INTEGER', $query); |
|
|
|
|
|
|
|
|
// 替换 AUTO_INCREMENT 为 PRIMARY KEY AUTOINCREMENT (仅适用于 INTEGER 类型) |
|
|
|
|
|
|
|
|
//替换AUTO_INCREMENT为PRIMARY KEY AUTOINCREMENT (仅适用于INTEGER类型) |
|
|
$query = preg_replace('/\bINTEGER\s+NOT NULL\s+PRIMARY KEY AUTOINCREMENT/i', 'INTEGER PRIMARY KEY AUTOINCREMENT', $query); |
|
|
$query = preg_replace('/\bINTEGER\s+NOT NULL\s+PRIMARY KEY AUTOINCREMENT/i', 'INTEGER PRIMARY KEY AUTOINCREMENT', $query); |
|
|
$query = preg_replace('/\bAUTO_INCREMENT\b/i', '', $query); |
|
|
$query = preg_replace('/\bAUTO_INCREMENT\b/i', '', $query); |
|
|
|
|
|
|
|
|
// 移除 MySQL 特有的 ENGINE、CHARSET、COLLATE、TYPE 选项 |
|
|
|
|
|
|
|
|
//移除MySQL特有的ENGINE、CHARSET、COLLATE、TYPE选项 |
|
|
$query = preg_replace('/ENGINE=\S+/i', '', $query); |
|
|
$query = preg_replace('/ENGINE=\S+/i', '', $query); |
|
|
$query = preg_replace('/DEFAULT CHARSET=\S+/i', '', $query); |
|
|
$query = preg_replace('/DEFAULT CHARSET=\S+/i', '', $query); |
|
|
$query = preg_replace('/COLLATE=\S+/i', '', $query); |
|
|
$query = preg_replace('/COLLATE=\S+/i', '', $query); |
|
|
$query = preg_replace('/TYPE=MyISAM;/i', '', $query); |
|
|
$query = preg_replace('/TYPE=MyISAM;/i', '', $query); |
|
|
|
|
|
|
|
|
// 移除 COMMENT 语法(SQLite 不支持) |
|
|
|
|
|
|
|
|
//移除COMMENT语法(SQLite不支持) |
|
|
$query = preg_replace('/COMMENT\s+\'[^\']*\'/i', '', $query); |
|
|
$query = preg_replace('/COMMENT\s+\'[^\']*\'/i', '', $query); |
|
|
|
|
|
|
|
|
// 移除 KEY 和 UNIQUE KEY 定义(SQLite 会自动管理索引),同时处理 USING BTREE |
|
|
|
|
|
|
|
|
//移除KEY和UNIQUE KEY定义(SQLite会自动管理索引),同时处理USING BTREE |
|
|
$query = preg_replace('/,?\s*KEY\s+\S+\s*\([^)]*\)\s*(USING BTREE)?/i', '', $query); |
|
|
$query = preg_replace('/,?\s*KEY\s+\S+\s*\([^)]*\)\s*(USING BTREE)?/i', '', $query); |
|
|
$query = preg_replace('/,?\s*UNIQUE KEY\s+\S+\s*\([^)]*\)\s*(USING BTREE)?/i', '', $query); |
|
|
$query = preg_replace('/,?\s*UNIQUE KEY\s+\S+\s*\([^)]*\)\s*(USING BTREE)?/i', '', $query); |
|
|
|
|
|
|
|
|
// 移除不完整的 UNIQUE 定义 |
|
|
|
|
|
|
|
|
//移除不完整的UNIQUE定义 |
|
|
$query = preg_replace('/,?\s*UNIQUE\s*(?!\()/', '', $query); |
|
|
$query = preg_replace('/,?\s*UNIQUE\s*(?!\()/', '', $query); |
|
|
|
|
|
|
|
|
// 替换 ENUM 和 SET 为 TEXT |
|
|
|
|
|
|
|
|
//替换ENUM和SET为TEXT |
|
|
$query = preg_replace('/\b(ENUM|SET)\([^)]*\)/i', 'TEXT', $query); |
|
|
$query = preg_replace('/\b(ENUM|SET)\([^)]*\)/i', 'TEXT', $query); |
|
|
|
|
|
|
|
|
// 替换 MEDIUMTEXT 为 TEXT |
|
|
|
|
|
|
|
|
//替换MEDIUMTEXT为TEXT |
|
|
$query = preg_replace('/\bMEDIUMTEXT\b/i', 'TEXT', $query); |
|
|
$query = preg_replace('/\bMEDIUMTEXT\b/i', 'TEXT', $query); |
|
|
|
|
|
|
|
|
// 处理 DEFAULT CURRENT_TIMESTAMP |
|
|
|
|
|
|
|
|
//处理DEFAULT CURRENT_TIMESTAMP |
|
|
$query = preg_replace('/DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP/i', 'DEFAULT CURRENT_TIMESTAMP', $query); |
|
|
$query = preg_replace('/DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP/i', 'DEFAULT CURRENT_TIMESTAMP', $query); |
|
|
|
|
|
|
|
|
// 处理 DEFAULT 值 |
|
|
|
|
|
|
|
|
//处理DEFAULT值 |
|
|
$query = preg_replace('/DEFAULT\s+\'([^\']+)\'/i', 'DEFAULT "$1"', $query); |
|
|
$query = preg_replace('/DEFAULT\s+\'([^\']+)\'/i', 'DEFAULT "$1"', $query); |
|
|
|
|
|
|
|
|
// 处理 PRIMARY KEY 只能用于 INTEGER |
|
|
|
|
|
|
|
|
//处理PRIMARY KEY只能用于INTEGER |
|
|
if (preg_match('/PRIMARY KEY \(`(\w+)`\)/', $query, $matches)) { |
|
|
if (preg_match('/PRIMARY KEY \(`(\w+)`\)/', $query, $matches)) { |
|
|
$primaryKeyColumn = $matches[1]; |
|
|
$primaryKeyColumn = $matches[1]; |
|
|
$query = preg_replace('/,?\s*PRIMARY KEY\s*\(`' . $primaryKeyColumn . '`\)/i', '', $query); |
|
|
$query = preg_replace('/,?\s*PRIMARY KEY\s*\(`' . $primaryKeyColumn . '`\)/i', '', $query); |
|
|
$query = preg_replace('/(`' . $primaryKeyColumn . '`\s+INTEGER)/i', '$1 PRIMARY KEY', $query); |
|
|
$query = preg_replace('/(`' . $primaryKeyColumn . '`\s+INTEGER)/i', '$1 PRIMARY KEY', $query); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 处理 CONCAT 替换为 SQLite 兼容形式 |
|
|
|
|
|
|
|
|
//处理CONCAT替换为SQLite兼容形式 |
|
|
if (preg_match('/CONCAT\(([^)]*?)\)/i', $query, $matches)) { |
|
|
if (preg_match('/CONCAT\(([^)]*?)\)/i', $query, $matches)) { |
|
|
$query = preg_replace('/CONCAT\(([^)]*?)\)/i', str_replace(",", "||", $matches[1]), $query); |
|
|
$query = preg_replace('/CONCAT\(([^)]*?)\)/i', str_replace(",", "||", $matches[1]), $query); |
|
|
$query = str_replace("'||'", "','", $query); |
|
|
$query = str_replace("'||'", "','", $query); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 修正 FIND_IN_SET 替换 |
|
|
|
|
|
|
|
|
//修正FIND_IN_SET替换 |
|
|
$query = preg_replace("/FIND_IN_SET\('([\w]+)', arc.flag\)>0/i", "(',' || arc.flag || ',') LIKE '%,\\1,%'", $query); |
|
|
$query = preg_replace("/FIND_IN_SET\('([\w]+)', arc.flag\)>0/i", "(',' || arc.flag || ',') LIKE '%,\\1,%'", $query); |
|
|
$query = preg_replace("/FIND_IN_SET\('([\w]+)', arc.flag\)<1/i", "(',' || arc.flag || ',') NOT LIKE '%,\\1,%'", $query); |
|
|
$query = preg_replace("/FIND_IN_SET\('([\w]+)', arc.flag\)<1/i", "(',' || arc.flag || ',') NOT LIKE '%,\\1,%'", $query); |
|
|
|
|
|
|
|
|
// 修正 FIND_IN_SET 替换(允许列名包含点号) |
|
|
|
|
|
|
|
|
//修正FIND_IN_SET替换(允许列名包含点号) |
|
|
$query = preg_replace_callback( |
|
|
$query = preg_replace_callback( |
|
|
"/FIND_IN_SET\s*\(\s*'([^']+)'\s*,\s*([a-zA-Z0-9_`\.]+)\s*\)/i", |
|
|
"/FIND_IN_SET\s*\(\s*'([^']+)'\s*,\s*([a-zA-Z0-9_`\.]+)\s*\)/i", |
|
|
function ($matches) { |
|
|
function ($matches) { |
|
|
// 返回 SQLite 兼容的 LIKE 语法 |
|
|
|
|
|
|
|
|
//返回SQLite兼容的LIKE语法 |
|
|
return "(',' || " . $matches[2] . " || ',' LIKE '%," . $matches[1] . ",%')"; |
|
|
return "(',' || " . $matches[2] . " || ',' LIKE '%," . $matches[1] . ",%')"; |
|
|
}, |
|
|
}, |
|
|
$query |
|
|
$query |
|
|
); |
|
|
); |
|
|
// 替换 FIELD 函数为 CASE 表达式 |
|
|
|
|
|
|
|
|
//替换FIELD函数为CASE表达式 |
|
|
$query = preg_replace_callback( |
|
|
$query = preg_replace_callback( |
|
|
'/\bFIELD\s*\(\s*([^,]+)\s*,\s*((?:\'[^\']+\'|`[^`]+`|[^),]+(?:,\s*)?)+)\s*\)/i', |
|
|
'/\bFIELD\s*\(\s*([^,]+)\s*,\s*((?:\'[^\']+\'|`[^`]+`|[^),]+(?:,\s*)?)+)\s*\)/i', |
|
|
function ($matches) { |
|
|
function ($matches) { |
|
|
$field = trim($matches[1]); |
|
|
$field = trim($matches[1]); |
|
|
$values = trim($matches[2]); |
|
|
$values = trim($matches[2]); |
|
|
|
|
|
|
|
|
// 更精确分割值列表(支持带引号、反引号及无空格分隔的数值) |
|
|
|
|
|
|
|
|
//更精确分割值列表(支持带引号、反引号及无空格分隔的数值) |
|
|
preg_match_all('/\'[^\']+\'|`[^`]+`|\d+|\w+/', $values, $valueParts); |
|
|
preg_match_all('/\'[^\']+\'|`[^`]+`|\d+|\w+/', $values, $valueParts); |
|
|
|
|
|
|
|
|
$cases = []; |
|
|
$cases = []; |
|
|
$position = 1; |
|
|
$position = 1; |
|
|
foreach ($valueParts[0] as $value) { |
|
|
foreach ($valueParts[0] as $value) { |
|
|
$cases[] = "WHEN $field = $value THEN $position"; |
|
|
$cases[] = "WHEN $field = $value THEN $position"; |
|
|
$position++; |
|
|
$position++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return "(CASE " . implode(' ', $cases) . " ELSE 0 END)"; |
|
|
return "(CASE " . implode(' ', $cases) . " ELSE 0 END)"; |
|
|
}, |
|
|
}, |
|
|
$query |
|
|
$query |
|
|
); |
|
|
); |
|
|
// 新增的转换逻辑 |
|
|
|
|
|
|
|
|
//新增的转换逻辑 |
|
|
$query = preg_replace("/SHOW fields FROM `([\w]+)`/i", "PRAGMA table_info('\\1') ", $query); |
|
|
$query = preg_replace("/SHOW fields FROM `([\w]+)`/i", "PRAGMA table_info('\\1') ", $query); |
|
|
$query = preg_replace("/SHOW CREATE TABLE `([\w]+)`/i", "SELECT 0,sql FROM sqlite_master WHERE name='\\1'; ", $query); |
|
|
$query = preg_replace("/SHOW CREATE TABLE `([\w]+)`/i", "SELECT 0,sql FROM sqlite_master WHERE name='\\1'; ", $query); |
|
|
$query = preg_replace("/Show Tables/i", "SELECT name FROM sqlite_master WHERE type = \"table\"", $query); |
|
|
$query = preg_replace("/Show Tables/i", "SELECT name FROM sqlite_master WHERE type = \"table\"", $query); |
|
|
$query = str_replace("\'", "\"", $query); |
|
|
$query = str_replace("\'", "\"", $query); |
|
|
$query = str_replace('\t\n', "", $query); |
|
|
$query = str_replace('\t\n', "", $query); |
|
|
$query = str_ireplace('rand', 'RANDOM', $query); |
|
|
$query = str_ireplace('rand', 'RANDOM', $query); |
|
|
|
|
|
|
|
|
return trim($query); |
|
|
return trim($query); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//自定义函数接口 |
|
|
//自定义函数接口 |
|
|
if (file_exists(DEDEINC.'/extend.func.php')) { |
|
|
if (file_exists(DEDEINC.'/extend.func.php')) { |
|
|
require_once(DEDEINC.'/extend.func.php'); |
|
|
require_once(DEDEINC.'/extend.func.php'); |
|
|