|
From: <red...@us...> - 2013-08-24 04:39:04
|
Revision: 11967
http://sourceforge.net/p/xoops/svn/11967
Author: redheadedrod
Date: 2013-08-24 04:38:58 +0000 (Sat, 24 Aug 2013)
Log Message:
-----------
Updated 2.6 database connectors.
mysqldatabase.php - merged my version and Richards version.
connection.php - Added support for most items in legacy connector. Added query and queryForce(Old queryF), added some prefix based functions to use doctrine functions with prefix already added. Also added XoopsQueryBuilder to take new query builder function.
querybuilder.php - xoopsquerybuilder class is an extension of Doctrine querybuilder. Adds prefix versions of all the functions related to tables.
xoopsload.php - added xoopsquerybuilder to loader.
databasefactory.php - commented out code I didn't end up using and updated to support new features in doctrine connector.
Modified Paths:
--------------
XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/connection.php
XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/databasefactory.php
XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/mysqldatabase.php
XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/xoopsload.php
Added Paths:
-----------
XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/querybuilder.php
Modified: XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/connection.php
===================================================================
--- XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/connection.php 2013-08-23 00:10:44 UTC (rev 11966)
+++ XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/connection.php 2013-08-24 04:38:58 UTC (rev 11967)
@@ -26,7 +26,25 @@
*/
class XoopsConnection extends \Doctrine\DBAL\Connection
{
+ private $safe = true;
+ public $allowedWebChanges = true;
+
+ public function __construct(array $params,
+ \Doctrine\DBAL\Driver\PDOMySql\Driver $driver,
+ \Doctrine\DBAL\Configuration $config = null,
+ \Doctrine\Common\EventManager $eventManager = null)
+ {
+ // $this->prefix = XOOPS_DB_PREFIX;
+ //$this->allowWebChanges = ($_SERVER['REQUEST_METHOD'] != 'GET');
+ if (!defined('XOOPS_DB_PROXY')) {
+ $this->safe = true;
+ } else {
+ $this->safe = false;
+ }
+ parent::__construct($params, $driver, $config, $eventManager);
+ }
+
/**
* Prepend the prefix.'_' to the given tablename
* If tablename is empty, just return the prefix.
@@ -35,13 +53,155 @@
*
* @return string prefixed tablename, or prefix if tablename is empty
*/
- public function prefix($tablename = '')
+ public static function prefix($tablename = '')
{
+ static $prefix = XOOPS_DB_PREFIX;
if ($tablename != '') {
- return XOOPS_DB_PREFIX . '_' . $tablename;
+ return $prefix . '_' . $tablename;
} else {
- return XOOPS_DB_PREFIX;
+ return $prefix;
}
}
+ /**
+ * Inserts a table row with specified data.
+ *
+ * Adds prefix to the name of the table then passes to normal function.
+ *
+ * @param string $tableName The name of the table to insert data into.
+ * @param array $data An associative array containing column-value pairs.
+ * @param array $types Types of the inserted data.
+ * @return integer The number of affected rows.
+ */
+ public function insertPrefix($tableName, array $data, array $types = array())
+ {
+ $tableName = $this->prefix($tableName);
+ return $this->insert($tableName, $data, $types);
+ }
+
+
+ /**
+ * Executes an SQL UPDATE statement on a table.
+ *
+ * Adds prefix to the name of the table then passes to normal function.
+ *
+ * @param string $tableName The name of the table to update.
+ * @param array $data
+ * @param array $identifier The update criteria. An associative array containing column-value pairs.
+ * @param array $types Types of the merged $data and $identifier arrays in that order.
+ * @return integer The number of affected rows.
+ */
+ public function updatePrefix($tableName, array $data, array $identifier, array $types = array())
+ {
+ $tableName = $this->prefix($tableName);
+ return $this->update($tableName, $data, $identifier, $types);
+ }
+
+
+ /**
+ * Executes an SQL DELETE statement on a table.
+ *
+ * Adds prefix to the name of the table then passes to normal function.
+ *
+ * @param string $tableName The name of the table on which to delete.
+ * @param array $identifier The deletion criteria. An associative array containing column-value pairs.
+ * @return integer The number of affected rows.
+ */
+ public function deletePrefix($tableName, array $identifier)
+ {
+ $tableName = $this->prefix($tableName);
+ return $this->delete($tableName, $identifier);
+ }
+
+
+
+ /**
+ * perform a query on the database
+ *
+ * @param string $sql a valid MySQL query
+ * @return bool|resource query result or FALSE if not successful
+ * or TRUE if successful and no result
+ */
+ public function queryForce()
+ {
+ $sql = func_get_arg(0);
+ $xoopsPreload = XoopsPreload::getInstance();
+ $xoopsPreload->triggerEvent('core.database.query.start');
+ try {
+ $result = call_user_func_array(array('parent', 'query'), func_get_args());
+ }
+ catch (Exception $e) {
+ $result=false;
+ }
+ /* if(is_object($result)) {
+ $this->_lastResult = clone $result; // Has to be clone or it is reference.
+ } */
+ $xoopsPreload->triggerEvent('core.database.query.end');
+
+ if ($result) {
+ $xoopsPreload->triggerEvent('core.database.query.success', (array($sql)));
+ return $result;
+ } else {
+ $xoopsPreload->triggerEvent('core.database.query.failure', (array($sql, $this)));
+ return false;
+ }
+ }
+
+ /**
+ * perform a query
+ *
+ * This method is empty and does nothing! It should therefore only be
+ * used if nothing is exactly what you want done! ;-)
+ *
+ * @param string $sql a valid MySQL query
+ * @abstract
+ */
+ public function query()
+ {
+ if ($this->safe) {
+ return call_user_func_array(array($this, "queryForce"), func_get_args());
+ } else {
+ $sql = ltrim(func_get_arg(0));
+ if (!$this->allowedWebChanges && strtolower(substr($sql, 0, 6)) != 'select') {
+ //trigger_error('Database updates are not allowed during processing of a GET request', E_USER_WARNING);
+ return false;
+ }
+ return call_user_func_array(array($this, "queryForce"), func_get_args());
+ }
+ }
+
+ /**
+ * perform queries from SQL dump file in a batch
+ *
+ * @param string $file file path to an SQL dump file
+ * @return bool FALSE if failed reading SQL file or TRUE if the file has been read and queries executed
+ */
+ public function queryFromFile($file)
+ {
+ if (false !== ($fp = fopen($file, 'r'))) {
+ $sql_queries = trim(fread($fp, filesize($file)));
+ SqlUtility::splitMySqlFile($pieces, $sql_queries);
+ foreach ($pieces as $query) {
+ // [0] contains the prefixed query
+ // [4] contains unprefixed table name
+ $prefixed_query = SqlUtility::prefixQuery(trim($query), $this->prefix());
+ if ($prefixed_query != false) {
+ $this->query($prefixed_query[0]);
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Create a new instance of a SQL query builder.
+ *
+ * @return \Doctrine\DBAL\Query\QueryBuilder
+ */
+ public function createXoopsQueryBuilder()
+ {
+ return new XoopsQueryBuilder($this);
+ }
+
}
\ No newline at end of file
Modified: XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/databasefactory.php
===================================================================
--- XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/databasefactory.php 2013-08-23 00:10:44 UTC (rev 11966)
+++ XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/databasefactory.php 2013-08-24 04:38:58 UTC (rev 11967)
@@ -21,6 +21,14 @@
defined('XOOPS_ROOT_PATH') or die('Restricted access');
+/**
+ * XoopsDatabaseFactory
+ *
+ * @package Kernel
+ * @author Kazumi Ono <on...@xo...>
+ * @access public
+ */
+
class XoopsDatabaseFactory
{
@@ -30,6 +38,9 @@
* if the class has not been instantiated yet, this will also take
* care of that
*
+ * NOTE: Persistance connection is not included. XOOPS_DB_PCONNECT is ignored.
+ * allowWebChanges also needs to be addressed
+ *
* @static
* @staticvar XoopsDatabase The only instance of database class
* @return XoopsDatabase Reference to the only instance of database class
@@ -50,7 +61,10 @@
// 'unix_socket' => ?,
'charset' => XOOPS_DB_CHARSET,
'driver' => 'pdo_mysql',
- 'wrapperClass' => 'XoopsConnection'
+ 'wrapperClass' => 'XoopsConnection',
+ // 'driverOptions' => array (PDO::ATTR_PERSISTENT => TRUE)
+ // this is how you call them but not compatible with doctrine.
+
);
$instance = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
// Legacy support
@@ -76,7 +90,7 @@
return $instance;
}
- static function getNewDatabaseConnection()
+ /* static function getNewDatabaseConnection()
{
static $instance;
if (!isset($instance)) {
@@ -84,14 +98,14 @@
$xoopsPreload = XoopsPreload::getInstance();
$xoopsPreload->triggerEvent('core.class.database.databasefactory.connection', array(&$class));
/* @var $instance XoopsDatabase */
- $instance = new $class();
+ /* $instance = new $class();
$instance->setPrefix(XOOPS_DB_PREFIX);
if (!$instance->connect()) {
trigger_error('notrace:Unable to connect to database', E_USER_ERROR);
}
}
return $instance;
- }
+ } */
/**
* Gets a reference to the only instance of database class. Currently
Modified: XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/mysqldatabase.php
===================================================================
--- XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/mysqldatabase.php 2013-08-23 00:10:44 UTC (rev 11966)
+++ XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/mysqldatabase.php 2013-08-24 04:38:58 UTC (rev 11967)
@@ -19,6 +19,7 @@
* @since 1.0.0
* @author Kazumi Ono <on...@xo...>
* @version $Id$
+ *
*/
defined('XOOPS_ROOT_PATH') or die('Restricted access');
@@ -26,8 +27,7 @@
/**
* connection to a mysql database
*
- * @abstract
- * @author Kazumi Ono <on...@xo...>
+ * @author Kazumi Ono <on...@xo...>
* @copyright copyright (c) 2000-2003 XOOPS.org
* @package class
* @subpackage database
@@ -36,11 +36,37 @@
{
/**
- * @var object keep track of last result since we need it for getAffectedRows
+ * Database connection
+ *
+ * @var resource
*/
+ public $conn;
+
+ /**
+ * Database connection
+ *
+ * @var resource
+ */
private $_lastResult;
/**
+ * Database connection
+ *
+ * @var resource
+ */
+ private $_connect = false;
+
+ /**
+ * Database connection
+ *
+ * @var resource
+ */
+ public $_selectdb;
+
+
+
+
+ /**
* connect to the database
*
* @param bool $selectdb select the database now?
@@ -48,29 +74,13 @@
*/
public function connect($selectdb = true)
{
- static $db_charset_set;
+ $this->_connect = (is_object($this->conn));
+ $this->_selectdb = $selectdb;
+ $this->allowWebChanges = ($_SERVER['REQUEST_METHOD'] != 'GET');
+ return $this->_connect;
+ }
- $config = new \Doctrine\DBAL\Configuration();
- $connectionParams = array(
- 'dbname' => XOOPS_DB_NAME,
- 'user' => XOOPS_DB_USER,
- 'password' => XOOPS_DB_PASS,
- 'host' => XOOPS_DB_HOST,
-// 'port' => ?,
-// 'unix_socket' => ?,
- 'charset' => XOOPS_DB_CHARSET,
- 'driver' => 'pdo_mysql'
- );
- $this->conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
- if (!$this->conn) {
- $xoopsPreload = XoopsPreload::getInstance();
- $xoopsPreload->triggerEvent('core.database.noconn', array($this));
- return false;
- }
- return true;
- }
-
/**
* generate an ID for a new row
*
@@ -173,6 +183,10 @@
*/
public function getAffectedRows()
{
+ if(!is_object($this->_lastResult))
+ {
+ return null;
+ }
return $this->_lastResult->rowCount();
}
@@ -236,7 +250,9 @@
*/
public function quote($string)
{
- return $this->conn->quote($string);
+ //return $this->conn->quote($string);
+ //There was a reason for this longer command but can't recall what it was...
+ return str_replace("\\\"", '"', str_replace("\\"", '"', $this->conn->quote($string)));
}
/**
@@ -264,7 +280,9 @@
catch (Exception $e) {
$result=false;
}
- $this->_lastResult = $result;
+ if(is_object($result)) {
+ $this->_lastResult = clone $result; // Has to be clone or it is reference.
+ }
$xoopsPreload->triggerEvent('core.database.query.end');
if ($result) {
@@ -326,10 +344,16 @@
*/
public function getFieldName($result, $offset)
{
- $xoops = Xoops::getInstance();
- $xoops->deprecated('getFieldName() is deprecated. See how to replace it in file ' . __FILE__ . ' line ' . __LINE__);
+ try
+ {
+ $temp = $result->getColumnMeta($offset);
+ return $temp['name'];
+ }
+ catch (PDOException $e)
+ {
+ return null;
+ }
- return null;
}
/**
@@ -344,10 +368,32 @@
*/
public function getFieldType($result, $offset)
{
- $xoops = Xoops::getInstance();
- $xoops->deprecated('getFieldType() is deprecated. See how to replace it in file ' . __FILE__ . ' line ' . __LINE__);
+ try
+ {
+ $temp = ($result->getColumnMeta($offset));
+ $t = $temp['native_type'];
- return null;
+ $temp = (string)(
+ ((($t == 'STRING') || ($t == 'VAR_STRING') ) ? 'string' : '' ) .
+ ( (in_array($t, array('TINY', 'SHORT', 'LONG', 'LONGLONG', 'INT24'))) ? 'int' : '' ) .
+ ( (in_array($t, array('FLOAT', 'DOUBLE', 'DECIMAL', 'NEWDECIMAL'))) ? 'real' : '' ) .
+ ( ($t == 'TIMESTAMP') ? 'timestamp' : '' ) .
+ ( ($t == 'YEAR') ? 'year' : '') .
+ ( (($t == 'DATE') || ($t == 'NEWDATE') ) ? 'date' : '' ) .
+ ( ($t == 'TIME') ? 'time' : '' ) .
+ ( ($t == 'SET') ? 'set' : '' ) .
+ ( ($t == 'ENUM') ? 'enum' : '' ) .
+ ( ($t == 'GEOMETRY') ? 'geometry' : '' ) .
+ ( ($t == 'DATETIME') ? 'datetime' : '' ) .
+ ( (in_array($t, array('TINY_BLOB', 'BLOB', 'MEDIUM_BLOB', 'LONG_BLOB'))) ? 'blob' : '' ) .
+ ( ($t == 'NULL') ? 'null' : '' )
+ );
+ return $temp;
+ }
+ catch (PDOException $e)
+ {
+ return null;
+ }
}
/**
@@ -366,11 +412,12 @@
*
* @return object Doctrine\DBAL\Connection
*/
+ /* not needed
public function direct()
{
return $this->conn;
}
-
+ */
}
/**
Added: XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/querybuilder.php
===================================================================
--- XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/querybuilder.php (rev 0)
+++ XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/database/querybuilder.php 2013-08-24 04:38:58 UTC (rev 11967)
@@ -0,0 +1,190 @@
+<?php
+/*
+ You may not change or alter any portion of this comment or credits
+ of supporting developers from this source code or any supporting source code
+ which is considered copyrighted (c) material of the original comment or credit authors.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+*/
+
+defined('XOOPS_ROOT_PATH') or die('Restricted access');
+
+/**
+ * Connection wrapper for Doctrine DBAL Connection
+ *
+ * @category Xoops\Database\XoopsConnection
+ * @package Xoops
+ * @author readheadedrod
+ * @author Richard Griffith <ri...@ge...>
+ * @copyright 2013 The XOOPS Project http://sourceforge.net/projects/xoops/
+ * @license http://www.fsf.org/copyleft/gpl.html GNU public license
+ * @version Release: 1.0
+ * @link http://xoops.org
+ * @since 1.0
+ */
+
+
+class XoopsQueryBuilder extends \Doctrine\DBAL\Query\QueryBuilder
+{
+
+
+ public function __construct(\Doctrine\DBAL\Connection $connection)
+ {
+ $this->prefix = XOOPS_DB_PREFIX;
+ parent::__construct($connection);
+ }
+
+
+ /**
+ * Turns the query being built into a bulk delete query that ranges over
+ * a certain table.
+ *
+ * <code>
+ * $qb = $conn->createQueryBuilder()
+ * ->delete('users', 'u')
+ * ->where('u.id = :user_id');
+ * ->setParameter(':user_id', 1);
+ * </code>
+ *
+ * @param string $delete The table whose rows are subject to the deletion. Adds table prefix.
+ * @param string $alias The table alias used in the constructed query.
+ * @return QueryBuilder This QueryBuilder instance.
+ */
+ public function deletePrefix($delete = null, $alias = null)
+ {
+ $delete = XoopsConnection::prefix($delete);
+ return $this->delete($delete, $alias);
+ }
+
+ /**
+ * Turns the query being built into a bulk update query that ranges over
+ * a certain table
+ *
+ * <code>
+ * $qb = $conn->createQueryBuilder()
+ * ->update('users', 'u')
+ * ->set('u.password', md5('password'))
+ * ->where('u.id = ?');
+ * </code>
+ *
+ * @param string $update The table whose rows are subject to the update. Adds table prefix.
+ * @param string $alias The table alias used in the constructed query.
+ * @return QueryBuilder This QueryBuilder instance.
+ */
+ public function updatePrefix($update = null, $alias = null)
+ {
+ $update = XoopsConnection::prefix($update);
+ return $this->update($update, $alias);
+ }
+
+ /**
+ * Create and add a query root corresponding to the table identified by the
+ * given alias, forming a cartesian product with any existing query roots.
+ *
+ * <code>
+ * $qb = $conn->createQueryBuilder()
+ * ->select('u.id')
+ * ->from('users', 'u')
+ * </code>
+ *
+ * @param string $from The table. Adds table prefix.
+ * @param string $alias The alias of the table
+ * @return QueryBuilder This QueryBuilder instance.
+ */
+ public function fromPrefix($from, $alias)
+ {
+ $from = XoopsConnection::prefix($from);
+ return $this->from($from, $alias);
+ }
+
+ /**
+ * Creates and adds a join to the query.
+ *
+ * <code>
+ * $qb = $conn->createQueryBuilder()
+ * ->select('u.name')
+ * ->from('users', 'u')
+ * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1');
+ * </code>
+ *
+ * @param string $fromAlias The alias that points to a from clause
+ * @param string $join The table name to join. Adds table prefix.
+ * @param string $alias The alias of the join table
+ * @param string $condition The condition for the join
+ * @return QueryBuilder This QueryBuilder instance.
+ */
+ public function joinPrefix($fromAlias, $join, $alias, $condition = null)
+ {
+ $join = XoopsConnection::prefix($join);
+ return $this->join($fromAlias, $join, $alias, $condition);
+ }
+
+
+ /**
+ * Creates and adds a join to the query.
+ *
+ * <code>
+ * $qb = $conn->createQueryBuilder()
+ * ->select('u.name')
+ * ->from('users', 'u')
+ * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
+ * </code>
+ *
+ * @param string $fromAlias The alias that points to a from clause
+ * @param string $join The table name to join. Adds table prefix.
+ * @param string $alias The alias of the join table
+ * @param string $condition The condition for the join
+ * @return QueryBuilder This QueryBuilder instance.
+ */
+ public function innerJoinPrefix($fromAlias, $join, $alias, $condition = null)
+ {
+ $join = XoopsConnection::prefix($join);
+ return $this->innerJoin($fromAlias, $join, $alias, $condition);
+ }
+
+ /**
+ * Creates and adds a left join to the query.
+ *
+ * <code>
+ * $qb = $conn->createQueryBuilder()
+ * ->select('u.name')
+ * ->from('users', 'u')
+ * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
+ * </code>
+ *
+ * @param string $fromAlias The alias that points to a from clause
+ * @param string $join The table name to join. Adds table prefix.
+ * @param string $alias The alias of the join table
+ * @param string $condition The condition for the join
+ * @return QueryBuilder This QueryBuilder instance.
+ */
+ public function leftJoinPrefix($fromAlias, $join, $alias, $condition = null)
+ {
+ $join = XoopsConnection::prefix($join);
+ return $this->leftJoin($fromAlias, $join, $alias, $condition);
+ }
+
+ /**
+ * Creates and adds a right join to the query.
+ *
+ * <code>
+ * $qb = $conn->createQueryBuilder()
+ * ->select('u.name')
+ * ->from('users', 'u')
+ * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
+ * </code>
+ *
+ * @param string $fromAlias The alias that points to a from clause
+ * @param string $join The table name to join. Adds table prefix.
+ * @param string $alias The alias of the join table
+ * @param string $condition The condition for the join
+ * @return QueryBuilder This QueryBuilder instance.
+ */
+ public function rightJoinPrefix($fromAlias, $join, $alias, $condition = null)
+ {
+ $join = XoopsConnection::prefix($join);
+ return $this->rightJoin($fromAlias, $join, $alias, $condition);
+ }
+}
\ No newline at end of file
Modified: XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/xoopsload.php
===================================================================
--- XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/xoopsload.php 2013-08-23 00:10:44 UTC (rev 11966)
+++ XoopsCore/branches/2.6.x/2.6.0_doctrine/htdocs/class/xoopsload.php 2013-08-24 04:38:58 UTC (rev 11967)
@@ -263,6 +263,7 @@
'xoopsconfigoptionhandler' => XOOPS_ROOT_PATH . '/kernel/configoption.php',
'xoopsdatabase' => XOOPS_ROOT_PATH . '/class/database/database.php',
'xoopsconnection' => XOOPS_ROOT_PATH . '/class/database/connection.php',
+ 'xoopsquerybuilder' => XOOPS_ROOT_PATH . '/class/database/querybuilder.php',
'xoopsdatabasefactory' => XOOPS_ROOT_PATH . '/class/database/databasefactory.php',
'xoopsdatabasemanager' => XOOPS_ROOT_PATH . '/class/database/manager.php',
'xoopsdownloader' => XOOPS_ROOT_PATH . '/class/downloader.php',
|