-
- class DBManager
- {
- var $dbHost = '';
- var $dbUser = '';
- var $dbPassword = '';
- var $dbSchema = '';
-
- function __construct ($host,$user,$password,$schema)
- {
- $this->dbHost = $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)
- {
- 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 = "^[`'"]Aボート葉付き";
-
- $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 & $ステートメント)
- {
- $newStmt = '';
- foreach($statement as $sentence)
- {
- $newStmt = $newStmt.trim($sentence)."n";
- }
-
- $statement = $newStmt;
- }
- self: :saveByQuery($segment);
-
- return true;
- }
-
- プライベート関数 saveByQuery($sqlArray)
- {
- $conn = mysql_connect($this->dbHost,$this->dbUser,$ this->dbPassword);
-
- mysql_select_db($this->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)<=$tokensKey)
- return false;
-
- if('' == トリム($tokens[$tokensKey]))
- {
- return self::findTableName($sqlFlagTree,$tokens,$tokensKey+1, $tableName);
- }
- else
- {
- foreach($sqlFlagTree as $flag => $v)
- {
- 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);
- }
- }
- }
- }
-
- return false;
- }
- }
- function writeArrayToFile($fileName,$dataArray,$delimiter="rn")
- {
- $handle=fopen($fileName, "wb");
-
- $text = '';
-
- foreach($dataArray as $data)
- {
- $text = $text.$data.$delimiter;
- }
- fwrite($handle,$text);
- }
复制代
2、调用方法:
-
- $dbM = new DBManager('localhost','root','root','6639');
- $dbM->createFromFile('data.sql',null,'');
复制代
|