"Fossies" - the Fresh Open Source Software Archive 
Member "phpMyAdmin-5.1.0-all-languages/libraries/classes/Plugins/Import/ImportSql.php" (24 Feb 2021, 6211 Bytes) of package /linux/www/phpMyAdmin-5.1.0-all-languages.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "ImportSql.php":
5.0.4-english_vs_5.1.0-english.
1 <?php
2 /**
3 * SQL import plugin for phpMyAdmin
4 */
5
6 declare(strict_types=1);
7
8 namespace PhpMyAdmin\Plugins\Import;
9
10 use PhpMyAdmin\DatabaseInterface;
11 use PhpMyAdmin\File;
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;
19 use function count;
20 use function implode;
21 use function mb_strlen;
22 use function preg_replace;
23
24 /**
25 * Handles the import for the SQL format
26 */
27 class ImportSql extends ImportPlugin
28 {
29 public function __construct()
30 {
31 parent::__construct();
32 $this->setProperties();
33 }
34
35 /**
36 * Sets the import plugin properties.
37 * Called in the constructor.
38 *
39 * @return void
40 */
41 protected function setProperties()
42 {
43 global $dbi;
44
45 $importPluginProperties = new ImportPluginProperties();
46 $importPluginProperties->setText('SQL');
47 $importPluginProperties->setExtension('sql');
48 $importPluginProperties->setOptionsText(__('Options'));
49
50 $compats = $dbi->getCompatibilities();
51 if (count($compats) > 0) {
52 $values = [];
53 foreach ($compats as $val) {
54 $values[$val] = $val;
55 }
56
57 // create the root group that will be the options field for
58 // $importPluginProperties
59 // this will be shown as "Format specific options"
60 $importSpecificOptions = new OptionsPropertyRootGroup(
61 'Format Specific Options'
62 );
63
64 // general options main group
65 $generalOptions = new OptionsPropertyMainGroup('general_opts');
66 // create primary items and add them to the group
67 $leaf = new SelectPropertyItem(
68 'compatibility',
69 __('SQL compatibility mode:')
70 );
71 $leaf->setValues($values);
72 $leaf->setDoc(
73 [
74 'manual_MySQL_Database_Administration',
75 'Server_SQL_mode',
76 ]
77 );
78 $generalOptions->addProperty($leaf);
79 $leaf = new BoolPropertyItem(
80 'no_auto_value_on_zero',
81 __('Do not use <code>AUTO_INCREMENT</code> for zero values')
82 );
83 $leaf->setDoc(
84 [
85 'manual_MySQL_Database_Administration',
86 'Server_SQL_mode',
87 'sqlmode_no_auto_value_on_zero',
88 ]
89 );
90 $generalOptions->addProperty($leaf);
91
92 // add the main group to the root group
93 $importSpecificOptions->addProperty($generalOptions);
94 // set the options for the import plugin property item
95 $importPluginProperties->setOptions($importSpecificOptions);
96 }
97
98 $this->properties = $importPluginProperties;
99 }
100
101 /**
102 * Handles the whole import logic
103 *
104 * @param array $sql_data 2-element array with sql data
105 *
106 * @return void
107 */
108 public function doImport(?File $importHandle = null, array &$sql_data = [])
109 {
110 global $error, $timeout_passed, $dbi;
111
112 // Handle compatibility options.
113 $this->setSQLMode($dbi, $_REQUEST);
114
115 $bq = new BufferedQuery();
116 if (isset($_POST['sql_delimiter'])) {
117 $bq->setDelimiter($_POST['sql_delimiter']);
118 }
119
120 /**
121 * Will be set in Import::getNextChunk().
122 *
123 * @global bool $GLOBALS ['finished']
124 */
125 $GLOBALS['finished'] = false;
126
127 while (! $error && (! $timeout_passed)) {
128 // Getting the first statement, the remaining data and the last
129 // delimiter.
130 $statement = $bq->extract();
131
132 // If there is no full statement, we are looking for more data.
133 if (empty($statement)) {
134 // Importing new data.
135 $newData = $this->import->getNextChunk($importHandle);
136
137 // Subtract data we didn't handle yet and stop processing.
138 if ($newData === false) {
139 $GLOBALS['offset'] -= mb_strlen($bq->query);
140 break;
141 }
142
143 // Checking if the input buffer has finished.
144 if ($newData === true) {
145 $GLOBALS['finished'] = true;
146 break;
147 }
148
149 // Convert CR (but not CRLF) to LF otherwise all queries may
150 // not get executed on some platforms.
151 $bq->query .= preg_replace("/\r($|[^\n])/", "\n$1", $newData);
152
153 continue;
154 }
155
156 // Executing the query.
157 $this->import->runQuery($statement, $statement, $sql_data);
158 }
159
160 // Extracting remaining statements.
161 while (! $error && ! $timeout_passed && ! empty($bq->query)) {
162 $statement = $bq->extract(true);
163 if (empty($statement)) {
164 continue;
165 }
166
167 $this->import->runQuery($statement, $statement, $sql_data);
168 }
169
170 // Finishing.
171 $this->import->runQuery('', '', $sql_data);
172 }
173
174 /**
175 * Handle compatibility options
176 *
177 * @param DatabaseInterface $dbi Database interface
178 * @param array $request Request array
179 *
180 * @return void
181 */
182 private function setSQLMode($dbi, array $request)
183 {
184 $sql_modes = [];
185 if (isset($request['sql_compatibility'])
186 && $request['sql_compatibility'] !== 'NONE'
187 ) {
188 $sql_modes[] = $request['sql_compatibility'];
189 }
190 if (isset($request['sql_no_auto_value_on_zero'])) {
191 $sql_modes[] = 'NO_AUTO_VALUE_ON_ZERO';
192 }
193 if (count($sql_modes) <= 0) {
194 return;
195 }
196
197 $dbi->tryQuery(
198 'SET SQL_MODE="' . implode(',', $sql_modes) . '"'
199 );
200 }
201 }