first
This commit is contained in:
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Database;
|
||||
|
||||
abstract class AbstractPlatformConnector {
|
||||
|
||||
protected $_prefixJoker = '#__';
|
||||
|
||||
protected $_prefix = '';
|
||||
|
||||
public function getPrefix() {
|
||||
return $this->_prefix;
|
||||
}
|
||||
|
||||
public function parsePrefix($query) {
|
||||
return str_replace($this->_prefixJoker, $this->_prefix, $query);
|
||||
}
|
||||
|
||||
abstract public function insertId();
|
||||
|
||||
abstract public function query($query, $attributes = false);
|
||||
|
||||
/**
|
||||
* Return with one row by query string
|
||||
*
|
||||
* @param string $query
|
||||
* @param array|bool $attributes for parameter binding
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function queryRow($query, $attributes = false);
|
||||
|
||||
abstract public function queryAll($query, $attributes = false, $type = "assoc", $key = null);
|
||||
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @param bool $escape
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function quote($text, $escape = true);
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param null $as
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function quoteName($name, $as = null);
|
||||
|
||||
public function checkError($result) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getCharsetCollate();
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Nextend\Framework\Database;
|
||||
|
||||
|
||||
abstract class AbstractPlatformConnectorTable {
|
||||
|
||||
protected $primaryKeyColumn = "id";
|
||||
|
||||
/** @var AbstractPlatformConnector */
|
||||
protected static $connector;
|
||||
|
||||
protected $tableName;
|
||||
|
||||
public function __construct($tableName) {
|
||||
|
||||
$this->tableName = self::$connector->getPrefix() . $tableName;
|
||||
}
|
||||
|
||||
public function getTableName() {
|
||||
return $this->tableName;
|
||||
}
|
||||
|
||||
abstract public function findByPk($primaryKey);
|
||||
|
||||
abstract public function findByAttributes(array $attributes, $fields = false, $order = false);
|
||||
|
||||
abstract public function findAll($order = false);
|
||||
|
||||
/**
|
||||
* Return with all row by attributes
|
||||
*
|
||||
* @param array $attributes
|
||||
* @param bool|array $fields
|
||||
* @param bool|string $order
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function findAllByAttributes(array $attributes, $fields = false, $order = false);
|
||||
|
||||
/**
|
||||
* Insert new row
|
||||
*
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
abstract public function insert(array $attributes);
|
||||
|
||||
abstract public function insertId();
|
||||
|
||||
/**
|
||||
* Update row(s) by param(s)
|
||||
*
|
||||
* @param array $attributes
|
||||
* @param array $conditions
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function update(array $attributes, array $conditions);
|
||||
|
||||
/**
|
||||
* Update one row by primary key with $attributes
|
||||
*
|
||||
* @param mixed $primaryKey
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function updateByPk($primaryKey, array $attributes);
|
||||
|
||||
/**
|
||||
* Delete one with by primary key
|
||||
*
|
||||
* @param mixed $primaryKey
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function deleteByPk($primaryKey);
|
||||
|
||||
/**
|
||||
* Delete all rows by attributes
|
||||
*
|
||||
* @param array $conditions
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function deleteByAttributes(array $conditions);
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Database;
|
||||
|
||||
use Nextend\Framework\Database\Joomla\JoomlaConnector;
|
||||
use Nextend\Framework\Database\Joomla\JoomlaConnectorTable;
|
||||
use Nextend\Framework\Database\WordPress\WordPressConnector;
|
||||
use Nextend\Framework\Database\WordPress\WordPressConnectorTable;
|
||||
use Nextend\Framework\Pattern\SingletonTrait;
|
||||
|
||||
class Database {
|
||||
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var AbstractPlatformConnector
|
||||
*/
|
||||
private static $platformConnector;
|
||||
|
||||
protected function init() {
|
||||
self::$platformConnector = new WordPressConnector();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $tableName
|
||||
*
|
||||
* @return AbstractPlatformConnectorTable
|
||||
*/
|
||||
public static function getTable($tableName) {
|
||||
return new WordPressConnectorTable($tableName);
|
||||
}
|
||||
|
||||
public static function getPrefix() {
|
||||
return self::$platformConnector->getPrefix();
|
||||
}
|
||||
|
||||
public static function parsePrefix($query) {
|
||||
return self::$platformConnector->parsePrefix($query);
|
||||
}
|
||||
|
||||
public static function insertId() {
|
||||
|
||||
return self::$platformConnector->insertId();
|
||||
}
|
||||
|
||||
public static function query($query, $attributes = false) {
|
||||
|
||||
return self::$platformConnector->query($query, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return with one row by query string
|
||||
*
|
||||
* @param string $query
|
||||
* @param array|bool $attributes for parameter binding
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function queryRow($query, $attributes = false) {
|
||||
|
||||
return self::$platformConnector->queryRow($query, $attributes);
|
||||
}
|
||||
|
||||
public static function queryAll($query, $attributes = false, $type = "assoc", $key = null) {
|
||||
|
||||
return self::$platformConnector->queryAll($query, $attributes, $type, $key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @param bool $escape
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function quote($text, $escape = true) {
|
||||
|
||||
return self::$platformConnector->quote($text, $escape);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param null $as
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function quoteName($name, $as = null) {
|
||||
|
||||
return self::$platformConnector->quoteName($name, $as);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getCharsetCollate() {
|
||||
|
||||
return self::$platformConnector->getCharsetCollate();
|
||||
}
|
||||
}
|
||||
|
||||
Database::getInstance();
|
@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Database\WordPress;
|
||||
|
||||
use Nextend\Framework\Database\AbstractPlatformConnector;
|
||||
use Nextend\Framework\Notification\Notification;
|
||||
use Nextend\SmartSlider3\Platform\SmartSlider3Platform;
|
||||
use wpdb;
|
||||
|
||||
class WordPressConnector extends AbstractPlatformConnector {
|
||||
|
||||
/** @var wpdb $wpdb */
|
||||
private $db;
|
||||
|
||||
public function __construct() {
|
||||
/** @var wpdb $wpdb */ global $wpdb;
|
||||
$this->db = $wpdb;
|
||||
$this->_prefix = $wpdb->prefix;
|
||||
|
||||
WordPressConnectorTable::init($this, $this->db);
|
||||
}
|
||||
|
||||
public function query($query, $attributes = false) {
|
||||
if ($attributes) {
|
||||
foreach ($attributes as $key => $value) {
|
||||
$replaceTo = is_numeric($value) ? $value : $this->db->prepare('%s', $value);
|
||||
$query = str_replace($key, $replaceTo, $query);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->checkError($this->db->query($query));
|
||||
}
|
||||
|
||||
public function insertId() {
|
||||
return $this->db->insert_id;
|
||||
}
|
||||
|
||||
private function _querySQL($query, $attributes = false) {
|
||||
|
||||
$args = array('');
|
||||
|
||||
if ($attributes) {
|
||||
foreach ($attributes as $key => $value) {
|
||||
$replaceTo = is_numeric($value) ? '%d' : '%s';
|
||||
$query = str_replace($key, $replaceTo, $query);
|
||||
$args[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (count($args) > 1) {
|
||||
$args[0] = $query;
|
||||
|
||||
return call_user_func_array(array(
|
||||
$this->db,
|
||||
'prepare'
|
||||
), $args);
|
||||
} else {
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
|
||||
public function queryRow($query, $attributes = false) {
|
||||
return $this->checkError($this->db->get_row($this->_querySQL($query, $attributes), ARRAY_A));
|
||||
}
|
||||
|
||||
public function queryAll($query, $attributes = false, $type = "assoc", $key = null) {
|
||||
$result = $this->checkError($this->db->get_results($this->_querySQL($query, $attributes), $type == 'assoc' ? ARRAY_A : OBJECT_K));
|
||||
if (!$key) {
|
||||
return $result;
|
||||
}
|
||||
$realResult = array();
|
||||
|
||||
for ($i = 0; $i < count($result); $i++) {
|
||||
$key = $type == 'assoc' ? $result[i][$key] : $result[i]->{$key};
|
||||
$realResult[$key] = $result[i];
|
||||
}
|
||||
|
||||
return $realResult;
|
||||
}
|
||||
|
||||
public function quote($text, $escape = true) {
|
||||
return '\'' . (esc_sql($text)) . '\'';
|
||||
}
|
||||
|
||||
public function quoteName($name, $as = null) {
|
||||
if (strpos($name, '.') !== false) {
|
||||
return $name;
|
||||
} else {
|
||||
$q = '`';
|
||||
if (strlen($q) == 1) {
|
||||
return $q . $name . $q;
|
||||
} else {
|
||||
return $q[0] . $name . $q[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function checkError($result) {
|
||||
if (!empty($this->db->last_error)) {
|
||||
if (is_admin()) {
|
||||
$lastError = $this->db->last_error;
|
||||
$lastQuery = $this->db->last_query;
|
||||
|
||||
$possibleErrors = array(
|
||||
'Duplicate entry' => 'Your table column doesn\'t have auto increment, while it should have.',
|
||||
'command denied' => 'Your database user has limited access and isn\'t able to run all commands which are necessary for our code to work.',
|
||||
'Duplicate key name' => 'Your database user has limited access and isn\'t able to run DROP or ALTER database commands.',
|
||||
'Can\'t DROP' => 'Your database user has limited access and isn\'t able to run DROP or ALTER database commands.'
|
||||
);
|
||||
|
||||
$errorMessage = sprintf(n2_('If you see this message after the repair database process, please %1$scontact us%2$s with the log:'), '<a href="https://smartslider3.com/contact-us/support/" target="_blank">', '</a>');
|
||||
|
||||
foreach ($possibleErrors as $error => $cause) {
|
||||
if (strpos($lastError, $error) !== false) {
|
||||
$errorMessage = n2_($cause) . ' ' . n2_('Contact your server host and ask them to fix this for you!');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$message = array(
|
||||
n2_('Unexpected database error.'),
|
||||
'',
|
||||
'<a href="' . wp_nonce_url(add_query_arg(array('repairss3' => '1'), SmartSlider3Platform::getAdminUrl()), 'repairss3') . '" class="n2_button n2_button--big n2_button--blue">' . n2_('Try to repair database') . '</a>',
|
||||
'',
|
||||
$errorMessage,
|
||||
'',
|
||||
'<b>' . $lastError . '</b>',
|
||||
$lastQuery
|
||||
);
|
||||
Notification::error(implode('<br>', $message), array(
|
||||
'wide' => true
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getCharsetCollate() {
|
||||
|
||||
return $this->db->get_charset_collate();
|
||||
}
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Nextend\Framework\Database\WordPress;
|
||||
|
||||
use Nextend\Framework\Database\AbstractPlatformConnector;
|
||||
use Nextend\Framework\Database\AbstractPlatformConnectorTable;
|
||||
use wpdb;
|
||||
|
||||
class WordPressConnectorTable extends AbstractPlatformConnectorTable {
|
||||
|
||||
/** @var wpdb */
|
||||
protected static $db;
|
||||
|
||||
/**
|
||||
* @param AbstractPlatformConnector $connector
|
||||
* @param wpdb $db
|
||||
*/
|
||||
public static function init($connector, $db) {
|
||||
self::$connector = $connector;
|
||||
self::$db = $db;
|
||||
}
|
||||
|
||||
public function findByPk($primaryKey) {
|
||||
$query = self::$db->prepare("SELECT * FROM " . $this->tableName . " WHERE " . self::$connector->quoteName($this->primaryKeyColumn) . " = %s", $primaryKey);
|
||||
|
||||
return self::$connector->checkError(self::$db->get_row($query, ARRAY_A));
|
||||
}
|
||||
|
||||
public function findByAttributes(array $attributes, $fields = false, $order = false) {
|
||||
|
||||
return self::$connector->checkError(self::$db->get_row($this->_findByAttributesSQL($attributes, $fields, $order), ARRAY_A));
|
||||
}
|
||||
|
||||
|
||||
public function findAll($order = false) {
|
||||
|
||||
return self::$connector->checkError(self::$db->get_results($this->_findByAttributesSQL(array(), false, $order), ARRAY_A));
|
||||
}
|
||||
|
||||
public function findAllByAttributes(array $attributes, $fields = false, $order = false) {
|
||||
|
||||
return self::$connector->checkError(self::$db->get_results($this->_findByAttributesSQL($attributes, $fields, $order), ARRAY_A));
|
||||
}
|
||||
|
||||
public function insert(array $attributes) {
|
||||
return self::$connector->checkError(self::$db->insert($this->tableName, $attributes));
|
||||
}
|
||||
|
||||
public function insertId() {
|
||||
return self::$db->insert_id;
|
||||
}
|
||||
|
||||
public function update(array $attributes, array $conditions) {
|
||||
|
||||
return self::$connector->checkError(self::$db->update($this->tableName, $attributes, $conditions));
|
||||
}
|
||||
|
||||
public function updateByPk($primaryKey, array $attributes) {
|
||||
|
||||
$where = array();
|
||||
$where[$this->primaryKeyColumn] = $primaryKey;
|
||||
self::$connector->checkError(self::$db->update($this->tableName, $attributes, $where));
|
||||
}
|
||||
|
||||
public function deleteByPk($primaryKey) {
|
||||
$where = array();
|
||||
$where[$this->primaryKeyColumn] = $primaryKey;
|
||||
self::$connector->checkError(self::$db->delete($this->tableName, $where));
|
||||
}
|
||||
|
||||
public function deleteByAttributes(array $conditions) {
|
||||
self::$connector->checkError(self::$db->delete($this->tableName, $conditions));
|
||||
}
|
||||
|
||||
private function _findByAttributesSQL(array $attributes, $fields = array(), $order = false) {
|
||||
|
||||
$args = array('');
|
||||
|
||||
$query = 'SELECT ';
|
||||
if (!empty($fields)) {
|
||||
|
||||
$fields = array_map(array(
|
||||
self::$connector,
|
||||
'quoteName'
|
||||
), $fields);
|
||||
|
||||
$query .= implode(', ', $fields);
|
||||
} else {
|
||||
$query .= '*';
|
||||
}
|
||||
$query .= ' FROM ' . $this->tableName;
|
||||
|
||||
$where = array();
|
||||
foreach ($attributes as $key => $val) {
|
||||
$valueFormat = is_numeric($val) ? '%d' : '%s';
|
||||
if ($key === 'hash') {
|
||||
/**
|
||||
* @see SSDEV-3927
|
||||
* Hashes are typically 32 character long and generated by md5 hashing algorithm.
|
||||
* In rare cases the hashed value might consist of numbers only, which can lead to overflow.
|
||||
* MD5 hashes should be prepared always as string values.
|
||||
*/
|
||||
|
||||
$valueFormat = '%s';
|
||||
}
|
||||
$where[] = self::$connector->quoteName($key) . ' = ' . $valueFormat;
|
||||
$args[] = $val;
|
||||
}
|
||||
if (count($where)) {
|
||||
$query .= ' WHERE ' . implode(' AND ', $where);
|
||||
}
|
||||
|
||||
if ($order) {
|
||||
$query .= ' ORDER BY ' . $order;
|
||||
}
|
||||
|
||||
if (count($args) > 1) {
|
||||
$args[0] = $query;
|
||||
|
||||
return call_user_func_array(array(
|
||||
self::$db,
|
||||
'prepare'
|
||||
), $args);
|
||||
} else {
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user