primo commit
This commit is contained in:
199
libraries/f0f/database/database.php
Normal file
199
libraries/f0f/database/database.php
Normal file
@ -0,0 +1,199 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Database connector class.
|
||||
*
|
||||
* @since 11.1
|
||||
* @deprecated 13.3 (Platform) & 4.0 (CMS)
|
||||
*/
|
||||
abstract class F0FDatabase
|
||||
{
|
||||
/**
|
||||
* Execute the SQL statement.
|
||||
*
|
||||
* @return mixed A database cursor resource on success, boolean false on failure.
|
||||
*
|
||||
* @since 11.1
|
||||
* @throws RuntimeException
|
||||
* @deprecated 13.1 (Platform) & 4.0 (CMS)
|
||||
*/
|
||||
public function query()
|
||||
{
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add('F0FDatabase::query() is deprecated, use F0FDatabaseDriver::execute() instead.', JLog::WARNING, 'deprecated');
|
||||
}
|
||||
|
||||
return $this->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of available database connectors. The list will only be populated with connectors that both
|
||||
* the class exists and the static test method returns true. This gives us the ability to have a multitude
|
||||
* of connector classes that are self-aware as to whether or not they are able to be used on a given system.
|
||||
*
|
||||
* @return array An array of available database connectors.
|
||||
*
|
||||
* @since 11.1
|
||||
* @deprecated 13.1 (Platform) & 4.0 (CMS)
|
||||
*/
|
||||
public static function getConnectors()
|
||||
{
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add('F0FDatabase::getConnectors() is deprecated, use F0FDatabaseDriver::getConnectors() instead.', JLog::WARNING, 'deprecated');
|
||||
}
|
||||
|
||||
return F0FDatabaseDriver::getConnectors();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the error message from the database connection.
|
||||
*
|
||||
* @param boolean $escaped True to escape the message string for use in JavaScript.
|
||||
*
|
||||
* @return string The error message for the most recent query.
|
||||
*
|
||||
* @deprecated 13.3 (Platform) & 4.0 (CMS)
|
||||
* @since 11.1
|
||||
*/
|
||||
public function getErrorMsg($escaped = false)
|
||||
{
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add('F0FDatabase::getErrorMsg() is deprecated, use exception handling instead.', JLog::WARNING, 'deprecated');
|
||||
}
|
||||
|
||||
if ($escaped)
|
||||
{
|
||||
return addslashes($this->errorMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->errorMsg;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the error number from the database connection.
|
||||
*
|
||||
* @return integer The error number for the most recent query.
|
||||
*
|
||||
* @since 11.1
|
||||
* @deprecated 13.3 (Platform) & 4.0 (CMS)
|
||||
*/
|
||||
public function getErrorNum()
|
||||
{
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add('F0FDatabase::getErrorNum() is deprecated, use exception handling instead.', JLog::WARNING, 'deprecated');
|
||||
}
|
||||
|
||||
return $this->errorNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to return a F0FDatabaseDriver instance based on the given options. There are three global options and then
|
||||
* the rest are specific to the database driver. The 'driver' option defines which F0FDatabaseDriver class is
|
||||
* used for the connection -- the default is 'mysqli'. The 'database' option determines which database is to
|
||||
* be used for the connection. The 'select' option determines whether the connector should automatically select
|
||||
* the chosen database.
|
||||
*
|
||||
* Instances are unique to the given options and new objects are only created when a unique options array is
|
||||
* passed into the method. This ensures that we don't end up with unnecessary database connection resources.
|
||||
*
|
||||
* @param array $options Parameters to be passed to the database driver.
|
||||
*
|
||||
* @return F0FDatabaseDriver A database object.
|
||||
*
|
||||
* @since 11.1
|
||||
* @deprecated 13.1 (Platform) & 4.0 (CMS)
|
||||
*/
|
||||
public static function getInstance($options = array())
|
||||
{
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add('F0FDatabase::getInstance() is deprecated, use F0FDatabaseDriver::getInstance() instead.', JLog::WARNING, 'deprecated');
|
||||
}
|
||||
|
||||
return F0FDatabaseDriver::getInstance($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a string of multiple queries into an array of individual queries.
|
||||
*
|
||||
* @param string $query Input SQL string with which to split into individual queries.
|
||||
*
|
||||
* @return array The queries from the input string separated into an array.
|
||||
*
|
||||
* @since 11.1
|
||||
* @deprecated 13.1 (Platform) & 4.0 (CMS)
|
||||
*/
|
||||
public static function splitSql($query)
|
||||
{
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add('F0FDatabase::splitSql() is deprecated, use F0FDatabaseDriver::splitSql() instead.', JLog::WARNING, 'deprecated');
|
||||
}
|
||||
|
||||
return F0FDatabaseDriver::splitSql($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the most recent error message for the database connector.
|
||||
*
|
||||
* @param boolean $showSQL True to display the SQL statement sent to the database as well as the error.
|
||||
*
|
||||
* @return string The error message for the most recent query.
|
||||
*
|
||||
* @since 11.1
|
||||
* @deprecated 13.3 (Platform) & 4.0 (CMS)
|
||||
*/
|
||||
public function stderr($showSQL = false)
|
||||
{
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add('F0FDatabase::stderr() is deprecated.', JLog::WARNING, 'deprecated');
|
||||
}
|
||||
|
||||
if ($this->errorNum != 0)
|
||||
{
|
||||
return JText::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $this->errorNum, $this->errorMsg)
|
||||
. ($showSQL ? "<br />SQL = <pre>$this->sql</pre>" : '');
|
||||
}
|
||||
else
|
||||
{
|
||||
return JText::_('JLIB_DATABASE_FUNCTION_NOERROR');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to see if the connector is available.
|
||||
*
|
||||
* @return boolean True on success, false otherwise.
|
||||
*
|
||||
* @since 11.1
|
||||
* @deprecated 12.3 (Platform) & 4.0 (CMS) - Use F0FDatabaseDriver::isSupported() instead.
|
||||
*/
|
||||
public static function test()
|
||||
{
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add('F0FDatabase::test() is deprecated. Use F0FDatabaseDriver::isSupported() instead.', JLog::WARNING, 'deprecated');
|
||||
}
|
||||
|
||||
return static::isSupported();
|
||||
}
|
||||
}
|
||||
2255
libraries/f0f/database/driver.php
Normal file
2255
libraries/f0f/database/driver.php
Normal file
File diff suppressed because it is too large
Load Diff
778
libraries/f0f/database/driver/joomla.php
Normal file
778
libraries/f0f/database/driver/joomla.php
Normal file
@ -0,0 +1,778 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* This crazy three line bit is required to convince Joomla! to load JDatabaseInterface which is on the same file as the
|
||||
* abstract JDatabaseDriver class for reasons that beat me. It makes no sense. Furthermore, jimport on Joomla! 3.4
|
||||
* doesn't seem to actually load the file, merely registering the association in the autoloader. Hence the class_exists
|
||||
* in here.
|
||||
*/
|
||||
jimport('joomla.database.driver');
|
||||
jimport('joomla.database.driver.mysqli');
|
||||
class_exists('JDatabaseDriver', true);
|
||||
|
||||
/**
|
||||
* Joomla! pass-through database driver.
|
||||
*/
|
||||
class F0FDatabaseDriverJoomla extends F0FDatabase implements F0FDatabaseInterface
|
||||
{
|
||||
/** @var F0FDatabase The real database connection object */
|
||||
private $dbo;
|
||||
|
||||
/**
|
||||
* @var string 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.
|
||||
* @since 11.1
|
||||
*/
|
||||
protected $nameQuote = '';
|
||||
|
||||
/**
|
||||
* Is this driver supported
|
||||
*
|
||||
* @since 11.2
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Database object constructor
|
||||
*
|
||||
* @param array $options List of options used to configure the connection
|
||||
*/
|
||||
public function __construct($options = array())
|
||||
{
|
||||
// Get best matching Akeeba Backup driver instance
|
||||
$this->dbo = JFactory::getDbo();
|
||||
|
||||
$reflection = new ReflectionClass($this->dbo);
|
||||
|
||||
try
|
||||
{
|
||||
$refProp = $reflection->getProperty('nameQuote');
|
||||
$refProp->setAccessible(true);
|
||||
$this->nameQuote = $refProp->getValue($this->dbo);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$this->nameQuote = '`';
|
||||
}
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
if (method_exists($this->dbo, 'close'))
|
||||
{
|
||||
$this->dbo->close();
|
||||
}
|
||||
elseif (method_exists($this->dbo, 'disconnect'))
|
||||
{
|
||||
$this->dbo->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public function disconnect()
|
||||
{
|
||||
$this->close();
|
||||
}
|
||||
|
||||
public function open()
|
||||
{
|
||||
if (method_exists($this->dbo, 'open'))
|
||||
{
|
||||
$this->dbo->open();
|
||||
}
|
||||
elseif (method_exists($this->dbo, 'connect'))
|
||||
{
|
||||
$this->dbo->connect();
|
||||
}
|
||||
}
|
||||
|
||||
public function connect()
|
||||
{
|
||||
$this->open();
|
||||
}
|
||||
|
||||
public function connected()
|
||||
{
|
||||
if (method_exists($this->dbo, 'connected'))
|
||||
{
|
||||
return $this->dbo->connected();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function escape($text, $extra = false)
|
||||
{
|
||||
return $this->dbo->escape($text, $extra);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
if (method_exists($this->dbo, 'execute'))
|
||||
{
|
||||
return $this->dbo->execute();
|
||||
}
|
||||
|
||||
return $this->dbo->query();
|
||||
}
|
||||
|
||||
public function getAffectedRows()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getAffectedRows'))
|
||||
{
|
||||
return $this->dbo->getAffectedRows();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getCollation()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getCollation'))
|
||||
{
|
||||
return $this->dbo->getCollation();
|
||||
}
|
||||
|
||||
return 'utf8_general_ci';
|
||||
}
|
||||
|
||||
public function getConnection()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getConnection'))
|
||||
{
|
||||
return $this->dbo->getConnection();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getCount()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getCount'))
|
||||
{
|
||||
return $this->dbo->getCount();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getDateFormat()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getDateFormat'))
|
||||
{
|
||||
return $this->dbo->getDateFormat();
|
||||
}
|
||||
|
||||
return 'Y-m-d H:i:s';;
|
||||
}
|
||||
|
||||
public function getMinimum()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getMinimum'))
|
||||
{
|
||||
return $this->dbo->getMinimum();
|
||||
}
|
||||
|
||||
return '5.0.40';
|
||||
}
|
||||
|
||||
public function getNullDate()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getNullDate'))
|
||||
{
|
||||
return $this->dbo->getNullDate();
|
||||
}
|
||||
|
||||
return '0000-00-00 00:00:00';
|
||||
}
|
||||
|
||||
public function getNumRows($cursor = null)
|
||||
{
|
||||
if (method_exists($this->dbo, 'getNumRows'))
|
||||
{
|
||||
return $this->dbo->getNumRows($cursor);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getQuery($new = false)
|
||||
{
|
||||
if (method_exists($this->dbo, 'getQuery'))
|
||||
{
|
||||
return $this->dbo->getQuery($new);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getTableColumns($table, $typeOnly = true)
|
||||
{
|
||||
if (method_exists($this->dbo, 'getTableColumns'))
|
||||
{
|
||||
return $this->dbo->getTableColumns($table, $typeOnly);
|
||||
}
|
||||
|
||||
$result = $this->dbo->getTableFields(array($table), $typeOnly);
|
||||
|
||||
return $result[$table];
|
||||
}
|
||||
|
||||
public function getTableKeys($tables)
|
||||
{
|
||||
if (method_exists($this->dbo, 'getTableKeys'))
|
||||
{
|
||||
return $this->dbo->getTableKeys($tables);
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getTableList()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getTableList'))
|
||||
{
|
||||
return $this->dbo->getTableList();
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getVersion()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getVersion'))
|
||||
{
|
||||
return $this->dbo->getVersion();
|
||||
}
|
||||
|
||||
return '5.0.40';
|
||||
}
|
||||
|
||||
public function insertid()
|
||||
{
|
||||
if (method_exists($this->dbo, 'insertid'))
|
||||
{
|
||||
return $this->dbo->insertid();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function insertObject($table, &$object, $key = null)
|
||||
{
|
||||
if (method_exists($this->dbo, 'insertObject'))
|
||||
{
|
||||
return $this->dbo->insertObject($table, $object, $key);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function loadAssoc()
|
||||
{
|
||||
if (method_exists($this->dbo, 'loadAssoc'))
|
||||
{
|
||||
return $this->dbo->loadAssoc();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function loadAssocList($key = null, $column = null)
|
||||
{
|
||||
if (method_exists($this->dbo, 'loadAssocList'))
|
||||
{
|
||||
return $this->dbo->loadAssocList($key, $column);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function loadObject($class = 'stdClass')
|
||||
{
|
||||
if (method_exists($this->dbo, 'loadObject'))
|
||||
{
|
||||
return $this->dbo->loadObject($class);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function loadObjectList($key = '', $class = 'stdClass')
|
||||
{
|
||||
if (method_exists($this->dbo, 'loadObjectList'))
|
||||
{
|
||||
return $this->dbo->loadObjectList($key, $class);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function loadResult()
|
||||
{
|
||||
if (method_exists($this->dbo, 'loadResult'))
|
||||
{
|
||||
return $this->dbo->loadResult();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function loadRow()
|
||||
{
|
||||
if (method_exists($this->dbo, 'loadRow'))
|
||||
{
|
||||
return $this->dbo->loadRow();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function loadRowList($key = null)
|
||||
{
|
||||
if (method_exists($this->dbo, 'loadRowList'))
|
||||
{
|
||||
return $this->dbo->loadRowList($key);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function lockTable($tableName)
|
||||
{
|
||||
if (method_exists($this->dbo, 'lockTable'))
|
||||
{
|
||||
return $this->dbo->lockTable($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function quote($text, $escape = true)
|
||||
{
|
||||
if (method_exists($this->dbo, 'quote'))
|
||||
{
|
||||
return $this->dbo->quote($text, $escape);
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
public function select($database)
|
||||
{
|
||||
if (method_exists($this->dbo, 'select'))
|
||||
{
|
||||
return $this->dbo->select($database);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setQuery($query, $offset = 0, $limit = 0)
|
||||
{
|
||||
if (method_exists($this->dbo, 'setQuery'))
|
||||
{
|
||||
return $this->dbo->setQuery($query, $offset, $limit);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function transactionCommit($toSavepoint = false)
|
||||
{
|
||||
if (method_exists($this->dbo, 'transactionCommit'))
|
||||
{
|
||||
$this->dbo->transactionCommit($toSavepoint);
|
||||
}
|
||||
}
|
||||
|
||||
public function transactionRollback($toSavepoint = false)
|
||||
{
|
||||
if (method_exists($this->dbo, 'transactionRollback'))
|
||||
{
|
||||
$this->dbo->transactionRollback($toSavepoint);
|
||||
}
|
||||
}
|
||||
|
||||
public function transactionStart($asSavepoint = false)
|
||||
{
|
||||
if (method_exists($this->dbo, 'transactionStart'))
|
||||
{
|
||||
$this->dbo->transactionStart($asSavepoint);
|
||||
}
|
||||
}
|
||||
|
||||
public function unlockTables()
|
||||
{
|
||||
if (method_exists($this->dbo, 'unlockTables'))
|
||||
{
|
||||
return $this->dbo->unlockTables();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function updateObject($table, &$object, $key, $nulls = false)
|
||||
{
|
||||
if (method_exists($this->dbo, 'updateObject'))
|
||||
{
|
||||
return $this->dbo->updateObject($table, $object, $key, $nulls);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getLog()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getLog'))
|
||||
{
|
||||
return $this->dbo->getLog();
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
public function dropTable($table, $ifExists = true)
|
||||
{
|
||||
if (method_exists($this->dbo, 'dropTable'))
|
||||
{
|
||||
return $this->dbo->dropTable($table, $ifExists);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTableCreate($tables)
|
||||
{
|
||||
if (method_exists($this->dbo, 'getTableCreate'))
|
||||
{
|
||||
return $this->dbo->getTableCreate($tables);
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
|
||||
{
|
||||
if (method_exists($this->dbo, 'renameTable'))
|
||||
{
|
||||
return $this->dbo->renameTable($oldTable, $newTable, $backup, $prefix);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setUtf()
|
||||
{
|
||||
if (method_exists($this->dbo, 'setUtf'))
|
||||
{
|
||||
return $this->dbo->setUtf();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
protected function freeResult($cursor = null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get an array of values from the <var>$offset</var> field in each row of the result set from
|
||||
* the database query.
|
||||
*
|
||||
* @param integer $offset The row offset to use to build the result array.
|
||||
*
|
||||
* @return mixed The return value or null if the query failed.
|
||||
*
|
||||
* @since 11.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function loadColumn($offset = 0)
|
||||
{
|
||||
if (method_exists($this->dbo, 'loadColumn'))
|
||||
{
|
||||
return $this->dbo->loadColumn($offset);
|
||||
}
|
||||
|
||||
return $this->dbo->loadResultArray($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap an SQL statement identifier name such as column, table or database names in quotes to prevent injection
|
||||
* risks and reserved word conflicts.
|
||||
*
|
||||
* @param mixed $name The identifier name to wrap in quotes, or an array of identifier names to wrap in quotes.
|
||||
* Each type supports dot-notation name.
|
||||
* @param mixed $as The AS query part associated to $name. It can be string or array, in latter case it has to be
|
||||
* same length of $name; if is null there will not be any AS part for string or array element.
|
||||
*
|
||||
* @return mixed The quote wrapped name, same type of $name.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function quoteName($name, $as = null)
|
||||
{
|
||||
if (is_string($name))
|
||||
{
|
||||
$quotedName = $this->quoteNameStr(explode('.', $name));
|
||||
|
||||
$quotedAs = '';
|
||||
|
||||
if (!is_null($as))
|
||||
{
|
||||
settype($as, 'array');
|
||||
$quotedAs .= ' AS ' . $this->quoteNameStr($as);
|
||||
}
|
||||
|
||||
return $quotedName . $quotedAs;
|
||||
}
|
||||
else
|
||||
{
|
||||
$fin = array();
|
||||
|
||||
if (is_null($as))
|
||||
{
|
||||
foreach ($name as $str)
|
||||
{
|
||||
$fin[] = $this->quoteName($str);
|
||||
}
|
||||
}
|
||||
elseif (is_array($name) && (count($name) == count($as)))
|
||||
{
|
||||
$count = count($name);
|
||||
|
||||
for ($i = 0; $i < $count; $i++)
|
||||
{
|
||||
$fin[] = $this->quoteName($name[$i], $as[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
return $fin;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Quote strings coming from quoteName call.
|
||||
*
|
||||
* @param array $strArr Array of strings coming from quoteName dot-explosion.
|
||||
*
|
||||
* @return string Dot-imploded string of quoted parts.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
protected function quoteNameStr($strArr)
|
||||
{
|
||||
$parts = array();
|
||||
$q = $this->nameQuote;
|
||||
|
||||
foreach ($strArr as $part)
|
||||
{
|
||||
if (is_null($part))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strlen($q) == 1)
|
||||
{
|
||||
$parts[] = $q . $part . $q;
|
||||
}
|
||||
else
|
||||
{
|
||||
$parts[] = $q{0} . $part . $q{1};
|
||||
}
|
||||
}
|
||||
|
||||
return implode('.', $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the error message from the database connection.
|
||||
*
|
||||
* @param boolean $escaped True to escape the message string for use in JavaScript.
|
||||
*
|
||||
* @return string The error message for the most recent query.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function getErrorMsg($escaped = false)
|
||||
{
|
||||
if (method_exists($this->dbo, 'getErrorMsg'))
|
||||
{
|
||||
$errorMessage = $this->dbo->getErrorMsg();
|
||||
}
|
||||
else
|
||||
{
|
||||
$errorMessage = $this->errorMsg;
|
||||
}
|
||||
|
||||
if ($escaped)
|
||||
{
|
||||
return addslashes($errorMessage);
|
||||
}
|
||||
|
||||
return $errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the error number from the database connection.
|
||||
*
|
||||
* @return integer The error number for the most recent query.
|
||||
*
|
||||
* @since 11.1
|
||||
* @deprecated 13.3 (Platform) & 4.0 (CMS)
|
||||
*/
|
||||
public function getErrorNum()
|
||||
{
|
||||
if (method_exists($this->dbo, 'getErrorNum'))
|
||||
{
|
||||
$errorNum = $this->dbo->getErrorNum();
|
||||
}
|
||||
else
|
||||
{
|
||||
$errorNum = $this->getErrorNum;
|
||||
}
|
||||
|
||||
return $errorNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the most recent error message for the database connector.
|
||||
*
|
||||
* @param boolean $showSQL True to display the SQL statement sent to the database as well as the error.
|
||||
*
|
||||
* @return string The error message for the most recent query.
|
||||
*/
|
||||
public function stderr($showSQL = false)
|
||||
{
|
||||
if (method_exists($this->dbo, 'stderr'))
|
||||
{
|
||||
return $this->dbo->stderr($showSQL);
|
||||
}
|
||||
|
||||
return parent::stderr($showSQL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to proxy all calls to the loaded database driver object
|
||||
*/
|
||||
public function __call($name, array $arguments)
|
||||
{
|
||||
if (is_null($this->dbo))
|
||||
{
|
||||
throw new Exception('F0F database driver is not loaded');
|
||||
}
|
||||
|
||||
if (method_exists($this->dbo, $name) || in_array($name, array('q', 'nq', 'qn', 'query')))
|
||||
{
|
||||
switch ($name)
|
||||
{
|
||||
case 'execute':
|
||||
$name = 'query';
|
||||
break;
|
||||
|
||||
case 'q':
|
||||
$name = 'quote';
|
||||
break;
|
||||
|
||||
case 'qn':
|
||||
case 'nq':
|
||||
switch (count($arguments))
|
||||
{
|
||||
case 0 :
|
||||
$result = $this->quoteName();
|
||||
break;
|
||||
case 1 :
|
||||
$result = $this->quoteName($arguments[0]);
|
||||
break;
|
||||
case 2:
|
||||
default:
|
||||
$result = $this->quoteName($arguments[0], $arguments[1]);
|
||||
break;
|
||||
}
|
||||
return $result;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
switch (count($arguments))
|
||||
{
|
||||
case 0 :
|
||||
$result = $this->dbo->$name();
|
||||
break;
|
||||
case 1 :
|
||||
$result = $this->dbo->$name($arguments[0]);
|
||||
break;
|
||||
case 2:
|
||||
$result = $this->dbo->$name($arguments[0], $arguments[1]);
|
||||
break;
|
||||
case 3:
|
||||
$result = $this->dbo->$name($arguments[0], $arguments[1], $arguments[2]);
|
||||
break;
|
||||
case 4:
|
||||
$result = $this->dbo->$name($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
|
||||
break;
|
||||
case 5:
|
||||
$result = $this->dbo->$name($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
|
||||
break;
|
||||
default:
|
||||
// Resort to using call_user_func_array for many segments
|
||||
$result = call_user_func_array(array($this->dbo, $name), $arguments);
|
||||
}
|
||||
|
||||
if (class_exists('JDatabase') && is_object($result) && ($result instanceof JDatabase))
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('Method ' . $name . ' not found in F0FDatabase');
|
||||
}
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
if (isset($this->dbo->$name) || property_exists($this->dbo, $name))
|
||||
{
|
||||
return $this->dbo->$name;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->dbo->$name = null;
|
||||
user_error('Database driver does not support property ' . $name);
|
||||
}
|
||||
}
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
if (isset($this->dbo->name) || property_exists($this->dbo, $name))
|
||||
{
|
||||
$this->dbo->$name = $value;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->dbo->$name = null;
|
||||
user_error('Database driver not support property ' . $name);
|
||||
}
|
||||
}
|
||||
}
|
||||
577
libraries/f0f/database/driver/mysql.php
Normal file
577
libraries/f0f/database/driver/mysql.php
Normal file
@ -0,0 +1,577 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* MySQL database driver
|
||||
*
|
||||
* @see http://dev.mysql.com/doc/
|
||||
* @since 12.1
|
||||
* @deprecated Will be removed when the minimum supported PHP version no longer includes the deprecated PHP `mysql` extension
|
||||
*/
|
||||
class F0FDatabaseDriverMysql extends F0FDatabaseDriverMysqli
|
||||
{
|
||||
/**
|
||||
* The name of the database driver.
|
||||
*
|
||||
* @var string
|
||||
* @since 12.1
|
||||
*/
|
||||
public $name = 'mysql';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $options Array of database options with keys: host, user, password, database, select.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function __construct($options)
|
||||
{
|
||||
// PHP's `mysql` extension is not present in PHP 7, block instantiation in this environment
|
||||
if (PHP_MAJOR_VERSION >= 7)
|
||||
{
|
||||
throw new RuntimeException(
|
||||
'This driver is unsupported in PHP 7, please use the MySQLi or PDO MySQL driver instead.'
|
||||
);
|
||||
}
|
||||
|
||||
// Get some basic values from the options.
|
||||
$options['host'] = (isset($options['host'])) ? $options['host'] : 'localhost';
|
||||
$options['user'] = (isset($options['user'])) ? $options['user'] : 'root';
|
||||
$options['password'] = (isset($options['password'])) ? $options['password'] : '';
|
||||
$options['database'] = (isset($options['database'])) ? $options['database'] : '';
|
||||
$options['select'] = (isset($options['select'])) ? (bool) $options['select'] : true;
|
||||
|
||||
// Finalize initialisation.
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the database if needed.
|
||||
*
|
||||
* @return void Returns void if the database connected successfully.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function connect()
|
||||
{
|
||||
if ($this->connection)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure the MySQL extension for PHP is installed and enabled.
|
||||
if (!self::isSupported())
|
||||
{
|
||||
throw new RuntimeException('Could not connect to MySQL.');
|
||||
}
|
||||
|
||||
// Attempt to connect to the server.
|
||||
if (!($this->connection = @ mysql_connect($this->options['host'], $this->options['user'], $this->options['password'], true)))
|
||||
{
|
||||
throw new RuntimeException('Could not connect to MySQL.');
|
||||
}
|
||||
|
||||
// Set sql_mode to non_strict mode
|
||||
mysql_query("SET @@SESSION.sql_mode = '';", $this->connection);
|
||||
|
||||
// If auto-select is enabled select the given database.
|
||||
if ($this->options['select'] && !empty($this->options['database']))
|
||||
{
|
||||
$this->select($this->options['database']);
|
||||
}
|
||||
|
||||
// Pre-populate the UTF-8 Multibyte compatibility flag based on server version
|
||||
$this->utf8mb4 = $this->serverClaimsUtf8mb4Support();
|
||||
|
||||
// Set the character set (needed for MySQL 4.1.2+).
|
||||
$this->utf = $this->setUtf();
|
||||
|
||||
// Turn MySQL profiling ON in debug mode:
|
||||
if ($this->debug && $this->hasProfiling())
|
||||
{
|
||||
mysql_query("SET profiling = 1;", $this->connection);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects the database.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function disconnect()
|
||||
{
|
||||
// Close the connection.
|
||||
if (is_resource($this->connection))
|
||||
{
|
||||
foreach ($this->disconnectHandlers as $h)
|
||||
{
|
||||
call_user_func_array($h, array( &$this));
|
||||
}
|
||||
|
||||
mysql_close($this->connection);
|
||||
}
|
||||
|
||||
$this->connection = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function escape($text, $extra = false)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$result = mysql_real_escape_string($text, $this->getConnection());
|
||||
|
||||
if ($extra)
|
||||
{
|
||||
$result = addcslashes($result, '%_');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to see if the MySQL connector is available.
|
||||
*
|
||||
* @return boolean True on success, false otherwise.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
return (function_exists('mysql_connect'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the connection to the server is active.
|
||||
*
|
||||
* @return boolean True if connected to the database engine.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function connected()
|
||||
{
|
||||
if (is_resource($this->connection))
|
||||
{
|
||||
return @mysql_ping($this->connection);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE for the previous executed SQL statement.
|
||||
*
|
||||
* @return integer The number of affected rows.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function getAffectedRows()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
return mysql_affected_rows($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of returned rows for the previous executed SQL statement.
|
||||
* This command is only valid for statements like SELECT or SHOW that return an actual result set.
|
||||
* To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use getAffectedRows().
|
||||
*
|
||||
* @param resource $cursor An optional database cursor resource to extract the row count from.
|
||||
*
|
||||
* @return integer The number of returned rows.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function getNumRows($cursor = null)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
return mysql_num_rows($cursor ? $cursor : $this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of the database connector.
|
||||
*
|
||||
* @return string The database connector version.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
return mysql_get_server_info($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function insertid()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
return mysql_insert_id($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the SQL statement.
|
||||
*
|
||||
* @return mixed A database cursor resource on success, boolean false on failure.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
if (!is_resource($this->connection))
|
||||
{
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'database');
|
||||
}
|
||||
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->sql instanceof F0FDatabaseQuery) && ($this->limit > 0 || $this->offset > 0))
|
||||
{
|
||||
$query .= ' LIMIT ' . $this->offset . ', ' . $this->limit;
|
||||
}
|
||||
|
||||
// Increment the query counter.
|
||||
$this->count++;
|
||||
|
||||
// Reset the error values.
|
||||
$this->errorNum = 0;
|
||||
$this->errorMsg = '';
|
||||
|
||||
// If debugging is enabled then let's log the query.
|
||||
if ($this->debug)
|
||||
{
|
||||
// Add the query to the object queue.
|
||||
$this->log[] = $query;
|
||||
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add($query, JLog::DEBUG, 'databasequery');
|
||||
}
|
||||
|
||||
$this->timings[] = microtime(true);
|
||||
}
|
||||
|
||||
// Execute the query. Error suppression is used here to prevent warnings/notices that the connection has been lost.
|
||||
$this->cursor = @mysql_query($query, $this->connection);
|
||||
|
||||
if ($this->debug)
|
||||
{
|
||||
$this->timings[] = microtime(true);
|
||||
|
||||
if (defined('DEBUG_BACKTRACE_IGNORE_ARGS'))
|
||||
{
|
||||
$this->callStacks[] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->callStacks[] = debug_backtrace();
|
||||
}
|
||||
}
|
||||
|
||||
// If an error occurred handle it.
|
||||
if (!$this->cursor)
|
||||
{
|
||||
// Get the error number and message before we execute any more queries.
|
||||
$this->errorNum = $this->getErrorNumber();
|
||||
$this->errorMsg = $this->getErrorMessage($query);
|
||||
|
||||
// Check if the server was disconnected.
|
||||
if (!$this->connected())
|
||||
{
|
||||
try
|
||||
{
|
||||
// Attempt to reconnect.
|
||||
$this->connection = null;
|
||||
$this->connect();
|
||||
}
|
||||
// If connect fails, ignore that exception and throw the normal exception.
|
||||
catch (RuntimeException $e)
|
||||
{
|
||||
// Get the error number and message.
|
||||
$this->errorNum = $this->getErrorNumber();
|
||||
$this->errorMsg = $this->getErrorMessage($query);
|
||||
|
||||
// Throw the normal query exception.
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'database-error');
|
||||
}
|
||||
|
||||
throw new RuntimeException($this->errorMsg, $this->errorNum, $e);
|
||||
}
|
||||
|
||||
// Since we were able to reconnect, run the query again.
|
||||
return $this->execute();
|
||||
}
|
||||
// The server was not disconnected.
|
||||
else
|
||||
{
|
||||
// Throw the normal query exception.
|
||||
if (class_exists('JLog'))
|
||||
{
|
||||
JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'database-error');
|
||||
}
|
||||
|
||||
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.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function select($database)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
if (!$database)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!mysql_select_db($database, $this->connection))
|
||||
{
|
||||
throw new RuntimeException('Could not connect to database');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the connection to use UTF-8 character encoding.
|
||||
*
|
||||
* @return boolean True on success.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function setUtf()
|
||||
{
|
||||
// If UTF is not supported return false immediately
|
||||
if (!$this->utf)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make sure we're connected to the server
|
||||
$this->connect();
|
||||
|
||||
// Which charset should I use, plain utf8 or multibyte utf8mb4?
|
||||
$charset = $this->utf8mb4 ? 'utf8mb4' : 'utf8';
|
||||
|
||||
$result = @mysql_set_charset($charset, $this->connection);
|
||||
|
||||
/**
|
||||
* If I could not set the utf8mb4 charset then the server doesn't support utf8mb4 despite claiming otherwise.
|
||||
* This happens on old MySQL server versions (less than 5.5.3) using the mysqlnd PHP driver. Since mysqlnd
|
||||
* masks the server version and reports only its own we can not be sure if the server actually does support
|
||||
* UTF-8 Multibyte (i.e. it's MySQL 5.5.3 or later). Since the utf8mb4 charset is undefined in this case we
|
||||
* catch the error and determine that utf8mb4 is not supported!
|
||||
*/
|
||||
if (!$result && $this->utf8mb4)
|
||||
{
|
||||
$this->utf8mb4 = false;
|
||||
$result = @mysql_set_charset('utf8', $this->connection);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
protected function fetchArray($cursor = null)
|
||||
{
|
||||
return mysql_fetch_row($cursor ? $cursor : $this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
protected function fetchAssoc($cursor = null)
|
||||
{
|
||||
return mysql_fetch_assoc($cursor ? $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.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
protected function fetchObject($cursor = null, $class = 'stdClass')
|
||||
{
|
||||
return mysql_fetch_object($cursor ? $cursor : $this->cursor, $class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
protected function freeResult($cursor = null)
|
||||
{
|
||||
mysql_free_result($cursor ? $cursor : $this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to check if profiling is available
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @since 3.1.3
|
||||
*/
|
||||
private function hasProfiling()
|
||||
{
|
||||
try
|
||||
{
|
||||
$res = mysql_query("SHOW VARIABLES LIKE 'have_profiling'", $this->connection);
|
||||
$row = mysql_fetch_assoc($res);
|
||||
|
||||
return isset($row);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the database server claim to have support for UTF-8 Multibyte (utf8mb4) collation?
|
||||
*
|
||||
* libmysql supports utf8mb4 since 5.5.3 (same version as the MySQL server). mysqlnd supports utf8mb4 since 5.0.9.
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @since CMS 3.5.0
|
||||
*/
|
||||
private function serverClaimsUtf8mb4Support()
|
||||
{
|
||||
$client_version = mysql_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', '>=');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the actual SQL Error number
|
||||
*
|
||||
* @return integer The SQL Error number
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
protected function getErrorNumber()
|
||||
{
|
||||
return (int) mysql_errno($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the actual SQL Error message
|
||||
*
|
||||
* @param string $query The SQL Query that fails
|
||||
*
|
||||
* @return string The SQL Error message
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
protected function getErrorMessage($query)
|
||||
{
|
||||
$errorMessage = (string) mysql_error($this->connection);
|
||||
|
||||
// Replace the Databaseprefix with `#__` if we are not in Debug
|
||||
if (!$this->debug)
|
||||
{
|
||||
$errorMessage = str_replace($this->tablePrefix, '#__', $errorMessage);
|
||||
$query = str_replace($this->tablePrefix, '#__', $query);
|
||||
}
|
||||
|
||||
return $errorMessage . ' SQL=' . $query;
|
||||
}
|
||||
}
|
||||
1019
libraries/f0f/database/driver/mysqli.php
Normal file
1019
libraries/f0f/database/driver/mysqli.php
Normal file
File diff suppressed because it is too large
Load Diff
712
libraries/f0f/database/driver/oracle.php
Normal file
712
libraries/f0f/database/driver/oracle.php
Normal file
@ -0,0 +1,712 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Oracle database driver
|
||||
*
|
||||
* @see http://php.net/pdo
|
||||
* @since 12.1
|
||||
*/
|
||||
class F0FDatabaseDriverOracle extends F0FDatabaseDriverPdo
|
||||
{
|
||||
/**
|
||||
* The name of the database driver.
|
||||
*
|
||||
* @var string
|
||||
* @since 12.1
|
||||
*/
|
||||
public $name = 'oracle';
|
||||
|
||||
/**
|
||||
* The type of the database server family supported by this driver.
|
||||
*
|
||||
* @var string
|
||||
* @since CMS 3.5.0
|
||||
*/
|
||||
public $serverType = 'oracle';
|
||||
|
||||
/**
|
||||
* 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 12.1
|
||||
*/
|
||||
protected $nameQuote = '"';
|
||||
|
||||
/**
|
||||
* Returns the current dateformat
|
||||
*
|
||||
* @var string
|
||||
* @since 12.1
|
||||
*/
|
||||
protected $dateformat;
|
||||
|
||||
/**
|
||||
* Returns the current character set
|
||||
*
|
||||
* @var string
|
||||
* @since 12.1
|
||||
*/
|
||||
protected $charset;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $options List of options used to configure the connection
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function __construct($options)
|
||||
{
|
||||
$options['driver'] = 'oci';
|
||||
$options['charset'] = (isset($options['charset'])) ? $options['charset'] : 'AL32UTF8';
|
||||
$options['dateformat'] = (isset($options['dateformat'])) ? $options['dateformat'] : 'RRRR-MM-DD HH24:MI:SS';
|
||||
|
||||
$this->charset = $options['charset'];
|
||||
$this->dateformat = $options['dateformat'];
|
||||
|
||||
// Finalize initialisation
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->freeResult();
|
||||
unset($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the database if needed.
|
||||
*
|
||||
* @return void Returns void if the database connected successfully.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function connect()
|
||||
{
|
||||
if ($this->connection)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
parent::connect();
|
||||
|
||||
if (isset($this->options['schema']))
|
||||
{
|
||||
$this->setQuery('ALTER SESSION SET CURRENT_SCHEMA = ' . $this->quoteName($this->options['schema']))->execute();
|
||||
}
|
||||
|
||||
$this->setDateFormat($this->dateformat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects the database.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function disconnect()
|
||||
{
|
||||
// Close the connection.
|
||||
$this->freeResult();
|
||||
unset($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a table from the database.
|
||||
*
|
||||
* Note: The IF EXISTS flag is unused in the Oracle driver.
|
||||
*
|
||||
* @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 F0FDatabaseDriverOracle Returns this object to support chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function dropTable($tableName, $ifExists = true)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$query = $this->getQuery(true)
|
||||
->setQuery('DROP TABLE :tableName');
|
||||
$query->bind(':tableName', $tableName);
|
||||
|
||||
$this->setQuery($query);
|
||||
|
||||
$this->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 12.1
|
||||
*/
|
||||
public function getCollation()
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the database connection collation, as reported by the driver. If the connector doesn't support
|
||||
* reporting this value please return an empty string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getConnectionCollation()
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a query to run and verify the database is operational.
|
||||
*
|
||||
* @return string The query to check the health of the DB.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function getConnectedQuery()
|
||||
{
|
||||
return 'SELECT 1 FROM dual';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current date format
|
||||
* This method should be useful in the case that
|
||||
* somebody actually wants to use a different
|
||||
* date format and needs to check what the current
|
||||
* one is to see if it needs to be changed.
|
||||
*
|
||||
* @return string The current date format
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function getDateFormat()
|
||||
{
|
||||
return $this->dateformat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the table CREATE statement that creates the given tables.
|
||||
*
|
||||
* Note: You must have the correct privileges before this method
|
||||
* will return usable results!
|
||||
*
|
||||
* @param mixed $tables A table name or a list of table names.
|
||||
*
|
||||
* @return array A list of the create SQL for the tables.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableCreate($tables)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$result = array();
|
||||
$query = $this->getQuery(true)
|
||||
->select('dbms_metadata.get_ddl(:type, :tableName)')
|
||||
->from('dual')
|
||||
->bind(':type', 'TABLE');
|
||||
|
||||
// Sanitize input to an array and iterate over the list.
|
||||
settype($tables, 'array');
|
||||
|
||||
foreach ($tables as $table)
|
||||
{
|
||||
$query->bind(':tableName', $table);
|
||||
$this->setQuery($query);
|
||||
$statement = (string) $this->loadResult();
|
||||
$result[$table] = $statement;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableColumns($table, $typeOnly = true)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$columns = array();
|
||||
$query = $this->getQuery(true);
|
||||
|
||||
$fieldCasing = $this->getOption(PDO::ATTR_CASE);
|
||||
|
||||
$this->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
|
||||
|
||||
$table = strtoupper($table);
|
||||
|
||||
$query->select('*');
|
||||
$query->from('ALL_TAB_COLUMNS');
|
||||
$query->where('table_name = :tableName');
|
||||
|
||||
$prefixedTable = str_replace('#__', strtoupper($this->tablePrefix), $table);
|
||||
$query->bind(':tableName', $prefixedTable);
|
||||
$this->setQuery($query);
|
||||
$fields = $this->loadObjectList();
|
||||
|
||||
if ($typeOnly)
|
||||
{
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
$columns[$field->COLUMN_NAME] = $field->DATA_TYPE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
$columns[$field->COLUMN_NAME] = $field;
|
||||
$columns[$field->COLUMN_NAME]->Default = null;
|
||||
}
|
||||
}
|
||||
|
||||
$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 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableKeys($table)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$query = $this->getQuery(true);
|
||||
|
||||
$fieldCasing = $this->getOption(PDO::ATTR_CASE);
|
||||
|
||||
$this->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
|
||||
|
||||
$table = strtoupper($table);
|
||||
$query->select('*')
|
||||
->from('ALL_CONSTRAINTS')
|
||||
->where('table_name = :tableName')
|
||||
->bind(':tableName', $table);
|
||||
|
||||
$this->setQuery($query);
|
||||
$keys = $this->loadObjectList();
|
||||
|
||||
$this->setOption(PDO::ATTR_CASE, $fieldCasing);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get an array of all tables in the database (schema).
|
||||
*
|
||||
* @param string $databaseName The database (schema) name
|
||||
* @param boolean $includeDatabaseName Whether to include the schema name in the results
|
||||
*
|
||||
* @return array An array of all the tables in the database.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableList($databaseName = null, $includeDatabaseName = false)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$query = $this->getQuery(true);
|
||||
|
||||
if ($includeDatabaseName)
|
||||
{
|
||||
$query->select('owner, table_name');
|
||||
}
|
||||
else
|
||||
{
|
||||
$query->select('table_name');
|
||||
}
|
||||
|
||||
$query->from('all_tables');
|
||||
|
||||
if ($databaseName)
|
||||
{
|
||||
$query->where('owner = :database')
|
||||
->bind(':database', $databaseName);
|
||||
}
|
||||
|
||||
$query->order('table_name');
|
||||
|
||||
$this->setQuery($query);
|
||||
|
||||
if ($includeDatabaseName)
|
||||
{
|
||||
$tables = $this->loadAssocList();
|
||||
}
|
||||
else
|
||||
{
|
||||
$tables = $this->loadColumn();
|
||||
}
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of the database connector.
|
||||
*
|
||||
* @return string The database connector version.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$this->setQuery("select value from nls_database_parameters where parameter = 'NLS_RDBMS_VERSION'");
|
||||
|
||||
return $this->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 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function select($database)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Oracle Date Format for the session
|
||||
* Default date format for Oracle is = DD-MON-RR
|
||||
* The default date format for this driver is:
|
||||
* 'RRRR-MM-DD HH24:MI:SS' since it is the format
|
||||
* that matches the MySQL one used within most Joomla
|
||||
* tables.
|
||||
*
|
||||
* @param string $dateFormat Oracle Date Format String
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function setDateFormat($dateFormat = 'DD-MON-RR')
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$this->setQuery("ALTER SESSION SET NLS_DATE_FORMAT = '$dateFormat'");
|
||||
|
||||
if (!$this->execute())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->setQuery("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = '$dateFormat'");
|
||||
|
||||
if (!$this->execute())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->dateformat = $dateFormat;
|
||||
|
||||
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 12.1
|
||||
*/
|
||||
public function setUtf()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks a table in the database.
|
||||
*
|
||||
* @param string $table The name of the table to unlock.
|
||||
*
|
||||
* @return F0FDatabaseDriverOracle Returns this object to support chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function lockTable($table)
|
||||
{
|
||||
$this->setQuery('LOCK TABLE ' . $this->quoteName($table) . ' IN EXCLUSIVE MODE')->execute();
|
||||
|
||||
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 Oracle.
|
||||
* @param string $prefix Not used by Oracle.
|
||||
*
|
||||
* @return F0FDatabaseDriverOracle Returns this object to support chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
|
||||
{
|
||||
$this->setQuery('RENAME ' . $oldTable . ' TO ' . $newTable)->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks tables in the database.
|
||||
*
|
||||
* @return F0FDatabaseDriverOracle Returns this object to support chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function unlockTables()
|
||||
{
|
||||
$this->setQuery('COMMIT')->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to see if the PDO ODBC connector is available.
|
||||
*
|
||||
* @return boolean True on success, false otherwise.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
return class_exists('PDO') && in_array('oci', PDO::getAvailableDrivers());
|
||||
}
|
||||
|
||||
/**
|
||||
* This function replaces a string identifier <var>$prefix</var> with the string held is the
|
||||
* <var>tablePrefix</var> class variable.
|
||||
*
|
||||
* @param string $query The SQL statement to prepare.
|
||||
* @param string $prefix The common table prefix.
|
||||
*
|
||||
* @return string The processed SQL statement.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function replacePrefix($query, $prefix = '#__')
|
||||
{
|
||||
$startPos = 0;
|
||||
$quoteChar = "'";
|
||||
$literal = '';
|
||||
|
||||
$query = trim($query);
|
||||
$n = strlen($query);
|
||||
|
||||
while ($startPos < $n)
|
||||
{
|
||||
$ip = strpos($query, $prefix, $startPos);
|
||||
|
||||
if ($ip === false)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
$j = strpos($query, "'", $startPos);
|
||||
|
||||
if ($j === false)
|
||||
{
|
||||
$j = $n;
|
||||
}
|
||||
|
||||
$literal .= str_replace($prefix, $this->tablePrefix, substr($query, $startPos, $j - $startPos));
|
||||
$startPos = $j;
|
||||
|
||||
$j = $startPos + 1;
|
||||
|
||||
if ($j >= $n)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Quote comes first, find end of quote
|
||||
while (true)
|
||||
{
|
||||
$k = strpos($query, $quoteChar, $j);
|
||||
$escaped = false;
|
||||
|
||||
if ($k === false)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
$l = $k - 1;
|
||||
|
||||
while ($l >= 0 && $query{$l} == '\\')
|
||||
{
|
||||
$l--;
|
||||
$escaped = !$escaped;
|
||||
}
|
||||
|
||||
if ($escaped)
|
||||
{
|
||||
$j = $k + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if ($k === false)
|
||||
{
|
||||
// Error in the query - no end quote; ignore it
|
||||
break;
|
||||
}
|
||||
|
||||
$literal .= substr($query, $startPos, $k - $startPos + 1);
|
||||
$startPos = $k + 1;
|
||||
}
|
||||
|
||||
if ($startPos < $n)
|
||||
{
|
||||
$literal .= substr($query, $startPos, $n - $startPos);
|
||||
}
|
||||
|
||||
return $literal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to commit a transaction.
|
||||
*
|
||||
* @param boolean $toSavepoint If true, commit to the last savepoint.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.3
|
||||
* @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 12.3
|
||||
* @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 SAVEPOINT ' . $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
|
||||
*
|
||||
* @since 12.3
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function transactionStart($asSavepoint = false)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
if (!$asSavepoint || !$this->transactionDepth)
|
||||
{
|
||||
return parent::transactionStart($asSavepoint);
|
||||
}
|
||||
|
||||
$savepoint = 'SP_' . $this->transactionDepth;
|
||||
$this->setQuery('SAVEPOINT ' . $this->quoteName($savepoint));
|
||||
|
||||
if ($this->execute())
|
||||
{
|
||||
$this->transactionDepth++;
|
||||
}
|
||||
}
|
||||
}
|
||||
1106
libraries/f0f/database/driver/pdo.php
Normal file
1106
libraries/f0f/database/driver/pdo.php
Normal file
File diff suppressed because it is too large
Load Diff
559
libraries/f0f/database/driver/pdomysql.php
Normal file
559
libraries/f0f/database/driver/pdomysql.php
Normal file
@ -0,0 +1,559 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* MySQL database driver supporting PDO based connections
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Database
|
||||
* @see http://php.net/manual/en/ref.pdo-mysql.php
|
||||
* @since 3.4
|
||||
*/
|
||||
class F0FDatabaseDriverPdomysql extends F0FDatabaseDriverPdo
|
||||
{
|
||||
/**
|
||||
* The name of the database driver.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.4
|
||||
*/
|
||||
public $name = 'pdomysql';
|
||||
|
||||
/**
|
||||
* The type of the database server family supported by this driver.
|
||||
*
|
||||
* @var string
|
||||
* @since CMS 3.5.0
|
||||
*/
|
||||
public $serverType = 'mysql';
|
||||
|
||||
/**
|
||||
* 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 3.4
|
||||
*/
|
||||
protected $nameQuote = '`';
|
||||
|
||||
/**
|
||||
* The null or zero representation of a timestamp for the database driver. This should be
|
||||
* defined in child classes to hold the appropriate value for the engine.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.4
|
||||
*/
|
||||
protected $nullDate = '0000-00-00 00:00:00';
|
||||
|
||||
/**
|
||||
* The minimum supported database version.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.4
|
||||
*/
|
||||
protected static $dbMinimum = '5.0.4';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $options Array of database options with keys: host, user, password, database, select.
|
||||
*
|
||||
* @since 3.4
|
||||
*/
|
||||
public function __construct($options)
|
||||
{
|
||||
/**
|
||||
* Pre-populate the UTF-8 Multibyte compatibility flag. Unfortuantely PDO won't report the server version
|
||||
* unless we're connected to it and we cannot connect to it unless we know if it supports utf8mb4 which requires
|
||||
* us knowing the server version. Between this chicken and egg issue we _assume_ it's supported and we'll just
|
||||
* catch any problems at connection time.
|
||||
*/
|
||||
$this->utf8mb4 = true;
|
||||
|
||||
// Get some basic values from the options.
|
||||
$options['driver'] = 'mysql';
|
||||
$options['charset'] = (isset($options['charset'])) ? $options['charset'] : 'utf8';
|
||||
|
||||
if ($this->utf8mb4 && ($options['charset'] == 'utf8'))
|
||||
{
|
||||
$options['charset'] = 'utf8mb4';
|
||||
}
|
||||
|
||||
$this->charset = $options['charset'];
|
||||
|
||||
// Finalize initialisation.
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the database if needed.
|
||||
*
|
||||
* @return void Returns void if the database connected successfully.
|
||||
*
|
||||
* @since 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function connect()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Try to connect to MySQL
|
||||
parent::connect();
|
||||
}
|
||||
catch (\RuntimeException $e)
|
||||
{
|
||||
// If the connection failed but not because of the wrong character set bubble up the exception
|
||||
if (!$this->utf8mb4 || ($this->options['charset'] != 'utf8mb4'))
|
||||
{
|
||||
throw $e;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the connection failed and I was trying to use the utf8mb4 charset then it is likely that the server
|
||||
* doesn't support utf8mb4 despite claiming otherwise.
|
||||
*
|
||||
* This happens on old MySQL server versions (less than 5.5.3) using the mysqlnd PHP driver. Since mysqlnd
|
||||
* masks the server version and reports only its own we can not be sure if the server actually does support
|
||||
* UTF-8 Multibyte (i.e. it's MySQL 5.5.3 or later). Since the utf8mb4 charset is undefined in this case we
|
||||
* catch the error and determine that utf8mb4 is not supported!
|
||||
*/
|
||||
$this->utf8mb4 = false;
|
||||
$this->options['charset'] = 'utf8';
|
||||
|
||||
parent::connect();
|
||||
}
|
||||
|
||||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to see if the MySQL connector is available.
|
||||
*
|
||||
* @return boolean True on success, false otherwise.
|
||||
*
|
||||
* @since 3.4
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
return class_exists('PDO') && in_array('mysql', PDO::getAvailableDrivers());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 F0FDatabaseDriverPdomysql Returns this object to support chaining.
|
||||
*
|
||||
* @since 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function dropTable($tableName, $ifExists = true)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$query = $this->getQuery(true);
|
||||
|
||||
$query->setQuery('DROP TABLE ' . ($ifExists ? 'IF EXISTS ' : '') . $this->quoteName($tableName));
|
||||
|
||||
$this->setQuery($query);
|
||||
|
||||
$this->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.
|
||||
*
|
||||
* @since 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function select($database)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$this->setQuery('USE ' . $this->quoteName($database));
|
||||
|
||||
$this->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (string) or boolean false if not supported.
|
||||
*
|
||||
* @since 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getCollation()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
// Attempt to get the database collation by accessing the server system variable.
|
||||
$this->setQuery('SHOW VARIABLES LIKE "collation_database"');
|
||||
$result = $this->loadObject();
|
||||
|
||||
if (property_exists($result, 'Value'))
|
||||
{
|
||||
return $result->Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the database connection collation, as reported by the driver. If the connector doesn't support
|
||||
* reporting this value please return an empty string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getConnectionCollation()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
// Attempt to get the database collation by accessing the server system variable.
|
||||
$this->setQuery('SHOW VARIABLES LIKE "collation_connection"');
|
||||
$result = $this->loadObject();
|
||||
|
||||
if (property_exists($result, 'Value'))
|
||||
{
|
||||
return $result->Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @since 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableCreate($tables)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
// Initialise variables.
|
||||
$result = array();
|
||||
|
||||
// Sanitize input to an array and iterate over the list.
|
||||
settype($tables, 'array');
|
||||
|
||||
foreach ($tables as $table)
|
||||
{
|
||||
$this->setQuery('SHOW CREATE TABLE ' . $this->quoteName($table));
|
||||
|
||||
$row = $this->loadRow();
|
||||
|
||||
// Populate the result array based on the create statements.
|
||||
$result[$table] = $row[1];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableColumns($table, $typeOnly = true)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$result = array();
|
||||
|
||||
// Set the query to get the table fields statement.
|
||||
$this->setQuery('SHOW FULL COLUMNS FROM ' . $this->quoteName($table));
|
||||
|
||||
$fields = $this->loadObjectList();
|
||||
|
||||
// If we only want the type as the value add just that to the list.
|
||||
if ($typeOnly)
|
||||
{
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
$result[$field->Field] = preg_replace("/[(0-9)]/", '', $field->Type);
|
||||
}
|
||||
}
|
||||
// If we want the whole field data object add that to the list.
|
||||
else
|
||||
{
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
$result[$field->Field] = $field;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableKeys($table)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
// Get the details columns information.
|
||||
$this->setQuery('SHOW KEYS FROM ' . $this->quoteName($table));
|
||||
|
||||
$keys = $this->loadObjectList();
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get an array of all tables in the database.
|
||||
*
|
||||
* @return array An array of all the tables in the database.
|
||||
*
|
||||
* @since 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableList()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
// Set the query to get the tables statement.
|
||||
$this->setQuery('SHOW TABLES');
|
||||
$tables = $this->loadColumn();
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of the database connector.
|
||||
*
|
||||
* @return string The database connector version.
|
||||
*
|
||||
* @since 3.4
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
return $this->getOption(PDO::ATTR_SERVER_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks a table in the database.
|
||||
*
|
||||
* @param string $table The name of the table to unlock.
|
||||
*
|
||||
* @return F0FDatabaseDriverPdomysql Returns this object to support chaining.
|
||||
*
|
||||
* @since 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function lockTable($table)
|
||||
{
|
||||
$this->setQuery('LOCK TABLES ' . $this->quoteName($table) . ' WRITE')->execute();
|
||||
|
||||
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 MySQL.
|
||||
* @param string $prefix Not used by MySQL.
|
||||
*
|
||||
* @return F0FDatabaseDriverPdomysql Returns this object to support chaining.
|
||||
*
|
||||
* @since 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
|
||||
{
|
||||
$this->setQuery('RENAME TABLE ' . $this->quoteName($oldTable) . ' TO ' . $this->quoteName($newTable));
|
||||
|
||||
$this->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to escape a string for usage in an SQL statement.
|
||||
*
|
||||
* Oracle escaping reference:
|
||||
* http://www.orafaq.com/wiki/SQL_FAQ#How_does_one_escape_special_characters_when_writing_SQL_queries.3F
|
||||
*
|
||||
* SQLite escaping notes:
|
||||
* http://www.sqlite.org/faq.html#q14
|
||||
*
|
||||
* Method body is as implemented by the Zend Framework
|
||||
*
|
||||
* 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 3.4
|
||||
*/
|
||||
public function escape($text, $extra = false)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
if (is_int($text) || is_float($text))
|
||||
{
|
||||
return $text;
|
||||
}
|
||||
|
||||
$result = substr($this->connection->quote($text), 1, -1);
|
||||
|
||||
if ($extra)
|
||||
{
|
||||
$result = addcslashes($result, '%_');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks tables in the database.
|
||||
*
|
||||
* @return F0FDatabaseDriverPdomysql Returns this object to support chaining.
|
||||
*
|
||||
* @since 3.4
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function unlockTables()
|
||||
{
|
||||
$this->setQuery('UNLOCK TABLES')->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to commit a transaction.
|
||||
*
|
||||
* @param boolean $toSavepoint If true, commit to the last savepoint.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.4
|
||||
* @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 3.4
|
||||
* @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 SAVEPOINT ' . $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
|
||||
*
|
||||
* @since 3.4
|
||||
* @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));
|
||||
|
||||
if ($this->execute())
|
||||
{
|
||||
$this->transactionDepth++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1529
libraries/f0f/database/driver/postgresql.php
Normal file
1529
libraries/f0f/database/driver/postgresql.php
Normal file
File diff suppressed because it is too large
Load Diff
30
libraries/f0f/database/driver/sqlazure.php
Normal file
30
libraries/f0f/database/driver/sqlazure.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* SQL Server database driver
|
||||
*
|
||||
* @see http://msdn.microsoft.com/en-us/library/ee336279.aspx
|
||||
* @since 12.1
|
||||
*/
|
||||
class F0FDatabaseDriverSqlazure extends F0FDatabaseDriverSqlsrv
|
||||
{
|
||||
/**
|
||||
* The name of the database driver.
|
||||
*
|
||||
* @var string
|
||||
* @since 12.1
|
||||
*/
|
||||
public $name = 'sqlazure';
|
||||
}
|
||||
484
libraries/f0f/database/driver/sqlite.php
Normal file
484
libraries/f0f/database/driver/sqlite.php
Normal file
@ -0,0 +1,484 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* SQLite database driver
|
||||
*
|
||||
* @see http://php.net/pdo
|
||||
* @since 12.1
|
||||
*/
|
||||
class F0FDatabaseDriverSqlite extends F0FDatabaseDriverPdo
|
||||
{
|
||||
/**
|
||||
* The name of the database driver.
|
||||
*
|
||||
* @var string
|
||||
* @since 12.1
|
||||
*/
|
||||
public $name = 'sqlite';
|
||||
|
||||
/**
|
||||
* The type of the database server family supported by this driver.
|
||||
*
|
||||
* @var string
|
||||
* @since CMS 3.5.0
|
||||
*/
|
||||
public $serverType = 'sqlite';
|
||||
|
||||
/**
|
||||
* 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 12.1
|
||||
*/
|
||||
protected $nameQuote = '`';
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->freeResult();
|
||||
unset($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects the database.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
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 F0FDatabaseDriverSqlite Returns this object to support chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function dropTable($tableName, $ifExists = true)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$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 12.1
|
||||
*/
|
||||
public function escape($text, $extra = false)
|
||||
{
|
||||
if (is_int($text) || is_float($text))
|
||||
{
|
||||
return $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 mixed The collation in use by the database or boolean false if not supported.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function getCollation()
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the database connection collation, as reported by the driver. If the connector doesn't support
|
||||
* reporting this value please return an empty string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getConnectionCollation()
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableCreate($tables)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
// Sanitize input to an array and iterate over the list.
|
||||
settype($tables, 'array');
|
||||
|
||||
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 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableColumns($table, $typeOnly = true)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$columns = array();
|
||||
$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.
|
||||
// TODO: Come up with and implement a standard across databases.
|
||||
$columns[$field->NAME] = (object) array(
|
||||
'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 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableKeys($table)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$keys = array();
|
||||
$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.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getTableList()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$type = 'table';
|
||||
|
||||
$query = $this->getQuery(true)
|
||||
->select('name')
|
||||
->from('sqlite_master')
|
||||
->where('type = :type')
|
||||
->bind(':type', $type)
|
||||
->order('name');
|
||||
|
||||
$this->setQuery($query);
|
||||
|
||||
$tables = $this->loadColumn();
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of the database connector.
|
||||
*
|
||||
* @return string The database connector version.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$this->setQuery("SELECT sqlite_version()");
|
||||
|
||||
return $this->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 12.1
|
||||
* @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 12.1
|
||||
*/
|
||||
public function setUtf()
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks a table in the database.
|
||||
*
|
||||
* @param string $table The name of the table to unlock.
|
||||
*
|
||||
* @return F0FDatabaseDriverSqlite Returns this object to support chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
* @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 F0FDatabaseDriverSqlite Returns this object to support chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
|
||||
{
|
||||
$this->setQuery('ALTER TABLE ' . $oldTable . ' RENAME TO ' . $newTable)->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks tables in the database.
|
||||
*
|
||||
* @return F0FDatabaseDriverSqlite Returns this object to support chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
* @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 12.1
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
return class_exists('PDO') && in_array('sqlite', PDO::getAvailableDrivers());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to commit a transaction.
|
||||
*
|
||||
* @param boolean $toSavepoint If true, commit to the last savepoint.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.3
|
||||
* @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 12.3
|
||||
* @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));
|
||||
|
||||
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
|
||||
*
|
||||
* @since 12.3
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function transactionStart($asSavepoint = false)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
if (!$asSavepoint || !$this->transactionDepth)
|
||||
{
|
||||
parent::transactionStart($asSavepoint);
|
||||
}
|
||||
|
||||
$savepoint = 'SP_' . $this->transactionDepth;
|
||||
$this->setQuery('SAVEPOINT ' . $this->quoteName($savepoint));
|
||||
|
||||
if ($this->execute())
|
||||
{
|
||||
$this->transactionDepth++;
|
||||
}
|
||||
}
|
||||
}
|
||||
1171
libraries/f0f/database/driver/sqlsrv.php
Normal file
1171
libraries/f0f/database/driver/sqlsrv.php
Normal file
File diff suppressed because it is too large
Load Diff
127
libraries/f0f/database/factory.php
Normal file
127
libraries/f0f/database/factory.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Joomla Platform Database Factory class
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
class F0FDatabaseFactory
|
||||
{
|
||||
/**
|
||||
* Contains the current F0FDatabaseFactory instance
|
||||
*
|
||||
* @var F0FDatabaseFactory
|
||||
* @since 12.1
|
||||
*/
|
||||
private static $_instance = null;
|
||||
|
||||
/**
|
||||
* Method to return a F0FDatabaseDriver instance based on the given options. There are three global options and then
|
||||
* the rest are specific to the database driver. The 'database' option determines which database is to
|
||||
* be used for the connection. The 'select' option determines whether the connector should automatically select
|
||||
* the chosen database.
|
||||
*
|
||||
* Instances are unique to the given options and new objects are only created when a unique options array is
|
||||
* passed into the method. This ensures that we don't end up with unnecessary database connection resources.
|
||||
*
|
||||
* @param string $name Name of the database driver you'd like to instantiate
|
||||
* @param array $options Parameters to be passed to the database driver.
|
||||
*
|
||||
* @return F0FDatabaseDriver A database driver object.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getDriver($name = 'joomla', $options = array())
|
||||
{
|
||||
// Sanitize the database connector options.
|
||||
$options['driver'] = preg_replace('/[^A-Z0-9_\.-]/i', '', $name);
|
||||
$options['database'] = (isset($options['database'])) ? $options['database'] : null;
|
||||
$options['select'] = (isset($options['select'])) ? $options['select'] : true;
|
||||
|
||||
// Derive the class name from the driver.
|
||||
$class = 'F0FDatabaseDriver' . ucfirst(strtolower($options['driver']));
|
||||
|
||||
// If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
|
||||
if (!class_exists($class))
|
||||
{
|
||||
throw new RuntimeException(sprintf('Unable to load Database Driver: %s', $options['driver']));
|
||||
}
|
||||
|
||||
// Create our new F0FDatabaseDriver connector based on the options given.
|
||||
try
|
||||
{
|
||||
$instance = new $class($options);
|
||||
}
|
||||
catch (RuntimeException $e)
|
||||
{
|
||||
throw new RuntimeException(sprintf('Unable to connect to the Database: %s', $e->getMessage()), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an instance of the factory object.
|
||||
*
|
||||
* @return F0FDatabaseFactory
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
return self::$_instance ? self::$_instance : new F0FDatabaseFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current query object or a new F0FDatabaseQuery object.
|
||||
*
|
||||
* @param string $name Name of the driver you want an query object for.
|
||||
* @param F0FDatabaseDriver $db Optional F0FDatabaseDriver instance
|
||||
*
|
||||
* @return F0FDatabaseQuery The current query object or a new object extending the F0FDatabaseQuery class.
|
||||
*
|
||||
* @since 12.1
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getQuery($name, F0FDatabaseDriver $db = null)
|
||||
{
|
||||
// Derive the class name from the driver.
|
||||
$class = 'F0FDatabaseQuery' . ucfirst(strtolower($name));
|
||||
|
||||
// Make sure we have a query class for this driver.
|
||||
if (!class_exists($class))
|
||||
{
|
||||
// If it doesn't exist we are at an impasse so throw an exception.
|
||||
throw new RuntimeException('Database Query class not found');
|
||||
}
|
||||
|
||||
return new $class($db);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an instance of a factory object to return on subsequent calls of getInstance.
|
||||
*
|
||||
* @param F0FDatabaseFactory $instance A F0FDatabaseFactory object.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public static function setInstance(F0FDatabaseFactory $instance = null)
|
||||
{
|
||||
self::$_instance = $instance;
|
||||
}
|
||||
}
|
||||
1020
libraries/f0f/database/installer.php
Normal file
1020
libraries/f0f/database/installer.php
Normal file
File diff suppressed because it is too large
Load Diff
37
libraries/f0f/database/interface.php
Normal file
37
libraries/f0f/database/interface.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
if (!interface_exists('JDatabaseInterface'))
|
||||
{
|
||||
/**
|
||||
* Joomla Platform Database Interface
|
||||
*
|
||||
* @since 11.2
|
||||
*/
|
||||
interface JDatabaseInterface
|
||||
{
|
||||
/**
|
||||
* Test to see if the connector is available.
|
||||
*
|
||||
* @return boolean True on success, false otherwise.
|
||||
*
|
||||
* @since 11.2
|
||||
*/
|
||||
public static function isSupported();
|
||||
}
|
||||
}
|
||||
|
||||
interface F0FDatabaseInterface extends JDatabaseInterface
|
||||
{
|
||||
}
|
||||
251
libraries/f0f/database/iterator.php
Normal file
251
libraries/f0f/database/iterator.php
Normal file
@ -0,0 +1,251 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Database iterator
|
||||
*/
|
||||
abstract class F0FDatabaseIterator implements Iterator
|
||||
{
|
||||
/**
|
||||
* The database cursor.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $cursor;
|
||||
|
||||
/**
|
||||
* The class of object to create.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $class;
|
||||
|
||||
/**
|
||||
* The name of the column to use for the key of the database record.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
private $_column;
|
||||
|
||||
/**
|
||||
* The current database record.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
private $_current;
|
||||
|
||||
/**
|
||||
* The current database record as a F0FTable object.
|
||||
*
|
||||
* @var F0FTable
|
||||
*/
|
||||
private $_currentTable;
|
||||
|
||||
/**
|
||||
* A numeric or string key for the current database record.
|
||||
*
|
||||
* @var scalar
|
||||
*/
|
||||
private $_key;
|
||||
|
||||
/**
|
||||
* The number of fetched records.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $_fetched = 0;
|
||||
|
||||
/**
|
||||
* A F0FTable object created using the class type $class, used by getTable
|
||||
*
|
||||
* @var F0FTable
|
||||
*/
|
||||
private $_tableObject = null;
|
||||
|
||||
/**
|
||||
* Returns an iterator object for a specific database type
|
||||
*
|
||||
* @param string $dbName The database type, e.g. mysql, mysqli, sqlazure etc.
|
||||
* @param mixed $cursor The database cursor
|
||||
* @param string $column An option column to use as the iterator key
|
||||
* @param string $class The table class of the returned objects
|
||||
* @param array $config Configuration parameters to push to the table class
|
||||
*
|
||||
* @return F0FDatabaseIterator
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function &getIterator($dbName, $cursor, $column = null, $class, $config = array())
|
||||
{
|
||||
$className = 'F0FDatabaseIterator' . ucfirst($dbName);
|
||||
|
||||
$object = new $className($cursor, $column, $class, $config);
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Database iterator constructor.
|
||||
*
|
||||
* @param mixed $cursor The database cursor.
|
||||
* @param string $column An option column to use as the iterator key.
|
||||
* @param string $class The table class of the returned objects.
|
||||
* @param array $config Configuration parameters to push to the table class
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function __construct($cursor, $column = null, $class, $config = array())
|
||||
{
|
||||
// Figure out the type and prefix of the class by the class name
|
||||
$parts = F0FInflector::explode($class);
|
||||
|
||||
if(count($parts) != 3)
|
||||
{
|
||||
throw new InvalidArgumentException('Invalid table name, expected a pattern like ComponentTableFoobar got '.$class);
|
||||
}
|
||||
|
||||
$this->_tableObject = F0FTable::getInstance($parts[2], ucfirst($parts[0]) . ucfirst($parts[1]))->getClone();
|
||||
|
||||
$this->cursor = $cursor;
|
||||
$this->class = 'stdClass';
|
||||
$this->_column = $column;
|
||||
$this->_fetched = 0;
|
||||
|
||||
$this->next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Database iterator destructor.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->cursor)
|
||||
{
|
||||
$this->freeResult($this->cursor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The current element in the iterator.
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @see Iterator::current()
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->_currentTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* The key of the current element in the iterator.
|
||||
*
|
||||
* @return scalar
|
||||
*
|
||||
* @see Iterator::key()
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves forward to the next result from the SQL query.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @see Iterator::next()
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
// Set the default key as being the number of fetched object
|
||||
$this->_key = $this->_fetched;
|
||||
|
||||
// Try to get an object
|
||||
$this->_current = $this->fetchObject();
|
||||
|
||||
// If an object has been found
|
||||
if ($this->_current)
|
||||
{
|
||||
$this->_currentTable = $this->getTable();
|
||||
|
||||
// Set the key as being the indexed column (if it exists)
|
||||
if (isset($this->_current->{$this->_column}))
|
||||
{
|
||||
$this->_key = $this->_current->{$this->_column};
|
||||
}
|
||||
|
||||
// Update the number of fetched object
|
||||
$this->_fetched++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewinds the iterator.
|
||||
*
|
||||
* This iterator cannot be rewound.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @see Iterator::rewind()
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current position of the iterator is valid.
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @see Iterator::valid()
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return (boolean) $this->_current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an object.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
abstract protected function fetchObject();
|
||||
|
||||
/**
|
||||
* Method to free up the memory used for the result set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function freeResult();
|
||||
|
||||
/**
|
||||
* Returns the data in $this->_current as a F0FTable instance
|
||||
*
|
||||
* @return F0FTable
|
||||
*
|
||||
* @throws OutOfBoundsException
|
||||
*/
|
||||
protected function getTable()
|
||||
{
|
||||
if (!$this->valid())
|
||||
{
|
||||
throw new OutOfBoundsException('Cannot get item past iterator\'s bounds', 500);
|
||||
}
|
||||
|
||||
$this->_tableObject->bind($this->_current);
|
||||
|
||||
return $this->_tableObject;
|
||||
}
|
||||
}
|
||||
20
libraries/f0f/database/iterator/azure.php
Normal file
20
libraries/f0f/database/iterator/azure.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* SQL azure database iterator.
|
||||
*/
|
||||
class F0FDatabaseIteratorAzure extends F0FDatabaseIteratorSqlsrv
|
||||
{
|
||||
}
|
||||
51
libraries/f0f/database/iterator/mysql.php
Normal file
51
libraries/f0f/database/iterator/mysql.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* MySQL database iterator.
|
||||
*/
|
||||
class F0FDatabaseIteratorMysql extends F0FDatabaseIterator
|
||||
{
|
||||
/**
|
||||
* Get the number of rows in the result set for the executed SQL given by the cursor.
|
||||
*
|
||||
* @return integer The number of rows in the result set.
|
||||
*
|
||||
* @see Countable::count()
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return @mysql_num_rows($this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an object.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchObject()
|
||||
{
|
||||
return @mysql_fetch_object($this->cursor, $this->class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to free up the memory used for the result set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function freeResult()
|
||||
{
|
||||
@mysql_free_result($this->cursor);
|
||||
}
|
||||
}
|
||||
51
libraries/f0f/database/iterator/mysqli.php
Normal file
51
libraries/f0f/database/iterator/mysqli.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* MySQLi database iterator.
|
||||
*/
|
||||
class F0FDatabaseIteratorMysqli extends F0FDatabaseIterator
|
||||
{
|
||||
/**
|
||||
* Get the number of rows in the result set for the executed SQL given by the cursor.
|
||||
*
|
||||
* @return integer The number of rows in the result set.
|
||||
*
|
||||
* @see Countable::count()
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return @mysqli_num_rows($this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an object.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchObject()
|
||||
{
|
||||
return @mysqli_fetch_object($this->cursor, $this->class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to free up the memory used for the result set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function freeResult()
|
||||
{
|
||||
@mysqli_free_result($this->cursor);
|
||||
}
|
||||
}
|
||||
20
libraries/f0f/database/iterator/oracle.php
Normal file
20
libraries/f0f/database/iterator/oracle.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* MySQLi database iterator.
|
||||
*/
|
||||
class F0FDatabaseIteratorOracle extends F0FDatabaseIteratorPdo
|
||||
{
|
||||
}
|
||||
68
libraries/f0f/database/iterator/pdo.php
Normal file
68
libraries/f0f/database/iterator/pdo.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* PDO database iterator.
|
||||
*/
|
||||
class F0FDatabaseIteratorPdo extends F0FDatabaseIterator
|
||||
{
|
||||
/**
|
||||
* Get the number of rows in the result set for the executed SQL given by the cursor.
|
||||
*
|
||||
* @return integer The number of rows in the result set.
|
||||
*
|
||||
* @see Countable::count()
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
if (!empty($this->cursor) && $this->cursor instanceof PDOStatement)
|
||||
{
|
||||
return @$this->cursor->rowCount();
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an object.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchObject()
|
||||
{
|
||||
if (!empty($this->cursor) && $this->cursor instanceof PDOStatement)
|
||||
{
|
||||
return @$this->cursor->fetchObject($this->class);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to free up the memory used for the result set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function freeResult()
|
||||
{
|
||||
if (!empty($this->cursor) && $this->cursor instanceof PDOStatement)
|
||||
{
|
||||
@$this->cursor->closeCursor();
|
||||
}
|
||||
}
|
||||
}
|
||||
20
libraries/f0f/database/iterator/pdomysql.php
Normal file
20
libraries/f0f/database/iterator/pdomysql.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* MySQLi database iterator.
|
||||
*/
|
||||
class F0FDatabaseIteratorPdomysql extends F0FDatabaseIteratorPdo
|
||||
{
|
||||
}
|
||||
51
libraries/f0f/database/iterator/postgresql.php
Normal file
51
libraries/f0f/database/iterator/postgresql.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* PostgreSQL database iterator.
|
||||
*/
|
||||
class F0FDatabaseIteratorPostgresql extends F0FDatabaseIterator
|
||||
{
|
||||
/**
|
||||
* Get the number of rows in the result set for the executed SQL given by the cursor.
|
||||
*
|
||||
* @return integer The number of rows in the result set.
|
||||
*
|
||||
* @see Countable::count()
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return @pg_num_rows($this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an object.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchObject()
|
||||
{
|
||||
return @pg_fetch_object($this->cursor, null, $this->class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to free up the memory used for the result set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function freeResult()
|
||||
{
|
||||
@pg_free_result($this->cursor);
|
||||
}
|
||||
}
|
||||
20
libraries/f0f/database/iterator/sqlite.php
Normal file
20
libraries/f0f/database/iterator/sqlite.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* MySQLi database iterator.
|
||||
*/
|
||||
class F0FDatabaseIteratorSqlite extends F0FDatabaseIteratorPdo
|
||||
{
|
||||
}
|
||||
51
libraries/f0f/database/iterator/sqlsrv.php
Normal file
51
libraries/f0f/database/iterator/sqlsrv.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* SQL server database iterator.
|
||||
*/
|
||||
class F0FDatabaseIteratorSqlsrv extends F0FDatabaseIterator
|
||||
{
|
||||
/**
|
||||
* Get the number of rows in the result set for the executed SQL given by the cursor.
|
||||
*
|
||||
* @return integer The number of rows in the result set.
|
||||
*
|
||||
* @see Countable::count()
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return @sqlsrv_num_rows($this->cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch a row from the result set cursor as an object.
|
||||
*
|
||||
* @return mixed Either the next row from the result set or false if there are no more rows.
|
||||
*/
|
||||
protected function fetchObject()
|
||||
{
|
||||
return @sqlsrv_fetch_object($this->cursor, $this->class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to free up the memory used for the result set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function freeResult()
|
||||
{
|
||||
@sqlsrv_free_stmt($this->cursor);
|
||||
}
|
||||
}
|
||||
1732
libraries/f0f/database/query.php
Normal file
1732
libraries/f0f/database/query.php
Normal file
File diff suppressed because it is too large
Load Diff
132
libraries/f0f/database/query/element.php
Normal file
132
libraries/f0f/database/query/element.php
Normal file
@ -0,0 +1,132 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Query Element Class.
|
||||
*
|
||||
* @property-read string $name The name of the element.
|
||||
* @property-read array $elements An array of elements.
|
||||
* @property-read string $glue Glue piece.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
class F0FDatabaseQueryElement
|
||||
{
|
||||
/**
|
||||
* @var string The name of the element.
|
||||
* @since 11.1
|
||||
*/
|
||||
protected $name = null;
|
||||
|
||||
/**
|
||||
* @var array An array of elements.
|
||||
* @since 11.1
|
||||
*/
|
||||
protected $elements = null;
|
||||
|
||||
/**
|
||||
* @var string Glue piece.
|
||||
* @since 11.1
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function __construct($name, $elements, $glue = ',')
|
||||
{
|
||||
$this->elements = array();
|
||||
$this->name = $name;
|
||||
$this->glue = $glue;
|
||||
|
||||
$this->append($elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function to convert the query element to a string.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function append($elements)
|
||||
{
|
||||
if (is_array($elements))
|
||||
{
|
||||
$this->elements = array_merge($this->elements, $elements);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->elements = array_merge($this->elements, array($elements));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the elements of this element.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function getElements()
|
||||
{
|
||||
return $this->elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to provide deep copy support to nested objects and arrays
|
||||
* when cloning.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
foreach ($this as $k => $v)
|
||||
{
|
||||
if (is_object($v) || is_array($v))
|
||||
{
|
||||
$this->{$k} = unserialize(serialize($v));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
72
libraries/f0f/database/query/limitable.php
Normal file
72
libraries/f0f/database/query/limitable.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
if (!interface_exists('JDatabaseQueryLimitable'))
|
||||
{
|
||||
/**
|
||||
* Joomla Database 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.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
interface JDatabaseQueryLimitable
|
||||
{
|
||||
/**
|
||||
* 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 F0FDatabaseQueryLimitable 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 F0FDatabaseQuery Returns this object to allow chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function setLimit($limit = 0, $offset = 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Joomla Database 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.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
interface F0FDatabaseQueryLimitable extends JDatabaseQueryLimitable
|
||||
{
|
||||
}
|
||||
23
libraries/f0f/database/query/mysql.php
Normal file
23
libraries/f0f/database/query/mysql.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Query Building Class.
|
||||
*
|
||||
* @since 11.1
|
||||
* @deprecated Will be removed when the minimum supported PHP version no longer includes the deprecated PHP `mysql` extension
|
||||
*/
|
||||
class F0FDatabaseQueryMysql extends F0FDatabaseQueryMysqli
|
||||
{
|
||||
}
|
||||
143
libraries/f0f/database/query/mysqli.php
Normal file
143
libraries/f0f/database/query/mysqli.php
Normal file
@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Query Building Class.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
class F0FDatabaseQueryMysqli extends F0FDatabaseQuery implements F0FDatabaseQueryLimitable
|
||||
{
|
||||
/**
|
||||
* @var integer The offset for the result set.
|
||||
* @since 12.1
|
||||
*/
|
||||
protected $offset;
|
||||
|
||||
/**
|
||||
* @var integer The limit for the result set.
|
||||
* @since 12.1
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function processLimit($query, $limit, $offset = 0)
|
||||
{
|
||||
if ($limit > 0 || $offset > 0)
|
||||
{
|
||||
$query .= ' LIMIT ' . $offset . ', ' . $limit;
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
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) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 F0FDatabaseQuery Returns this object to allow chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function setLimit($limit = 0, $offset = 0)
|
||||
{
|
||||
$this->limit = (int) $limit;
|
||||
$this->offset = (int) $offset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return correct regexp operator for mysqli.
|
||||
*
|
||||
* Ensure that the regexp operator is mysqli compatible.
|
||||
*
|
||||
* Usage:
|
||||
* $query->where('field ' . $query->regexp($search));
|
||||
*
|
||||
* @param string $value The regex pattern.
|
||||
*
|
||||
* @return string Returns the regex operator.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function regexp($value)
|
||||
{
|
||||
return ' REGEXP ' . $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return correct rand() function for Mysql.
|
||||
*
|
||||
* Ensure that the rand() function is Mysql compatible.
|
||||
*
|
||||
* Usage:
|
||||
* $query->Rand();
|
||||
*
|
||||
* @return string The correct rand function.
|
||||
*
|
||||
* @since 3.5
|
||||
*/
|
||||
public function Rand()
|
||||
{
|
||||
return ' RAND() ';
|
||||
}
|
||||
}
|
||||
205
libraries/f0f/database/query/oracle.php
Normal file
205
libraries/f0f/database/query/oracle.php
Normal file
@ -0,0 +1,205 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Oracle Query Building Class.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
class F0FDatabaseQueryOracle extends F0FDatabaseQueryPdo implements F0FDatabaseQueryPreparable, F0FDatabaseQueryLimitable
|
||||
{
|
||||
/**
|
||||
* @var integer The offset for the result set.
|
||||
* @since 12.1
|
||||
*/
|
||||
protected $offset;
|
||||
|
||||
/**
|
||||
* @var integer The limit for the result set.
|
||||
* @since 12.1
|
||||
*/
|
||||
protected $limit;
|
||||
|
||||
/**
|
||||
* @var array Bounded object array
|
||||
* @since 12.1
|
||||
*/
|
||||
protected $bounded = array();
|
||||
|
||||
/**
|
||||
* 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 F0FDatabaseQueryOracle
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function bind($key = null, &$value = null, $dataType = PDO::PARAM_STR, $length = 0, $driverOptions = array())
|
||||
{
|
||||
// Case 1: Empty Key (reset $bounded array)
|
||||
if (empty($key))
|
||||
{
|
||||
$this->bounded = array();
|
||||
|
||||
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 12.1
|
||||
*/
|
||||
public function &getBounded($key = null)
|
||||
{
|
||||
if (empty($key))
|
||||
{
|
||||
return $this->bounded;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($this->bounded[$key]))
|
||||
{
|
||||
return $this->bounded[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 F0FDatabaseQueryOracle Returns this object to allow chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function clear($clause = null)
|
||||
{
|
||||
switch ($clause)
|
||||
{
|
||||
case null:
|
||||
$this->bounded = array();
|
||||
break;
|
||||
}
|
||||
|
||||
parent::clear($clause);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 F0FDatabaseQueryLimitable 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)
|
||||
{
|
||||
// Check if we need to mangle the query.
|
||||
if ($limit || $offset)
|
||||
{
|
||||
$query = "SELECT joomla2.*
|
||||
FROM (
|
||||
SELECT joomla1.*, ROWNUM AS joomla_db_rownum
|
||||
FROM (
|
||||
" . $query . "
|
||||
) joomla1
|
||||
) joomla2";
|
||||
|
||||
// Check if the limit value is greater than zero.
|
||||
if ($limit > 0)
|
||||
{
|
||||
$query .= ' WHERE joomla2.joomla_db_rownum BETWEEN ' . ($offset + 1) . ' AND ' . ($offset + $limit);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check if there is an offset and then use this.
|
||||
if ($offset)
|
||||
{
|
||||
$query .= ' WHERE joomla2.joomla_db_rownum > ' . ($offset + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 F0FDatabaseQueryOracle Returns this object to allow chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function setLimit($limit = 0, $offset = 0)
|
||||
{
|
||||
$this->limit = (int) $limit;
|
||||
$this->offset = (int) $offset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
22
libraries/f0f/database/query/pdo.php
Normal file
22
libraries/f0f/database/query/pdo.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* PDO Query Building Class.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
class F0FDatabaseQueryPdo extends F0FDatabaseQuery
|
||||
{
|
||||
}
|
||||
24
libraries/f0f/database/query/pdomysql.php
Normal file
24
libraries/f0f/database/query/pdomysql.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Query Building Class.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Database
|
||||
* @since 3.4
|
||||
*/
|
||||
class F0FDatabaseQueryPdomysql extends F0FDatabaseQueryMysqli
|
||||
{
|
||||
}
|
||||
661
libraries/f0f/database/query/postgresql.php
Normal file
661
libraries/f0f/database/query/postgresql.php
Normal file
@ -0,0 +1,661 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Query Building Class.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
class F0FDatabaseQueryPostgresql extends F0FDatabaseQuery implements F0FDatabaseQueryLimitable
|
||||
{
|
||||
/**
|
||||
* @var object The FOR UPDATE element used in "FOR UPDATE" lock
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $forUpdate = null;
|
||||
|
||||
/**
|
||||
* @var object The FOR SHARE element used in "FOR SHARE" lock
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $forShare = null;
|
||||
|
||||
/**
|
||||
* @var object The NOWAIT element used in "FOR SHARE" and "FOR UPDATE" lock
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $noWait = null;
|
||||
|
||||
/**
|
||||
* @var object The LIMIT element
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $limit = null;
|
||||
|
||||
/**
|
||||
* @var object The OFFSET element
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $offset = null;
|
||||
|
||||
/**
|
||||
* @var object The RETURNING element of INSERT INTO
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $returning = null;
|
||||
|
||||
/**
|
||||
* Magic function to convert the query to a string, only for postgresql specific query
|
||||
*
|
||||
* @return string The completed query.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$query = '';
|
||||
|
||||
switch ($this->type)
|
||||
{
|
||||
case 'select':
|
||||
$query .= (string) $this->select;
|
||||
$query .= (string) $this->from;
|
||||
|
||||
if ($this->join)
|
||||
{
|
||||
// Special case for joins
|
||||
foreach ($this->join as $join)
|
||||
{
|
||||
$query .= (string) $join;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->where)
|
||||
{
|
||||
$query .= (string) $this->where;
|
||||
}
|
||||
|
||||
if ($this->group)
|
||||
{
|
||||
$query .= (string) $this->group;
|
||||
}
|
||||
|
||||
if ($this->having)
|
||||
{
|
||||
$query .= (string) $this->having;
|
||||
}
|
||||
|
||||
if ($this->order)
|
||||
{
|
||||
$query .= (string) $this->order;
|
||||
}
|
||||
|
||||
if ($this->forUpdate)
|
||||
{
|
||||
$query .= (string) $this->forUpdate;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($this->forShare)
|
||||
{
|
||||
$query .= (string) $this->forShare;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->noWait)
|
||||
{
|
||||
$query .= (string) $this->noWait;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
$query .= (string) $this->update;
|
||||
$query .= (string) $this->set;
|
||||
|
||||
if ($this->join)
|
||||
{
|
||||
$onWord = ' ON ';
|
||||
|
||||
// Workaround for special case of JOIN with UPDATE
|
||||
foreach ($this->join as $join)
|
||||
{
|
||||
$joinElem = $join->getElements();
|
||||
|
||||
$joinArray = explode($onWord, $joinElem[0]);
|
||||
|
||||
$this->from($joinArray[0]);
|
||||
$this->where($joinArray[1]);
|
||||
}
|
||||
|
||||
$query .= (string) $this->from;
|
||||
}
|
||||
|
||||
if ($this->where)
|
||||
{
|
||||
$query .= (string) $this->where;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'insert':
|
||||
$query .= (string) $this->insert;
|
||||
|
||||
if ($this->values)
|
||||
{
|
||||
if ($this->columns)
|
||||
{
|
||||
$query .= (string) $this->columns;
|
||||
}
|
||||
|
||||
$elements = $this->values->getElements();
|
||||
|
||||
if (!($elements[0] instanceof $this))
|
||||
{
|
||||
$query .= ' VALUES ';
|
||||
}
|
||||
|
||||
$query .= (string) $this->values;
|
||||
|
||||
if ($this->returning)
|
||||
{
|
||||
$query .= (string) $this->returning;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$query = parent::__toString();
|
||||
break;
|
||||
}
|
||||
|
||||
if ($this instanceof F0FDatabaseQueryLimitable)
|
||||
{
|
||||
$query = $this->processLimit($query, $this->limit, $this->offset);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 F0FDatabaseQueryPostgresql Returns this object to allow chaining.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function clear($clause = null)
|
||||
{
|
||||
switch ($clause)
|
||||
{
|
||||
case 'limit':
|
||||
$this->limit = null;
|
||||
break;
|
||||
|
||||
case 'offset':
|
||||
$this->offset = null;
|
||||
break;
|
||||
|
||||
case 'forUpdate':
|
||||
$this->forUpdate = null;
|
||||
break;
|
||||
|
||||
case 'forShare':
|
||||
$this->forShare = null;
|
||||
break;
|
||||
|
||||
case 'noWait':
|
||||
$this->noWait = null;
|
||||
break;
|
||||
|
||||
case 'returning':
|
||||
$this->returning = null;
|
||||
break;
|
||||
|
||||
case 'select':
|
||||
case 'update':
|
||||
case 'delete':
|
||||
case 'insert':
|
||||
case 'from':
|
||||
case 'join':
|
||||
case 'set':
|
||||
case 'where':
|
||||
case 'group':
|
||||
case 'having':
|
||||
case 'order':
|
||||
case 'columns':
|
||||
case 'values':
|
||||
parent::clear($clause);
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->type = null;
|
||||
$this->limit = null;
|
||||
$this->offset = null;
|
||||
$this->forUpdate = null;
|
||||
$this->forShare = null;
|
||||
$this->noWait = null;
|
||||
$this->returning = null;
|
||||
parent::clear($clause);
|
||||
break;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts a value to a char.
|
||||
*
|
||||
* Ensure that the value is properly quoted before passing to the method.
|
||||
*
|
||||
* Usage:
|
||||
* $query->select($query->castAsChar('a'));
|
||||
*
|
||||
* @param string $value The value to cast as a char.
|
||||
*
|
||||
* @return string Returns the cast value.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function castAsChar($value)
|
||||
{
|
||||
return $value . '::text';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 11.3
|
||||
*/
|
||||
public function concatenate($values, $separator = null)
|
||||
{
|
||||
if ($separator)
|
||||
{
|
||||
return implode(' || ' . $this->quote($separator) . ' || ', $values);
|
||||
}
|
||||
else
|
||||
{
|
||||
return implode(' || ', $values);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current date and time.
|
||||
*
|
||||
* @return string Return string used in query to obtain
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function currentTimestamp()
|
||||
{
|
||||
return 'NOW()';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the FOR UPDATE lock on select's output row
|
||||
*
|
||||
* @param string $table_name The table to lock
|
||||
* @param string $glue The glue by which to join the conditions. Defaults to ',' .
|
||||
*
|
||||
* @return F0FDatabaseQueryPostgresql FOR UPDATE query element
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function forUpdate($table_name, $glue = ',')
|
||||
{
|
||||
$this->type = 'forUpdate';
|
||||
|
||||
if (is_null($this->forUpdate))
|
||||
{
|
||||
$glue = strtoupper($glue);
|
||||
$this->forUpdate = new F0FDatabaseQueryElement('FOR UPDATE', 'OF ' . $table_name, "$glue ");
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->forUpdate->append($table_name);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the FOR SHARE lock on select's output row
|
||||
*
|
||||
* @param string $table_name The table to lock
|
||||
* @param string $glue The glue by which to join the conditions. Defaults to ',' .
|
||||
*
|
||||
* @return F0FDatabaseQueryPostgresql FOR SHARE query element
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function forShare($table_name, $glue = ',')
|
||||
{
|
||||
$this->type = 'forShare';
|
||||
|
||||
if (is_null($this->forShare))
|
||||
{
|
||||
$glue = strtoupper($glue);
|
||||
$this->forShare = new F0FDatabaseQueryElement('FOR SHARE', 'OF ' . $table_name, "$glue ");
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->forShare->append($table_name);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get a string to extract year from date column.
|
||||
*
|
||||
* Usage:
|
||||
* $query->select($query->year($query->quoteName('dateColumn')));
|
||||
*
|
||||
* @param string $date Date column containing year to be extracted.
|
||||
*
|
||||
* @return string Returns string to extract year from a date.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function year($date)
|
||||
{
|
||||
return 'EXTRACT (YEAR FROM ' . $date . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get a string to extract month from date column.
|
||||
*
|
||||
* Usage:
|
||||
* $query->select($query->month($query->quoteName('dateColumn')));
|
||||
*
|
||||
* @param string $date Date column containing month to be extracted.
|
||||
*
|
||||
* @return string Returns string to extract month from a date.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function month($date)
|
||||
{
|
||||
return 'EXTRACT (MONTH FROM ' . $date . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get a string to extract day from date column.
|
||||
*
|
||||
* Usage:
|
||||
* $query->select($query->day($query->quoteName('dateColumn')));
|
||||
*
|
||||
* @param string $date Date column containing day to be extracted.
|
||||
*
|
||||
* @return string Returns string to extract day from a date.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function day($date)
|
||||
{
|
||||
return 'EXTRACT (DAY FROM ' . $date . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get a string to extract hour from date column.
|
||||
*
|
||||
* Usage:
|
||||
* $query->select($query->hour($query->quoteName('dateColumn')));
|
||||
*
|
||||
* @param string $date Date column containing hour to be extracted.
|
||||
*
|
||||
* @return string Returns string to extract hour from a date.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function hour($date)
|
||||
{
|
||||
return 'EXTRACT (HOUR FROM ' . $date . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get a string to extract minute from date column.
|
||||
*
|
||||
* Usage:
|
||||
* $query->select($query->minute($query->quoteName('dateColumn')));
|
||||
*
|
||||
* @param string $date Date column containing minute to be extracted.
|
||||
*
|
||||
* @return string Returns string to extract minute from a date.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function minute($date)
|
||||
{
|
||||
return 'EXTRACT (MINUTE FROM ' . $date . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get a string to extract seconds from date column.
|
||||
*
|
||||
* Usage:
|
||||
* $query->select($query->second($query->quoteName('dateColumn')));
|
||||
*
|
||||
* @param string $date Date column containing second to be extracted.
|
||||
*
|
||||
* @return string Returns string to extract second from a date.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function second($date)
|
||||
{
|
||||
return 'EXTRACT (SECOND FROM ' . $date . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the NOWAIT lock on select's output row
|
||||
*
|
||||
* @return F0FDatabaseQueryPostgresql NO WAIT query element
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function noWait ()
|
||||
{
|
||||
$this->type = 'noWait';
|
||||
|
||||
if (is_null($this->noWait))
|
||||
{
|
||||
$this->noWait = new F0FDatabaseQueryElement('NOWAIT', null);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the LIMIT clause to the query
|
||||
*
|
||||
* @param integer $limit An int of how many row will be returned
|
||||
*
|
||||
* @return F0FDatabaseQueryPostgresql Returns this object to allow chaining.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function limit($limit = 0)
|
||||
{
|
||||
if (is_null($this->limit))
|
||||
{
|
||||
$this->limit = new F0FDatabaseQueryElement('LIMIT', (int) $limit);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the OFFSET clause to the query
|
||||
*
|
||||
* @param integer $offset An int for skipping row
|
||||
*
|
||||
* @return F0FDatabaseQueryPostgresql Returns this object to allow chaining.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function offset($offset = 0)
|
||||
{
|
||||
if (is_null($this->offset))
|
||||
{
|
||||
$this->offset = new F0FDatabaseQueryElement('OFFSET', (int) $offset);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the RETURNING element to INSERT INTO statement.
|
||||
*
|
||||
* @param mixed $pkCol The name of the primary key column.
|
||||
*
|
||||
* @return F0FDatabaseQueryPostgresql Returns this object to allow chaining.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function returning($pkCol)
|
||||
{
|
||||
if (is_null($this->returning))
|
||||
{
|
||||
$this->returning = new F0FDatabaseQueryElement('RETURNING', $pkCol);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 F0FDatabaseQueryPostgresql Returns this object to allow chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function setLimit($limit = 0, $offset = 0)
|
||||
{
|
||||
$this->limit = (int) $limit;
|
||||
$this->offset = (int) $offset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 12.1
|
||||
*/
|
||||
public function processLimit($query, $limit, $offset = 0)
|
||||
{
|
||||
if ($limit > 0)
|
||||
{
|
||||
$query .= ' LIMIT ' . $limit;
|
||||
}
|
||||
|
||||
if ($offset > 0)
|
||||
{
|
||||
$query .= ' OFFSET ' . $offset;
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to the current date and time in Postgresql.
|
||||
* Usage:
|
||||
* $query->select($query->dateAdd());
|
||||
* Prefixing the interval with a - (negative sign) will cause subtraction to be used.
|
||||
*
|
||||
* @param datetime $date The date to add to
|
||||
* @param string $interval The string representation of the appropriate number of units
|
||||
* @param string $datePart The part of the date to perform the addition on
|
||||
*
|
||||
* @return string The string with the appropriate sql for addition of dates
|
||||
*
|
||||
* @since 13.1
|
||||
* @note Not all drivers support all units. Check appropriate references
|
||||
* @link http://www.postgresql.org/docs/9.0/static/functions-datetime.html.
|
||||
*/
|
||||
public function dateAdd($date, $interval, $datePart)
|
||||
{
|
||||
if (substr($interval, 0, 1) != '-')
|
||||
{
|
||||
return "timestamp '" . $date . "' + interval '" . $interval . " " . $datePart . "'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "timestamp '" . $date . "' - interval '" . ltrim($interval, '-') . " " . $datePart . "'";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return correct regexp operator for Postgresql.
|
||||
*
|
||||
* Ensure that the regexp operator is Postgresql compatible.
|
||||
*
|
||||
* Usage:
|
||||
* $query->where('field ' . $query->regexp($search));
|
||||
*
|
||||
* @param string $value The regex pattern.
|
||||
*
|
||||
* @return string Returns the regex operator.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function regexp($value)
|
||||
{
|
||||
return ' ~* ' . $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return correct rand() function for Postgresql.
|
||||
*
|
||||
* Ensure that the rand() function is Postgresql compatible.
|
||||
*
|
||||
* Usage:
|
||||
* $query->Rand();
|
||||
*
|
||||
* @return string The correct rand function.
|
||||
*
|
||||
* @since 3.5
|
||||
*/
|
||||
public function Rand()
|
||||
{
|
||||
return ' RANDOM() ';
|
||||
}
|
||||
}
|
||||
62
libraries/f0f/database/query/preparable.php
Normal file
62
libraries/f0f/database/query/preparable.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
if (!interface_exists('JDatabaseQueryPreparable'))
|
||||
{
|
||||
/**
|
||||
* Joomla 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 12.1
|
||||
*/
|
||||
interface JDatabaseQueryPreparable
|
||||
{
|
||||
/**
|
||||
* 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 F0FDatabaseQuery
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function bind($key = null, &$value = null, $dataType = PDO::PARAM_STR, $length = 0, $driverOptions = array());
|
||||
|
||||
/**
|
||||
* 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 12.1
|
||||
*/
|
||||
public function &getBounded($key = null);
|
||||
}
|
||||
}
|
||||
|
||||
interface F0FDatabaseQueryPreparable extends JDatabaseQueryPreparable
|
||||
{
|
||||
|
||||
}
|
||||
33
libraries/f0f/database/query/sqlazure.php
Normal file
33
libraries/f0f/database/query/sqlazure.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Query Building Class.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
class F0FDatabaseQuerySqlazure extends F0FDatabaseQuerySqlsrv
|
||||
{
|
||||
/**
|
||||
* 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 11.1
|
||||
*/
|
||||
protected $name_quotes = '';
|
||||
}
|
||||
279
libraries/f0f/database/query/sqlite.php
Normal file
279
libraries/f0f/database/query/sqlite.php
Normal file
@ -0,0 +1,279 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* SQLite Query Building Class.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
class F0FDatabaseQuerySqlite extends F0FDatabaseQueryPdo implements F0FDatabaseQueryPreparable, F0FDatabaseQueryLimitable
|
||||
{
|
||||
/**
|
||||
* @var integer The offset for the result set.
|
||||
* @since 12.1
|
||||
*/
|
||||
protected $offset;
|
||||
|
||||
/**
|
||||
* @var integer The limit for the result set.
|
||||
* @since 12.1
|
||||
*/
|
||||
protected $limit;
|
||||
|
||||
/**
|
||||
* @var array Bounded object array
|
||||
* @since 12.1
|
||||
*/
|
||||
protected $bounded = array();
|
||||
|
||||
/**
|
||||
* 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 F0FDatabaseQuerySqlite
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function bind($key = null, &$value = null, $dataType = PDO::PARAM_STR, $length = 0, $driverOptions = array())
|
||||
{
|
||||
// Case 1: Empty Key (reset $bounded array)
|
||||
if (empty($key))
|
||||
{
|
||||
$this->bounded = array();
|
||||
|
||||
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 12.1
|
||||
*/
|
||||
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 13.1
|
||||
*/
|
||||
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 F0FDatabaseQuerySqlite Returns this object to allow chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function clear($clause = null)
|
||||
{
|
||||
switch ($clause)
|
||||
{
|
||||
case null:
|
||||
$this->bounded = array();
|
||||
break;
|
||||
}
|
||||
|
||||
parent::clear($clause);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 11.1
|
||||
*/
|
||||
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 F0FDatabaseQueryLimitable 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)
|
||||
{
|
||||
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 F0FDatabaseQuerySqlite Returns this object to allow chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function setLimit($limit = 0, $offset = 0)
|
||||
{
|
||||
$this->limit = (int) $limit;
|
||||
$this->offset = (int) $offset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to the current date and time.
|
||||
* Usage:
|
||||
* $query->select($query->dateAdd());
|
||||
* Prefixing the interval with a - (negative sign) will cause subtraction to be used.
|
||||
*
|
||||
* @param datetime $date The date or datetime to add to
|
||||
* @param string $interval The string representation of the appropriate number of units
|
||||
* @param string $datePart The part of the date to perform the addition on
|
||||
*
|
||||
* @return string The string with the appropriate sql for addition of dates
|
||||
*
|
||||
* @since 13.1
|
||||
* @link http://www.sqlite.org/lang_datefunc.html
|
||||
*/
|
||||
public function dateAdd($date, $interval, $datePart)
|
||||
{
|
||||
// SQLite does not support microseconds as a separate unit. Convert the interval to seconds
|
||||
if (strcasecmp($datePart, 'microseconds') == 0)
|
||||
{
|
||||
$interval = .001 * $interval;
|
||||
$datePart = 'seconds';
|
||||
}
|
||||
|
||||
if (substr($interval, 0, 1) != '-')
|
||||
{
|
||||
return "datetime('" . $date . "', '+" . $interval . " " . $datePart . "')";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "datetime('" . $date . "', '" . $interval . " " . $datePart . "')";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current date and time.
|
||||
*
|
||||
* Usage:
|
||||
* $query->where('published_up < '.$query->currentTimestamp());
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 3.4
|
||||
*/
|
||||
public function currentTimestamp()
|
||||
{
|
||||
return 'CURRENT_TIMESTAMP';
|
||||
}
|
||||
}
|
||||
380
libraries/f0f/database/query/sqlsrv.php
Normal file
380
libraries/f0f/database/query/sqlsrv.php
Normal file
@ -0,0 +1,380 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FrameworkOnFramework
|
||||
* @subpackage database
|
||||
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*
|
||||
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning F0FTable objects
|
||||
* instead of plain stdClass objects
|
||||
*/
|
||||
|
||||
// Protect from unauthorized access
|
||||
defined('F0F_INCLUDED') or die;
|
||||
|
||||
/**
|
||||
* Query Building Class.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
class F0FDatabaseQuerySqlsrv extends F0FDatabaseQuery implements F0FDatabaseQueryLimitable
|
||||
{
|
||||
/**
|
||||
* 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 11.1
|
||||
*/
|
||||
protected $name_quotes = '`';
|
||||
|
||||
/**
|
||||
* The null or zero representation of a timestamp for the database driver. This should be
|
||||
* defined in child classes to hold the appropriate value for the engine.
|
||||
*
|
||||
* @var string
|
||||
* @since 11.1
|
||||
*/
|
||||
protected $null_date = '1900-01-01 00:00:00';
|
||||
|
||||
/**
|
||||
* @var integer The affected row limit for the current SQL statement.
|
||||
* @since 3.2
|
||||
*/
|
||||
protected $limit = 0;
|
||||
|
||||
/**
|
||||
* @var integer The affected row offset to apply for the current SQL statement.
|
||||
* @since 3.2
|
||||
*/
|
||||
protected $offset = 0;
|
||||
|
||||
/**
|
||||
* Magic function to convert the query to a string.
|
||||
*
|
||||
* @return string The completed query.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$query = '';
|
||||
|
||||
switch ($this->type)
|
||||
{
|
||||
case 'select':
|
||||
$query .= (string) $this->select;
|
||||
$query .= (string) $this->from;
|
||||
|
||||
if ($this->join)
|
||||
{
|
||||
// Special case for joins
|
||||
foreach ($this->join as $join)
|
||||
{
|
||||
$query .= (string) $join;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->where)
|
||||
{
|
||||
$query .= (string) $this->where;
|
||||
}
|
||||
|
||||
if ($this->group)
|
||||
{
|
||||
$query .= (string) $this->group;
|
||||
}
|
||||
|
||||
if ($this->order)
|
||||
{
|
||||
$query .= (string) $this->order;
|
||||
}
|
||||
|
||||
if ($this->having)
|
||||
{
|
||||
$query .= (string) $this->having;
|
||||
}
|
||||
|
||||
if ($this instanceof F0FDatabaseQueryLimitable && ($this->limit > 0 || $this->offset > 0))
|
||||
{
|
||||
$query = $this->processLimit($query, $this->limit, $this->offset);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'insert':
|
||||
$query .= (string) $this->insert;
|
||||
|
||||
// Set method
|
||||
if ($this->set)
|
||||
{
|
||||
$query .= (string) $this->set;
|
||||
}
|
||||
// Columns-Values method
|
||||
elseif ($this->values)
|
||||
{
|
||||
if ($this->columns)
|
||||
{
|
||||
$query .= (string) $this->columns;
|
||||
}
|
||||
|
||||
$elements = $this->insert->getElements();
|
||||
$tableName = array_shift($elements);
|
||||
|
||||
$query .= 'VALUES ';
|
||||
$query .= (string) $this->values;
|
||||
|
||||
if ($this->autoIncrementField)
|
||||
{
|
||||
$query = 'SET IDENTITY_INSERT ' . $tableName . ' ON;' . $query . 'SET IDENTITY_INSERT ' . $tableName . ' OFF;';
|
||||
}
|
||||
|
||||
if ($this->where)
|
||||
{
|
||||
$query .= (string) $this->where;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
$query .= (string) $this->delete;
|
||||
$query .= (string) $this->from;
|
||||
|
||||
if ($this->join)
|
||||
{
|
||||
// Special case for joins
|
||||
foreach ($this->join as $join)
|
||||
{
|
||||
$query .= (string) $join;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->where)
|
||||
{
|
||||
$query .= (string) $this->where;
|
||||
}
|
||||
|
||||
if ($this->order)
|
||||
{
|
||||
$query .= (string) $this->order;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
$query .= (string) $this->update;
|
||||
|
||||
if ($this->join)
|
||||
{
|
||||
// Special case for joins
|
||||
foreach ($this->join as $join)
|
||||
{
|
||||
$query .= (string) $join;
|
||||
}
|
||||
}
|
||||
|
||||
$query .= (string) $this->set;
|
||||
|
||||
if ($this->where)
|
||||
{
|
||||
$query .= (string) $this->where;
|
||||
}
|
||||
|
||||
if ($this->order)
|
||||
{
|
||||
$query .= (string) $this->order;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$query = parent::__toString();
|
||||
break;
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts a value to a char.
|
||||
*
|
||||
* Ensure that the value is properly quoted before passing to the method.
|
||||
*
|
||||
* @param string $value The value to cast as a char.
|
||||
*
|
||||
* @return string Returns the cast value.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function castAsChar($value)
|
||||
{
|
||||
return 'CAST(' . $value . ' as NVARCHAR(10))';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the function to determine the length of a character string.
|
||||
*
|
||||
* @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 11.1
|
||||
*/
|
||||
public function charLength($field, $operator = null, $condition = null)
|
||||
{
|
||||
return 'DATALENGTH(' . $field . ')' . (isset($operator) && isset($condition) ? ' ' . $operator . ' ' . $condition : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function concatenate($values, $separator = null)
|
||||
{
|
||||
if ($separator)
|
||||
{
|
||||
return '(' . implode('+' . $this->quote($separator) . '+', $values) . ')';
|
||||
}
|
||||
else
|
||||
{
|
||||
return '(' . implode('+', $values) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current date and time.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function currentTimestamp()
|
||||
{
|
||||
return 'GETDATE()';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the length of a string in bytes.
|
||||
*
|
||||
* @param string $value The string to measure.
|
||||
*
|
||||
* @return integer
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function length($value)
|
||||
{
|
||||
return 'LEN(' . $value . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to the current date and time.
|
||||
* Usage:
|
||||
* $query->select($query->dateAdd());
|
||||
* Prefixing the interval with a - (negative sign) will cause subtraction to be used.
|
||||
*
|
||||
* @param datetime $date The date to add to; type may be time or datetime.
|
||||
* @param string $interval The string representation of the appropriate number of units
|
||||
* @param string $datePart The part of the date to perform the addition on
|
||||
*
|
||||
* @return string The string with the appropriate sql for addition of dates
|
||||
*
|
||||
* @since 13.1
|
||||
* @note Not all drivers support all units.
|
||||
* @link http://msdn.microsoft.com/en-us/library/ms186819.aspx for more information
|
||||
*/
|
||||
public function dateAdd($date, $interval, $datePart)
|
||||
{
|
||||
return "DATEADD('" . $datePart . "', '" . $interval . "', '" . $date . "'" . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 12.1
|
||||
*/
|
||||
public function processLimit($query, $limit, $offset = 0)
|
||||
{
|
||||
if ($limit == 0 && $offset == 0)
|
||||
{
|
||||
return $query;
|
||||
}
|
||||
|
||||
$start = $offset + 1;
|
||||
$end = $offset + $limit;
|
||||
|
||||
$orderBy = stristr($query, 'ORDER BY');
|
||||
|
||||
if (is_null($orderBy) || empty($orderBy))
|
||||
{
|
||||
$orderBy = 'ORDER BY (select 0)';
|
||||
}
|
||||
|
||||
$query = str_ireplace($orderBy, '', $query);
|
||||
|
||||
$rowNumberText = ', ROW_NUMBER() OVER (' . $orderBy . ') AS RowNumber FROM ';
|
||||
|
||||
$query = preg_replace('/\sFROM\s/i', $rowNumberText, $query, 1);
|
||||
$query = 'SELECT * FROM (' . $query . ') A WHERE A.RowNumber BETWEEN ' . $start . ' AND ' . $end;
|
||||
|
||||
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 F0FDatabaseQuery Returns this object to allow chaining.
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public function setLimit($limit = 0, $offset = 0)
|
||||
{
|
||||
$this->limit = (int) $limit;
|
||||
$this->offset = (int) $offset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return correct rand() function for MSSQL.
|
||||
*
|
||||
* Ensure that the rand() function is MSSQL compatible.
|
||||
*
|
||||
* Usage:
|
||||
* $query->Rand();
|
||||
*
|
||||
* @return string The correct rand function.
|
||||
*
|
||||
* @since 3.5
|
||||
*/
|
||||
public function Rand()
|
||||
{
|
||||
return ' NEWID() ';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user