Skip to content

Commit

Permalink
Update helper.php
Browse files Browse the repository at this point in the history
  • Loading branch information
funadmin authored Jan 20, 2022
1 parent da0bacf commit 5ee89d4
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,34 +499,28 @@ function is_really_writable($file)
*/
if (!function_exists('importsql')) {

function importsql($name)
{
function importsql($name){
$service = new Service(App::instance()); // 获取service 服务
$addons_path = $service->getAddonsPath(); // 插件列表
$sqlFile = $addons_path . $name . DS . 'install.sql';
if (is_file($sqlFile)) {
$lines = file($sqlFile);
$tempLine = '';
foreach ($lines as $line) {
if (substr($line, 0, 2) == '--' || $line == '' || substr($line, 0, 2) == '/*')
continue;
$tempLine .= $line;
if (substr(trim($line), -1, 1) == ';' and $line != 'COMMIT;') {
$tempLine = str_ireplace('__PREFIX__', config('database.connections.mysql.prefix'), $tempLine);
$tempLine = str_ireplace('INSERT INTO ', 'INSERT IGNORE INTO ', $tempLine);
try {
preg_match('/CREATE\s+TABLE\s+\`?(\w+)`/i', $tempLine, $tables);
if(isset($table[1])){
$sql = "show tables like '".$tables[1]."'";
$table = Db::query($sql);
if($table) continue;
$gz = fopen($sqlFile, 'r');
$sql = '';
while(1) {
$sql .= fgets($gz);
if(preg_match('/.*;$/', trim($sql))) {
$sql = preg_replace('/(\/\*(\s|.)*?\*\/);/','',$sql);
$sql = str_replace('__PREFIX__', config('database.connections.mysql.prefix'),$sql);
if(strpos($sql,'CREATE TABLE')!==false || strpos($sql,'INSERT INTO')!==false || strpos($sql,'ALTER TABLE')!==false || strpos($sql,'DROP TABLE')!==false){
try {
Db::execute($sql);
} catch (\Exception $e) {
throw new Exception($e->getMessage());
}
Db::execute($tempLine);
} catch (\PDOException $e) {
throw new PDOException($e->getMessage());
}
$tempLine = '';
$sql = '';
}
if(feof($gz)) break;
}
}
return true;
Expand Down

0 comments on commit 5ee89d4

Please sign in to comment.