Phpsql File Import class (example)

Source: Internet
Author: User
Phpsql File Import class (example)

  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)', $ prefix = '', $ commenter = array ('#','--'))
  15. {
  16. // Determine whether a file exists
  17. If (! File_exists ($ sqlPath ))
  18. Return false;
  19. $ Handle = fopen ($ sqlPath, 'RB ');
  20. $ SqlStr = fread ($ handle, filesize ($ sqlPath ));
  21. // Use the statement delimiter of SQL syntax to separate
  22. $ Segment = explode (";", trim ($ sqlStr ));
  23. // Var_dump ($ segment );
  24. // Remove comments and extra blank lines
  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. // Determine whether it is a comment
  34. $ IsComment = false;
  35. Foreach ($ commenter as $ comer)
  36. {
  37. If (eregi ("^ (". $ comer. ")", trim ($ subSentence )))
  38. {
  39. $ IsComment = true;
  40. Break;
  41. }
  42. }
  43. // If it is not a comment, it is considered an SQL statement
  44. If (! $ IsComment)
  45. $ NewStatement [] = $ subSentence;
  46. }
  47. }
  48. $ Statement = $ newStatement;
  49. }
  50. // Add a prefix to the table name
  51. If (''! = $ Prefix)
  52. {
  53. // Valid only when the TABLE name appears in the first row, for example, create table talbeName
  54. $ RegxTable = "^ [\ '\"] {0, 1} [\ _ a-zA-Z] + [_ a-zA-Z0-9] * [\' \ "] {0, 1} $ "; // process the regular expression of the table name
  55. $ RegxLeftWall = "^ [\ '\"] fl ";
  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. // Combine SQL statements
  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"); // declare the character set
  103. Mysql_query ($ SQL );
  104. }
  105. Mysql_close ($ conn );
  106. }
  107. Private function findTableName ($ sqlFlagTree, $ tokens, $ tokensKey = 0, & $ tableName = array ())
  108. {
  109. $ RegxLeftWall = "^ [\ '\"] fl ";
  110. If (count ($ tokens) <= $ tokensKey)
  111. Return false;
  112. If (''= trim ($ tokens [$ tokensKey])
  113. {
  114. Return self: findTableName ($ sqlFlagTree, $ tokens, $ tokensKey + 1, $ tableName );
  115. }
  116. Else
  117. {
  118. Foreach ($ sqlFlagTree as $ flag => $ v)
  119. {
  120. If (eregi ($ flag, $ tokens [$ tokensKey])
  121. {
  122. If (0 = $ v)
  123. {
  124. $ TableName ['name'] = $ tokens [$ tokensKey];
  125. If (eregi ($ regxLeftWall, $ tableName ['name'])
  126. {
  127. $ TableName ['leftwall'] = $ tableName ['name'] {0 };
  128. }
  129. Return true;
  130. }
  131. Else {
  132. Return self: findTableName ($ v, $ tokens, $ tokensKey + 1, & $ tableName );
  133. }
  134. }
  135. }
  136. }
  137. Return false;
  138. }
  139. }
  140. Function writeArrayToFile ($ fileName, $ dataArray, $ delimiter = "\ r \ n ")
  141. {
  142. $ Handle = fopen ($ fileName, "wb ");
  143. $ Text = '';
  144. Foreach ($ dataArray as $ data)
  145. {
  146. $ Text = $ text. $ data. $ delimiter;
  147. }
  148. Fwrite ($ handle, $ text );
  149. }

2. call method:

  1. $ DbM = new DBManager ('localhost', 'root', 'root', '123 ');
  2. $ DbM-> createFromFile ('data. SQL ', null ,'');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.