Removed the old database manager.
This commit is contained in:
parent
6956a3a0d9
commit
aa35631719
|
@ -1,167 +0,0 @@
|
|||
<?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\Database;
|
||||
|
||||
use FuzeWorks\Module;
|
||||
use FuzeWorks\Config;
|
||||
use FuzeWorks\Logger;
|
||||
use FuzeWorks\Events;
|
||||
use FuzeWorks\EventPriority;
|
||||
use PDO;
|
||||
use FuzeWorks\DatabaseException;
|
||||
|
||||
/**
|
||||
* Database Class.
|
||||
*
|
||||
* This class is a wrapper for PDO.
|
||||
*
|
||||
* @author Abel Hoogeveen <abel@techfuze.net>
|
||||
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
||||
*/
|
||||
class Main
|
||||
{
|
||||
use Module;
|
||||
|
||||
/**
|
||||
* The default database connection.
|
||||
*
|
||||
* @var PDO Class
|
||||
*/
|
||||
private $DBH;
|
||||
public $prefix;
|
||||
|
||||
public function onLoad()
|
||||
{
|
||||
Config::$dbActive = true;
|
||||
Events::addListener(array($this, 'shutdown'), 'coreShutdownEvent', EventPriority::NORMAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a database.
|
||||
*
|
||||
* @param StdObject Config, like the database config in Application/Config
|
||||
*/
|
||||
public function connect($config = null)
|
||||
{
|
||||
// If nothing is given, connect to database from the main config, otherwise use the served configuration
|
||||
if (is_null($config)) {
|
||||
$db = Config::get('database');
|
||||
} else {
|
||||
$db = $config;
|
||||
}
|
||||
|
||||
if (empty($db->type) || empty($db->host)) {
|
||||
throw (new DatabaseException('Database is not configured!'));
|
||||
}
|
||||
|
||||
// Get the DSN for popular types of databases or a custom DSN
|
||||
switch (strtolower($db->type)) {
|
||||
case 'mysql':
|
||||
$dsn = 'mysql:host='.$db->host.';';
|
||||
$dsn .= (!empty($db->database) ? 'dbname='.$db->database.';' : '');
|
||||
break;
|
||||
case 'custom':
|
||||
$dsn = $db->dsn;
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
Logger::logInfo("Connecting to '".$dsn."'", 'Database');
|
||||
// And create the connection
|
||||
$this->DBH = new PDO($dsn, $db->username, $db->password, (isset($db->options) ? $db->options : null));
|
||||
$this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
Logger::logInfo('Connected to database', 'Database');
|
||||
|
||||
// And set the prefix
|
||||
$this->prefix = $db->prefix;
|
||||
} catch (Exception $e) {
|
||||
throw (new DatabaseException('Could not connect to the database: "'.$e->getMessage().'"'));
|
||||
}
|
||||
}
|
||||
|
||||
public function shutdown()
|
||||
{
|
||||
Logger::log('Closing open database connections');
|
||||
$this->DBH = null;
|
||||
}
|
||||
|
||||
public function getPrefix()
|
||||
{
|
||||
if (!$this->is_active()) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
return $this->prefix;
|
||||
}
|
||||
|
||||
public function is_active()
|
||||
{
|
||||
if ($this->DBH === null) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function __call($name, $params)
|
||||
{
|
||||
if ($this->is_active()) {
|
||||
return call_user_func_array(array($this->DBH, $name), $params);
|
||||
} else {
|
||||
$this->connect();
|
||||
|
||||
return call_user_func_array(array($this->DBH, $name), $params);
|
||||
}
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
if ($this->is_active()) {
|
||||
return $this->DBH->$name;
|
||||
} else {
|
||||
$this->connect();
|
||||
|
||||
return $this->DBH->$name;
|
||||
}
|
||||
}
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
if ($this->is_active()) {
|
||||
$this->DBH->$name = $value;
|
||||
} else {
|
||||
$this->connect();
|
||||
$this->DBH->$name = $value;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
<?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
|
||||
*/
|
||||
|
||||
return array(
|
||||
|
||||
'module_class' => 'Module\Database\Main',
|
||||
'module_file' => 'class.main.php',
|
||||
'module_name' => 'Database',
|
||||
|
||||
'abstract' => false,
|
||||
'dependencies' => array(),
|
||||
'events' => array(),
|
||||
'sections' => array(),
|
||||
'aliases' => array(),
|
||||
|
||||
'name' => 'FuzeWorks Database Module',
|
||||
'description' => 'PDO Wrapper class for FuzeWorks',
|
||||
'author' => 'core',
|
||||
'version' => '1.0.0',
|
||||
'website' => 'http://fuzeworks.techfuze.net/',
|
||||
|
||||
'date_created' => '30-04-2015',
|
||||
'date_updated' => '30-04-2015',
|
||||
|
||||
'enabled' => true,
|
||||
);
|
|
@ -1,245 +0,0 @@
|
|||
<?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\Module;
|
||||
use FuzeWorks\Modules;
|
||||
use FuzeWorks\ModelServer;
|
||||
use FuzeWorks\DatabaseException;
|
||||
use FuzeWorks\Config;
|
||||
use FuzeWorks\Logger;
|
||||
use PDOException;
|
||||
|
||||
/**
|
||||
* Main class of the database utilities and model providers.
|
||||
*
|
||||
* @author Abel Hoogeveen <abel@techfuze.net>
|
||||
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
||||
*/
|
||||
class Main implements ModelServer
|
||||
{
|
||||
use Module;
|
||||
|
||||
public $fields = array();
|
||||
public $primary = 'id';
|
||||
public $table = '';
|
||||
|
||||
public function onLoad()
|
||||
{
|
||||
include_once self::getModulePath().'/class.query.php';
|
||||
}
|
||||
|
||||
public function giveModel($type)
|
||||
{
|
||||
return new Model();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class Model.
|
||||
*
|
||||
* Models provide an easy connection to database tables. Each table requires its own model. You don't need to worry about your queries or syntax anymore, because
|
||||
* models will handle queries and error handling. Because of this, you can freely change your database infrastructure without fear of needing to change your
|
||||
* table names at thousands of places. The model is the only place where you have to change your database names or fields.
|
||||
*
|
||||
* Models also allow custom methods to be created on them. You can use those methods to create specific operations or joins and then use the newly created method
|
||||
* everywhere in your project. The code is at one place, the usages all over your project. Isn't that great?
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
class Model
|
||||
{
|
||||
/**
|
||||
* @var string The name of the database table
|
||||
*/
|
||||
public $table = '';
|
||||
|
||||
/**
|
||||
* Traditional query interface.
|
||||
*
|
||||
* @param string $query
|
||||
* @param null $binds
|
||||
*
|
||||
* @return mixed returns fetched rows if available, otherwise returns number of affected rows
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function query($query, $binds = null)
|
||||
{
|
||||
if (Config::get('database')->debug) {
|
||||
Logger::log('Manuel Query: '.$query, 'Database Model');
|
||||
}
|
||||
|
||||
try {
|
||||
$sth = Modules::get('core/database')->prepare($query);
|
||||
if ($binds === null) {
|
||||
$sth->execute();
|
||||
} else {
|
||||
$sth->execute($binds);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new DatabaseException('Could not execute SQL-query due PDO-exception '.$e->getMessage());
|
||||
}
|
||||
|
||||
if ($sth->columnCount() > 0) {
|
||||
// Fetch results
|
||||
$result = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||
} else {
|
||||
// Fetch number of affected rows
|
||||
$result = $sth->rowCount();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the table you wish to approach.
|
||||
*
|
||||
* @param string $table Table name
|
||||
*/
|
||||
public function setTable($table)
|
||||
{
|
||||
$this->table = $table;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The default table will be set to $this->table.
|
||||
*
|
||||
* @see Query::select
|
||||
*
|
||||
* @return Query
|
||||
*/
|
||||
public function select()
|
||||
{
|
||||
$queryBuilder = new Query();
|
||||
$queryBuilder->setTable($this->table);
|
||||
call_user_func_array(array($queryBuilder, 'select'), func_get_args());
|
||||
$queryBuilder->from();
|
||||
|
||||
return $queryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* The default table will be set to $this->table.
|
||||
*
|
||||
* @see Query::update
|
||||
*
|
||||
* @return Query
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
$queryBuilder = new Query();
|
||||
$queryBuilder->setTable($this->table);
|
||||
$queryBuilder->update($this->table);
|
||||
|
||||
return $queryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* The default table will be set to $this->table.
|
||||
*
|
||||
* @see Query::delete
|
||||
*
|
||||
* @return Query
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$queryBuilder = new Query();
|
||||
$queryBuilder->setTable($this->table);
|
||||
call_user_func_array(array($queryBuilder, 'delete'), func_get_args());
|
||||
$queryBuilder->from();
|
||||
|
||||
return $queryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* The default table will be set to $this->table.
|
||||
*
|
||||
* @see Query::insert
|
||||
*
|
||||
* @param $array Array with values
|
||||
*
|
||||
* @return Query
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function insert($array)
|
||||
{
|
||||
$queryBuilder = new Query();
|
||||
$queryBuilder->setTable($this->table);
|
||||
call_user_func_array(array($queryBuilder, 'insert'), func_get_args());
|
||||
|
||||
return $queryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* The default table will be set to $this->table.
|
||||
*
|
||||
* @see Query::replace
|
||||
*
|
||||
* @param $array Array with values
|
||||
*
|
||||
* @return Query
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function replace($array)
|
||||
{
|
||||
$queryBuilder = new Query();
|
||||
$queryBuilder->setTable($this->table);
|
||||
call_user_func_array(array($queryBuilder, 'replace'), func_get_args());
|
||||
|
||||
return $queryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return latest insert id.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLastInsertId()
|
||||
{
|
||||
return Modules::get('core/database')->lastInsertId();
|
||||
}
|
||||
|
||||
public function __call($name, $params)
|
||||
{
|
||||
return call_user_func_array(array(Modules::get('core/database'), $name), $params);
|
||||
}
|
||||
}
|
|
@ -1,891 +0,0 @@
|
|||
<?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
|
||||
{
|
||||
/**
|
||||
* @var array An array containing all the counted functions
|
||||
*/
|
||||
private $functions = array();
|
||||
|
||||
/**
|
||||
* @var null|string The table used
|
||||
*/
|
||||
private $table = '';
|
||||
|
||||
/**
|
||||
* @var null|\PDOStatement
|
||||
*/
|
||||
private $sth = null;
|
||||
|
||||
/**
|
||||
* @param string $table The string to set as a table, optional
|
||||
*/
|
||||
public function __construct($table = '')
|
||||
{
|
||||
if ($table != '') {
|
||||
$this->setTable($table);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Each argument is another field to select.
|
||||
*
|
||||
* @param string $field,... The field to select, if no field is given, all fields(*) will be selected
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function select($field = '*')
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalSelect($field = '*')
|
||||
{
|
||||
return array(
|
||||
'sql' => 'SELECT '.($field == '*' ? '*' : implode(', ', func_get_args()).''),
|
||||
'binds' => null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* From.
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalFrom($table = null)
|
||||
{
|
||||
$tables = func_get_args();
|
||||
|
||||
$query = 'FROM ';
|
||||
|
||||
if (count($tables) == 0) {
|
||||
$tables[] = "$this->table";
|
||||
} else {
|
||||
foreach ($tables as $i => $t) {
|
||||
if ($t == '' && $this->table == '') {
|
||||
throw new Exception('No table given');
|
||||
}
|
||||
|
||||
$tables[$i] = strpos($t, ' ') === false ? "$t" : ''.implode(' ', explode(' ', $t));
|
||||
}
|
||||
}
|
||||
|
||||
$query .= implode(', ', $tables);
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* (Inner) join.
|
||||
*
|
||||
* @param string $table The table to join
|
||||
* @param string $type The type of join to perform (JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN)
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function join($table, $type = '')
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
$query = '';
|
||||
|
||||
if ($type != '') {
|
||||
$query .= $type.' ';
|
||||
}
|
||||
|
||||
$query .= 'JOIN '.(strpos($table, ' ') === false ? "$table" : ''.implode(' ', explode(' ', $table)));
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Left join.
|
||||
*
|
||||
* @param string $table The table to join
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function left_join($table)
|
||||
{
|
||||
return $this->join($table, 'LEFT');
|
||||
}
|
||||
|
||||
/**
|
||||
* Right join.
|
||||
*
|
||||
* @param string $table The table to join
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function right_join($table)
|
||||
{
|
||||
return $this->join($table, 'RIGHT');
|
||||
}
|
||||
|
||||
/**
|
||||
* Full join.
|
||||
*
|
||||
* @param string $table The table to join
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function full_join($table)
|
||||
{
|
||||
return $this->join($table, 'FULL');
|
||||
}
|
||||
|
||||
/**
|
||||
* On.
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
return $this->where($field, $arg2, $arg3, 'ON');
|
||||
}
|
||||
|
||||
/**
|
||||
* Where.
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function where($field = null, $arg2 = null, $arg3 = null, $type = 'WHERE')
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalWhere($field = null, $arg2 = null, $arg3 = null, $type = 'WHERE')
|
||||
{
|
||||
if ($field == null) {
|
||||
return array('sql' => $type, 'binds' => array());
|
||||
}
|
||||
|
||||
if ($arg2 === null) {
|
||||
return $this->internalSql($type.' '.$field);
|
||||
}
|
||||
|
||||
//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);
|
||||
|
||||
$query = '';
|
||||
$binds = array();
|
||||
|
||||
switch ($operator) {
|
||||
case 'IN':
|
||||
$query .= $type.($type == '' ? '' : ' ').$field.' IN (';
|
||||
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
$query .= '?,';
|
||||
$binds[] = $v;
|
||||
}
|
||||
|
||||
//Remove the trailing comma and close it
|
||||
$query = rtrim($query, ',').')';
|
||||
} elseif (get_class($value) == 'System\Core\Query') {
|
||||
/* @var Query $value */
|
||||
$data = $this->internalSql($value->getSql(), $value->getBinds());
|
||||
$query .= $data['sql'].')';
|
||||
$binds = array_merge($binds, $data['binds']);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'BETWEEN':
|
||||
$query .= $type.($type == '' ? '' : ' ').$field.' BETWEEN ? AND ?';
|
||||
$binds[] = $value[0];
|
||||
$binds[] = $value[1];
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$query .= $type.($type == '' ? '' : ' ').$field.' '.$operator.' ?';
|
||||
$binds[] = $value;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => $binds,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*And, keep in mind this is only the OR statement. For A = B call ->where.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function internalOr()
|
||||
{
|
||||
return array(
|
||||
'sql' => 'OR',
|
||||
'binds' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* And, keep in mind this is only the AND statement. For A = B call ->where.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function internalAnd()
|
||||
{
|
||||
return array(
|
||||
'sql' => 'AND',
|
||||
'binds' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* An opening bracket.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function open()
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalOpen()
|
||||
{
|
||||
return array(
|
||||
'sql' => '(',
|
||||
'binds' => '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A closing bracket.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalClose()
|
||||
{
|
||||
return array(
|
||||
'sql' => ')',
|
||||
'binds' => null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalOrder($field)
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Group by.
|
||||
*
|
||||
* Each argument is another field to group by.
|
||||
*
|
||||
* @param string $field,... The field to group by
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function groupBy($field)
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalGroupBy($field)
|
||||
{
|
||||
$query = 'GROUP BY ';
|
||||
|
||||
$first = true;
|
||||
|
||||
foreach (func_get_args() as $field) {
|
||||
if (!$first) {
|
||||
$query .= ', ';
|
||||
}
|
||||
|
||||
$first = false;
|
||||
|
||||
$query .= $field;
|
||||
}
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* limit.
|
||||
*
|
||||
* @param $limit int Limit
|
||||
* @param int $offset int Offset
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function limit($limit, $offset = 0)
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalLimit($limit, $offset = 0)
|
||||
{
|
||||
return array(
|
||||
'sql' => 'LIMIT '.$offset.', '.$limit,
|
||||
'binds' => null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalUpdate($table = '')
|
||||
{
|
||||
if ($table === '') {
|
||||
if ($this->table == '') {
|
||||
throw new Exception('No table given');
|
||||
}
|
||||
|
||||
$table = $this->table;
|
||||
}
|
||||
|
||||
return array(
|
||||
'sql' => 'UPDATE '.$table,
|
||||
'binds' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set.
|
||||
*
|
||||
* @param $data array|string Key value, $field => $value or raw SQL
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set($data)
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalSet($data)
|
||||
{
|
||||
if (is_string($data)) {
|
||||
return $this->internalSql('SET '.$data);
|
||||
}
|
||||
|
||||
$query = 'SET';
|
||||
$binds = array();
|
||||
|
||||
$first = true;
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
if (!$first) {
|
||||
$query .= ',';
|
||||
}
|
||||
|
||||
$first = false;
|
||||
$query .= ' '.$field.'=?';
|
||||
|
||||
$binds[] = $value;
|
||||
}
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => $binds,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalDelete()
|
||||
{
|
||||
return array(
|
||||
'sql' => 'DELETE',
|
||||
'binds' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalInsert($array, $table = '')
|
||||
{
|
||||
if ($table === '') {
|
||||
if ($this->table == '') {
|
||||
throw new DatabaseException('No table given');
|
||||
}
|
||||
|
||||
$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 (';
|
||||
|
||||
//Add all the values as ? and add them to the binds
|
||||
foreach ($array as $field => $value) {
|
||||
$query .= '?,';
|
||||
|
||||
$binds[] = $value;
|
||||
}
|
||||
|
||||
$query = rtrim($query, ',').')';
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => $binds,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace.
|
||||
*
|
||||
* @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 replace($array, $table = '')
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
$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) {
|
||||
$query .= '?,';
|
||||
|
||||
$binds[] = $value;
|
||||
}
|
||||
|
||||
$query = rtrim($query, ',').')';
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => $binds,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add raw SQL to the query string.
|
||||
*
|
||||
* @param string $sql The SQL to add
|
||||
* @param array $binds The optional binds to add
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function sql($sql, $binds = array())
|
||||
{
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalSql($sql, $binds = array())
|
||||
{
|
||||
return array(
|
||||
'sql' => $sql,
|
||||
'binds' => $binds,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds The SQL query and its binds.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
$sql = array();
|
||||
$binds = array();
|
||||
|
||||
foreach ($this->functions as $i => $function) {
|
||||
//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])) {
|
||||
$function['arguments'][2] = null;
|
||||
}
|
||||
|
||||
$function['arguments'][3] = '';
|
||||
}
|
||||
|
||||
$data = call_user_func_array(array($this, $function['name']), $function['arguments']);
|
||||
|
||||
$sql[] = $data['sql'];
|
||||
|
||||
if ($data['binds'] != null) {
|
||||
$binds = array_merge($binds, $data['binds']);
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'sql' => implode(' ', $sql),
|
||||
'binds' => $binds,
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Returns the query as an array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSql()
|
||||
{
|
||||
return $this->build()['sql'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the built SQL query.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBinds()
|
||||
{
|
||||
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) {
|
||||
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 {
|
||||
$this->sth->execute($this->getBinds());
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
throw new DatabaseException('Could not execute SQL-query due PDO-exception '.$e->getMessage());
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the results of the query in the given type. All the arguments of this function will be passed onto
|
||||
* fetchAll.
|
||||
*
|
||||
* @param int $type The default type is \PDO::FETCH_ASSOC, all the types that are possible for fetchAll() are valid
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function getResults($type = \PDO::FETCH_ASSOC)
|
||||
{
|
||||
if (!isset($this->sth)) {
|
||||
$this->execute();
|
||||
}
|
||||
|
||||
return call_user_func_array(
|
||||
array(
|
||||
$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
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
$name = 'internal'.ucfirst($name);
|
||||
|
||||
if (!method_exists($this, $name)) {
|
||||
throw new DatabaseException('Unknown function "'.$name.'"');
|
||||
}
|
||||
|
||||
$this->functions[] = array(
|
||||
'name' => $name,
|
||||
'arguments' => $arguments,
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
<?php
|
||||
|
||||
return array(
|
||||
|
||||
'module_class' => 'Module\DatabaseUtils\Main',
|
||||
'module_file' => 'class.model.php',
|
||||
'module_name' => 'DatabaseUtils',
|
||||
|
||||
'abstract' => false,
|
||||
'dependencies' => array('core/database'),
|
||||
'events' => array(),
|
||||
'sections' => array(),
|
||||
'aliases' => array(),
|
||||
|
||||
'name' => 'FuzeWorks Database Utilities',
|
||||
'description' => 'Automatically build SQL queries using methods in this class',
|
||||
'author' => 'core',
|
||||
'version' => '1.0.0',
|
||||
'website' => 'http://fuzeworks.techfuze.net/',
|
||||
|
||||
'date_created' => '29-04-2015',
|
||||
'date_updated' => '29-04-2015',
|
||||
|
||||
'enabled' => true,
|
||||
);
|
Loading…
Reference in New Issue