-
-
class DBManager
- {
- var $dbHost = '';
- var $dbUser = ''
-
- var $dbPassword = '';
- var $dbSchema = '';
-
- function __construct($host,$user,$password,$schema)
- {
- $this->dbHostost,$schema)
- {
- $this->dbHostost = $host;
- $this->dbUser = $user;
- $this->dbPassword = $password;
- $this->dbSchema = $schema;
- }
-
- function createFromFile($sqlPath,$delimiter = '(;n)|((;rn))|(;r)',$prefix = '',$commenter = array('#','--'))
- {
- //判斷檔案是否存在
- if(!file_exists($sqlPath))
- return false;
-
- $handle = fopen($sqlPath,'rb');
-
- $sqlStr = fread($handle,filesize($sqlPath));
-
- //透過sql語法的語句分割符進行分割
- $segment = explode(";",trim ($sqlStr));
-
- //var_dump($segment);
-
- //去掉註解與多餘的空白行
- foreach($segment as & $statement)
- {
- $sentence = explode("n",$statement);
-
- $newStatement = array();
-
- foreach($sentence as $subSentence)
- {🎜> foreach($sentence as $subSentence)
- {
- {
- if('' != trim($subSentence))
- {
- //判斷是會否是註解
- $isComment = false;
- foreach($commenter as $comer)
- {
- if(eregi("^(".$comer.")",trim($subSentence)))
- {
- $isComment = true;
- break;
- }
- }
- //如果不是註釋,則認為是sql語句
- if(!$isComment)
- $newStatement[] = $subSentence;
- }
- }
-
- $statement = $newStatement;
- }
- //對錶名加上前綴
- if('' != $prefix)
- {
-
-
- //只有表名在第一行出現時才有效例如CREATE TABLE talbeName
-
- $regxTable = "^[`'"]{0,1}[_a-zA-Z] [_a-zA-Z0-9]* [`'"]{0,1}$";//處理表名的正規表示式
- $regxLeftWall = "^[`'"]一葉扁舟";
-
- $sqlFlagTree = array (
- "CREATE" => array(
- "TABLE" => array(
- "$regxTable" => 0
- )
- ),
- "INSERT" => array(
- "INTO" => array(
- "$regxTable" => 0
- )
- )
-
- );
-
- foreach($segment as & $statement )
- {
- $tokens = split(" ",$statement[0]);
-
- $tableName = array();
- $this->findTableName($sqlFlagTree,$tokens ,0,$tableName);
-
- if(empty($tableName['leftWall']))
- {
- $newTableName = $prefix.$tableName['name'];
- }
- else{
- $newTableName = $tableName['leftWall'].$prefix.substr($tableName['name'],1);
- }
-
- $statement[0 ] = str_replace($tableName['name'],$newTableName,$statement[0]);
- }
-
- }
- //組合sql語句
- foreach($segment as & $statement)
- {
- $newStmt = '';
- foreach($statement as $sentence)
- {
- $newStmt = $newStmt.trim($sentence)."n";
- }
-
- $statement = $newStmt;
- }
- self::saveByQuery($segment);
-
- return true;
- }
-
- return true;
- }
- }
- private function saveByQuery($sqlArray)
- {
- $conn = mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword);
-
- mysql_select_db( >dbSchema); foreach($sqlArray as $sql) { mysql_query("set names utf8"); //宣告字元集 mysql_query($sql); } mysql_close($conn); }
-
- private function findTableName($sqlFlagTree,$tokens,$tokensKey=0,& $tableName = array())
- {
- $regxLeftWall = "^[`'"]一葉扁舟" ;
-
- if(count($tokens) return false;
-
- if('' == trim($tokens[$tokensKey]))
- {
- return self::findTableName($sqlFlagTree,$tokens,$tokensKey 1,$tableName);
- }
- else
- {
- foreach(sqlFlagTree as $flag = $ )
- {
- if(eregi($flag,$tokens[$tokensKey]))
- {
- if(0==$v)
- {
- $tableName[ 'name '] = $tokens[$tokensKey];
-
- if(eregi($regxLeftWall,$tableName['name']))
- {
- $tableName['leftWall'] = $tableName[ 'name']{0};
- }
-
- return true;
- }
- else{
- return self::findTableName($v,$tokens,$tokensKey 1 ,& $tableName);
- }
- }
- }
- }
-
- 回傳false;
- }
- }
- function writeArrayToFile($fileName,$file($file,$Name delimiter="rn")
- {
- $handle=fopen($fileName, "wb");
-
- $text = '';
-
- foreach($dataArray 作為 $資料)
- {
- $text = $text.$data.$delimiter;
- }
- fwrite($handle,$text);
- }
複製程式碼
2,呼叫方法:
-
- $dbM = new DBManager('localhost','root','root','6639');
- $dbM->createFromFile ( 'data.sql',null,'');
複製程式碼
|