-
-
/** - * SQL 파일 읽기 및 데이터베이스에 쓰기
- * @version 1.01 데모.php
- * @author xingshaocheng
- * @edit: bbs.it-home.org
- */
- class DBManager
- {
- var $dbHost = '';
- var $dbUser = '';
- var $dbPassword = '';
- var $dbSchema = '';
-
- 함수 __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))
- false 반환;
-
- $handle = fopen($sqlPath,'rb');
-
- $sqlStr = fread($handle,filesize($sqlPath));
-
- / / SQL 구문의 문 구분 기호를 통해 분할
- $segment =explore(";",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}$";//테이블 처리를 위한 정규식 names
- $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($ 문장을 $문장으로)
- {
- $newStmt = $newStmt.trim($sentence)."n";
- }
-
- $statement = $newStmt;
- }
-
- //테스트용---------
- //var_dump($segment);
- // writeArrayToFile('data.txt',$segment);
- //--------------------------------- -
-
- 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($sql);
- }
- mysql_close($ conn);
- }
-
- 비공개 함수 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_');
- ?>
-
复代码
|