- 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 = "^[\" \ "\" Skiff ";
- $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;
- }
- 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 ("Set names UTF8"); declaring character sets
- mysql_query ($sql);
- }
- Mysql_close ($conn);
- }
- Private Function Findtablename ($sqlFlagTree, $tokens, $tokensKey =0,& $tableName = Array ())
- {
- $regxLeftWall = "^[\" \ "\" Skiff ";
- 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);
- }
Copy Code2, call Method:
- $dbM = new DBManager (' localhost ', ' root ', ' root ', ' 6639 ');
- $dbM->createfromfile (' Data.sql ', null, ');
Copy Code |