Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JDatabaseDriver::splitSql() handle comments and remove trimSql functions #9369

Merged
merged 8 commits into from
Mar 14, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 22 additions & 49 deletions administrator/components/com_admin/script.php
Original file line number Diff line number Diff line change
Expand Up @@ -1756,30 +1756,34 @@ private function convertTablesToUtf8mb4()
{
foreach ($queries2 as $query2)
{
if ($trimmedQuery = $this->trimQuery($query2))
// Downgrade the query if utf8mb4 isn't supported
if (!$utf8mb4Support)
{
// Downgrade the query if utf8mb4 isn't supported
if (!$utf8mb4Support)
{
$trimmedQuery = $this->convertUtf8mb4QueryToUtf8($trimmedQuery);
}

try
{
$db->setQuery($trimmedQuery)->execute();
}
catch (Exception $e)
{
$converted = 0;

// Still render the error message from the Exception object
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
}
$query2 = $this->convertUtf8mb4QueryToUtf8($query2);
}

try
{
$db->setQuery($query2)->execute();
}
catch (Exception $e)
{
$converted = 0;

// Still render the error message from the Exception object
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the string I added last week here (check the database fixer thing)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will not add it in the loop because there it will be repeated. I leave the sql error there for more info, and add the new message below as summary in case if some error happened before in the loop.

}
}
}
}

// Show if there was some error
if ($converted == 0)
{
// Show an error message telling to check database problems
JFactory::getApplication()->enqueueMessage(JText::_('JLIB_DATABASE_ERROR_DATABASE_UPGRADE_FAILED'), 'error');
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it here instead, after the loop.

// Set flag in database if the update is done.
$db->setQuery('UPDATE ' . $db->quoteName('#__utf8_conversion')
. ' SET ' . $db->quoteName('converted') . ' = ' . $converted . ';')->execute();
Expand Down Expand Up @@ -1865,35 +1869,4 @@ private function convertUtf8mb4QueryToUtf8($query)
// Replace utf8mb4 with utf8
return str_replace('utf8mb4', 'utf8', $query);
}

/**
* Trim comment and blank lines out of a query string
*
* @param string $query query string to be trimmed
*
* @return string String with leading comment lines removed
*
* @since 3.5
*/
private function trimQuery($query)
{
$query = trim($query);

while (substr($query, 0, 1) == '#' || substr($query, 0, 2) == '--' || substr($query, 0, 2) == '/*')
{
$endChars = (substr($query, 0, 1) == '#' || substr($query, 0, 2) == '--') ? "\n" : "*/";

if ($position = strpos($query, $endChars))
{
$query = trim(substr($query, $position + strlen($endChars)));
}
else
{
// If no newline, the rest of the file is a comment, so return an empty string.
return '';
}
}

return trim($query);
}
}
52 changes: 9 additions & 43 deletions administrator/components/com_installer/models/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,16 @@ public function convertTablesToUtf8mb4()
{
foreach ($queries2 as $query2)
{
if ($trimmedQuery = $this->trimQuery($query2))
try
{
$db->setQuery($db->convertUtf8mb4QueryToUtf8($query2))->execute();
}
catch (Exception $e)
{
try
{
$db->setQuery($db->convertUtf8mb4QueryToUtf8($trimmedQuery))->execute();
}
catch (Exception $e)
{
$converted = 0;

// Still render the error message from the Exception object
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
}
$converted = 0;

// Still render the error message from the Exception object
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
}
}
}
Expand Down Expand Up @@ -421,35 +418,4 @@ private function prepareUtf8mb4StatusTable()
. ' (' . $db->quoteName('converted') . ') VALUES (0);')->execute();
}
}

/**
* Trim comment and blank lines out of a query string
*
* @param string $query query string to be trimmed
*
* @return string String with leading comment lines removed
*
* @since 3.5
*/
private function trimQuery($query)
{
$query = trim($query);

while (substr($query, 0, 1) == '#' || substr($query, 0, 2) == '--' || substr($query, 0, 2) == '/*')
{
$endChars = (substr($query, 0, 1) == '#' || substr($query, 0, 2) == '--') ? "\n" : "*/";

if ($position = strpos($query, $endChars))
{
$query = trim(substr($query, $position + strlen($endChars)));
}
else
{
// If no newline, the rest of the file is a comment, so return an empty string.
return '';
}
}

return trim($query);
}
}
124 changes: 42 additions & 82 deletions libraries/cms/installer/installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,25 +870,22 @@ public function parseQueries(SimpleXMLElement $element)
// Process each query in the $queries array (children of $tagName).
foreach ($queries as $query)
{
if ($trimmedQuery = $this->trimQuery($query->data()))
// If we don't have UTF-8 Multibyte support we'll have to convert queries to plain UTF-8
if ($doUtf8mb4ToUtf8)
{
// If we don't have UTF-8 Multibyte support we'll have to convert queries to plain UTF-8
if ($doUtf8mb4ToUtf8)
{
$trimmedQuery = $this->convertUtf8mb4QueryToUtf8($trimmedQuery);
}

$db->setQuery($trimmedQuery);
$query = $this->convertUtf8mb4QueryToUtf8($query);
}

if (!$db->execute())
{
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), JLog::WARNING, 'jerror');
$db->setQuery($query);

