first commit
This commit is contained in:
1586
administrator/components/com_akeebabackup/engine/Driver/Base.php
Normal file
1586
administrator/components/com_akeebabackup/engine/Driver/Base.php
Normal file
File diff suppressed because it is too large
Load Diff
1054
administrator/components/com_akeebabackup/engine/Driver/Mysql.php
Normal file
1054
administrator/components/com_akeebabackup/engine/Driver/Mysql.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,586 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
use Akeeba\Engine\Driver\Query\Mysqli as QueryMysqli;
|
||||
use Akeeba\Engine\FixMySQLHostname;
|
||||
use mysqli_result;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* MySQL Improved (mysqli) database driver for Akeeba Engine
|
||||
*
|
||||
* Based on Joomla! Platform 11.2
|
||||
*/
|
||||
class Mysqli extends Mysql
|
||||
{
|
||||
use FixMySQLHostname;
|
||||
|
||||
/**
|
||||
* The name of the database driver.
|
||||
*
|
||||
* @var string
|
||||
* @since 11.1
|
||||
*/
|
||||
public $name = 'mysqli';
|
||||
|
||||
/** @var \mysqli|null The db connection resource */
|
||||
protected $connection = '';
|
||||
|
||||
/** @var mysqli_result|null The database connection cursor from the last query. */
|
||||
protected $cursor;
|
||||
|
||||
protected $port;
|
||||
|
||||
protected $socket;
|
||||
|
||||
protected $ssl = [];
|
||||
|
||||
/** @var bool Are we in the process of reconnecting to the database server? */
|
||||
private $isReconnecting = false;
|
||||
|
||||
/**
|
||||
* Database object constructor
|
||||
*
|
||||
* @param array $options List of options used to configure the connection
|
||||
*/
|
||||
public function __construct($options)
|
||||
{
|
||||
$this->driverType = 'mysql';
|
||||
|
||||
// Init
|
||||
$this->nameQuote = '`';
|
||||
|
||||
$options['ssl'] = $options['ssl'] ?? [];
|
||||
$options['ssl'] = is_array($options['ssl']) ? $options['ssl'] : [];
|
||||
|
||||
$options['ssl']['enable'] = ($options['ssl']['enable'] ?? $options['dbencryption'] ?? false) ?: false;
|
||||
$options['ssl']['cipher'] = ($options['ssl']['cipher'] ?? $options['dbsslcipher'] ?? null) ?: null;
|
||||
$options['ssl']['ca'] = ($options['ssl']['ca'] ?? $options['dbsslca'] ?? null) ?: null;
|
||||
$options['ssl']['capath'] = ($options['ssl']['capath'] ?? $options['dbsslcapath'] ?? null) ?: null;
|
||||
$options['ssl']['key'] = ($options['ssl']['key'] ?? $options['dbsslkey'] ?? null) ?: null;
|
||||
$options['ssl']['cert'] = ($options['ssl']['cert'] ?? $options['dbsslcert'] ?? null) ?: null;
|
||||
$options['ssl']['verify_server_cert'] = ($options['ssl']['verify_server_cert'] ?? $options['dbsslverifyservercert'] ?? false) ?: false;
|
||||
|
||||
// Figure out if a port is included in the host name
|
||||
$this->fixHostnamePortSocket($options['host'], $options['port'], $options['socket']);
|
||||
|
||||
// Set the information
|
||||
$this->host = $options['host'] ?? 'localhost';
|
||||
$this->user = $options['user'] ?? '';
|
||||
$this->password = $options['password'] ?? '';
|
||||
$this->port = $options['port'] ?? '';
|
||||
$this->socket = $options['socket'] ?? '';
|
||||
$this->_database = $options['database'] ?? '';
|
||||
$this->selectDatabase = $options['select'] ?? true;
|
||||
$this->ssl = $options['ssl'] ?? [];
|
||||
|
||||
// Finalize initialization. Also opens the connection.
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to see if the MySQL connector is available.
|
||||
*
|
||||
* @return boolean True on success, false otherwise.
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
return (function_exists('mysqli_connect'));
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
$return = false;
|
||||
|
||||
if (is_object($this->cursor) && ($this->cursor instanceof mysqli_result))
|
||||
{
|
||||
try
|
||||
{
|
||||
@$this->cursor->free();
|
||||
}
|
||||
catch (\Throwable $e)
|
||||
{
|
||||
}
|
||||
|
||||
$this->cursor = null;
|
||||
}
|
||||
|
||||
if (is_object($this->connection) && ($this->connection instanceof \mysqli))
|
||||
{
|
||||
try
|
||||
{
|
||||
$return = @$this->connection->close();
|
||||
}
|
||||
catch (\Throwable $e)
|
||||
{
|
||||
$return = false;
|
||||
}
|
||||
}
|
||||
|
||||
$this->connection = null;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the connection to the server is active.
|
||||
*
|
||||
* @return boolean True if connected to the database engine.
|
||||
*/
|
||||
public function connected()
|
||||
{
|
||||
if (is_object($this->connection))
|
||||
{
|
||||
return @mysqli_ping($this->connection);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to escape a string for usage in an SQL statement.
|
||||
*
|
||||
* @param string $text The string to be escaped.
|
||||
* @param boolean $extra Optional parameter to provide extra escaping.
|
||||
*
|
||||
* @return string The escaped string.
|
||||
*/
|
||||
public function escape($text, $extra = false)
|
||||
{
|
||||
if (is_null($text))
|
||||
{
|
||||
return 'NULL';
|
||||
}
|
||||
|
||||
$result = @mysqli_real_escape_string($this->getConnection(), $text);
|
||||
|
||||
if ($result === false)
|
||||
{
|
||||
// Attempt to reconnect.
|
||||
try
|
||||
{
|
||||
$this->connection = null;
|
||||
$this->open();
|
||||
|
||||
$result = @mysqli_real_escape_string($this->getConnection(), $text);;
|
||||
}
|
||||
catch (RuntimeException $e)
|
||||
{
|
||||
$result = $this->unsafe_escape($text);
|
||||
}
|
||||
}
|
||||
|
||||
if ($extra)
|
||||
{
|
||||
$result = addcslashes($result, '%_');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an associative array.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
public function fetchAssoc($cursor = null)
|
||||
{
|
||||
return mysqli_fetch_assoc($cursor ?: $this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to free up the memory used for the result set.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function freeResult($cursor = null)
|
||||
{
|
||||
mysqli_free_result($cursor ?: $this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of affected rows for the previous executed SQL statement.
|
||||
*
|
||||
* @return integer The number of affected rows.
|
||||
*/
|
||||
public function getAffectedRows()
|
||||
{
|
||||
return mysqli_affected_rows($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of returned rows for the previous executed SQL statement.
|
||||
*
|
||||
* @param mysqli_result $cursor An optional database cursor resource to extract the row count from.
|
||||
*
|
||||
* @return integer The number of returned rows.
|
||||
*/
|
||||
public function getNumRows($cursor = null)
|
||||
{
|
||||
return mysqli_num_rows($cursor ?: $this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current or query, or new JDatabaseQuery object.
|
||||
*
|
||||
* @param boolean $new False to return the last query set, True to return a new JDatabaseQuery object.
|
||||
*
|
||||
* @return mixed The current value of the internal SQL variable or a new JDatabaseQuery object.
|
||||
*/
|
||||
public function getQuery($new = false)
|
||||
{
|
||||
if ($new)
|
||||
{
|
||||
return new QueryMysqli($this);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->sql;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of the database connector.
|
||||
*
|
||||
* @return string The database connector version.
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
return mysqli_get_server_info($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the database engine supports UTF-8 character encoding.
|
||||
*
|
||||
* @return boolean True if supported.
|
||||
*/
|
||||
public function hasUTF()
|
||||
{
|
||||
$mariadb = stripos($this->connection->server_info, 'mariadb') !== false;
|
||||
$client_version = mysqli_get_client_info();
|
||||
$server_version = $this->getVersion();
|
||||
|
||||
if (version_compare($server_version, '5.5.3', '<'))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($mariadb && version_compare($server_version, '10.0.0', '<'))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strpos($client_version, 'mysqlnd') !== false)
|
||||
{
|
||||
$client_version = preg_replace('/^\D+([\d.]+).*/', '$1', $client_version);
|
||||
|
||||
return version_compare($client_version, '5.0.9', '>=');
|
||||
}
|
||||
|
||||
return version_compare($client_version, '5.5.3', '>=');
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the auto-incremented value from the last INSERT statement.
|
||||
*
|
||||
* @return integer The value of the auto-increment field from the last inserted row.
|
||||
*/
|
||||
public function insertid()
|
||||
{
|
||||
return mysqli_insert_id($this->connection);
|
||||
}
|
||||
|
||||
public function open()
|
||||
{
|
||||
if ($this->connected())
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->close();
|
||||
}
|
||||
|
||||
// perform a number of fatality checks, then return gracefully
|
||||
if (!function_exists('mysqli_connect'))
|
||||
{
|
||||
$this->errorNum = 1;
|
||||
$this->errorMsg = 'The MySQL adapter "mysqli" is not available.';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Let's prepare a connection
|
||||
$this->connection = mysqli_init();
|
||||
|
||||
$connectionFlags = 0;
|
||||
|
||||
// For SSL/TLS connection encryption.
|
||||
if ($this->ssl !== [] && $this->ssl['enable'] === true)
|
||||
{
|
||||
$connectionFlags = $connectionFlags | MYSQLI_CLIENT_SSL;
|
||||
|
||||
// Verify server certificate is only available in PHP 5.6.16+. See https://www.php.net/ChangeLog-5.php#5.6.16
|
||||
if (isset($this->ssl['verify_server_cert']))
|
||||
{
|
||||
// New constants in PHP 5.6.16+. See https://www.php.net/ChangeLog-5.php#5.6.16
|
||||
if ($this->ssl['verify_server_cert'] === true && defined('MYSQLI_CLIENT_SSL_VERIFY_SERVER_CERT'))
|
||||
{
|
||||
$connectionFlags = $connectionFlags | MYSQLI_CLIENT_SSL_VERIFY_SERVER_CERT;
|
||||
}
|
||||
elseif ($this->ssl['verify_server_cert'] === false && defined('MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT'))
|
||||
{
|
||||
$connectionFlags = $connectionFlags | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
|
||||
}
|
||||
elseif (defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT'))
|
||||
{
|
||||
$this->connection->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, $this->ssl['verify_server_cert']);
|
||||
}
|
||||
}
|
||||
|
||||
// Add SSL/TLS options only if changed.
|
||||
$this->connection->ssl_set(
|
||||
($this->ssl['key'] ?? null) ?: null,
|
||||
($this->ssl['cert'] ?? null) ?: null,
|
||||
($this->ssl['ca'] ?? null) ?: null,
|
||||
($this->ssl['capath'] ?? null) ?: null,
|
||||
($this->ssl['cipher'] ?? null) ?: null
|
||||
);
|
||||
}
|
||||
|
||||
// Attempt to connect to the server, use error suppression to silence warnings and allow us to throw an Exception separately.
|
||||
try
|
||||
{
|
||||
$connected = @$this->connection->real_connect(
|
||||
$this->host,
|
||||
$this->user,
|
||||
$this->password ?: null,
|
||||
null,
|
||||
$this->port ?: 3306,
|
||||
$this->socket ?: null,
|
||||
$connectionFlags
|
||||
);
|
||||
}
|
||||
catch (\Throwable $e)
|
||||
{
|
||||
$connected = false;
|
||||
}
|
||||
|
||||
// connect to the server
|
||||
if (!$connected)
|
||||
{
|
||||
$this->errorNum = 2;
|
||||
$this->errorMsg = 'Could not connect to MySQL';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Set sql_mode to non_strict mode
|
||||
mysqli_query($this->connection, "SET @@SESSION.sql_mode = '';");
|
||||
|
||||
if ($this->selectDatabase && !empty($this->_database))
|
||||
{
|
||||
if (!$this->select($this->_database))
|
||||
{
|
||||
$this->errorNum = 3;
|
||||
$this->errorMsg = "Cannot select database {$this->_database}";
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->setUTF();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the SQL statement.
|
||||
*
|
||||
* @return mixed A database cursor resource on success, boolean false on failure.
|
||||
*/
|
||||
public function query()
|
||||
{
|
||||
$this->open();
|
||||
|
||||
if (!is_object($this->connection))
|
||||
{
|
||||
throw new RuntimeException($this->errorMsg, $this->errorNum);
|
||||
}
|
||||
|
||||
// Take a local copy so that we don't modify the original query and cause issues later
|
||||
$query = $this->replacePrefix((string) $this->sql);
|
||||
if ($this->limit > 0 || $this->offset > 0)
|
||||
{
|
||||
$query .= ' LIMIT ' . $this->offset . ', ' . $this->limit;
|
||||
}
|
||||
|
||||
// Increment the query counter.
|
||||
$this->count++;
|
||||
|
||||
// If debugging is enabled then let's log the query.
|
||||
if ($this->debug)
|
||||
{
|
||||
// Add the query to the object queue.
|
||||
$this->log[] = $query;
|
||||
}
|
||||
|
||||
// Reset the error values.
|
||||
$this->errorNum = 0;
|
||||
$this->errorMsg = '';
|
||||
|
||||
// Execute the query. Error suppression is used here to prevent warnings/notices that the connection has been lost.
|
||||
$this->cursor = @mysqli_query($this->connection, $query);
|
||||
|
||||
// If an error occurred handle it.
|
||||
if (!$this->cursor)
|
||||
{
|
||||
$this->errorNum = 0;
|
||||
$this->errorMsg = '';
|
||||
|
||||
if ($this->connection)
|
||||
{
|
||||
$this->errorNum = (int) @mysqli_errno($this->connection);
|
||||
$this->errorMsg = (string) @mysqli_error($this->connection) . ' SQL=' . $query;
|
||||
}
|
||||
|
||||
// Check if the server was disconnected.
|
||||
if (!$this->connected() && !$this->isReconnecting)
|
||||
{
|
||||
$this->isReconnecting = true;
|
||||
|
||||
try
|
||||
{
|
||||
// Attempt to reconnect.
|
||||
$this->connection = null;
|
||||
$this->open();
|
||||
}
|
||||
// If connect fails, ignore that exception and throw the normal exception.
|
||||
catch (RuntimeException $e)
|
||||
{
|
||||
throw new RuntimeException($this->errorMsg, $this->errorNum);
|
||||
}
|
||||
|
||||
// Since we were able to reconnect, run the query again.
|
||||
$result = $this->query();
|
||||
$this->isReconnecting = false;
|
||||
|
||||
return $result;
|
||||
}
|
||||
// The server was not disconnected.
|
||||
elseif ($this->errorNum != 0)
|
||||
{
|
||||
throw new RuntimeException($this->errorMsg, $this->errorNum);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->cursor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a database for use.
|
||||
*
|
||||
* @param string $database The name of the database to select for use.
|
||||
*
|
||||
* @return boolean True if the database was successfully selected.
|
||||
*/
|
||||
public function select($database)
|
||||
{
|
||||
if (!$database)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!mysqli_select_db($this->connection, $database))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the connection to use UTF-8 character encoding.
|
||||
*
|
||||
* @return boolean True on success.
|
||||
*/
|
||||
public function setUTF()
|
||||
{
|
||||
$result = false;
|
||||
|
||||
if ($this->supportsUtf8mb4())
|
||||
{
|
||||
$result = @mysqli_set_charset($this->connection, 'utf8mb4');
|
||||
}
|
||||
|
||||
if (!$result)
|
||||
{
|
||||
$result = @mysqli_set_charset($this->connection, 'utf8');
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this database server support UTF-8 four byte (utf8mb4) collation?
|
||||
*
|
||||
* libmysql supports utf8mb4 since 5.5.3 (same version as the MySQL server). mysqlnd supports utf8mb4 since 5.0.9.
|
||||
*
|
||||
* This method's code is based on WordPress' wpdb::has_cap() method
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function supportsUtf8mb4()
|
||||
{
|
||||
$client_version = mysqli_get_client_info();
|
||||
|
||||
if (strpos($client_version, 'mysqlnd') !== false)
|
||||
{
|
||||
$client_version = preg_replace('/^\D+([\d.]+).*/', '$1', $client_version);
|
||||
|
||||
return version_compare($client_version, '5.0.9', '>=');
|
||||
}
|
||||
else
|
||||
{
|
||||
return version_compare($client_version, '5.5.3', '>=');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an array.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchArray($cursor = null)
|
||||
{
|
||||
return mysqli_fetch_row($cursor ?: $this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an object.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
* @param string $class The class name to use for the returned row object.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchObject($cursor = null, $class = 'stdClass')
|
||||
{
|
||||
return mysqli_fetch_object($cursor ?: $this->cursor, $class);
|
||||
}
|
||||
}
|
||||
373
administrator/components/com_akeebabackup/engine/Driver/None.php
Normal file
373
administrator/components/com_akeebabackup/engine/Driver/None.php
Normal file
@@ -0,0 +1,373 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
use Akeeba\Engine\Driver\Query\Base as QueryBase;
|
||||
|
||||
/**
|
||||
* Dummy driver class for flat-file CMS
|
||||
*/
|
||||
class None extends Base
|
||||
{
|
||||
public static $dbtech = 'none';
|
||||
/**
|
||||
* The name of the database driver.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0
|
||||
*/
|
||||
public $name = 'none';
|
||||
|
||||
public function __construct(array $options)
|
||||
{
|
||||
$this->driverType = 'none';
|
||||
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to see if this db driver is available
|
||||
*
|
||||
* @return boolean True on success, false otherwise.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function open()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the database connection
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the connection to the server is active.
|
||||
*
|
||||
* @return boolean True if connected to the database engine.
|
||||
*/
|
||||
public function connected()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a table from the database.
|
||||
*
|
||||
* @param string $table The name of the database table to drop.
|
||||
* @param boolean $ifExists Optionally specify that the table must exist before it is dropped.
|
||||
*
|
||||
* @return Base Returns this object to support chaining.
|
||||
*/
|
||||
public function dropTable($table, $ifExists = true)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to escape a string for usage in an SQL statement.
|
||||
*
|
||||
* @param string $text The string to be escaped.
|
||||
* @param boolean $extra Optional parameter to provide extra escaping.
|
||||
*
|
||||
* @return string The escaped string.
|
||||
*/
|
||||
public function escape($text, $extra = false)
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an associative array.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
public function fetchAssoc($cursor = null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to free up the memory used for the result set.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function freeResult($cursor = null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of affected rows for the previous executed SQL statement.
|
||||
*
|
||||
* @return integer The number of affected rows.
|
||||
*/
|
||||
public function getAffectedRows()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the database collation in use by sampling a text field of a table in the database.
|
||||
*
|
||||
* @return mixed The collation in use by the database or boolean false if not supported.
|
||||
*/
|
||||
public function getCollation()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of returned rows for the previous executed SQL statement.
|
||||
*
|
||||
* @param resource $cursor An optional database cursor resource to extract the row count from.
|
||||
*
|
||||
* @return integer The number of returned rows.
|
||||
*/
|
||||
public function getNumRows($cursor = null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current query object or a new QueryBase object.
|
||||
*
|
||||
* @param boolean $new False to return the current query object, True to return a new QueryBase object.
|
||||
*
|
||||
* @return QueryBase The current query object or a new object extending the QueryBase class.
|
||||
*/
|
||||
public function getQuery($new = false)
|
||||
{
|
||||
return $this->sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves field information about the given tables.
|
||||
*
|
||||
* @param string $table The name of the database table.
|
||||
* @param boolean $typeOnly True (default) to only return field types.
|
||||
*
|
||||
* @return array An array of fields by table.
|
||||
*/
|
||||
public function getTableColumns($table, $typeOnly = true)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the table CREATE statement that creates the given tables.
|
||||
*
|
||||
* @param mixed $tables A table name or a list of table names.
|
||||
*
|
||||
* @return array A list of the create SQL for the tables.
|
||||
*/
|
||||
public function getTableCreate($tables)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves field information about the given tables.
|
||||
*
|
||||
* @param mixed $tables A table name or a list of table names.
|
||||
*
|
||||
* @return array An array of keys for the table(s).
|
||||
*/
|
||||
public function getTableKeys($tables)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get an array of all tables in the database.
|
||||
*
|
||||
* @return array An array of all the tables in the database.
|
||||
*/
|
||||
public function getTableList()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with the names of tables, views, procedures, functions and triggers
|
||||
* in the database. The table names are the keys of the tables, whereas the value is
|
||||
* the type of each element: table, view, merge, temp, procedure, function or trigger.
|
||||
* Note that merge are MRG_MYISAM tables and temp is non-permanent data table, usually
|
||||
* set up as temporary, black hole or federated tables. These two types should never,
|
||||
* ever, have their data dumped in the SQL dump file.
|
||||
*
|
||||
* @param bool $abstract Return or normal names? Defaults to true (names)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($abstract = true)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of the database connector
|
||||
*
|
||||
* @return string The database connector version.
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
return '0.0.0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the auto-incremented value from the last INSERT statement.
|
||||
*
|
||||
* @return integer The value of the auto-increment field from the last inserted row.
|
||||
*/
|
||||
public function insertid()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks a table in the database.
|
||||
*
|
||||
* @param string $tableName The name of the table to unlock.
|
||||
*
|
||||
* @return Base Returns this object to support chaining.
|
||||
*/
|
||||
public function lockTable($tableName)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the SQL statement.
|
||||
*
|
||||
* @return mixed A database cursor resource on success, boolean false on failure.
|
||||
*/
|
||||
public function query()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renames a table in the database.
|
||||
*
|
||||
* @param string $oldTable The name of the table to be renamed
|
||||
* @param string $newTable The new name for the table.
|
||||
* @param string $backup Table prefix
|
||||
* @param string $prefix For the table - used to rename constraints in non-mysql databases
|
||||
*
|
||||
* @return Base Returns this object to support chaining.
|
||||
*/
|
||||
public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a database for use.
|
||||
*
|
||||
* @param string $database The name of the database to select for use.
|
||||
*
|
||||
* @return boolean True if the database was successfully selected.
|
||||
*/
|
||||
public function select($database)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the connection to use UTF-8 character encoding.
|
||||
*
|
||||
* @return boolean True on success.
|
||||
*/
|
||||
public function setUTF()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to commit a transaction.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function transactionCommit()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to roll back a transaction.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function transactionRollback()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to initialize a transaction.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function transactionStart()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks tables in the database.
|
||||
*
|
||||
* @return Base Returns this object to support chaining.
|
||||
*/
|
||||
public function unlockTables()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an array.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchArray($cursor = null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an object.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
* @param string $class The class name to use for the returned row object.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchObject($cursor = null, $class = 'stdClass')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,774 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
use Akeeba\Engine\Driver\Query\Pdomysql as QueryPdomysql;
|
||||
use Akeeba\Engine\FixMySQLHostname;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use PDOStatement;
|
||||
use ReflectionClass;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* PDO MySQL database driver for Akeeba Engine
|
||||
*
|
||||
* Based on Joomla! Platform 12.1
|
||||
*/
|
||||
class Pdomysql extends Mysql
|
||||
{
|
||||
use FixMySQLHostname;
|
||||
|
||||
/**
|
||||
* The default cipher suite for TLS connections.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $defaultCipherSuite = [
|
||||
'AES128-GCM-SHA256',
|
||||
'AES256-GCM-SHA384',
|
||||
'AES128-CBC-SHA256',
|
||||
'AES256-CBC-SHA384',
|
||||
'DES-CBC3-SHA',
|
||||
];
|
||||
|
||||
/**
|
||||
* The name of the database driver.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'pdomysql';
|
||||
|
||||
/** @var string Connection character set */
|
||||
protected $charset = 'utf8mb4';
|
||||
|
||||
/** @var PDO The db connection resource */
|
||||
protected $connection = null;
|
||||
|
||||
/** @var PDOStatement The database connection cursor from the last query. */
|
||||
protected $cursor;
|
||||
|
||||
/** @var array Driver options for PDO */
|
||||
protected $driverOptions = [];
|
||||
|
||||
protected $ssl = [];
|
||||
|
||||
/** @var bool Are we in the process of reconnecting to the database server? */
|
||||
private $isReconnecting = false;
|
||||
|
||||
/**
|
||||
* Database object constructor
|
||||
*
|
||||
* @param array $options List of options used to configure the connection
|
||||
*/
|
||||
public function __construct($options)
|
||||
{
|
||||
$this->driverType = 'mysql';
|
||||
|
||||
// Init
|
||||
$this->nameQuote = '`';
|
||||
|
||||
$options['ssl'] = $options['ssl'] ?? [];
|
||||
$options['ssl'] = is_array($options['ssl']) ? $options['ssl'] : [];
|
||||
|
||||
$options['ssl']['enable'] = ($options['ssl']['enable'] ?? $options['dbencryption'] ?? false) ?: false;
|
||||
$options['ssl']['cipher'] = ($options['ssl']['cipher'] ?? $options['dbsslcipher'] ?? null) ?: null;
|
||||
$options['ssl']['ca'] = ($options['ssl']['ca'] ?? $options['dbsslca'] ?? null) ?: null;
|
||||
$options['ssl']['capath'] = ($options['ssl']['capath'] ?? $options['dbsslcapath'] ?? null) ?: null;
|
||||
$options['ssl']['key'] = ($options['ssl']['key'] ?? $options['dbsslkey'] ?? null) ?: null;
|
||||
$options['ssl']['cert'] = ($options['ssl']['cert'] ?? $options['dbsslcert'] ?? null) ?: null;
|
||||
$options['ssl']['verify_server_cert'] = ($options['ssl']['verify_server_cert'] ?? $options['dbsslverifyservercert'] ?? false) ?: false;
|
||||
|
||||
// Figure out if a port is included in the host name
|
||||
$this->fixHostnamePortSocket($options['host'], $options['port'], $options['socket']);
|
||||
|
||||
// Open the connection
|
||||
$this->host = $options['host'] ?? 'localhost';
|
||||
$this->user = $options['user'] ?? '';
|
||||
$this->password = $options['password'] ?? '';
|
||||
$this->port = $options['port'] ?? '';
|
||||
$this->socket = $options['socket'] ?? '';
|
||||
$this->_database = $options['database'] ?? '';
|
||||
$this->selectDatabase = $options['select'] ?? true;
|
||||
$this->ssl = $options['ssl'] ?? [];
|
||||
|
||||
$this->charset = $options['charset'] ?? 'utf8mb4';
|
||||
$this->driverOptions = $options['driverOptions'] ?? [];
|
||||
$this->tablePrefix = $options['prefix'] ?? '';
|
||||
$this->connection = $options['connection'] ?? null;
|
||||
$this->errorNum = 0;
|
||||
$this->count = 0;
|
||||
$this->log = [];
|
||||
$this->options = $options;
|
||||
|
||||
if (!is_object($this->connection))
|
||||
{
|
||||
$this->open();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to see if the MySQL connector is available.
|
||||
*
|
||||
* @return boolean True on success, false otherwise.
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
if (!defined('\PDO::ATTR_DRIVER_NAME'))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return in_array('mysql', PDO::getAvailableDrivers());
|
||||
}
|
||||
|
||||
/**
|
||||
* PDO does not support serialize
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
$serializedProperties = [];
|
||||
|
||||
$reflect = new ReflectionClass($this);
|
||||
|
||||
// Get properties of the current class
|
||||
$properties = $reflect->getProperties();
|
||||
|
||||
foreach ($properties as $property)
|
||||
{
|
||||
// Do not serialize properties that are \PDO
|
||||
if ($property->isStatic() == false && !($this->{$property->name} instanceof PDO))
|
||||
{
|
||||
array_push($serializedProperties, $property->name);
|
||||
}
|
||||
}
|
||||
|
||||
return $serializedProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wake up after serialization
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __wakeup()
|
||||
{
|
||||
// Get connection back
|
||||
$this->__construct($this->options);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
$return = false;
|
||||
|
||||
if (is_object($this->cursor))
|
||||
{
|
||||
$this->cursor->closeCursor();
|
||||
}
|
||||
|
||||
$this->connection = null;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the connection to the server is active.
|
||||
*
|
||||
* @return boolean True if connected to the database engine.
|
||||
*/
|
||||
public function connected()
|
||||
{
|
||||
if (!is_object($this->connection))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
/** @var PDOStatement $statement */
|
||||
$statement = $this->connection->prepare('SELECT 1');
|
||||
$executed = $statement->execute();
|
||||
$ret = 0;
|
||||
|
||||
if ($executed)
|
||||
{
|
||||
$row = [0];
|
||||
|
||||
if (!empty($statement) && $statement instanceof PDOStatement)
|
||||
{
|
||||
$row = $statement->fetch(PDO::FETCH_NUM);
|
||||
}
|
||||
|
||||
$ret = $row[0];
|
||||
}
|
||||
|
||||
$status = $ret == 1;
|
||||
|
||||
$statement->closeCursor();
|
||||
$statement = null;
|
||||
}
|
||||
// If we catch an exception here, we must not be connected.
|
||||
catch (Exception $e)
|
||||
{
|
||||
$status = false;
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to escape a string for usage in an SQL statement.
|
||||
*
|
||||
* @param string $text The string to be escaped.
|
||||
* @param boolean $extra Optional parameter to provide extra escaping.
|
||||
*
|
||||
* @return string The escaped string.
|
||||
*/
|
||||
public function escape($text, $extra = false)
|
||||
{
|
||||
if (is_int($text) || is_float($text))
|
||||
{
|
||||
return $text;
|
||||
}
|
||||
|
||||
if (is_null($text))
|
||||
{
|
||||
return 'NULL';
|
||||
}
|
||||
|
||||
$result = substr($this->connection->quote($text), 1, -1);
|
||||
|
||||
if ($extra)
|
||||
{
|
||||
$result = addcslashes($result, '%_');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an associative array.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
public function fetchAssoc($cursor = null)
|
||||
{
|
||||
$ret = null;
|
||||
|
||||
if (!empty($cursor) && $cursor instanceof PDOStatement)
|
||||
{
|
||||
$ret = $cursor->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
elseif ($this->cursor instanceof PDOStatement)
|
||||
{
|
||||
$ret = $this->cursor->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to free up the memory used for the result set.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function freeResult($cursor = null)
|
||||
{
|
||||
if ($cursor instanceof PDOStatement)
|
||||
{
|
||||
$cursor->closeCursor();
|
||||
$cursor = null;
|
||||
}
|
||||
|
||||
if ($this->cursor instanceof PDOStatement)
|
||||
{
|
||||
$this->cursor->closeCursor();
|
||||
$this->cursor = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of affected rows for the previous executed SQL statement.
|
||||
*
|
||||
* @return integer The number of affected rows.
|
||||
*/
|
||||
public function getAffectedRows()
|
||||
{
|
||||
if ($this->cursor instanceof PDOStatement)
|
||||
{
|
||||
return $this->cursor->rowCount();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of returned rows for the previous executed SQL statement.
|
||||
*
|
||||
* @param resource $cursor An optional database cursor resource to extract the row count from.
|
||||
*
|
||||
* @return integer The number of returned rows.
|
||||
*/
|
||||
public function getNumRows($cursor = null)
|
||||
{
|
||||
if ($cursor instanceof PDOStatement)
|
||||
{
|
||||
return $cursor->rowCount();
|
||||
}
|
||||
|
||||
if ($this->cursor instanceof PDOStatement)
|
||||
{
|
||||
return $this->cursor->rowCount();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current or query, or new JDatabaseQuery object.
|
||||
*
|
||||
* @param boolean $new False to return the last query set, True to return a new JDatabaseQuery object.
|
||||
*
|
||||
* @return mixed The current value of the internal SQL variable or a new JDatabaseQuery object.
|
||||
*/
|
||||
public function getQuery($new = false)
|
||||
{
|
||||
if ($new)
|
||||
{
|
||||
return new QueryPdomysql($this);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->sql;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of the database connector.
|
||||
*
|
||||
* @return string The database connector version.
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
$version = $this->connection->getAttribute(\PDO::ATTR_SERVER_VERSION);
|
||||
|
||||
if (stripos($version, 'mariadb') !== false)
|
||||
{
|
||||
// MariaDB: Strip off any leading '5.5.5-', if present
|
||||
return preg_replace('/^5\.5\.5-/', '', $version);
|
||||
}
|
||||
|
||||
return $version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the database engine supports UTF-8 character encoding.
|
||||
*
|
||||
* @return boolean True if supported.
|
||||
*/
|
||||
public function hasUTF()
|
||||
{
|
||||
$serverVersion = $this->getVersion();
|
||||
$mariadb = stripos($serverVersion, 'mariadb') !== false;
|
||||
|
||||
// At this point we know the client supports utf8mb4. Now we must check if the server supports utf8mb4 as well.
|
||||
$utf8mb4 = version_compare($serverVersion, '5.5.3', '>=');
|
||||
|
||||
if ($mariadb && version_compare($serverVersion, '10.0.0', '<'))
|
||||
{
|
||||
$utf8mb4 = false;
|
||||
}
|
||||
|
||||
return $utf8mb4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the auto-incremented value from the last INSERT statement.
|
||||
*
|
||||
* @return integer The value of the auto-increment field from the last inserted row.
|
||||
*/
|
||||
public function insertid()
|
||||
{
|
||||
// Error suppress this to prevent PDO warning us that the driver doesn't support this operation.
|
||||
return @$this->connection->lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the next row in the result set from the database query as an object.
|
||||
*
|
||||
* @param string $class The class name to use for the returned row object.
|
||||
*
|
||||
* @return mixed The result of the query as an array, false if there are no more rows.
|
||||
*/
|
||||
public function loadNextObject($class = 'stdClass')
|
||||
{
|
||||
// Execute the query and get the result set cursor.
|
||||
if (!$this->cursor)
|
||||
{
|
||||
if (!($this->execute()))
|
||||
{
|
||||
return $this->errorNum ? null : false;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the next row from the result set as an object of type $class.
|
||||
if ($row = $this->fetchObject(null, $class))
|
||||
{
|
||||
return $row;
|
||||
}
|
||||
|
||||
// Free up system resources and return.
|
||||
$this->freeResult();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the next row in the result set from the database query as an array.
|
||||
*
|
||||
* @return mixed The result of the query as an array, false if there are no more rows.
|
||||
*/
|
||||
public function loadNextRow()
|
||||
{
|
||||
// Execute the query and get the result set cursor.
|
||||
if (!$this->cursor)
|
||||
{
|
||||
if (!($this->execute()))
|
||||
{
|
||||
return $this->errorNum ? null : false;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the next row from the result set as an object of type $class.
|
||||
if ($row = $this->fetchArray())
|
||||
{
|
||||
return $row;
|
||||
}
|
||||
|
||||
// Free up system resources and return.
|
||||
$this->freeResult();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function open()
|
||||
{
|
||||
if ($this->connected())
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->close();
|
||||
}
|
||||
|
||||
if (!isset($this->charset))
|
||||
{
|
||||
$this->charset = 'utf8mb4';
|
||||
}
|
||||
|
||||
$this->port = $this->port ?: 3306;
|
||||
|
||||
$format = 'mysql:host=#HOST#;port=#PORT#;dbname=#DBNAME#;charset=#CHARSET#';
|
||||
|
||||
if ($this->socket)
|
||||
{
|
||||
$format = 'mysql:socket=#SOCKET#;dbname=#DBNAME#;charset=#CHARSET#';
|
||||
}
|
||||
|
||||
$replace = ['#HOST#', '#PORT#', '#SOCKET#', '#DBNAME#', '#CHARSET#'];
|
||||
$with = [$this->host, $this->port, $this->socket, $this->_database, $this->charset];
|
||||
|
||||
// Create the connection string:
|
||||
$connectionString = str_replace($replace, $with, $format);
|
||||
|
||||
// For SSL/TLS connection encryption.
|
||||
if ($this->ssl !== [] && $this->ssl['enable'] === true)
|
||||
{
|
||||
$sslContextIsNull = true;
|
||||
|
||||
// If customised, add cipher suite, ca file path, ca path, private key file path and certificate file path to PDO driver options.
|
||||
foreach (['cipher', 'ca', 'capath', 'key', 'cert'] as $key => $value)
|
||||
{
|
||||
if ($this->ssl[$value] !== null)
|
||||
{
|
||||
$this->driverOptions[constant('\PDO::MYSQL_ATTR_SSL_' . strtoupper($value))] = $this->ssl[$value];
|
||||
|
||||
$sslContextIsNull = false;
|
||||
}
|
||||
}
|
||||
|
||||
// PDO, if no cipher, ca, capath, cert and key are set, can't start TLS one-way connection, set a common ciphers suite to force it.
|
||||
if ($sslContextIsNull === true)
|
||||
{
|
||||
$this->driverOptions[\PDO::MYSQL_ATTR_SSL_CIPHER] = implode(':', static::$defaultCipherSuite);
|
||||
}
|
||||
|
||||
// If customised, for capable systems (PHP 7.0.14+ and 7.1.4+) verify certificate chain and Common Name to driver options.
|
||||
if ($this->ssl['verify_server_cert'] !== null && defined('\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT'))
|
||||
{
|
||||
$this->driverOptions[\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = $this->ssl['verify_server_cert'];
|
||||
}
|
||||
}
|
||||
|
||||
// connect to the server
|
||||
try
|
||||
{
|
||||
$this->connection = new PDO(
|
||||
$connectionString,
|
||||
$this->user,
|
||||
$this->password,
|
||||
$this->driverOptions
|
||||
);
|
||||
}
|
||||
catch (PDOException $e)
|
||||
{
|
||||
// If we tried connecting through utf8mb4 and we failed let's retry with regular utf8
|
||||
if ($this->charset == 'utf8mb4')
|
||||
{
|
||||
$this->charset = 'UTF8';
|
||||
$this->open();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->errorNum = 2;
|
||||
$this->errorMsg = 'Could not connect to MySQL via PDO: ' . $e->getMessage();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset the SQL mode of the connection
|
||||
try
|
||||
{
|
||||
$this->connection->exec("SET @@SESSION.sql_mode = '';");
|
||||
}
|
||||
// Ignore any exceptions (incompatible MySQL versions)
|
||||
catch (Exception $e)
|
||||
{
|
||||
}
|
||||
|
||||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
|
||||
|
||||
if ($this->selectDatabase && !empty($this->_database))
|
||||
{
|
||||
$this->select($this->_database);
|
||||
}
|
||||
|
||||
$this->freeResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the SQL statement.
|
||||
*
|
||||
* @return mixed A database cursor resource on success, boolean false on failure.
|
||||
*/
|
||||
public function query()
|
||||
{
|
||||
if (!is_object($this->connection))
|
||||
{
|
||||
$this->open();
|
||||
}
|
||||
|
||||
$this->freeResult();
|
||||
|
||||
// Take a local copy so that we don't modify the original query and cause issues later
|
||||
$query = $this->replacePrefix((string) $this->sql);
|
||||
|
||||
if ($this->limit > 0 || $this->offset > 0)
|
||||
{
|
||||
$query .= ' LIMIT ' . $this->offset . ', ' . $this->limit;
|
||||
}
|
||||
|
||||
// Increment the query counter.
|
||||
$this->count++;
|
||||
|
||||
// If debugging is enabled then let's log the query.
|
||||
if ($this->debug)
|
||||
{
|
||||
// Add the query to the object queue.
|
||||
$this->log[] = $query;
|
||||
}
|
||||
|
||||
// Reset the error values.
|
||||
$this->errorNum = 0;
|
||||
$this->errorMsg = '';
|
||||
|
||||
// Execute the query. Error suppression is used here to prevent warnings/notices that the connection has been lost.
|
||||
try
|
||||
{
|
||||
$this->cursor = $this->connection->query($query);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
}
|
||||
|
||||
// If an error occurred handle it.
|
||||
if (!$this->cursor)
|
||||
{
|
||||
$errorInfo = $this->connection->errorInfo();
|
||||
$this->errorNum = $errorInfo[1];
|
||||
$this->errorMsg = $errorInfo[2] . ' SQL=' . $query;
|
||||
|
||||
// Check if the server was disconnected.
|
||||
if (!$this->connected() && !$this->isReconnecting)
|
||||
{
|
||||
$this->isReconnecting = true;
|
||||
|
||||
try
|
||||
{
|
||||
// Attempt to reconnect.
|
||||
$this->connection = null;
|
||||
$this->open();
|
||||
}
|
||||
// If connect fails, ignore that exception and throw the normal exception.
|
||||
catch (RuntimeException $e)
|
||||
{
|
||||
throw new RuntimeException($this->errorMsg, $this->errorNum);
|
||||
}
|
||||
|
||||
// Since we were able to reconnect, run the query again.
|
||||
$result = $this->query();
|
||||
$this->isReconnecting = false;
|
||||
|
||||
return $result;
|
||||
}
|
||||
// The server was not disconnected.
|
||||
else
|
||||
{
|
||||
throw new RuntimeException($this->errorMsg, $this->errorNum);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->cursor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a database for use.
|
||||
*
|
||||
* @param string $database The name of the database to select for use.
|
||||
*
|
||||
* @return boolean True if the database was successfully selected.
|
||||
*/
|
||||
public function select($database)
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->connection->exec('USE ' . $this->quoteName($database));
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$errorInfo = $this->connection->errorInfo();
|
||||
$this->errorNum = $errorInfo[1];
|
||||
$this->errorMsg = $errorInfo[2];
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the connection to use UTF-8 character encoding.
|
||||
*
|
||||
* @return boolean True on success.
|
||||
*/
|
||||
public function setUTF()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to commit a transaction.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function transactionCommit()
|
||||
{
|
||||
$this->connection->commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to roll back a transaction.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function transactionRollback()
|
||||
{
|
||||
$this->connection->rollBack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to initialize a transaction.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function transactionStart()
|
||||
{
|
||||
$this->connection->beginTransaction();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an array.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchArray($cursor = null)
|
||||
{
|
||||
$ret = null;
|
||||
|
||||
if (!empty($cursor) && $cursor instanceof PDOStatement)
|
||||
{
|
||||
$ret = $cursor->fetch(PDO::FETCH_NUM);
|
||||
}
|
||||
elseif ($this->cursor instanceof PDOStatement)
|
||||
{
|
||||
$ret = $this->cursor->fetch(PDO::FETCH_NUM);
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an object.
|
||||
*
|
||||
* @param mixed $cursor The optional result set cursor from which to fetch the row.
|
||||
* @param string $class The class name to use for the returned row object.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchObject($cursor = null, $class = 'stdClass')
|
||||
{
|
||||
$ret = null;
|
||||
|
||||
if (!empty($cursor) && $cursor instanceof PDOStatement)
|
||||
{
|
||||
$ret = $cursor->fetchObject($class);
|
||||
}
|
||||
elseif ($this->cursor instanceof PDOStatement)
|
||||
{
|
||||
$ret = $this->cursor->fetchObject($class);
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver\Query;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
/**
|
||||
* Query Element Class.
|
||||
*
|
||||
* Based on Joomla! Platform 11.3
|
||||
*/
|
||||
class Element
|
||||
{
|
||||
/**
|
||||
* @var string The name of the element.
|
||||
*/
|
||||
protected $name = null;
|
||||
|
||||
/**
|
||||
* @var array An array of elements.
|
||||
*/
|
||||
protected $elements = null;
|
||||
|
||||
/**
|
||||
* @var string Glue piece.
|
||||
*/
|
||||
protected $glue = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $name The name of the element.
|
||||
* @param mixed $elements String or array.
|
||||
* @param string $glue The glue for elements.
|
||||
*/
|
||||
public function __construct($name, $elements, $glue = ',')
|
||||
{
|
||||
$this->elements = [];
|
||||
$this->name = $name;
|
||||
$this->glue = $glue;
|
||||
|
||||
$this->append($elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function to convert the query element to a string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
if (substr($this->name, -2) == '()')
|
||||
{
|
||||
return PHP_EOL . substr($this->name, 0, -2) . '(' . implode($this->glue, $this->elements) . ')';
|
||||
}
|
||||
else
|
||||
{
|
||||
return PHP_EOL . $this->name . ' ' . implode($this->glue, $this->elements);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends element parts to the internal list.
|
||||
*
|
||||
* @param mixed $elements String or array.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function append($elements)
|
||||
{
|
||||
if (is_array($elements))
|
||||
{
|
||||
$this->elements = array_merge($this->elements, $elements);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->elements = array_merge($this->elements, [$elements]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the elements of this element.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getElements()
|
||||
{
|
||||
return $this->elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to provide deep copy support to nested objects and arrays
|
||||
* when cloning.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
foreach ($this as $k => $v)
|
||||
{
|
||||
if (is_object($v) || is_array($v))
|
||||
{
|
||||
$this->{$k} = unserialize(serialize($v));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver\Query;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
use Akeeba\Engine\Driver\Query\Base as BaseQuery;
|
||||
|
||||
/**
|
||||
* Query Limitable Interface.
|
||||
* Adds bind/unbind methods as well as a getBounded() method
|
||||
* to retrieve the stored bounded variables on demand prior to
|
||||
* query execution.
|
||||
*
|
||||
* Based on Joomla! Platform 11.2
|
||||
*/
|
||||
interface Limitable
|
||||
{
|
||||
/**
|
||||
* Method to modify a query already in string format with the needed
|
||||
* additions to make the query limited to a particular number of
|
||||
* results, or start at a particular offset. This method is used
|
||||
* automatically by the __toString() method if it detects that the
|
||||
* query implements the Limitable interface.
|
||||
*
|
||||
* @param string $query The query in string format
|
||||
* @param integer $limit The limit for the result set
|
||||
* @param integer $offset The offset for the result set
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function processLimit($query, $limit, $offset = 0);
|
||||
|
||||
/**
|
||||
* Sets the offset and limit for the result set, if the database driver supports it.
|
||||
*
|
||||
* Usage:
|
||||
* $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
|
||||
* $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
|
||||
*
|
||||
* @param integer $limit The limit for the result set
|
||||
* @param integer $offset The offset for the result set
|
||||
*
|
||||
* @return BaseQuery Returns this object to allow chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function setLimit($limit = 0, $offset = 0);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver\Query;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
/**
|
||||
* Query Building Class.
|
||||
*
|
||||
* Based on Joomla! Platform 11.3
|
||||
*/
|
||||
class Mysql extends Mysqli
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver\Query;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
use Akeeba\Engine\Driver\Query\Base as BaseQuery;
|
||||
|
||||
/**
|
||||
* Query Building Class.
|
||||
*
|
||||
* Based on Joomla! Platform 11.3
|
||||
*/
|
||||
class Mysqli extends Base implements Limitable
|
||||
{
|
||||
/**
|
||||
* @var integer The offset for the result set.
|
||||
*/
|
||||
protected $offset;
|
||||
|
||||
/**
|
||||
* @var integer The limit for the result set.
|
||||
*/
|
||||
protected $limit;
|
||||
|
||||
/**
|
||||
* Method to modify a query already in string format with the needed
|
||||
* additions to make the query limited to a particular number of
|
||||
* results, or start at a particular offset.
|
||||
*
|
||||
* @param string $query The query in string format
|
||||
* @param integer $limit The limit for the result set
|
||||
* @param integer $offset The offset for the result set
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function processLimit($query, $limit, $offset = 0)
|
||||
{
|
||||
if ($limit > 0 || $offset > 0)
|
||||
{
|
||||
$query .= ' LIMIT ' . $offset . ', ' . $limit;
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the offset and limit for the result set, if the database driver supports it.
|
||||
*
|
||||
* Usage:
|
||||
* $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
|
||||
* $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
|
||||
*
|
||||
* @param integer $limit The limit for the result set
|
||||
* @param integer $offset The offset for the result set
|
||||
*
|
||||
* @return BaseQuery Returns this object to allow chaining.
|
||||
*/
|
||||
public function setLimit($limit = 0, $offset = 0)
|
||||
{
|
||||
$this->limit = (int) $limit;
|
||||
$this->offset = (int) $offset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenates an array of column names or values.
|
||||
*
|
||||
* @param array $values An array of values to concatenate.
|
||||
* @param string $separator As separator to place between each value.
|
||||
*
|
||||
* @return string The concatenated values.
|
||||
*/
|
||||
public function concatenate($values, $separator = null)
|
||||
{
|
||||
if ($separator)
|
||||
{
|
||||
$concat_string = 'CONCAT_WS(' . $this->quote($separator);
|
||||
|
||||
foreach ($values as $value)
|
||||
{
|
||||
$concat_string .= ', ' . $value;
|
||||
}
|
||||
|
||||
return $concat_string . ')';
|
||||
}
|
||||
else
|
||||
{
|
||||
return 'CONCAT(' . implode(',', $values) . ')';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver\Query;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
/**
|
||||
* Query Building Class.
|
||||
*
|
||||
* Based on Joomla! Platform 11.3
|
||||
*/
|
||||
class Pdomysql extends Mysqli implements Limitable
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver\Query;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
* Database Query Preparable Interface.
|
||||
*
|
||||
* Adds bind/unbind methods as well as a getBounded() method
|
||||
* to retrieve the stored bounded variables on demand prior to
|
||||
* query execution.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
interface Preparable
|
||||
{
|
||||
/**
|
||||
* Method to add a variable to an internal array that will be bound to a prepared SQL statement before query execution. Also
|
||||
* removes a variable that has been bounded from the internal bounded array when the passed in value is null.
|
||||
*
|
||||
* @param string|integer $key The key that will be used in your SQL query to reference the value. Usually of
|
||||
* the form ':key', but can also be an integer.
|
||||
* @param mixed &$value The value that will be bound. The value is passed by reference to support output
|
||||
* parameters such as those possible with stored procedures.
|
||||
* @param integer $dataType Constant corresponding to a SQL datatype.
|
||||
* @param integer $length The length of the variable. Usually required for OUTPUT parameters.
|
||||
* @param array $driverOptions Optional driver options to be used.
|
||||
*
|
||||
* @return Preparable
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function bind($key = null, &$value = null, $dataType = PDO::PARAM_STR, $length = 0, $driverOptions = []);
|
||||
|
||||
/**
|
||||
* Retrieves the bound parameters array when key is null and returns it by reference. If a key is provided then that item is
|
||||
* returned.
|
||||
*
|
||||
* @param mixed $key The bounded variable key to retrieve.
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function &getBounded($key = null);
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver\Query;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
use PDO;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* SQLite Query Building Class.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class Sqlite extends Base implements Preparable, Limitable
|
||||
{
|
||||
/**
|
||||
* The limit for the result set.
|
||||
*
|
||||
* @var integer
|
||||
* @since 1.0
|
||||
*/
|
||||
protected $limit;
|
||||
|
||||
/**
|
||||
* The offset for the result set.
|
||||
*
|
||||
* @var integer
|
||||
* @since 1.0
|
||||
*/
|
||||
protected $offset;
|
||||
|
||||
/**
|
||||
* Holds key / value pair of bound objects.
|
||||
*
|
||||
* @var mixed
|
||||
* @since 1.0
|
||||
*/
|
||||
protected $bounded = [];
|
||||
|
||||
/**
|
||||
* Method to add a variable to an internal array that will be bound to a prepared SQL statement before query execution. Also
|
||||
* removes a variable that has been bounded from the internal bounded array when the passed in value is null.
|
||||
*
|
||||
* @param string|integer $key The key that will be used in your SQL query to reference the value. Usually of
|
||||
* the form ':key', but can also be an integer.
|
||||
* @param mixed &$value The value that will be bound. The value is passed by reference to support output
|
||||
* parameters such as those possible with stored procedures.
|
||||
* @param integer $dataType Constant corresponding to a SQL datatype.
|
||||
* @param integer $length The length of the variable. Usually required for OUTPUT parameters.
|
||||
* @param array $driverOptions Optional driver options to be used.
|
||||
*
|
||||
* @return Sqlite Returns this object to allow chaining.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function bind($key = null, &$value = null, $dataType = PDO::PARAM_STR, $length = 0, $driverOptions = [])
|
||||
{
|
||||
// Case 1: Empty Key (reset $bounded array)
|
||||
if (empty($key))
|
||||
{
|
||||
$this->bounded = [];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Case 2: Key Provided, null value (unset key from $bounded array)
|
||||
if (is_null($value))
|
||||
{
|
||||
if (isset($this->bounded[$key]))
|
||||
{
|
||||
unset($this->bounded[$key]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$obj = new stdClass;
|
||||
|
||||
$obj->value = &$value;
|
||||
$obj->dataType = $dataType;
|
||||
$obj->length = $length;
|
||||
$obj->driverOptions = $driverOptions;
|
||||
|
||||
// Case 3: Simply add the Key/Value into the bounded array
|
||||
$this->bounded[$key] = $obj;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the bound parameters array when key is null and returns it by reference. If a key is provided then that item is
|
||||
* returned.
|
||||
*
|
||||
* @param mixed $key The bounded variable key to retrieve.
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function &getBounded($key = null)
|
||||
{
|
||||
if (empty($key))
|
||||
{
|
||||
return $this->bounded;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($this->bounded[$key]))
|
||||
{
|
||||
return $this->bounded[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of characters in a string.
|
||||
*
|
||||
* Note, use 'length' to find the number of bytes in a string.
|
||||
*
|
||||
* Usage:
|
||||
* $query->select($query->charLength('a'));
|
||||
*
|
||||
* @param string $field A value.
|
||||
* @param string $operator Comparison operator between charLength integer value and $condition
|
||||
* @param string $condition Integer value to compare charLength with.
|
||||
*
|
||||
* @return string The required char length call.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public function charLength($field, $operator = null, $condition = null)
|
||||
{
|
||||
return 'length(' . $field . ')' . (isset($operator) && isset($condition) ? ' ' . $operator . ' ' . $condition : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear data from the query or a specific clause of the query.
|
||||
*
|
||||
* @param string $clause Optionally, the name of the clause to clear, or nothing to clear the whole query.
|
||||
*
|
||||
* @return Sqlite Returns this object to allow chaining.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function clear($clause = null)
|
||||
{
|
||||
switch ($clause)
|
||||
{
|
||||
case null:
|
||||
$this->bounded = [];
|
||||
break;
|
||||
}
|
||||
|
||||
return parent::clear($clause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenates an array of column names or values.
|
||||
*
|
||||
* Usage:
|
||||
* $query->select($query->concatenate(array('a', 'b')));
|
||||
*
|
||||
* @param array $values An array of values to concatenate.
|
||||
* @param string $separator As separator to place between each value.
|
||||
*
|
||||
* @return string The concatenated values.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public function concatenate($values, $separator = null)
|
||||
{
|
||||
if ($separator)
|
||||
{
|
||||
return implode(' || ' . $this->quote($separator) . ' || ', $values);
|
||||
}
|
||||
else
|
||||
{
|
||||
return implode(' || ', $values);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to modify a query already in string format with the needed
|
||||
* additions to make the query limited to a particular number of
|
||||
* results, or start at a particular offset. This method is used
|
||||
* automatically by the __toString() method if it detects that the
|
||||
* query implements the LimitableInterface.
|
||||
*
|
||||
* @param string $query The query in string format
|
||||
* @param integer $limit The limit for the result set
|
||||
* @param integer $offset The offset for the result set
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function processLimit($query, $limit, $offset = 0)
|
||||
{
|
||||
if ($limit > 0 || $offset > 0)
|
||||
{
|
||||
$query .= ' LIMIT ' . $offset . ', ' . $limit;
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the offset and limit for the result set, if the database driver supports it.
|
||||
*
|
||||
* Usage:
|
||||
* $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
|
||||
* $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
|
||||
*
|
||||
* @param integer $limit The limit for the result set
|
||||
* @param integer $offset The offset for the result set
|
||||
*
|
||||
* @return Sqlite Returns this object to allow chaining.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function setLimit($limit = 0, $offset = 0)
|
||||
{
|
||||
$this->limit = (int) $limit;
|
||||
$this->offset = (int) $offset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
use Exception;
|
||||
|
||||
class QueryException extends Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,909 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba Engine
|
||||
*
|
||||
* @package akeebaengine
|
||||
* @copyright Copyright (c)2006-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 3, or later
|
||||
*/
|
||||
|
||||
namespace Akeeba\Engine\Driver;
|
||||
|
||||
defined('AKEEBAENGINE') || die();
|
||||
|
||||
use Akeeba\Engine\Driver\Query\Base as QueryBase;
|
||||
use Akeeba\Engine\Driver\Query\Limitable;
|
||||
use Akeeba\Engine\Driver\Query\Preparable;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use PDOStatement;
|
||||
use RuntimeException;
|
||||
use SQLite3;
|
||||
|
||||
/**
|
||||
* SQLite database driver supporting PDO based connections
|
||||
*
|
||||
* @see http://php.net/manual/en/ref.pdo-sqlite.php
|
||||
* @since 1.0
|
||||
*/
|
||||
class Sqlite extends Base
|
||||
{
|
||||
|
||||
public static $dbtech = 'sqlite';
|
||||
|
||||
/**
|
||||
* The name of the database driver.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0
|
||||
*/
|
||||
public $name = 'sqlite';
|
||||
|
||||
/** @var PDOStatement The database connection cursor from the last query. */
|
||||
protected $cursor;
|
||||
|
||||
/** @var array Contains the current query execution status */
|
||||
protected $executed = false;
|
||||
|
||||
/**
|
||||
* The character(s) used to quote SQL statement names such as table names or field names,
|
||||
* etc. The child classes should define this as necessary. If a single character string the
|
||||
* same character is used for both sides of the quoted name, else the first character will be
|
||||
* used for the opening quote and the second for the closing quote.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0
|
||||
*/
|
||||
protected $nameQuote = '`';
|
||||
|
||||
/** @var resource The prepared statement. */
|
||||
protected $prepared;
|
||||
|
||||
/** @var bool Are we in the process of reconnecting to the database server? */
|
||||
private $isReconnecting = false;
|
||||
|
||||
public function __construct(array $options)
|
||||
{
|
||||
$this->driverType = 'sqlite';
|
||||
|
||||
parent::__construct($options);
|
||||
|
||||
if (!is_object($this->connection))
|
||||
{
|
||||
$this->open();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to see if the PDO ODBC connector is available.
|
||||
*
|
||||
* @return boolean True on success, false otherwise.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
return class_exists('\\PDO') && in_array('sqlite', PDO::getAvailableDrivers());
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->freeResult();
|
||||
unset($this->connection);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
$return = false;
|
||||
|
||||
if (is_object($this->cursor))
|
||||
{
|
||||
$this->cursor->closeCursor();
|
||||
}
|
||||
|
||||
$this->connection = null;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the connection to the server is active.
|
||||
*
|
||||
* @return boolean True if connected to the database engine.
|
||||
*/
|
||||
public function connected()
|
||||
{
|
||||
return !empty($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects the database.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function disconnect()
|
||||
{
|
||||
$this->freeResult();
|
||||
unset($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a table from the database.
|
||||
*
|
||||
* @param string $tableName The name of the database table to drop.
|
||||
* @param boolean $ifExists Optionally specify that the table must exist before it is dropped.
|
||||
*
|
||||
* @return Sqlite Returns this object to support chaining.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function dropTable($tableName, $ifExists = true)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
$query = $this->getQuery(true);
|
||||
|
||||
$this->setQuery('DROP TABLE ' . ($ifExists ? 'IF EXISTS ' : '') . $query->quoteName($tableName));
|
||||
|
||||
$this->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to escape a string for usage in an SQLite statement.
|
||||
*
|
||||
* Note: Using query objects with bound variables is preferable to the below.
|
||||
*
|
||||
* @param string $text The string to be escaped.
|
||||
* @param boolean $extra Unused optional parameter to provide extra escaping.
|
||||
*
|
||||
* @return string The escaped string.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function escape($text, $extra = false)
|
||||
{
|
||||
if (is_int($text) || is_float($text))
|
||||
{
|
||||
return $text;
|
||||
}
|
||||
|
||||
if (is_null($text))
|
||||
{
|
||||
return 'NULL';
|
||||
}
|
||||
|
||||
return SQLite3::escapeString($text);
|
||||
}
|
||||
|
||||
public function fetchAssoc($cursor = null)
|
||||
{
|
||||
if (!empty($cursor) && $cursor instanceof PDOStatement)
|
||||
{
|
||||
return $cursor->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
if ($this->prepared instanceof PDOStatement)
|
||||
{
|
||||
return $this->prepared->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
}
|
||||
|
||||
public function freeResult($cursor = null)
|
||||
{
|
||||
$this->executed = false;
|
||||
|
||||
if ($cursor instanceof PDOStatement)
|
||||
{
|
||||
$cursor->closeCursor();
|
||||
$cursor = null;
|
||||
}
|
||||
|
||||
if ($this->prepared instanceof PDOStatement)
|
||||
{
|
||||
$this->prepared->closeCursor();
|
||||
$this->prepared = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function getAffectedRows()
|
||||
{
|
||||
$this->open();
|
||||
|
||||
if ($this->prepared instanceof PDOStatement)
|
||||
{
|
||||
return $this->prepared->rowCount();
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the database collation in use by sampling a text field of a table in the database.
|
||||
*
|
||||
* @return mixed The collation in use by the database or boolean false if not supported.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function getCollation()
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
public function getNumRows($cursor = null)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
if ($cursor instanceof PDOStatement)
|
||||
{
|
||||
return $cursor->rowCount();
|
||||
}
|
||||
elseif ($this->prepared instanceof PDOStatement)
|
||||
{
|
||||
return $this->prepared->rowCount();
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a PDO database connection attribute
|
||||
* http://www.php.net/manual/en/pdo.getattribute.php
|
||||
*
|
||||
* Usage: $db->getOption(PDO::ATTR_CASE);
|
||||
*
|
||||
* @param mixed $key One of the PDO::ATTR_* Constants
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function getOption($key)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
return $this->connection->getAttribute($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current query object or a new Query object.
|
||||
* We have to override the parent method since it will always return a PDO query, while we have a
|
||||
* specialized class for SQLite
|
||||
*
|
||||
* @param boolean $new False to return the current query object, True to return a new Query object.
|
||||
*
|
||||
* @return QueryBase The current query object or a new object extending the Query class.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getQuery($new = false)
|
||||
{
|
||||
if ($new)
|
||||
{
|
||||
return new Query\Sqlite($this);
|
||||
}
|
||||
|
||||
return $this->sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves field information about a given table.
|
||||
*
|
||||
* @param string $table The name of the database table.
|
||||
* @param boolean $typeOnly True to only return field types.
|
||||
*
|
||||
* @return array An array of fields for the database table.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function getTableColumns($table, $typeOnly = true)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
$columns = [];
|
||||
$query = $this->getQuery(true);
|
||||
|
||||
$fieldCasing = $this->getOption(PDO::ATTR_CASE);
|
||||
|
||||
$this->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
|
||||
|
||||
$table = strtoupper($table);
|
||||
|
||||
$query->setQuery('pragma table_info(' . $table . ')');
|
||||
|
||||
$this->setQuery($query);
|
||||
$fields = $this->loadObjectList();
|
||||
|
||||
if ($typeOnly)
|
||||
{
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
$columns[$field->NAME] = $field->TYPE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
// Do some dirty translation to MySQL output.
|
||||
$columns[$field->NAME] = (object) [
|
||||
'Field' => $field->NAME,
|
||||
'Type' => $field->TYPE,
|
||||
'Null' => ($field->NOTNULL == '1' ? 'NO' : 'YES'),
|
||||
'Default' => $field->DFLT_VALUE,
|
||||
'Key' => ($field->PK == '1' ? 'PRI' : ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$this->setOption(PDO::ATTR_CASE, $fieldCasing);
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the table CREATE statement that creates the given tables.
|
||||
*
|
||||
* Note: Doesn't appear to have support in SQLite
|
||||
*
|
||||
* @param mixed $tables A table name or a list of table names.
|
||||
*
|
||||
* @return array A list of the create SQL for the tables.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function getTableCreate($tables)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
// Sanitize input to an array and iterate over the list.
|
||||
$tables = (array) $tables;
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the details list of keys for a table.
|
||||
*
|
||||
* @param string $table The name of the table.
|
||||
*
|
||||
* @return array An array of the column specification for the table.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function getTableKeys($table)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
$keys = [];
|
||||
$query = $this->getQuery(true);
|
||||
|
||||
$fieldCasing = $this->getOption(PDO::ATTR_CASE);
|
||||
|
||||
$this->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
|
||||
|
||||
$table = strtoupper($table);
|
||||
$query->setQuery('pragma table_info( ' . $table . ')');
|
||||
|
||||
// $query->bind(':tableName', $table);
|
||||
|
||||
$this->setQuery($query);
|
||||
$rows = $this->loadObjectList();
|
||||
|
||||
foreach ($rows as $column)
|
||||
{
|
||||
if ($column->PK == 1)
|
||||
{
|
||||
$keys[$column->NAME] = $column;
|
||||
}
|
||||
}
|
||||
|
||||
$this->setOption(PDO::ATTR_CASE, $fieldCasing);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get an array of all tables in the database (schema).
|
||||
*
|
||||
* @return array An array of all the tables in the database.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function getTableList()
|
||||
{
|
||||
$this->open();
|
||||
|
||||
/* @type Query\Sqlite $query */
|
||||
$query = $this->getQuery(true);
|
||||
|
||||
$type = 'table';
|
||||
|
||||
$query->select('name');
|
||||
$query->from('sqlite_master');
|
||||
$query->where('type = :type');
|
||||
$query->bind(':type', $type);
|
||||
$query->order('name');
|
||||
|
||||
$this->setQuery($query);
|
||||
|
||||
$tables = $this->loadColumn();
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* There's no point on return "a list of tables" inside a SQLite database: we are simple going to
|
||||
* copy the whole database file in the new location
|
||||
*
|
||||
* @param bool $abstract
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($abstract = true)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of the database connector.
|
||||
*
|
||||
* @return string The database connector version.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
$this->open();
|
||||
|
||||
$this->setQuery("SELECT sqlite_version()");
|
||||
|
||||
return $this->loadResult();
|
||||
}
|
||||
|
||||
public function insertid()
|
||||
{
|
||||
$this->open();
|
||||
|
||||
// Error suppress this to prevent PDO warning us that the driver doesn't support this operation.
|
||||
return @$this->connection->lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks a table in the database.
|
||||
*
|
||||
* @param string $table The name of the table to unlock.
|
||||
*
|
||||
* @return Sqlite Returns this object to support chaining.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function lockTable($table)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function open()
|
||||
{
|
||||
if ($this->connected())
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->close();
|
||||
}
|
||||
|
||||
if (isset($this->options['version']) && $this->options['version'] == 2)
|
||||
{
|
||||
$format = 'sqlite2:#DBNAME#';
|
||||
}
|
||||
else
|
||||
{
|
||||
$format = 'sqlite:#DBNAME#';
|
||||
}
|
||||
|
||||
$replace = ['#DBNAME#'];
|
||||
$with = [$this->options['database']];
|
||||
|
||||
// Create the connection string:
|
||||
$connectionString = str_replace($replace, $with, $format);
|
||||
|
||||
try
|
||||
{
|
||||
$this->connection = new PDO(
|
||||
$connectionString,
|
||||
$this->options['user'],
|
||||
$this->options['password']
|
||||
);
|
||||
}
|
||||
catch (PDOException $e)
|
||||
{
|
||||
throw new RuntimeException('Could not connect to PDO' . ': ' . $e->getMessage(), 2, $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function query()
|
||||
{
|
||||
$this->open();
|
||||
|
||||
if (!is_object($this->connection))
|
||||
{
|
||||
throw new RuntimeException($this->errorMsg, $this->errorNum);
|
||||
}
|
||||
|
||||
// Take a local copy so that we don't modify the original query and cause issues later
|
||||
$sql = $this->replacePrefix((string) $this->sql);
|
||||
|
||||
if ($this->limit > 0 || $this->offset > 0)
|
||||
{
|
||||
$sql .= ' LIMIT ' . $this->limit;
|
||||
|
||||
if ($this->offset > 0)
|
||||
{
|
||||
$sql .= ' OFFSET ' . $this->offset;
|
||||
}
|
||||
}
|
||||
|
||||
// Increment the query counter.
|
||||
$this->count++;
|
||||
|
||||
// If debugging is enabled then let's log the query.
|
||||
if ($this->debug)
|
||||
{
|
||||
// Add the query to the object queue.
|
||||
$this->log[] = $sql;
|
||||
}
|
||||
|
||||
// Reset the error values.
|
||||
$this->errorNum = 0;
|
||||
$this->errorMsg = '';
|
||||
|
||||
// Execute the query.
|
||||
$this->executed = false;
|
||||
|
||||
if ($this->prepared instanceof PDOStatement)
|
||||
{
|
||||
// Bind the variables:
|
||||
if ($this->sql instanceof Preparable)
|
||||
{
|
||||
$bounded =& $this->sql->getBounded();
|
||||
|
||||
foreach ($bounded as $key => $obj)
|
||||
{
|
||||
$this->prepared->bindParam($key, $obj->value, $obj->dataType, $obj->length, $obj->driverOptions);
|
||||
}
|
||||
}
|
||||
|
||||
$this->executed = $this->prepared->execute();
|
||||
}
|
||||
|
||||
// If an error occurred handle it.
|
||||
if (!$this->executed)
|
||||
{
|
||||
// Get the error number and message before we execute any more queries.
|
||||
$errorNum = (int) $this->connection->errorCode();
|
||||
$errorMsg = (string) 'SQL: ' . implode(", ", $this->connection->errorInfo());
|
||||
|
||||
// Check if the server was disconnected.
|
||||
if (!$this->connected() && !$this->isReconnecting)
|
||||
{
|
||||
$this->isReconnecting = true;
|
||||
|
||||
try
|
||||
{
|
||||
// Attempt to reconnect.
|
||||
$this->connection = null;
|
||||
$this->open();
|
||||
}
|
||||
catch (RuntimeException $e)
|
||||
// If connect fails, ignore that exception and throw the normal exception.
|
||||
{
|
||||
// Get the error number and message.
|
||||
$this->errorNum = (int) $this->connection->errorCode();
|
||||
$this->errorMsg = (string) 'SQL: ' . implode(", ", $this->connection->errorInfo());
|
||||
|
||||
// Throw the normal query exception.
|
||||
throw new RuntimeException($this->errorMsg, $this->errorNum);
|
||||
}
|
||||
|
||||
// Since we were able to reconnect, run the query again.
|
||||
$result = $this->query();
|
||||
$this->isReconnecting = false;
|
||||
|
||||
return $result;
|
||||
}
|
||||
else
|
||||
// The server was not disconnected.
|
||||
{
|
||||
// Get the error number and message from before we tried to reconnect.
|
||||
$this->errorNum = $errorNum;
|
||||
$this->errorMsg = $errorMsg;
|
||||
|
||||
// Throw the normal query exception.
|
||||
throw new RuntimeException($this->errorMsg, $this->errorNum);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->prepared;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renames a table in the database.
|
||||
*
|
||||
* @param string $oldTable The name of the table to be renamed
|
||||
* @param string $newTable The new name for the table.
|
||||
* @param string $backup Not used by Sqlite.
|
||||
* @param string $prefix Not used by Sqlite.
|
||||
*
|
||||
* @return Sqlite Returns this object to support chaining.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
|
||||
{
|
||||
$this->setQuery('ALTER TABLE ' . $oldTable . ' RENAME TO ' . $newTable)->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a database for use.
|
||||
*
|
||||
* @param string $database The name of the database to select for use.
|
||||
*
|
||||
* @return boolean True if the database was successfully selected.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function select($database)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
$this->_database = $database;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an attribute on the PDO database handle.
|
||||
* http://www.php.net/manual/en/pdo.setattribute.php
|
||||
*
|
||||
* Usage: $db->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
|
||||
*
|
||||
* @param integer $key One of the PDO::ATTR_* Constants
|
||||
* @param mixed $value One of the associated PDO Constants
|
||||
* related to the particular attribute
|
||||
* key.
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function setOption($key, $value)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
return $this->connection->setAttribute($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the SQL statement string for later execution.
|
||||
*
|
||||
* @param mixed $query The SQL statement to set either as a JDatabaseQuery object or a string.
|
||||
* @param integer $offset The affected row offset to set.
|
||||
* @param integer $limit The maximum affected rows to set.
|
||||
* @param array $driverOptions The optional PDO driver options
|
||||
*
|
||||
* @return Base This object to support method chaining.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function setQuery($query, $offset = null, $limit = null, $driverOptions = [])
|
||||
{
|
||||
$this->open();
|
||||
|
||||
$this->freeResult();
|
||||
|
||||
if (is_string($query))
|
||||
{
|
||||
// Allows taking advantage of bound variables in a direct query:
|
||||
$query = $this->getQuery(true)->setQuery($query);
|
||||
}
|
||||
|
||||
if ($query instanceof Limitable && !is_null($offset) && !is_null($limit))
|
||||
{
|
||||
$query->setLimit($limit, $offset);
|
||||
}
|
||||
|
||||
$sql = $this->replacePrefix((string) $query);
|
||||
|
||||
$this->prepared = $this->connection->prepare($sql, $driverOptions);
|
||||
|
||||
// Store reference to the DatabaseQuery instance:
|
||||
parent::setQuery($query, $offset, $limit);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the connection to use UTF-8 character encoding.
|
||||
*
|
||||
* Returns false automatically for the Oracle driver since
|
||||
* you can only set the character set when the connection
|
||||
* is created.
|
||||
*
|
||||
* @return boolean True on success.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function setUTF()
|
||||
{
|
||||
$this->open();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to commit a transaction.
|
||||
*
|
||||
* @param boolean $toSavepoint If true, commit to the last savepoint.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function transactionCommit($toSavepoint = false)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
if (!$toSavepoint || $this->transactionDepth <= 1)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
if (!$toSavepoint || $this->transactionDepth == 1)
|
||||
{
|
||||
$this->connection->commit();
|
||||
}
|
||||
|
||||
$this->transactionDepth--;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->transactionDepth--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to roll back a transaction.
|
||||
*
|
||||
* @param boolean $toSavepoint If true, rollback to the last savepoint.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function transactionRollback($toSavepoint = false)
|
||||
{
|
||||
$this->connected();
|
||||
|
||||
if (!$toSavepoint || $this->transactionDepth <= 1)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
if (!$toSavepoint || $this->transactionDepth == 1)
|
||||
{
|
||||
$this->connection->rollBack();
|
||||
}
|
||||
|
||||
$this->transactionDepth--;
|
||||
}
|
||||
else
|
||||
{
|
||||
$savepoint = 'SP_' . ($this->transactionDepth - 1);
|
||||
$this->setQuery('ROLLBACK TO ' . $this->quoteName($savepoint));
|
||||
|
||||
if ($this->execute())
|
||||
{
|
||||
$this->transactionDepth--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to initialize a transaction.
|
||||
*
|
||||
* @param boolean $asSavepoint If true and a transaction is already active, a savepoint will be created.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function transactionStart($asSavepoint = false)
|
||||
{
|
||||
$this->connected();
|
||||
|
||||
if (!$asSavepoint || !$this->transactionDepth)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
if (!$asSavepoint || !$this->transactionDepth)
|
||||
{
|
||||
$this->connection->beginTransaction();
|
||||
}
|
||||
|
||||
$this->transactionDepth++;
|
||||
}
|
||||
else
|
||||
{
|
||||
$savepoint = 'SP_' . $this->transactionDepth;
|
||||
$this->setQuery('SAVEPOINT ' . $this->quoteName($savepoint));
|
||||
|
||||
if ($this->execute())
|
||||
{
|
||||
$this->transactionDepth++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks tables in the database.
|
||||
*
|
||||
* @return Sqlite Returns this object to support chaining.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @since 1.0
|
||||
*/
|
||||
public function unlockTables()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function fetchArray($cursor = null)
|
||||
{
|
||||
if (!empty($cursor) && $cursor instanceof PDOStatement)
|
||||
{
|
||||
return $cursor->fetch(PDO::FETCH_NUM);
|
||||
}
|
||||
|
||||
if ($this->prepared instanceof PDOStatement)
|
||||
{
|
||||
return $this->prepared->fetch(PDO::FETCH_NUM);
|
||||
}
|
||||
}
|
||||
|
||||
protected function fetchObject($cursor = null, $class = 'stdClass')
|
||||
{
|
||||
if (!empty($cursor) && $cursor instanceof PDOStatement)
|
||||
{
|
||||
return $cursor->fetchObject($class);
|
||||
}
|
||||
|
||||
if ($this->prepared instanceof PDOStatement)
|
||||
{
|
||||
return $this->prepared->fetchObject($class);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user