ImportSql.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * SQL import plugin for phpMyAdmin
  5. *
  6. * @package PhpMyAdmin-Import
  7. * @subpackage SQL
  8. */
  9. namespace PhpMyAdmin\Plugins\Import;
  10. use PhpMyAdmin\Import;
  11. use PhpMyAdmin\Plugins\ImportPlugin;
  12. use PhpMyAdmin\Properties\Plugins\ImportPluginProperties;
  13. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  14. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  15. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  16. use PhpMyAdmin\Properties\Options\Items\SelectPropertyItem;
  17. use PhpMyAdmin\SqlParser\Utils\BufferedQuery;
  18. /**
  19. * Handles the import for the SQL format
  20. *
  21. * @package PhpMyAdmin-Import
  22. * @subpackage SQL
  23. */
  24. class ImportSql extends ImportPlugin
  25. {
  26. /**
  27. * Constructor
  28. */
  29. public function __construct()
  30. {
  31. $this->setProperties();
  32. }
  33. /**
  34. * Sets the import plugin properties.
  35. * Called in the constructor.
  36. *
  37. * @return void
  38. */
  39. protected function setProperties()
  40. {
  41. $importPluginProperties = new ImportPluginProperties();
  42. $importPluginProperties->setText('SQL');
  43. $importPluginProperties->setExtension('sql');
  44. $importPluginProperties->setOptionsText(__('Options'));
  45. $compats = $GLOBALS['dbi']->getCompatibilities();
  46. if (count($compats) > 0) {
  47. $values = array();
  48. foreach ($compats as $val) {
  49. $values[$val] = $val;
  50. }
  51. // create the root group that will be the options field for
  52. // $importPluginProperties
  53. // this will be shown as "Format specific options"
  54. $importSpecificOptions = new OptionsPropertyRootGroup(
  55. "Format Specific Options"
  56. );
  57. // general options main group
  58. $generalOptions = new OptionsPropertyMainGroup("general_opts");
  59. // create primary items and add them to the group
  60. $leaf = new SelectPropertyItem(
  61. "compatibility",
  62. __('SQL compatibility mode:')
  63. );
  64. $leaf->setValues($values);
  65. $leaf->setDoc(
  66. array(
  67. 'manual_MySQL_Database_Administration',
  68. 'Server_SQL_mode',
  69. )
  70. );
  71. $generalOptions->addProperty($leaf);
  72. $leaf = new BoolPropertyItem(
  73. "no_auto_value_on_zero",
  74. __('Do not use <code>AUTO_INCREMENT</code> for zero values')
  75. );
  76. $leaf->setDoc(
  77. array(
  78. 'manual_MySQL_Database_Administration',
  79. 'Server_SQL_mode',
  80. 'sqlmode_no_auto_value_on_zero',
  81. )
  82. );
  83. $generalOptions->addProperty($leaf);
  84. // add the main group to the root group
  85. $importSpecificOptions->addProperty($generalOptions);
  86. // set the options for the import plugin property item
  87. $importPluginProperties->setOptions($importSpecificOptions);
  88. }
  89. $this->properties = $importPluginProperties;
  90. }
  91. /**
  92. * Handles the whole import logic
  93. *
  94. * @param array &$sql_data 2-element array with sql data
  95. *
  96. * @return void
  97. */
  98. public function doImport(array &$sql_data = array())
  99. {
  100. global $error, $timeout_passed;
  101. // Handle compatibility options.
  102. $this->_setSQLMode($GLOBALS['dbi'], $_REQUEST);
  103. $bq = new BufferedQuery();
  104. if (isset($_POST['sql_delimiter'])) {
  105. $bq->setDelimiter($_POST['sql_delimiter']);
  106. }
  107. /**
  108. * Will be set in Import::getNextChunk().
  109. *
  110. * @global bool $GLOBALS ['finished']
  111. */
  112. $GLOBALS['finished'] = false;
  113. while ((!$error) && (!$timeout_passed)) {
  114. // Getting the first statement, the remaining data and the last
  115. // delimiter.
  116. $statement = $bq->extract();
  117. // If there is no full statement, we are looking for more data.
  118. if (empty($statement)) {
  119. // Importing new data.
  120. $newData = Import::getNextChunk();
  121. // Subtract data we didn't handle yet and stop processing.
  122. if ($newData === false) {
  123. $GLOBALS['offset'] -= mb_strlen($bq->query);
  124. break;
  125. }
  126. // Checking if the input buffer has finished.
  127. if ($newData === true) {
  128. $GLOBALS['finished'] = true;
  129. break;
  130. }
  131. // Convert CR (but not CRLF) to LF otherwise all queries may
  132. // not get executed on some platforms.
  133. $bq->query .= preg_replace("/\r($|[^\n])/", "\n$1", $newData);
  134. continue;
  135. }
  136. // Executing the query.
  137. Import::runQuery($statement, $statement, $sql_data);
  138. }
  139. // Extracting remaining statements.
  140. while ((!$error) && (!$timeout_passed) && (!empty($bq->query))) {
  141. $statement = $bq->extract(true);
  142. if (!empty($statement)) {
  143. Import::runQuery($statement, $statement, $sql_data);
  144. }
  145. }
  146. // Finishing.
  147. Import::runQuery('', '', $sql_data);
  148. }
  149. /**
  150. * Handle compatibility options
  151. *
  152. * @param PhpMyAdmin\DatabaseInterface $dbi Database interface
  153. * @param array $request Request array
  154. *
  155. * @return void
  156. */
  157. private function _setSQLMode($dbi, array $request)
  158. {
  159. $sql_modes = array();
  160. if (isset($request['sql_compatibility'])
  161. && 'NONE' != $request['sql_compatibility']
  162. ) {
  163. $sql_modes[] = $request['sql_compatibility'];
  164. }
  165. if (isset($request['sql_no_auto_value_on_zero'])) {
  166. $sql_modes[] = 'NO_AUTO_VALUE_ON_ZERO';
  167. }
  168. if (count($sql_modes) > 0) {
  169. $dbi->tryQuery(
  170. 'SET SQL_MODE="' . implode(',', $sql_modes) . '"'
  171. );
  172. }
  173. }
  174. }