Core/Modules/databaseutils/class.query.php

892 lines
21 KiB
PHP
Raw Normal View History

<?php
/**
* FuzeWorks.
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
*
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
*
* @version Version 0.0.1
*/
namespace Module\DatabaseUtils;
use FuzeWorks\Modules;
use FuzeWorks\DatabaseException;
use FuzeWorks\Config;
use FuzeWorks\Logger;
/**
* Class Query.
*
* @author GOScripting
* @copyright Copyright (c) 2014 - 2015, GOScripting B.V. (http://goscripting.com)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
*
* @link http://goframework.net
*
* @method $this or() or(string $field, string $arg2) OR $arg2 is the value of the field, or an operator in which case the value is pushed to the third argument
* @method $this and() and(string $field, string $arg2) AND $arg2 is the value of the field, or an operator in which case the value is pushed to the third argument
*/
class Query
{
/**
2015-09-06 13:42:57 +00:00
* @var array An array containing all the counted functions
*/
2015-09-06 13:42:57 +00:00
private $functions = array();
/**
* @var null|string The table used
*/
private $table = '';
/**
* @var null|\PDOStatement
*/
private $sth = null;
/**
2015-09-06 13:42:57 +00:00
* @param string $table The string to set as a table, optional
*/
public function __construct($table = '')
{
if ($table != '') {
2015-09-06 13:42:57 +00:00
$this->setTable($table);
}
2015-09-06 13:42:57 +00:00
}
/**
* Each argument is another field to select.
*
2015-09-06 13:42:57 +00:00
* @param string $field,... The field to select, if no field is given, all fields(*) will be selected
*
* @return $this
*/
public function select($field = '*')
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalSelect($field = '*')
{
return array(
'sql' => 'SELECT '.($field == '*' ? '*' : implode(', ', func_get_args()).''),
'binds' => null,
2015-09-06 13:42:57 +00:00
);
}
/**
* From.
*
2015-09-06 13:42:57 +00:00
* @param $table,... null|string The table, if it is null, the table set with setTable() will be used
*
* @return $this
*
* @throws Exception
*/
public function from($table = null)
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalFrom($table = null)
{
2015-05-14 11:30:40 +00:00
$tables = func_get_args();
2015-09-06 13:42:57 +00:00
$query = 'FROM ';
if (count($tables) == 0) {
2015-09-06 13:42:57 +00:00
$tables[] = "$this->table";
} else {
foreach ($tables as $i => $t) {
if ($t == '' && $this->table == '') {
throw new Exception('No table given');
}
2015-05-14 11:30:40 +00:00
$tables[$i] = strpos($t, ' ') === false ? "$t" : ''.implode(' ', explode(' ', $t));
}
2015-05-14 11:30:40 +00:00
}
2015-09-06 13:42:57 +00:00
$query .= implode(', ', $tables);
2015-05-14 11:30:40 +00:00
2015-09-06 13:42:57 +00:00
return array(
'sql' => $query,
'binds' => null,
2015-09-06 13:42:57 +00:00
);
2015-05-14 11:30:40 +00:00
}
/**
* (Inner) join.
2015-05-14 11:30:40 +00:00
*
* @param string $table The table to join
* @param string $type The type of join to perform (JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN)
*
2015-05-14 11:30:40 +00:00
* @return $this
*
2015-05-14 11:30:40 +00:00
* @throws Exception
*/
public function join($table, $type = '')
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalJoin($table, $type = '')
{
if ($table === '') {
if ($this->table == '') {
throw new Exception('No table given');
}
$table = $this->table;
}
2015-09-06 13:42:57 +00:00
$query = '';
if ($type != '') {
$query .= $type.' ';
}
2015-05-14 11:30:40 +00:00
$query .= 'JOIN '.(strpos($table, ' ') === false ? "$table" : ''.implode(' ', explode(' ', $table)));
2015-09-06 13:42:57 +00:00
return array(
'sql' => $query,
'binds' => null,
2015-09-06 13:42:57 +00:00
);
}
/**
* Left join.
*
2015-05-14 11:30:40 +00:00
* @param string $table The table to join
*
2015-05-14 11:30:40 +00:00
* @return $this
*
2015-05-14 11:30:40 +00:00
* @throws Exception
*/
public function left_join($table)
{
2015-05-14 11:30:40 +00:00
return $this->join($table, 'LEFT');
}
/**
* Right join.
2015-05-14 11:30:40 +00:00
*
* @param string $table The table to join
*
2015-05-14 11:30:40 +00:00
* @return $this
*
2015-05-14 11:30:40 +00:00
* @throws Exception
*/
public function right_join($table)
{
2015-05-14 11:30:40 +00:00
return $this->join($table, 'RIGHT');
}
/**
* Full join.
2015-05-14 11:30:40 +00:00
*
* @param string $table The table to join
*
2015-05-14 11:30:40 +00:00
* @return $this
*
2015-05-14 11:30:40 +00:00
* @throws Exception
*/
public function full_join($table)
{
2015-05-14 11:30:40 +00:00
return $this->join($table, 'FULL');
}
/**
* On.
2015-05-14 11:30:40 +00:00
*
* @param string $field Field name, or raw SQL
* @param $arg2 string, The value of the field, or an operator in which case the value is pushed to $arg3
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
*
* @return $this
*/
public function on($field, $arg2 = null, $arg3 = null)
{
2015-05-14 11:30:40 +00:00
return $this->where($field, $arg2, $arg3, 'ON');
}
/**
* Where.
2015-05-14 11:30:40 +00:00
*
2015-09-06 13:42:57 +00:00
* @param string|null $field Field name, or raw SQL, If this is left empty, only the $type will be added to the query
* @param string $arg2, The value of the field, or an operator in which case the value is pushed to $arg3
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
* @param string $type Whether this is an WHERE or ON operation
*
2015-05-14 11:30:40 +00:00
* @return $this
*/
public function where($field = null, $arg2 = null, $arg3 = null, $type = 'WHERE')
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalWhere($field = null, $arg2 = null, $arg3 = null, $type = 'WHERE')
{
if ($field == null) {
2015-09-06 13:42:57 +00:00
return array('sql' => $type, 'binds' => array());
}
2015-05-14 11:30:40 +00:00
if ($arg2 === null) {
return $this->internalSql($type.' '.$field);
}
2015-09-06 13:42:57 +00:00
//The value is the second parameter, unless the second parameter is an operator, in which case it's the third
$value = ($arg3 == null ? $arg2 : $arg3);
//If the third parameter is not given, the default operator should be used
$operator = strtoupper($arg3 == null ? '=' : $arg2);
2015-09-06 13:42:57 +00:00
$query = '';
$binds = array();
2015-05-14 11:30:40 +00:00
switch ($operator) {
case 'IN':
$query .= $type.($type == '' ? '' : ' ').$field.' IN (';
if (is_array($value)) {
2015-09-06 13:42:57 +00:00
foreach ($value as $k => $v) {
$query .= '?,';
$binds[] = $v;
}
2015-09-06 13:42:57 +00:00
//Remove the trailing comma and close it
$query = rtrim($query, ',').')';
} elseif (get_class($value) == 'System\Core\Query') {
/* @var Query $value */
2015-09-06 13:42:57 +00:00
$data = $this->internalSql($value->getSql(), $value->getBinds());
$query .= $data['sql'].')';
2015-09-06 13:42:57 +00:00
$binds = array_merge($binds, $data['binds']);
}
break;
case 'BETWEEN':
$query .= $type.($type == '' ? '' : ' ').$field.' BETWEEN ? AND ?';
2015-09-06 13:42:57 +00:00
$binds[] = $value[0];
$binds[] = $value[1];
break;
default:
$query .= $type.($type == '' ? '' : ' ').$field.' '.$operator.' ?';
2015-09-06 13:42:57 +00:00
$binds[] = $value;
break;
}
2015-09-06 13:42:57 +00:00
return array(
'sql' => $query,
'binds' => $binds,
2015-09-06 13:42:57 +00:00
);
}
/**
*And, keep in mind this is only the OR statement. For A = B call ->where.
*
2015-09-06 13:42:57 +00:00
* @return array
*/
private function internalOr()
{
2015-09-06 13:42:57 +00:00
return array(
'sql' => 'OR',
'binds' => array(),
2015-09-06 13:42:57 +00:00
);
}
/**
* And, keep in mind this is only the AND statement. For A = B call ->where.
*
2015-09-06 13:42:57 +00:00
* @return array
*/
private function internalAnd()
{
2015-09-06 13:42:57 +00:00
return array(
'sql' => 'AND',
'binds' => array(),
2015-09-06 13:42:57 +00:00
);
}
/**
* An opening bracket.
*
* @return $this
*/
public function open()
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalOpen()
{
2015-09-06 13:42:57 +00:00
return array(
'sql' => '(',
'binds' => '',
2015-09-06 13:42:57 +00:00
);
}
/**
* A closing bracket.
*
* @return $this
*/
public function close()
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalClose()
{
2015-09-06 13:42:57 +00:00
return array(
'sql' => ')',
'binds' => null,
2015-09-06 13:42:57 +00:00
);
}
/**
* Order By.
*
* Each argument is another order. If you put a minus in front of the name, the order will be DESC instead of ASC
*
2015-09-06 13:42:57 +00:00
* @param string $field,... The field to order by, add a minus in front of the name and the order will be DESC
*
* @return $this
*/
public function order($field)
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalOrder($field)
{
2015-09-06 13:42:57 +00:00
$query = 'ORDER BY';
foreach (func_get_args() as $field) {
if (substr($query, -2) != 'BY') {
$query .= ',';
}
$mode = 'ASC';
if (substr($field, 0, 1) == '-') {
$field = substr($field, 1);
$mode = 'DESC';
}
$query .= ' '.$field.' '.$mode;
}
2015-09-06 13:42:57 +00:00
return array(
'sql' => $query,
'binds' => null,
2015-09-06 13:42:57 +00:00
);
}
/**
* Group by.
*
2015-09-06 13:42:57 +00:00
* Each argument is another field to group by.
*
2015-09-06 13:42:57 +00:00
* @param string $field,... The field to group by
*
* @return $this
*/
public function groupBy($field)
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalGroupBy($field)
{
2015-09-06 13:42:57 +00:00
$query = 'GROUP BY ';
2015-09-06 13:42:57 +00:00
$first = true;
foreach (func_get_args() as $field) {
if (!$first) {
2015-09-06 13:42:57 +00:00
$query .= ', ';
}
2015-09-06 13:42:57 +00:00
$first = false;
2015-09-06 13:42:57 +00:00
$query .= $field;
}
2015-09-06 13:42:57 +00:00
return array(
'sql' => $query,
'binds' => null,
);
}
2015-09-06 13:42:57 +00:00
/**
* limit.
2015-09-06 13:42:57 +00:00
*
* @param $limit int Limit
* @param int $offset int Offset
*
2015-09-06 13:42:57 +00:00
* @return $this
*/
public function limit($limit, $offset = 0)
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalLimit($limit, $offset = 0)
{
2015-09-06 13:42:57 +00:00
return array(
'sql' => 'LIMIT '.$offset.', '.$limit,
'binds' => null,
2015-09-06 13:42:57 +00:00
);
}
/**
* Having.
*
* @param $field
* @param $arg2 string, The value of the field, or an operator in which case the value is pushed to $arg3
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
*
* @return $this
*/
public function having($field = null, $arg2 = null, $arg3 = null)
{
return $this->where($field, $arg2, $arg3, 'HAVING');
}
/**
* Update.
*
* @param $table null|string Name of the table, if it is null, the table set with setTable() will be used
*
* @return $this
*
* @throws Exception
*/
public function update($table = '')
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
2015-09-06 13:42:57 +00:00
private function internalUpdate($table = '')
{
if ($table === '') {
if ($this->table == '') {
throw new Exception('No table given');
}
$table = $this->table;
}
2015-09-06 13:42:57 +00:00
return array(
'sql' => 'UPDATE '.$table,
'binds' => array(),
2015-09-06 13:42:57 +00:00
);
}
/**
* Set.
*
2015-05-14 11:30:40 +00:00
* @param $data array|string Key value, $field => $value or raw SQL
*
* @return $this
*/
public function set($data)
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalSet($data)
{
if (is_string($data)) {
2015-09-06 13:42:57 +00:00
return $this->internalSql('SET '.$data);
}
2015-09-06 13:42:57 +00:00
$query = 'SET';
$binds = array();
$first = true;
foreach ($data as $field => $value) {
if (!$first) {
2015-09-06 13:42:57 +00:00
$query .= ',';
}
$first = false;
$query .= ' '.$field.'=?';
2015-09-06 13:42:57 +00:00
$binds[] = $value;
}
2015-09-06 13:42:57 +00:00
return array(
'sql' => $query,
'binds' => $binds,
2015-09-06 13:42:57 +00:00
);
}
/**
* Delete.
*
* @return $this
*/
public function delete()
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
2015-09-06 13:42:57 +00:00
private function internalDelete()
{
return array(
'sql' => 'DELETE',
'binds' => array(),
2015-09-06 13:42:57 +00:00
);
}
/**
* Insert.
*
* @param array array Key value, $field => $value
* @param $table string|null Table name, if it is null, the table set with setTable() will be used
*
* @return $this
*
* @throws Exception
*/
public function insert($array, $table = '')
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalInsert($array, $table = '')
{
if ($table === '') {
if ($this->table == '') {
throw new DatabaseException('No table given');
}
2015-09-06 13:42:57 +00:00
$table = $this->table;
}
$query = '';
$binds = array();
//Implode the array to get a list with the fields
$query .= 'INSERT INTO '.$table.' ('.implode(',', array_keys($array)).') VALUES (';
2015-09-06 13:42:57 +00:00
//Add all the values as ? and add them to the binds
foreach ($array as $field => $value) {
2015-09-06 13:42:57 +00:00
$query .= '?,';
$binds[] = $value;
}
$query = rtrim($query, ',').')';
2015-09-06 13:42:57 +00:00
return array(
'sql' => $query,
'binds' => $binds,
2015-09-06 13:42:57 +00:00
);
}
/**
* Replace.
2015-09-06 13:42:57 +00:00
*
* @param array array Key value, $field => $value
2015-09-06 13:42:57 +00:00
* @param $table string|null Table name, if it is null, the table set with setTable() will be used
*
2015-09-06 13:42:57 +00:00
* @return $this
*
2015-09-06 13:42:57 +00:00
* @throws Exception
*/
public function replace($array, $table = '')
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
private function internalReplace($array, $table = '')
{
if ($table === '') {
if ($this->table == '') {
throw new DatabaseException('No table given');
}
$table = $this->table;
}
2015-09-06 13:42:57 +00:00
$query = '';
$binds = array();
//Implode the array to get a list with the fields
$query .= 'REPLACE INTO '.$table.' ('.implode(',', array_keys($array)).') VALUES (';
//Add all the values as ? and add them to the binds
foreach ($array as $field => $value) {
2015-09-06 13:42:57 +00:00
$query .= '?,';
2015-09-06 13:42:57 +00:00
$binds[] = $value;
}
$query = rtrim($query, ',').')';
2015-09-06 13:42:57 +00:00
return array(
'sql' => $query,
'binds' => $binds,
2015-09-06 13:42:57 +00:00
);
}
2015-05-14 11:30:40 +00:00
/**
* Add raw SQL to the query string.
*
* @param string $sql The SQL to add
* @param array $binds The optional binds to add
2015-05-14 11:30:40 +00:00
*
2015-09-06 13:42:57 +00:00
* @return $this
2015-05-14 11:30:40 +00:00
*/
public function sql($sql, $binds = array())
{
2015-09-06 13:42:57 +00:00
return $this->__call(__FUNCTION__, func_get_args());
}
2015-05-14 11:30:40 +00:00
private function internalSql($sql, $binds = array())
{
2015-09-06 13:42:57 +00:00
return array(
'sql' => $sql,
'binds' => $binds,
2015-09-06 13:42:57 +00:00
);
2015-05-14 11:30:40 +00:00
}
/**
2015-09-06 13:42:57 +00:00
* Builds The SQL query and its binds.
*
2015-09-06 13:42:57 +00:00
* @return array
2015-05-14 11:30:40 +00:00
*/
public function build()
{
2015-09-06 13:42:57 +00:00
$sql = array();
$binds = array();
2015-05-14 11:30:40 +00:00
foreach ($this->functions as $i => $function) {
2015-09-06 13:42:57 +00:00
//The current function is a WHERE or HAVING,
//and it after a or, and or open, this means we do not have to
//add the WHERE/HAVING statement again
if (($function['name'] == 'internalWhere'
|| $function['name'] == 'internalHaving')
&& ($this->functions[$i - 1]['name'] == 'internalOpen'
|| $this->functions[$i - 1]['name'] == 'internalAnd'
|| $this->functions[$i - 1]['name'] == 'internalOr')
) {
if (!isset($function['arguments'][2])) {
2015-09-06 13:42:57 +00:00
$function['arguments'][2] = null;
}
2015-09-06 13:42:57 +00:00
$function['arguments'][3] = '';
}
2015-05-14 11:30:40 +00:00
2015-09-06 13:42:57 +00:00
$data = call_user_func_array(array($this, $function['name']), $function['arguments']);
$sql[] = $data['sql'];
if ($data['binds'] != null) {
2015-09-06 13:42:57 +00:00
$binds = array_merge($binds, $data['binds']);
}
2015-09-06 13:42:57 +00:00
}
return array(
'sql' => implode(' ', $sql),
'binds' => $binds,
2015-09-06 13:42:57 +00:00
);
}
/**
* Returns the query as an array.
*
2015-09-06 13:42:57 +00:00
* @return array
*/
public function getSql()
{
2015-09-06 13:42:57 +00:00
return $this->build()['sql'];
}
/**
* Return the built SQL query.
*
2015-09-06 13:42:57 +00:00
* @return string
*/
public function getBinds()
{
2015-09-06 13:42:57 +00:00
return $this->build()['binds'];
}
/**
* @param $table
*
* @return $this
*/
public function setTable($table)
{
$this->table = $table;
return $this;
}
/**
* @return null|string
*/
public function getTable()
{
return $this->table;
}
/**
* @return $this
*/
public function commit()
{
Modules::get('core/database')->commit();
return $this;
}
/**
* @return $this
*/
public function beginTransaction()
{
Modules::get('core/database')->beginTransaction();
return $this;
}
/**
* @return $this
*/
public function rollback()
{
Modules::get('core/database')->rollback();
return $this;
}
/**
* Executes the query generated.
*
* @return $this
*
* @throws DatabaseException
*/
public function execute()
{
if (Config::get('database')->debug) {
2015-09-06 13:42:57 +00:00
Logger::log(get_class($this).': '.$this->getSql());
}
try {
$this->sth = Modules::get('core/database')->prepare($this->getSql());
if (count($this->getBinds()) === 0) {
$this->sth->execute();
} else {
2015-09-06 13:42:57 +00:00
$this->sth->execute($this->getBinds());
}
} catch (\PDOException $e) {
throw new DatabaseException('Could not execute SQL-query due PDO-exception '.$e->getMessage());
}
return $this;
}
/**
2015-09-06 13:42:57 +00:00
* Returns the results of the query in the given type. All the arguments of this function will be passed onto
* fetchAll.
*
2015-09-06 13:42:57 +00:00
* @param int $type The default type is \PDO::FETCH_ASSOC, all the types that are possible for fetchAll() are valid
*
2015-09-06 13:42:57 +00:00
* @return array
*
* @throws DatabaseException
*/
public function getResults($type = \PDO::FETCH_ASSOC)
{
if (!isset($this->sth)) {
$this->execute();
}
return call_user_func_array(
array(
2015-09-06 13:42:57 +00:00
$this->sth,
'fetchAll',
),
func_get_args()
);
}
/**
* Returns the amount of rows that are affected. ->execute must be called first.
*
* @return int Amount of rows that are affected
*
* @throws DatabaseException
*/
public function getRowCount()
{
if (!isset($this->sth)) {
$this->execute();
}
return $this->sth->rowCount();
}
/**
* @return string
*/
public function getLastInsertId()
{
return Modules::get('core/database')->lastInsertId();
}
/**
* All the called functions are saved in an array, so when build is called, the query is built from all the functions.
*
* @param $name string
* @param $arguments array
*
2015-09-06 13:42:57 +00:00
* @return $this
*
2015-09-06 13:42:57 +00:00
* @throws DatabaseException
*/
public function __call($name, $arguments)
{
$name = 'internal'.ucfirst($name);
if (!method_exists($this, $name)) {
throw new DatabaseException('Unknown function "'.$name.'"');
}
2015-09-06 13:42:57 +00:00
$this->functions[] = array(
'name' => $name,
'arguments' => $arguments,
2015-09-06 13:42:57 +00:00
);
return $this;
}
}