[ticket/10349] Unit tests: Consolidate schema comment removal functions

PHPBB3-10349
This commit is contained in:
Patrick Webster 2011-09-20 23:23:03 -05:00
parent bcaf65d7cd
commit 4bc11db0ff

View file

@ -234,12 +234,11 @@ class phpbb_database_test_connection_manager
} }
$filename = $directory . $schema . '_schema.sql'; $filename = $directory . $schema . '_schema.sql';
$remove_remarks = $this->dbms['COMMENTS'];
$queries = file_get_contents($filename); $queries = file_get_contents($filename);
$this->$remove_remarks($queries); $sql = $this->remove_comments($queries);
$sql = $this->split_sql($queries); $sql = $this->split_sql($sql);
foreach ($sql as $query) foreach ($sql as $query)
{ {
@ -271,60 +270,23 @@ class phpbb_database_test_connection_manager
unset($data[key($data)]); unset($data[key($data)]);
} }
if ($this->config['dbms'] == 'sqlite')
{
// remove comment lines starting with # - they are not proper sqlite
// syntax and break sqlite2
foreach ($data as $i => $query)
{
$data[$i] = preg_replace('/^#.*$/m', "\n", $query);
}
}
return $data; return $data;
} }
/** /**
* remove_remarks will strip the sql comment lines out of an uploaded sql file * Removes comments from schema files
*
* Note: This performs the functions of remove_remarks() and remove_comments() used during installation
*/ */
protected function remove_remarks(&$sql) protected function remove_comments($sql)
{ {
// Remove /* */ comments (http://ostermiller.org/findcomment.html)
$sql = preg_replace('#/\*(.|[\r\n])*?\*/#', "\n", $sql);
// Remove # style comments
$sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql)); $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
}
/** return $sql;
* remove_comments will strip the sql comment lines out of an uploaded sql file
* specifically for mssql and postgres type files in the install....
*/
protected function remove_comments(&$output)
{
$lines = explode("\n", $output);
$output = '';
// try to keep mem. use down
$linecount = sizeof($lines);
$in_comment = false;
for ($i = 0; $i < $linecount; $i++)
{
if (trim($lines[$i]) == '/*')
{
$in_comment = true;
}
if (!$in_comment)
{
$output .= $lines[$i] . "\n";
}
if (trim($lines[$i]) == '*/')
{
$in_comment = false;
}
}
unset($lines);
return $output;
} }
/** /**
@ -337,55 +299,46 @@ class phpbb_database_test_connection_manager
'SCHEMA' => 'firebird', 'SCHEMA' => 'firebird',
'DELIM' => ';;', 'DELIM' => ';;',
'PDO' => 'firebird', 'PDO' => 'firebird',
'COMMENTS' => 'remove_remarks',
), ),
'mysqli' => array( 'mysqli' => array(
'SCHEMA' => 'mysql_41', 'SCHEMA' => 'mysql_41',
'DELIM' => ';', 'DELIM' => ';',
'PDO' => 'mysql', 'PDO' => 'mysql',
'COMMENTS' => 'remove_remarks',
), ),
'mysql' => array( 'mysql' => array(
'SCHEMA' => 'mysql', 'SCHEMA' => 'mysql',
'DELIM' => ';', 'DELIM' => ';',
'PDO' => 'mysql', 'PDO' => 'mysql',
'COMMENTS' => 'remove_remarks',
), ),
'mssql' => array( 'mssql' => array(
'SCHEMA' => 'mssql', 'SCHEMA' => 'mssql',
'DELIM' => 'GO', 'DELIM' => 'GO',
'PDO' => 'odbc', 'PDO' => 'odbc',
'COMMENTS' => 'remove_comments',
), ),
'mssql_odbc'=> array( 'mssql_odbc'=> array(
'SCHEMA' => 'mssql', 'SCHEMA' => 'mssql',
'DELIM' => 'GO', 'DELIM' => 'GO',
'PDO' => 'odbc', 'PDO' => 'odbc',
'COMMENTS' => 'remove_comments',
), ),
'mssqlnative' => array( 'mssqlnative' => array(
'SCHEMA' => 'mssql', 'SCHEMA' => 'mssql',
'DELIM' => 'GO', 'DELIM' => 'GO',
'PDO' => 'sqlsrv', 'PDO' => 'sqlsrv',
'COMMENTS' => 'remove_comments',
), ),
'oracle' => array( 'oracle' => array(
'SCHEMA' => 'oracle', 'SCHEMA' => 'oracle',
'DELIM' => '/', 'DELIM' => '/',
'PDO' => 'oci', 'PDO' => 'oci',
'COMMENTS' => 'remove_comments',
), ),
'postgres' => array( 'postgres' => array(
'SCHEMA' => 'postgres', 'SCHEMA' => 'postgres',
'DELIM' => ';', 'DELIM' => ';',
'PDO' => 'pgsql', 'PDO' => 'pgsql',
'COMMENTS' => 'remove_comments',
), ),
'sqlite' => array( 'sqlite' => array(
'SCHEMA' => 'sqlite', 'SCHEMA' => 'sqlite',
'DELIM' => ';', 'DELIM' => ';',
'PDO' => 'sqlite2', 'PDO' => 'sqlite2',
'COMMENTS' => 'remove_remarks',
), ),
); );