Commit 5183ab9f authored by Radek Suski's avatar Radek Suski

Separating database driver to separate platform

parent a4b6681a
......@@ -24,145 +24,36 @@ namespace Sobi\Database;
defined( 'SOBI' ) || exit( 'Restricted access' );
use Sobi\C;
use Sobi\Framework;
use Sobi\Database\Platform\Joomla;
use Sobi\Database\Platform\Wordpress;
use Sobi\Error\Exception;
use Sobi\Utils\Serialiser;
use Sobi\Utils\StringUtils;
/**
* @author Radek Suski
* @version 1.0
* @created Wed, Nov 30, 2016 11:35:26
*/
class MySQLi
abstract class MySQLi
{
/*** Joomla Database object @var \JDatabaseDriverMysqli */
protected $db = null;
/*** @var string */
protected $prefix = '#__';
/*** @var int */
protected $count = 0;
/**
* @return \Sobi\Database\MySQLi
*/
public function __construct()
{
$this->db = \JFactory::getDBO();
return $this;
}
/**
* @return \Sobi\Database\MySQLi
*/
public static function & getInstance()
{
static $db = null;
if ( !$db || !( $db instanceof self ) ) {
$db = new self();
if ( !( $db ) ) {
$db = SOBI_CMS == 'wordpress' ? Wordpress::getInstance() : Joomla::getInstance();
}
return $db;
}
/**
* Returns the error number
* @deprecated
* @return int
*/
public function getErrorNum()
{
return $this->db->getErrorNum();
}
/**
* Returns the error message
* @deprecated
* @return string
*/
public function getErrorMsg()
{
return $this->db->getErrorMsg();
}
/**
* Proxy pattern
*
* @param string $method
* @param array $args
*
* @throws Exception
* @return mixed
*/
public function __call( $method, $args )
{
if ( $this->db && method_exists( $this->db, $method ) ) {
$Args = [];
// http://www.php.net/manual/en/function.call-user-func-array.php#91503
foreach ( $args as $k => &$arg ) {
$Args[ $k ] = &$arg;
}
return call_user_func_array( [ $this->db, $method ], $Args );
}
else {
throw new Exception( Framework::Txt( 'CALL_TO_UNDEFINED_CLASS_METHOD', get_class( $this->_type ), $method ) );
}
}
/**
* Returns a database escaped string
*
* @param string $text string to be escaped
* @param bool $esc extra escaping
*
* @return string
*/
public function getEscaped( $text, $esc = false )
{
return $this->db->getEscaped( StringUtils::Clean( $text ), $esc );
}
/**
* Returns a database escaped string
*
* @param string $text string to be escaped
* @param bool $esc extra escaping
*
* @return string
*/
public function escape( $text, $esc = false )
{
return $this->db->escape( $text, $esc );
}
/**
* Returns database null date format
*
* @return string Quoted null date string
*/
public function getNullDate()
{
return $this->db->getNullDate();
}
/**
* Sets the SQL query string for later execution.
*
* @param string $sql
*
* @return MySQLi
*/
public function & setQuery( $sql )
{
$sql = str_replace( 'spdb', $this->prefix . 'sobipro', $sql );
$sql = str_replace( 'NOW()', '\'' . gmdate( 'Y-m-d H:i:s' ) . '\'', $sql );
$this->db->setQuery( $sql );
return $this;
}
/* (non-PHPdoc)
* @see Site/lib/base/SPDatabase#loadFile($file)
*/
......@@ -809,15 +700,6 @@ class MySQLi
return $this;
}
/**
* Returns current query
*
* @return string
*/
public function getQuery()
{
return str_replace( $this->prefix, $this->db->getPrefix(), $this->db->getQuery() );
}
/**
* Returns queries counter
......@@ -841,172 +723,6 @@ class MySQLi
return $this->db->execute();
}
/**
* Loads the first field of the first row returned by the query.
*
* @throws Exception
* @return string
*/
public function loadResult()
{
try {
$r = $this->db->loadResult();
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Load an array of single field results into an array
*
* @throws Exception
* @return array
*/
public function loadResultArray()
{
try {
$r = $this->db->loadColumn();
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Load a assoc list of database rows
*
* @param string $key field name of a primary key
*
* @throws Exception
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
public function loadAssocList( $key = null )
{
try {
$r = $this->db->loadAssocList( $key );
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Loads the first row of a query into an object
*
* @throws Exception
* @return stdObject
*/
public function loadObject()
{
try {
$r = $this->db->loadObject();
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
if ( $r && is_object( $r ) ) {
$attr = get_object_vars( $r );
foreach ( $attr as $property => $value ) {
if ( is_string( $value ) && strstr( $value, '"' ) ) {
$r->$property = StringUtils::Clean( $value );
}
}
}
return $r;
}
/**
* Load a list of database objects
*
* @param string $key
*
* @throws Exception
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
public function loadObjectList( $key = null )
{
try {
$r = $this->db->loadObjectList( $key );
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Load the first row of the query.
*
* @throws Exception
* @return array
*/
public function loadRow()
{
try {
$r = $this->db->loadRow();
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Load a list of database rows (numeric column indexing)
*
* @param string $key field name of a primary key
*
* @throws Exception
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
public function loadRowList( $key = null )
{
try {
$r = $this->db->loadRowList( $key );
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Returns an error statement
* @deprecated
* @return string
*/
public function stderr()
{
return $this->db->stderr();
}
/**
* Returns the ID generated from the previous insert operation
*
* @return int
*/
public function insertid()
{
return $this->db->insertid();
}
/**
* executing query (update/insert etc)
......@@ -1029,30 +745,6 @@ class MySQLi
return $r;
}
/**
* Returns all rows of given table
*
* @param string $table
*
* @throws Exception
* @return array
*/
public function getColumns( $table )
{
static $cache = [];
if ( !( isset( $cache[ $table ] ) ) ) {
$this->setQuery( "SHOW COLUMNS FROM {$table}" );
try {
$cache[ $table ] = $this->loadResultArray();
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
}
return $cache[ $table ];
}
/**
* rolls back the current transaction, canceling its changes
*
......
<?php
/**
* @package: Sobi Framework
* @author
* Name: Sigrid Suski & Radek Suski, Sigsiu.NET GmbH
* Email: sobi[at]sigsiu.net
* Url: https://www.Sigsiu.NET
* @copyright Copyright (C) 2006 - 2016 Sigsiu.NET GmbH (https://www.sigsiu.net). All rights reserved.
* @license GNU/LGPL Version 3
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation, and under the additional terms according section 7 of GPL v3.
* See http://www.gnu.org/licenses/lgpl.html and https://www.sigsiu.net/licenses.
*
* 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. See the GNU General Public License for more details.
* @created
*/
namespace Sobi\Database\Platform;
use Sobi\Database\MySQLi;
use Sobi\Lib\Instance;
class Joomla extends MySQLi implements Platform
{
use Instance;
/*** Joomla Database object @var \JDatabaseDriverMysqli */
protected $db = null;
/*** @var string */
protected $prefix = '#__';
/**
* @return \Sobi\Database\Platform\Joomla
*/
public function __construct()
{
$this->db = \JFactory::getDBO();
return $this;
}
/**
* Returns the error number
* @deprecated
* @return int
*/
public function getErrorNum()
{
return $this->db->getErrorNum();
}
/**
* Returns the error message
* @deprecated
* @return string
*/
public function getErrorMsg()
{
return $this->db->getErrorMsg();
}
/**
* Proxy pattern
*
* @param string $method
* @param array $args
*
* @throws Exception
* @return mixed
*/
public function __call( $method, $args )
{
if ( $this->db && method_exists( $this->db, $method ) ) {
$Args = [];
// http://www.php.net/manual/en/function.call-user-func-array.php#91503
foreach ( $args as $k => &$arg ) {
$Args[ $k ] = &$arg;
}
return call_user_func_array( [ $this->db, $method ], $Args );
}
else {
throw new Exception( Framework::Txt( 'CALL_TO_UNDEFINED_CLASS_METHOD', get_class( $this->_type ), $method ) );
}
}
/**
* Returns a database escaped string
*
* @param string $text string to be escaped
* @param bool $esc extra escaping
*
* @return string
*/
public function getEscaped( $text, $esc = false )
{
return $this->db->getEscaped( StringUtils::Clean( $text ), $esc );
}
/**
* Returns a database escaped string
*
* @param string $text string to be escaped
* @param bool $esc extra escaping
*
* @return string
*/
public function escape( $text, $esc = false )
{
return $this->db->escape( $text, $esc );
}
/**
* Returns database null date format
*
* @return string Quoted null date string
*/
public function getNullDate()
{
return $this->db->getNullDate();
}
/**
* Sets the SQL query string for later execution.
*
* @param string $sql
*
* @return MySQLi
*/
public function & setQuery( $sql )
{
$sql = str_replace( 'spdb', $this->prefix . 'sobipro', $sql );
$sql = str_replace( 'NOW()', '\'' . gmdate( 'Y-m-d H:i:s' ) . '\'', $sql );
$this->db->setQuery( $sql );
return $this;
}
/**
* Returns current query
*
* @return string
*/
public function getQuery()
{
return str_replace( $this->prefix, $this->db->getPrefix(), $this->db->getQuery() );
}
/**
* Loads the first field of the first row returned by the query.
*
* @throws Exception
* @return string
*/
public function loadResult()
{
try {
$r = $this->db->loadResult();
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Load an array of single field results into an array
*
* @throws Exception
* @return array
*/
public function loadResultArray()
{
try {
$r = $this->db->loadColumn();
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Load a assoc list of database rows
*
* @param string $key field name of a primary key
*
* @throws Exception
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
public function loadAssocList( $key = null )
{
try {
$r = $this->db->loadAssocList( $key );
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Loads the first row of a query into an object
*
* @throws Exception
* @return stdObject
*/
public function loadObject()
{
try {
$r = $this->db->loadObject();
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
if ( $r && is_object( $r ) ) {
$attr = get_object_vars( $r );
foreach ( $attr as $property => $value ) {
if ( is_string( $value ) && strstr( $value, '"' ) ) {
$r->$property = StringUtils::Clean( $value );
}
}
}
return $r;
}
/**
* Load a list of database objects
*
* @param string $key
*
* @throws Exception
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
public function loadObjectList( $key = null )
{
try {
$r = $this->db->loadObjectList( $key );
$this->count++;
}
catch ( \Exception $e ) {
throw new Exception( $e->getMessage() );
}
return $r;
}
/**
* Load the first row of the query.
*
* @throws Exception
* @return array
*/
public function loadRow()
{
try {
$r = $this->db->loadRow();
$this->count++;
}
catch ( \Exception $e ) {