Merge pull request #6573 from rxu/ticket/17232

[ticket/17232] Improve SQL error messages handling
This commit is contained in:
Marc Alexander 2023-12-31 10:43:05 +01:00
commit 565053f508
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
7 changed files with 106 additions and 17 deletions

View file

@ -166,7 +166,16 @@ class mssql_odbc extends \phpbb\db\driver\mssql_base
if ($this->query_result === false) if ($this->query_result === false)
{ {
if (($this->query_result = @odbc_exec($this->db_connect_id, $query)) === false) try
{
$this->query_result = @odbc_exec($this->db_connect_id, $query);
}
catch (\Error $e)
{
// Do nothing as SQL driver will report the error
}
if ($this->query_result === false)
{ {
$this->sql_error($query); $this->sql_error($query);
} }

View file

@ -138,12 +138,22 @@ class mssqlnative extends \phpbb\db\driver\mssql_base
if ($this->query_result === false) if ($this->query_result === false)
{ {
if (($this->query_result = @sqlsrv_query($this->db_connect_id, $query, array(), $this->query_options)) === false) try
{
$this->query_result = @sqlsrv_query($this->db_connect_id, $query, array(), $this->query_options);
}
catch (\Error $e)
{
// Do nothing as SQL driver will report the error
}
if ($this->query_result === false)
{ {
$this->sql_error($query); $this->sql_error($query);
} }
// reset options for next query
$this->query_options = array(); // Reset options for the next query
$this->query_options = [];
if ($this->debug_sql_explain) if ($this->debug_sql_explain)
{ {

View file

@ -197,7 +197,16 @@ class mysqli extends \phpbb\db\driver\mysql_base
if ($this->query_result === false) if ($this->query_result === false)
{ {
if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false) try
{
$this->query_result = @mysqli_query($this->db_connect_id, $query);
}
catch (\Error $e)
{
// Do nothing as SQL driver will report the error
}
if ($this->query_result === false)
{ {
$this->sql_error($query); $this->sql_error($query);
} }
@ -345,24 +354,24 @@ class mysqli extends \phpbb\db\driver\mysql_base
{ {
if ($this->db_connect_id) if ($this->db_connect_id)
{ {
$error = array( $error = [
'message' => @mysqli_error($this->db_connect_id), 'message' => $this->db_connect_id->error,
'code' => @mysqli_errno($this->db_connect_id) 'code' => $this->db_connect_id->errno,
); ];
} }
else if (function_exists('mysqli_connect_error')) else if (function_exists('mysqli_connect_error'))
{ {
$error = array( $error = [
'message' => @mysqli_connect_error(), 'message' => $this->db_connect_id->connect_error,
'code' => @mysqli_connect_errno(), 'code' => $this->db_connect_id->connect_errno,
); ];
} }
else else
{ {
$error = array( $error = [
'message' => $this->connect_error, 'message' => $this->connect_error,
'code' => '', 'code' => '',
); ];
} }
return $error; return $error;

View file

@ -188,7 +188,16 @@ class postgres extends \phpbb\db\driver\driver
if ($this->query_result === false) if ($this->query_result === false)
{ {
if (($this->query_result = @pg_query($this->db_connect_id, $query)) === false) try
{
$this->query_result = @pg_query($this->db_connect_id, $query);
}
catch (\Error $e)
{
// Do nothing as SQL driver will report the error
}
if ($this->query_result === false)
{ {
$this->sql_error($query); $this->sql_error($query);
} }

View file

@ -138,7 +138,16 @@ class sqlite3 extends \phpbb\db\driver\driver
$query = preg_replace('/^INSERT INTO/', 'INSERT OR ROLLBACK INTO', $query); $query = preg_replace('/^INSERT INTO/', 'INSERT OR ROLLBACK INTO', $query);
} }
if (($this->query_result = @$this->dbo->query($query)) === false) try
{
$this->query_result = @$this->dbo->query($query);
}
catch (\Error $e)
{
// Do nothing as SQL driver will report the error
}
if ($this->query_result === false)
{ {
// Try to recover a lost database connection // Try to recover a lost database connection
if ($this->dbo && !@$this->dbo->lastErrorMsg()) if ($this->dbo && !@$this->dbo->lastErrorMsg())

View file

@ -33,6 +33,10 @@ class factory
{ {
return new \phpbb\db\tools\postgres($db_driver, $return_statements); return new \phpbb\db\tools\postgres($db_driver, $return_statements);
} }
else if ($db_driver instanceof \phpbb\db\driver\sqlite3)
{
return new \phpbb\db\tools\sqlite3($db_driver, $return_statements);
}
else if ($db_driver instanceof \phpbb\db\driver\driver_interface) else if ($db_driver instanceof \phpbb\db\driver\driver_interface)
{ {
return new \phpbb\db\tools\tools($db_driver, $return_statements); return new \phpbb\db\tools\tools($db_driver, $return_statements);

View file

@ -0,0 +1,39 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\tools;
/**
* Database Tools for handling cross-db actions such as altering columns, etc.
* Currently not supported is returning SQL for creating tables.
*/
class sqlite3 extends tools
{
/**
* {@inheritDoc}
*/
function sql_table_exists($table_name)
{
$this->db->sql_return_on_error(true);
$result = $this->db->sql_query("SELECT name FROM sqlite_master WHERE type='table' AND name='{$table_name}'");
$this->db->sql_return_on_error(false);
if (!empty($this->db->sql_fetchrowset($result)))
{
$this->db->sql_freeresult($result);
return true;
}
return false;
}
}