首页 >后端开发 >php教程 >php sql文件导入类(示例)

php sql文件导入类(示例)

WBOY
WBOY原创
2016-07-25 08:55:00892浏览
  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->dbHost = $host;
  10. $this->dbUser = $user;
  11. $this->dbPassword = $password;
  12. $this->dbSchema = $schema;
  13. }
  14. function createFromFile($sqlPath,$delimiter = '(;\n)|((;\r\n))|(;\r)',$prefix = '',$commenter = array('#','--'))
  15. {
  16. //判断文件是否存在
  17. if(!file_exists($sqlPath))
  18. return false;
  19. $handle = fopen($sqlPath,'rb');
  20. $sqlStr = fread($handle,filesize($sqlPath));
  21. //通过sql语法的语句分割符进行分割
  22. $segment = explode(";",trim($sqlStr));
  23. //var_dump($segment);
  24. //去掉注释和多余的空行
  25. foreach($segment as & $statement)
  26. {
  27. $sentence = explode("\n",$statement);
  28. $newStatement = array();
  29. foreach($sentence as $subSentence)
  30. {
  31. if('' != trim($subSentence))
  32. {
  33. //判断是会否是注释
  34. $isComment = false;
  35. foreach($commenter as $comer)
  36. {
  37. if(eregi("^(".$comer.")",trim($subSentence)))
  38. {
  39. $isComment = true;
  40. break;
  41. }
  42. }
  43. //如果不是注释,则认为是sql语句
  44. if(!$isComment)
  45. $newStatement[] = $subSentence;
  46. }
  47. }
  48. $statement = $newStatement;
  49. }
  50. //对表名加前缀
  51. if('' != $prefix)
  52. {
  53. //只有表名在第一行出现时才有效 例如 CREATE TABLE talbeName
  54. $regxTable = "^[\`\'\"]{0,1}[\_a-zA-Z]+[\_a-zA-Z0-9]*[\`\'\"]{0,1}$";//处理表名的正则表达式
  55. $regxLeftWall = "^[\`\'\"]一叶扁舟";
  56. $sqlFlagTree = array(
  57. "CREATE" => array(
  58. "TABLE" => array(
  59. "$regxTable" => 0
  60. )
  61. ),
  62. "INSERT" => array(
  63. "INTO" => array(
  64. "$regxTable" => 0
  65. )
  66. )
  67. );
  68. foreach($segment as & $statement)
  69. {
  70. $tokens = split(" ",$statement[0]);
  71. $tableName = array();
  72. $this->findTableName($sqlFlagTree,$tokens,0,$tableName);
  73. if(empty($tableName['leftWall']))
  74. {
  75. $newTableName = $prefix.$tableName['name'];
  76. }
  77. else{
  78. $newTableName = $tableName['leftWall'].$prefix.substr($tableName['name'],1);
  79. }
  80. $statement[0] = str_replace($tableName['name'],$newTableName,$statement[0]);
  81. }
  82. }
  83. //组合sql语句
  84. foreach($segment as & $statement)
  85. {
  86. $newStmt = '';
  87. foreach($statement as $sentence)
  88. {
  89. $newStmt = $newStmt.trim($sentence)."\n";
  90. }
  91. $statement = $newStmt;
  92. }
  93. self::saveByQuery($segment);
  94. return true;
  95. }
  96. private function saveByQuery($sqlArray)
  97. {
  98. $conn = mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword);
  99. mysql_select_db($this->dbSchema);
  100. foreach($sqlArray as $sql)
  101. {
  102. mysql_query("set names utf8"); //声明字符集
  103. mysql_query($sql);
  104. }
  105. mysql_close($conn);
  106. }
  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 => $v)
  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. return false;
  137. }
  138. }
  139. function writeArrayToFile($fileName,$dataArray,$delimiter="\r\n")
  140. {
  141. $handle=fopen($fileName, "wb");
  142. $text = '';
  143. foreach($dataArray as $data)
  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