3 * SQL import plugin for phpMyAdmin
6 declare(strict_types
=1);
8 namespace PhpMyAdmin\Plugins\Import
;
10 use PhpMyAdmin\DatabaseInterface
;
12 use PhpMyAdmin\Plugins\ImportPlugin
;
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\Properties\Plugins\ImportPluginProperties
;
18 use PhpMyAdmin\SqlParser\Utils\BufferedQuery
;
23 use function mb_strlen
;
24 use function preg_replace
;
27 * Handles the import for the SQL format
29 class ImportSql
extends ImportPlugin
32 * @psalm-return non-empty-lowercase-string
34 public function getName(): string
39 protected function setProperties(): ImportPluginProperties
41 $importPluginProperties = new ImportPluginProperties();
42 $importPluginProperties->setText('SQL');
43 $importPluginProperties->setExtension('sql');
44 $importPluginProperties->setOptionsText(__('Options'));
46 $compats = $GLOBALS['dbi']->getCompatibilities();
47 if (count($compats) > 0) {
49 foreach ($compats as $val) {
53 // create the root group that will be the options field for
54 // $importPluginProperties
55 // this will be shown as "Format specific options"
56 $importSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
58 // general options main group
59 $generalOptions = new OptionsPropertyMainGroup('general_opts');
60 // create primary items and add them to the group
61 $leaf = new SelectPropertyItem(
63 __('SQL compatibility mode:')
65 $leaf->setValues($values);
68 'manual_MySQL_Database_Administration',
72 $generalOptions->addProperty($leaf);
73 $leaf = new BoolPropertyItem(
74 'no_auto_value_on_zero',
75 __('Do not use <code>AUTO_INCREMENT</code> for zero values')
79 'manual_MySQL_Database_Administration',
81 'sqlmode_no_auto_value_on_zero',
84 $generalOptions->addProperty($leaf);
86 // add the main group to the root group
87 $importSpecificOptions->addProperty($generalOptions);
88 // set the options for the import plugin property item
89 $importPluginProperties->setOptions($importSpecificOptions);
92 return $importPluginProperties;
96 * Handles the whole import logic
98 * @param array $sql_data 2-element array with sql data
100 public function doImport(?File
$importHandle = null, array &$sql_data = []): void
102 // Handle compatibility options.
103 $this->setSQLMode($GLOBALS['dbi'], $_REQUEST);
105 $bq = new BufferedQuery();
106 if (isset($_POST['sql_delimiter'])) {
107 $bq->setDelimiter($_POST['sql_delimiter']);
111 * Will be set in Import::getNextChunk().
113 * @global bool $GLOBALS ['finished']
115 $GLOBALS['finished'] = false;
117 while (! $GLOBALS['error'] && (! $GLOBALS['timeout_passed'])) {
118 // Getting the first statement, the remaining data and the last
120 $statement = $bq->extract();
122 // If there is no full statement, we are looking for more data.
123 if (empty($statement)) {
124 // Importing new data.
125 $newData = $this->import
->getNextChunk($importHandle);
127 // Subtract data we didn't handle yet and stop processing.
128 if ($newData === false) {
129 $GLOBALS['offset'] -= mb_strlen($bq->query
);
133 // Checking if the input buffer has finished.
134 if ($newData === true) {
135 $GLOBALS['finished'] = true;
139 // Convert CR (but not CRLF) to LF otherwise all queries may
140 // not get executed on some platforms.
141 $bq->query
.= preg_replace("/\r($|[^\n])/", "\n$1", $newData);
146 // Executing the query.
147 $this->import
->runQuery($statement, $statement, $sql_data);
150 // Extracting remaining statements.
151 while (! $GLOBALS['error'] && ! $GLOBALS['timeout_passed'] && ! empty($bq->query
)) {
152 $statement = $bq->extract(true);
153 if (empty($statement)) {
157 $this->import
->runQuery($statement, $statement, $sql_data);
161 $this->import
->runQuery('', '', $sql_data);
165 * Handle compatibility options
167 * @param DatabaseInterface $dbi Database interface
168 * @param array $request Request array
170 private function setSQLMode($dbi, array $request): void
173 if (isset($request['sql_compatibility']) && $request['sql_compatibility'] !== 'NONE') {
174 $sql_modes[] = $request['sql_compatibility'];
177 if (isset($request['sql_no_auto_value_on_zero'])) {
178 $sql_modes[] = 'NO_AUTO_VALUE_ON_ZERO';
181 if (count($sql_modes) <= 0) {
186 'SET SQL_MODE="' . implode(',', $sql_modes) . '"'