/**
- * 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) | ( (; \ r \ n)) | (; \ r) ', $prefix = ', $commenter = Array (' # ', '--'))
- {
- Determine if a file exists
- if (!file_exists ($sqlPath))
- return false;
- $handle = fopen ($sqlPath, ' RB ');
- $SQLSTR = Fread ($handle, FileSize ($sqlPath));
- Splitting by a statement separator in SQL syntax
- $segment = Explode (";", Trim ($SQLSTR));
- Var_dump ($segment);
- Get rid of comments and extra blank lines
- foreach ($segment as & $statement)
- {
- $sentence = explode ("\ n", $statement);
- $newStatement = Array ();
- foreach ($sentence as $subSentence)
- {
- if ('! = Trim ($subSentence))
- {
- Judging whether it would be 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 an SQL statement
- if (! $isComment)
- $newStatement [] = $subSentence;
- }
- }
- $statement = $newStatement;
- }
- Prefix the table name
- if ('! = $prefix)
- {
- Only table names are valid when the first row appears, such as CREATE table Talbename
- $regxTable = "^[\ \" \ "]{0,1}[\_a-za-z]+[\_a-za-z0-9]*[\" \ "]{0,1}$";//Regular expression for table name processing
- $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]);
- }
- }
- Combining SQL statements
- 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 = "^[\" \ "[] developed";
- 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 = "\ r \ n")
- {
- $handle =fopen ($fileName, "WB");
- $text = ";
- foreach ($dataArray as $data)
- {
- $text = $text. $data. $delimiter;
- }
- Fwrite ($handle, $text);
- }
Test
- $dbM = new DBManager (' localhost ', ' w01f ', ' 123456 ', ' test ');
- $dbM->createfromfile (' Data.sql ', null, ' fff_ ');
- ?>
Copy Code |