PHP loads the SQL file and parses it into a semicolon-delimited array. (Supports stored procedures and function extraction, automatic filtering of annotations)
/** * Load SQL file as semicolon-delimited array * <br/> supports stored procedure and function extraction, automatically filters comments * <br/> For example: Var_export (Load_sql_file (' Mysql_routing_ Example/fn_cdr_parse_accountcode.sql ')); * @param string $path file path * @return Boolean|array * @since 1.0 <2015-5-27> Sochishun Added. */functionLoad_sql_file ($path,$FN _splitor= ';; ') { if(!file_exists($path)) { return false; } $lines=file($path, File_ignore_new_lines |file_skip_empty_lines); $aout=false; $str= ' '; $skip=false; $FN=false; foreach($lines as $line) { $line=Trim($line); //Filter Comments if(!$line|| 0 = = =Strpos($line, '---') | | 0 = = =Strpos($line, ' * ') | | 0 = = =Strpos($line, '/* ') | | (false!==Strpos($line, ' * * ') &&strlen($line) == (Strpos($line, ' * * ') + 2))) { if(!$skip&& 0 = = =Strpos($line, ‘/*‘)) { $skip=true; } if($skip&&false!==Strpos($line, ' * * ') &&strlen($line) == (Strpos($line, ' * * ') + 2)) { $skip=false; } Continue; } if($skip) { Continue; } //extracting stored procedures and functions if(0 = = =Strpos($line, ' DELIMITER '.$FN _splitor)) { $FN=true; Continue; } if(0 = = =Strpos($line, ' DELIMITER; ')) { $FN=false; $aout[] =$str; $str= ' '; Continue; } if($FN) { $str.=$line. ‘ ‘; Continue; } //extracting normal statements $str.=$line; if(false!==Strpos($line, ‘;‘) &&strlen($line) == (Strpos($line, ‘;‘) + 1)) { $aout[] =$str; $str= ' '; } } return $aout;}
Copyright NOTICE: This document is licensed under the attribution-Non-commercial use-sharing (CC BY-NC-SA 3.0 CN) International License Agreement, please specify the author and source. Title: Code Collection series--php--loading SQL files and parsing an array This article link: http://www.cnblogs.com/sochishun/p/7061588.html This article Sochishun (e-mail: 14507247#qq.com | blog: http://www.cnblogs.com/sochishun/) Published: June 23, 2017 |
Code collection--php--Loading SQL files and parsing an array