2015-05-06 19:25:57 +00:00
|
|
|
<?php
|
2015-08-29 15:43:13 +00:00
|
|
|
/**
|
|
|
|
* 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 - 2015, 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
|
|
|
|
*/
|
|
|
|
|
2015-05-06 19:25:57 +00:00
|
|
|
|
|
|
|
namespace Module\DatabaseUtils;
|
|
|
|
use \FuzeWorks\Module;
|
2015-10-11 18:14:49 +00:00
|
|
|
use \FuzeWorks\Modules;
|
2015-05-06 19:25:57 +00:00
|
|
|
use \FuzeWorks\Bus;
|
|
|
|
use \FuzeWorks\ModelServer;
|
|
|
|
use \FuzeWorks\DatabaseException;
|
|
|
|
|
2015-08-29 15:43:13 +00:00
|
|
|
/**
|
|
|
|
* Main class of the database utilities and model providers
|
|
|
|
* @package net.techfuze.fuzeworks.databaseutils
|
|
|
|
* @author Abel Hoogeveen <abel@techfuze.net>
|
|
|
|
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
|
|
|
|
*/
|
2016-01-17 13:42:39 +00:00
|
|
|
class Main implements ModelServer {
|
|
|
|
use Module;
|
2015-05-06 19:25:57 +00:00
|
|
|
|
|
|
|
public $fields = array();
|
|
|
|
public $primary = 'id';
|
|
|
|
public $table = '';
|
|
|
|
|
|
|
|
public function onLoad() {
|
2016-01-17 13:42:39 +00:00
|
|
|
require_once(self::getModulePath() . '/class.query.php');
|
2015-05-06 19:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function giveModel($type) {
|
2015-09-05 19:47:35 +00:00
|
|
|
return new Model();
|
2015-05-06 19:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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?
|
|
|
|
*
|
2015-08-29 15:43:13 +00:00
|
|
|
* @package net.techfuze.fuzeworks.databaseutils
|
|
|
|
* @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
|
2015-05-06 19:25:57 +00:00
|
|
|
*/
|
2015-09-05 19:47:35 +00:00
|
|
|
class Model {
|
2015-05-06 19:25:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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($this->config->database->debug)
|
2015-07-28 10:06:40 +00:00
|
|
|
$this->logger->log("Manuel Query: ".$query, "Database Model");
|
2015-05-06 19:25:57 +00:00
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
$sth = $this->mods->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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default table will be set to $this->table
|
|
|
|
* @see Query::select
|
|
|
|
* @return Query
|
|
|
|
*/
|
|
|
|
public function select(){
|
|
|
|
|
2015-09-06 13:42:57 +00:00
|
|
|
$queryBuilder = new Query();
|
2015-05-06 19:25:57 +00:00
|
|
|
$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(){
|
|
|
|
|
2015-09-06 13:42:57 +00:00
|
|
|
$queryBuilder = new Query();
|
2015-05-06 19:25:57 +00:00
|
|
|
$queryBuilder->setTable($this->table);
|
|
|
|
call_user_func_array(array($queryBuilder, 'update'), func_get_args());
|
|
|
|
|
|
|
|
return $queryBuilder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default table will be set to $this->table
|
|
|
|
* @see Query::delete
|
|
|
|
* @return Query
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function delete(){
|
|
|
|
|
2015-09-06 13:42:57 +00:00
|
|
|
$queryBuilder = new Query();
|
2015-05-06 19:25:57 +00:00
|
|
|
$queryBuilder->setTable($this->table);
|
|
|
|
call_user_func_array(array($queryBuilder, 'delete'), func_get_args());
|
|
|
|
|
|
|
|
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){
|
|
|
|
|
2015-09-06 13:42:57 +00:00
|
|
|
$queryBuilder = new Query();
|
2015-05-06 19:25:57 +00:00
|
|
|
$queryBuilder->setTable($this->table);
|
|
|
|
call_user_func_array(array($queryBuilder, 'insert'), func_get_args());
|
|
|
|
|
|
|
|
return $queryBuilder;
|
|
|
|
}
|
|
|
|
|
2015-10-11 18:14:49 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2015-05-06 19:25:57 +00:00
|
|
|
/**
|
|
|
|
* Return latest insert id
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getLastInsertId(){
|
|
|
|
return $this->mods->database->lastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __call($name, $params) {
|
2015-10-11 18:14:49 +00:00
|
|
|
return call_user_func_array(array(Modules::get('core/database'), $name), $params);
|
2015-05-06 19:25:57 +00:00
|
|
|
}
|
|
|
|
}
|