1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
<?php
namespace MatthiasWeb\RealMediaLibrary\comp\complexquery;
use MatthiasWeb\RealMediaLibrary\general;
use MatthiasWeb\RealMediaLibrary\base;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Define your complex SQL query. When implementing your complex query do
* not use global $wpdb; in your methods. Use ::getWpdb() instead.
*
* A complex query can be splitted into three parts:
* <ul>
* <li>1. Single Query: The query can be processed through one single SQL query with user defined variables</li>
* <li>2. Procedure: The procedure can write for example to an temporary table and reads again from this one</li>
* <li>3. Fallback: The fallback can throw an error or do the query through PHP functions</li>
* </ul>
*
* @example $rows = new MyComplexQuery($wpdb)->getResults();
*/
abstract class ComplexQuery extends base\Base {
private $wpdb;
private $isProcedurable;
const CACHE_OPTION_NAME = "mw_complex_query";
private $cache;
public function __construct($wpdb) {
$this->wpdb = $wpdb;
// Check the cache
$cache = $this->cache = $this->getCache();
if ($cache === false || time() > $cache["expire"]) {
$cache = array();
$cache["expire"] = strtotime("15 days");
$cache["isSQ"] = $this->isSingleQueriableWithUserDefinedVars();
$cache["isP"] = $this->isProcedurable();
update_site_option(ComplexQuery::CACHE_OPTION_NAME, $cache);
$this->cache = $cache;
}
}
/**
* Get the result from the three different result types: singleQuery, procedure
* or fallback.
*
* @returns mixed
*/
public function getResult() {
if ($this->isSingleQueriableWithUserDefinedVars()) {
return $this->singleQuery();
}else if ($this->isMysqli() && $this->isProcedurable()) {
return $this->procedure();
}else{
return $this->fallback();
}
}
/**
* This function is called when user defined variables are support. This method
* should return your expected result. It works with both mysqli_connect and mysql_connect.
*
* @see this::isSingleQueriableWithUserDefinedVars()
* @returns mixed
*/
abstract public function singleQuery();
/**
* This function is called when procedures (stored functions and
* procedures) are available. It is also necessery that mysqli is in
* use. mysql_connect does not support store_results() method. This method
* should return your expected result. You should work with this::hasProcedure()
* to install your procedure if not exists.
*
* Note: A procedure can for example write into a temporary table and reads from it again.
*
* @see this::isProcedurable()
* @see this::hasProcedure()
* @see this::install()
* @see this::getProcedureResults()
* @returns mixed
*/
abstract public function procedure();
/**
* This function is called when a single query is not possible and procedures
* are not allowed.
*
* @returns mixed
*/
abstract public function fallback();
/**
* Start an installer. Use this function in your procedure() method.
*
* @param callable $callable The callable to install the procedure for example
*/
final public function install($callable) {
// Avoid error messages in frontend
$this->getCore()->getActivator()->install(false, $callable);
}
/**
* Call a "CALL proc" SQL and parse the results.
*
* @param string $sql The SQL string to execute
* @param boolean $returnTrue When the CALL is successfully and has no results then return true instead of an empty array
* @returns Array or false when an error occured
*/
final protected function getProcedureResults($sql, $returnTrue = false) {
$mysqli = $this->getDbh();
if (($result = $mysqli->query($sql)) === false) {
return false;
}
$results = array();
// The procedure can be empty
if ($result === true) {
return $returnTrue ? true : $results;
}
// The procedure has results
while ($row = $result->fetch_array()) {
$results[] = $row;
}
$result->close();
return $results;
}
/**
* Checks if a given procedure is available for the current user.
*
* @returns boolean
*/
public function hasProcedure($procedure) {
$wpdb = $this->getWpdb();
$sql = $wpdb->prepare("SELECT COUNT(*)
FROM INFORMATION_SCHEMA.ROUTINES
WHERE UPPER(ROUTINE_TYPE)='PROCEDURE'
AND UPPER(ROUTINE_SCHEMA)=UPPER(%s)
AND UPPER(ROUTINE_NAME)=UPPER(%s)", $wpdb->dbname, $procedure);
return $wpdb->get_var($sql) > 0;
}
/**
* Checks if a single query is allowed to use @vars. For example MariaDB
* does not resolve @vars in a single query when they are not declared before.
* This method uses a cache (30 days).
*
* @returns boolean
*/
public function isSingleQueriableWithUserDefinedVars() {
// Cache
if (isset($this->cache["isSQ"])) {
return $this->cache["isSQ"];
}
return $this->getWpdb()->get_var("SELECT @var := 5 AS myvar") == "5";
}
/**
* Checks if procedures are allowed. This function uses a cache (30 days).
*
* @see https://stackoverflow.com/questions/609855/check-user-rights-before-attempting-to-create-database
* @returns boolean
*/
public function isProcedurable() {
// Cache
if (isset($this->cache["isP"])) {
return $this->cache["isP"];
}
$this->install(array($this, "_isProcedurable"));
return $this->isProcedurable;
}
final public function _isProcedurable() {
$wpdb = $this->getWpdb();
$wpdb->query("DROP PROCEDURE IF EXISTS _wp_realmedialibrary");
$this->isProcedurable = $wpdb->query("CREATE PROCEDURE _wp_realmedialibrary( ) BEGIN END");
}
/**
* Checks if the database handle is mysqli or not.
*
* @returns boolean
*/
public function isMysqli() {
return $this->wpdb->use_mysqli;
}
public function getWpdb() {
return $this->wpdb;
}
public function getDbh() {
return $this->wpdb->dbh;
}
public function getCache() {
return get_site_option(ComplexQuery::CACHE_OPTION_NAME);
}
}