-
-
/** - * Read sql file and write to database
- * @version 1.01 demo.php
- * @author xingshaocheng
- * @edit: bbs.it-home.org
- */
- 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('#','--'))
- {
- //Determine whether the file exists
- if(!file_exists($sqlPath))
- return false;
-
- $handle = fopen($sqlPath,'rb');
-
- $sqlStr = fread($handle,filesize($sqlPath));
-
- //Split through the statement delimiter of sql syntax
- $segment = explode( ";",trim($sqlStr));
-
- //var_dump($segment);
-
- //Remove comments and extra blank lines
- foreach($segment as & $statement)
- {
- $sentence = explode( "n",$statement);
-
- $newStatement = array();
-
- foreach($sentence as $subSentence)
- {
- if('' != trim($subSentence))
- {
- //Judge whether it will No is a comment
- $isComment = false;
- foreach($commenter as $comer)
- {
- if(eregi("^(".$comer.")",trim($subSentence)))
- {
- $isComment = true;
- break;
- }
- }
- //If it is not a comment, it is considered a sql statement
- if(!$isComment)
- $newStatement[] = $subSentence;
- }
- }
-
- $statement = $newStatement;
- }
- //Prefix the table name
- if('' != $prefix)
- {
-
-
- //Only valid when the table name appears in the first line. For example, CREATE TABLE talbeName
-
- $regxTable = "^[` '"]{0,1}[_a-zA-Z]+[_a-zA-Z0-9]*[`'"]{0,1}$";//Regular expression for processing table names
- $ regxLeftWall = "^[`'"]Developed";
-
- $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]);
- }
-
- }
- //Combined sql statement
- foreach($segment as & $statement)
- {
- $newStmt = '';
- foreach($statement as $sentence)
- {
- $newStmt = $newStmt.trim($sentence)."n";
- }
-
- $statement = $newStmt;
- }
-
- //For testing---------------------
- //var_dump($segment);
- //writeArrayToFile('data.txt',$segment);
- //----------------------------------
-
- self::saveByQuery($segment);
-
- return true;
- }
-
- private function saveByQuery($sqlArray)
- {
- $conn = mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword);
-
- mysql_select_db($this->dbSchema);
-
- foreach($sqlArray as $sql)
- {
- mysql_query($sql);
- }
- mysql_close($conn);
- }
-
- private function findTableName($sqlFlagTree,$tokens,$tokensKey=0,& $tableName = array())
- {
- $regxLeftWall = "^[`'"]发达";
-
- if(count($tokens)<=$tokensKey)
- return false;
-
- if('' == trim($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);
- }
//测试
- $dbM = new DBManager('localhost','w01f','123456','test');
- $dbM->createFromFile('data.sql',null,'fff_');
- ?>
-
复制代码
|