首頁  >  文章  >  後端開發  >  php sql檔案導入類別(範例)

php sql檔案導入類別(範例)

WBOY
WBOY原創
2016-07-25 08:55:00863瀏覽
  1. class DBManager
  2. {
  3. var $dbHost = '';
  4. var $dbUser = ''
  5. var $dbPassword = '';
  6. var $dbSchema = '';
  7. function __construct($host,$user,$password,$schema)
  8. {
  9. $this->dbHostost,$schema)
  10. {
  11. $this->dbHostost = $host;
  12. $this->dbUser = $user;
  13. $this->dbPassword = $password;
  14. $this->dbSchema = $schema;
  15. }
  16. function createFromFile($sqlPath,$delimiter = '(;n)|((;rn))|(;r)',$prefix = '',$commenter = array('#','--'))
  17. {
  18. //判斷檔案是否存在
  19. if(!file_exists($sqlPath))
  20. return false;
  21. $handle = fopen($sqlPath,'rb');
  22. $sqlStr = fread($handle,filesize($sqlPath));
  23. //透過sql語法的語句分割符進行分割
  24. $segment = explode(";",trim ($sqlStr));
  25. //var_dump($segment);
  26. //去掉註解與多餘的空白行
  27. foreach($segment as & $statement)
  28. {
  29. $sentence = explode("n",$statement);
  30. $newStatement = array();
  31. foreach($sentence as $subSentence)
  32. {🎜> foreach($sentence as $subSentence)
  33. {
  34. {
  35. if('' != trim($subSentence))
  36. {
  37. //判斷是會否是註解
  38. $isComment = false;
  39. foreach($commenter as $comer)
  40. {
  41. if(eregi("^(".$comer.")",trim($subSentence)))
  42. {
  43. $isComment = true;
  44. break;
  45. }
  46. }
  47. //如果不是註釋,則認為是sql語句
  48. if(!$isComment)
  49. $newStatement[] = $subSentence;
  50. }
  51. }
  52. $statement = $newStatement;
  53. }
  54. //對錶名加上前綴
  55. if('' != $prefix)
  56. {
  57. //只有表名在第一行出現時才有效例如CREATE TABLE talbeName
  58. $regxTable = "^[`'"]{0,1}[_a-zA-Z] [_a-zA-Z0-9]* [`'"]{0,1}$";//處理表名的正規表示式
  59. $regxLeftWall = "^[`'"]一葉扁舟";
  60. $sqlFlagTree = array (
  61. "CREATE" => array(
  62. "TABLE" => array(
  63. "$regxTable" => 0
  64. )
  65. ),
  66. "INSERT" => array(
  67. "INTO" => array(
  68. "$regxTable" => 0
  69. )
  70. )
  71. );
  72. foreach($segment as & $statement )
  73. {
  74. $tokens = split(" ",$statement[0]);
  75. $tableName = array();
  76. $this->findTableName($sqlFlagTree,$tokens ,0,$tableName);
  77. if(empty($tableName['leftWall']))
  78. {
  79. $newTableName = $prefix.$tableName['name'];
  80. }
  81. else{
  82. $newTableName = $tableName['leftWall'].$prefix.substr($tableName['name'],1);
  83. }
  84. $statement[0 ] = str_replace($tableName['name'],$newTableName,$statement[0]);
  85. }
  86. }
  87. //組合sql語句
  88. foreach($segment as & $statement)
  89. {
  90. $newStmt = '';
  91. foreach($statement as $sentence)
  92. {
  93. $newStmt = $newStmt.trim($sentence)."n";
  94. }
  95. $statement = $newStmt;
  96. }
  97. self::saveByQuery($segment);
  98. return true;
  99. }
  100. return true;
  101. }
  102. }
  103. private function saveByQuery($sqlArray)
  104. {
  105. $conn = mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword);
  106. mysql_select_db( >dbSchema); foreach($sqlArray as $sql) { mysql_query("set names utf8"); //宣告字元集 mysql_query($sql); } mysql_close($conn); }
  107. private function findTableName($sqlFlagTree,$tokens,$tokensKey=0,& $tableName = array())
  108. {
  109. $regxLeftWall = "^[`'"]一葉扁舟" ;
  110. if(count($tokens) return false;
  111. if('' == trim($tokens[$tokensKey]))
  112. {
  113. return self::findTableName($sqlFlagTree,$tokens,$tokensKey 1,$tableName);
  114. }
  115. else
  116. {
  117. foreach(sqlFlagTree as $flag = $ )
  118. {
  119. if(eregi($flag,$tokens[$tokensKey]))
  120. {
  121. if(0==$v)
  122. {
  123. $tableName[ 'name '] = $tokens[$tokensKey];
  124. if(eregi($regxLeftWall,$tableName['name']))
  125. {
  126. $tableName['leftWall'] = $tableName[ 'name']{0};
  127. }
  128. return true;
  129. }
  130. else{
  131. return self::findTableName($v,$tokens,$tokensKey 1 ,& $tableName);
  132. }
  133. }
  134. }
  135. }
  136. 回傳false;
  137. }
  138. }
  139. function writeArrayToFile($fileName,$file($file,$Name delimiter="rn")
  140. {
  141. $handle=fopen($fileName, "wb");
  142. $text = '';
  143. foreach($dataArray 作為 $資料)
  144. {
  145. $text = $text.$data.$delimiter;
  146. }
  147. fwrite($handle,$text);
  148. }
複製程式碼

2,呼叫方法:

  1. $dbM = new DBManager('localhost','root','root','6639');
  2. $dbM->createFromFile ( 'data.sql',null,'');
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn