/** * @dataProvider testExtractProvider */ public function testExtract($query, $chunkSize, array $options = array(), array $expected) { $chunks = str_split($query, $chunkSize); $count = count($chunks); /** * The array of extracted statements. * * @var array $statements */ $statements = array(); /** * The `BufferedQuery` instance used for extraction. * * @var BufferedQuery $bq */ $bq = new BufferedQuery('', $options); // Feeding chunks and extracting queries. $i = 0; while ($i < $count) { if ($stmt = $bq->extract()) { $statements[] = $stmt; } else { $bq->query .= $chunks[$i++]; } } // Feeding ended, extracting remaining queries. while ($stmt = $bq->extract(true)) { $statements[] = $stmt; } $this->assertEquals($expected, $statements); }
/** * Handles the whole import logic * * @param array &$sql_data 2-element array with sql data * * @return void */ public function doImport(&$sql_data = array()) { global $error, $timeout_passed; // Handle compatibility options. $this->_setSQLMode($GLOBALS['dbi'], $_REQUEST); $bq = new SqlParser\Utils\BufferedQuery(); if (isset($_POST['sql_delimiter'])) { $bq->setDelimiter($_POST['sql_delimiter']); } /** * Will be set in PMA_importGetNextChunk(). * * @global bool $GLOBALS ['finished'] */ $GLOBALS['finished'] = false; while (!$error && !$timeout_passed) { // Getting the first statement, the remaining data and the last // delimiter. $statement = $bq->extract(); // If there is no full statement, we are looking for more data. if (empty($statement)) { // Importing new data. $newData = PMA_importGetNextChunk(); // Subtract data we didn't handle yet and stop processing. if ($newData === false) { $GLOBALS['offset'] -= mb_strlen($bq->query); break; } // Checking if the input buffer has finished. if ($newData === true) { $GLOBALS['finished'] = true; break; } // Convert CR (but not CRLF) to LF otherwise all queries may // not get executed on some platforms. $bq->query .= preg_replace("/\r(\$|[^\n])/", "\n\$1", $newData); continue; } // Executing the query. PMA_importRunQuery($statement, $statement, $sql_data); } // Extracting remaining statements. while (!$error && !$timeout_passed && !empty($bq->query)) { $statement = $bq->extract(true); if (!empty($statement)) { PMA_importRunQuery($statement, $statement, $sql_data); } } // Finishing. PMA_importRunQuery('', '', $sql_data); }