primo commit

This commit is contained in:
2024-12-17 17:34:10 +01:00
commit e650f8df99
16435 changed files with 2451012 additions and 0 deletions

View File

@ -0,0 +1,528 @@
<?php
/**
* Part of the Joomla Framework Database Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Database\Sqlite;
use Joomla\Database\Pdo\PdoDriver;
/**
* SQLite database driver supporting PDO based connections
*
* @link https://www.php.net/manual/en/ref.pdo-sqlite.php
* @since 1.0
*/
class SqliteDriver extends PdoDriver
{
/**
* The name of the database driver.
*
* @var string
* @since 1.0
*/
public $name = 'sqlite';
/**
* The character(s) used to quote SQL statement names such as table names or field names, etc.
*
* 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 = '`';
/**
* Destructor.
*
* @since 1.0
*/
public function __destruct()
{
$this->disconnect();
}
/**
* Alter database's character set.
*
* @param string $dbName The database name that will be altered
*
* @return boolean|resource
*
* @since 2.0.0
* @throws \RuntimeException
*/
public function alterDbCharacterSet($dbName)
{
return false;
}
/**
* Connects to the database if needed.
*
* @return void Returns void if the database connected successfully.
*
* @since 2.0.0
* @throws \RuntimeException
*/
public function connect()
{
if ($this->connection) {
return;
}
parent::connect();
$this->connection->sqliteCreateFunction(
'ROW_NUMBER',
function ($init = null) {
static $rownum, $partition;
if ($init !== null) {
$rownum = $init;
$partition = null;
return $rownum;
}
$args = \func_get_args();
array_shift($args);
$partitionBy = $args ? implode(',', $args) : null;
if ($partitionBy === null || $partitionBy === $partition) {
$rownum++;
} else {
$rownum = 1;
$partition = $partitionBy;
}
return $rownum;
}
);
}
/**
* Create a new database using information from $options object.
*
* @param \stdClass $options Object used to pass user and database name to database driver. This object must have "db_name" and "db_user" set.
* @param boolean $utf True if the database supports the UTF-8 character set.
*
* @return boolean|resource
*
* @since 2.0.0
* @throws \RuntimeException
*/
public function createDatabase($options, $utf = true)
{
// SQLite doesn't have a query for this
return true;
}
/**
* 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)) {
return $text;
}
if (\is_float($text)) {
// Force the dot as a decimal point.
return str_replace(',', '.', $text);
}
return \SQLite3::escapeString($text);
}
/**
* Method to get the database collation in use by sampling a text field of a table in the database.
*
* @return string|boolean The collation in use by the database or boolean false if not supported.
*
* @since 1.0
*/
public function getCollation()
{
return false;
}
/**
* Method to get the database connection collation in use by sampling a text field of a table in the database.
*
* @return string|boolean The collation in use by the database connection (string) or boolean false if not supported.
*
* @since 1.6.0
* @throws \RuntimeException
*/
public function getConnectionCollation()
{
return false;
}
/**
* Method to get the database encryption details (cipher and protocol) in use.
*
* @return string The database encryption details.
*
* @since 2.0.0
* @throws \RuntimeException
*/
public function getConnectionEncryption(): string
{
// TODO: Not fake this
return '';
}
/**
* Method to test if the database TLS connections encryption are supported.
*
* @return boolean Whether the database supports TLS connections encryption.
*
* @since 2.0.0
*/
public function isConnectionEncryptionSupported(): bool
{
// TODO: Not fake this
return false;
}
/**
* 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.
*
* @since 1.0
* @throws \RuntimeException
*/
public function getTableCreate($tables)
{
$this->connect();
// Sanitize input to an array and iterate over the list.
$tables = (array) $tables;
return $tables;
}
/**
* 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.
*
* @since 1.0
* @throws \RuntimeException
*/
public function getTableColumns($table, $typeOnly = true)
{
$this->connect();
$columns = [];
$fieldCasing = $this->getOption(\PDO::ATTR_CASE);
$this->setOption(\PDO::ATTR_CASE, \PDO::CASE_UPPER);
$table = strtoupper($table);
$fields = $this->setQuery('pragma table_info(' . $table . ')')->loadObjectList();
if ($typeOnly) {
foreach ($fields as $field) {
$columns[$field->NAME] = $field->TYPE;
}
} else {
foreach ($fields as $field) {
// Do some dirty translation to MySQL output.
// TODO: Come up with and implement a standard across databases.
$columns[$field->NAME] = (object) [
'Field' => $field->NAME,
'Type' => $field->TYPE,
'Null' => $field->NOTNULL == '1' ? 'NO' : 'YES',
'Default' => $field->DFLT_VALUE,
'Key' => $field->PK != '0' ? 'PRI' : '',
];
}
}
$this->setOption(\PDO::ATTR_CASE, $fieldCasing);
return $columns;
}
/**
* 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.
*
* @since 1.0
* @throws \RuntimeException
*/
public function getTableKeys($table)
{
$this->connect();
$keys = [];
$fieldCasing = $this->getOption(\PDO::ATTR_CASE);
$this->setOption(\PDO::ATTR_CASE, \PDO::CASE_UPPER);
$table = strtoupper($table);
$rows = $this->setQuery('pragma table_info( ' . $table . ')')->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.
*
* @since 1.0
* @throws \RuntimeException
*/
public function getTableList()
{
$this->connect();
$type = 'table';
$query = $this->createQuery()
->select('name')
->from('sqlite_master')
->where('type = :type')
->bind(':type', $type)
->order('name');
return $this->setQuery($query)->loadColumn();
}
/**
* Get the version of the database connector.
*
* @return string The database connector version.
*
* @since 1.0
*/
public function getVersion()
{
$this->connect();
return $this->setQuery('SELECT sqlite_version()')->loadResult();
}
/**
* 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.
*
* @since 1.0
* @throws \RuntimeException
*/
public function select($database)
{
$this->connect();
return true;
}
/**
* 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->connect();
return false;
}
/**
* Locks a table in the database.
*
* @param string $table The name of the table to unlock.
*
* @return $this
*
* @since 1.0
* @throws \RuntimeException
*/
public function lockTable($table)
{
return $this;
}
/**
* 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 $this
*
* @since 1.0
* @throws \RuntimeException
*/
public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
{
$this->setQuery('ALTER TABLE ' . $oldTable . ' RENAME TO ' . $newTable)->execute();
return $this;
}
/**
* Method to truncate a table.
*
* @param string $table The table to truncate
*
* @return void
*
* @since 1.2.1
* @throws \RuntimeException
*/
public function truncateTable($table)
{
$this->setQuery('DELETE FROM ' . $this->quoteName($table))
->execute();
}
/**
* Unlocks tables in the database.
*
* @return $this
*
* @since 1.0
* @throws \RuntimeException
*/
public function unlockTables()
{
return $this;
}
/**
* 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') && class_exists('\\SQLite3') && \in_array('sqlite', \PDO::getAvailableDrivers(), true);
}
/**
* Method to commit a transaction.
*
* @param boolean $toSavepoint If true, commit to the last savepoint.
*
* @return void
*
* @since 1.0
* @throws \RuntimeException
*/
public function transactionCommit($toSavepoint = false)
{
$this->connect();
if (!$toSavepoint || $this->transactionDepth <= 1) {
parent::transactionCommit($toSavepoint);
} else {
$this->transactionDepth--;
}
}
/**
* Method to roll back a transaction.
*
* @param boolean $toSavepoint If true, rollback to the last savepoint.
*
* @return void
*
* @since 1.0
* @throws \RuntimeException
*/
public function transactionRollback($toSavepoint = false)
{
$this->connect();
if (!$toSavepoint || $this->transactionDepth <= 1) {
parent::transactionRollback($toSavepoint);
} else {
$savepoint = 'SP_' . ($this->transactionDepth - 1);
$this->setQuery('ROLLBACK TO ' . $this->quoteName($savepoint))->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
*
* @since 1.0
* @throws \RuntimeException
*/
public function transactionStart($asSavepoint = false)
{
$this->connect();
if (!$asSavepoint || !$this->transactionDepth) {
parent::transactionStart($asSavepoint);
} else {
$savepoint = 'SP_' . $this->transactionDepth;
$this->setQuery('SAVEPOINT ' . $this->quoteName($savepoint))->execute();
$this->transactionDepth++;
}
}
}

View File

@ -0,0 +1,269 @@
<?php
/**
* Part of the Joomla Framework Database Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Database\Sqlite;
use Joomla\Database\DatabaseQuery;
use Joomla\Database\Pdo\PdoQuery;
use Joomla\Database\Query\QueryElement;
/**
* SQLite Query Building Class.
*
* @since 1.0
*/
class SqliteQuery extends PdoQuery
{
/**
* Magic function to convert the query to a string.
*
* @return string The completed query.
*
* @since 2.0.0
*/
public function __toString()
{
switch ($this->type) {
case 'select':
if ($this->selectRowNumber) {
$orderBy = $this->selectRowNumber['orderBy'];
$orderColumnAlias = $this->selectRowNumber['orderColumnAlias'];
$column = "ROW_NUMBER() AS $orderColumnAlias";
if ($this->select === null) {
$query = PHP_EOL . 'SELECT 1'
. (string) $this->from
. (string) $this->where;
} else {
$tmpOffset = $this->offset;
$tmpLimit = $this->limit;
$this->offset = 0;
$this->limit = 0;
$tmpOrder = $this->order;
$this->order = null;
$query = parent::__toString();
$column = "w.*, $column";
$this->order = $tmpOrder;
$this->offset = $tmpOffset;
$this->limit = $tmpLimit;
}
// Special sqlite query to count ROW_NUMBER
$query = PHP_EOL . "SELECT $column"
. PHP_EOL . "FROM ($query" . PHP_EOL . "ORDER BY $orderBy"
. PHP_EOL . ') AS w,(SELECT ROW_NUMBER(0)) AS r'
// Forbid to flatten subqueries.
. ((string) $this->order ?: PHP_EOL . 'ORDER BY NULL');
return $this->processLimit($query, $this->limit, $this->offset);
}
break;
case 'querySet':
$query = $this->querySet;
if ($query->order || $query->limit || $query->offset) {
// If ORDER BY or LIMIT statement exist then parentheses is required for the first query
$query = PHP_EOL . "SELECT * FROM ($query)";
}
if ($this->merge) {
// Special case for merge
foreach ($this->merge as $element) {
$query .= (string) $element;
}
}
if ($this->order) {
$query .= (string) $this->order;
}
return $query;
case 'update':
if ($this->join) {
$table = $this->update->getElements();
$table = $table[0];
$tableName = explode(' ', $table);
$tableName = $tableName[0];
if ($this->columns === null) {
$fields = $this->db->getTableColumns($tableName);
foreach ($fields as $key => $value) {
$fields[$key] = $key;
}
$this->columns = new QueryElement('()', $fields);
}
$fields = $this->columns->getElements();
$elements = $this->set->getElements();
foreach ($elements as $nameValue) {
$setArray = explode(' = ', $nameValue, 2);
if ($setArray[0][0] === '`') {
// Unquote column name
$setArray[0] = substr($setArray[0], 1, -1);
}
$fields[$setArray[0]] = $setArray[1];
}
$select = new static($this->db);
$select->select(array_values($fields))
->from($table);
$select->join = $this->join;
$select->where = $this->where;
return 'INSERT OR REPLACE INTO ' . $tableName
. ' (' . implode(',', array_keys($fields)) . ')'
. (string) $select;
}
}
return parent::__toString();
}
/**
* 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|null $operator Comparison operator between charLength integer value and $condition
* @param string|null $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)
{
$statement = 'length(' . $field . ')';
if ($operator !== null && $condition !== null) {
$statement .= ' ' . $operator . ' ' . $condition;
}
return $statement;
}
/**
* Concatenates an array of column names or values.
*
* Usage:
* $query->select($query->concatenate(array('a', 'b')));
*
* @param string[] $values An array of values to concatenate.
* @param string|null $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 !== null) {
return implode(' || ' . $this->quote($separator) . ' || ', $values);
}
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.
*
* @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;
}
/**
* Return the number of the current row.
*
* Usage:
* $query->select('id');
* $query->selectRowNumber('ordering,publish_up DESC', 'new_ordering');
* $query->from('#__content');
*
* @param string $orderBy An expression of ordering for window function.
* @param string $orderColumnAlias An alias for new ordering column.
*
* @return $this
*
* @since 2.0.0
* @throws \RuntimeException
*/
public function selectRowNumber($orderBy, $orderColumnAlias)
{
$this->validateRowNumber($orderBy, $orderColumnAlias);
return $this;
}
/**
* Add a query to UNION with the current query.
*
* Usage:
* $query->union('SELECT name FROM #__foo')
* $query->union('SELECT name FROM #__foo', true)
*
* @param DatabaseQuery|string $query The DatabaseQuery object or string to union.
* @param boolean $distinct True to only return distinct rows from the union.
*
* @return $this
*
* @since 1.0
*/
public function union($query, $distinct = true)
{
// Set up the name with parentheses, the DISTINCT flag is redundant
return $this->merge($distinct ? 'UNION SELECT * FROM ()' : 'UNION ALL SELECT * FROM ()', $query);
}
/**
* Aggregate function to get input values concatenated into a string, separated by delimiter
*
* Usage:
* $query->groupConcat('id', ',');
*
* @param string $expression The expression to apply concatenation to, this may be a column name or complex SQL statement.
* @param string $separator The delimiter of each concatenated value
*
* @return string Input values concatenated into a string, separated by delimiter
*
* @since 2.0.0
*/
public function groupConcat($expression, $separator = ',')
{
return 'group_concat(' . $expression . ', ' . $this->quote($separator) . ')';
}
}