return false;
}
if (!$db->execute())
{
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), JLog::WARNING, 'jerror');

$update_count++;
return false;
}

$update_count++;
}

return $update_count;
Expand Down Expand Up @@ -974,25 +971,22 @@ public function parseSQLFiles($element)
// Process each query in the $queries array (split out of sql file).
foreach ($queries as $query)
{
if ($trimmedQuery = $this->trimQuery($query))
// If we don't have UTF-8 Multibyte support we'll have to convert queries to plain UTF-8
if ($doUtf8mb4ToUtf8)
{
// If we don't have UTF-8 Multibyte support we'll have to convert queries to plain UTF-8
if ($doUtf8mb4ToUtf8)
{
$trimmedQuery = $this->convertUtf8mb4QueryToUtf8($trimmedQuery);
}

$db->setQuery($trimmedQuery);
$query = $this->convertUtf8mb4QueryToUtf8($query);
}

if (!$db->execute())
{
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), JLog::WARNING, 'jerror');
$db->setQuery($query);

return false;
}
if (!$db->execute())
{
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), JLog::WARNING, 'jerror');

$update_count++;
return false;
}

$update_count++;
}
}
}
Expand Down Expand Up @@ -1175,31 +1169,28 @@ public function parseSchemaUpdates(SimpleXMLElement $schema, $eid)
// Process each query in the $queries array (split out of sql file).
foreach ($queries as $query)
{
if ($trimmedQuery = $this->trimQuery($query))
// If we don't have UTF-8 Multibyte support we'll have to convert queries to plain UTF-8
if ($doUtf8mb4ToUtf8)
{
// If we don't have UTF-8 Multibyte support we'll have to convert queries to plain UTF-8
if ($doUtf8mb4ToUtf8)
{
$trimmedQuery = $this->convertUtf8mb4QueryToUtf8($trimmedQuery);
}

$db->setQuery($trimmedQuery);

if (!$db->execute())
{
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), JLog::WARNING, 'jerror');

return false;
}
else
{
$queryString = (string) $trimmedQuery;
$queryString = str_replace(array("\r", "\n"), array('', ' '), substr($queryString, 0, 80));
JLog::add(JText::sprintf('JLIB_INSTALLER_UPDATE_LOG_QUERY', $file, $queryString), JLog::INFO, 'Update');
}

$update_count++;
$query = $this->convertUtf8mb4QueryToUtf8($query);
}

$db->setQuery($query);

if (!$db->execute())
{
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), JLog::WARNING, 'jerror');

return false;
}
else
{
$queryString = (string) $query;
$queryString = str_replace(array("\r", "\n"), array('', ' '), substr($queryString, 0, 80));
JLog::add(JText::sprintf('JLIB_INSTALLER_UPDATE_LOG_QUERY', $file, $queryString), JLog::INFO, 'Update');
}

$update_count++;
}
}
}
Expand Down Expand Up @@ -2485,35 +2476,4 @@ private function convertUtf8mb4QueryToUtf8($query)
// Replace utf8mb4 with utf8
return str_replace('utf8mb4', 'utf8', $query);
}

/**
* Trim comment and blank lines out of a query string
*
* @param string $query query string to be trimmed
*
* @return string String with leading comment lines removed
*
* @since 3.5
*/
private function trimQuery($query)
{
$query = trim($query);

while (substr($query, 0, 1) == '#' || substr($query, 0, 2) == '--' || substr($query, 0, 2) == '/*')
{
$endChars = (substr($query, 0, 1) == '#' || substr($query, 0, 2) == '--') ? "\n" : "*/";

if ($position = strpos($query, $endChars))
{
$query = trim(substr($query, $position + strlen($endChars)));
}
else
{
// If no newline, the rest of the file is a comment, so return an empty string.
return '';
}
}

return trim($query);
}
}
42 changes: 4 additions & 38 deletions libraries/cms/schema/changeset.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,47 +278,13 @@ private function getUpdateQueries(array $sqlfiles)

foreach ($queries as $query)
{
if ($trimmedQuery = $this->trimQuery($query))
{
$fileQueries = new stdClass;
$fileQueries->file = $file;
$fileQueries->updateQuery = $trimmedQuery;
$result[] = $fileQueries;
}
$fileQueries = new stdClass;
$fileQueries->file = $file;
$fileQueries->updateQuery = $query;
$result[] = $fileQueries;
}
}

return $result;
}

/**
* Trim comment and blank lines out of a query string
*
* @param string $query query string to be trimmed
*
* @return string String with leading comment lines removed
*
* @since 3.1
*/
private function trimQuery($query)
{
$query = trim($query);

while (substr($query, 0, 1) == '#' || substr($query, 0, 2) == '--' || substr($query, 0, 2) == '/*')
{
$endChars = (substr($query, 0, 1) == '#' || substr($query, 0, 2) == '--') ? "\n" : "*/";

if ($position = strpos($query, $endChars))
{
$query = trim(substr($query, $position + strlen($endChars)));
}
else
{
// If no newline, the rest of the file is a comment, so return an empty string.
return '';
}
}

return trim($query);
}
}
Loading