richdocuments/lib/Db/DbBase.php

221 lines
5.0 KiB
PHP
Raw Normal View History

2013-09-07 19:49:48 +03:00
<?php
/**
2015-12-16 17:57:44 +03:00
* ownCloud - Richdocuments App
2013-09-07 19:49:48 +03:00
*
* @author Victor Dubiniuk
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Richdocuments\Db;
2013-09-07 19:49:48 +03:00
2013-09-26 22:55:17 +03:00
/**
2017-03-14 11:59:37 +01:00
* Generic DB class
2013-09-26 22:55:17 +03:00
*/
abstract class DbBase {
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
protected $data;
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
protected $tableName;
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
protected $insertStatement;
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
protected $loadStatement;
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
public function __construct($data = array()){
$this->setData($data);
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Insert current object data into database
* @return mixed
*/
public function insert(){
$result = $this->execute($this->insertStatement);
return $result;
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Get id of the recently inserted record
* @return mixed
*/
public function getLastInsertId(){
2015-08-27 01:57:31 +03:00
return \OC::$server->getDatabaseConnection()->lastInsertId($this->tableName);
2013-09-27 18:43:10 +03:00
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Get single record by primary key
* @param int $value primary key value
2015-12-16 17:57:44 +03:00
* @return \OCA\Richdocuments\Db
2013-09-27 18:43:10 +03:00
*/
public function load($value){
if (!is_array($value)){
$value = array($value);
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
$result = $this->execute($this->loadStatement, $value);
$data = $result->fetch();
2013-09-27 18:43:10 +03:00
if (!is_array($data)){
$data = array();
}
$this->data = $data;
return $this;
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Get single record matching condition
* @param string $field for WHERE condition
* @param mixed $value matching value(s)
2015-12-16 17:57:44 +03:00
* @return \OCA\Richdocuments\Db
2013-09-27 18:43:10 +03:00
* @throws Exception
*/
public function loadBy($field, $value){
if (!is_array($value)){
$value = array($value);
}
$result = $this->execute('SELECT * FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
$data = $result->fetchAll();
if (!is_array($data) || !count($data)){
$this->data = array();
2017-03-14 11:59:37 +01:00
} elseif (count($data) !== 1) {
2013-09-27 18:43:10 +03:00
throw new Exception('Duplicate ' . $value . ' for the filed ' . $field);
} else {
$this->data = $data[0];
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
return $this;
}
/**
* Delete records matching the condition
* @param string $field for WHERE condition
* @param mixed $value matching value(s)
*/
public function deleteBy($field, $value){
if (!is_array($value)){
$value = array($value);
}
2013-10-15 21:12:50 +03:00
$count = count($value);
if ($count===0){
2014-05-14 00:06:10 +03:00
return;
2013-10-15 21:12:50 +03:00
} elseif ($count===1){
2014-05-14 00:06:10 +03:00
$this->execute('DELETE FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
2013-09-27 18:43:10 +03:00
} else {
$stmt = $this->buildInQuery($field, $value);
2014-05-14 00:06:10 +03:00
$this->execute('DELETE FROM ' . $this->tableName . ' WHERE ' . $stmt, $value);
2013-09-27 18:43:10 +03:00
}
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Get all records from the table
* @return array
*/
public function getCollection(){
$result = $this->execute('SELECT * FROM ' . $this->tableName);
$data = $result->fetchAll();
if (!is_array($data)){
$data = array();
}
return $data;
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Get array of matching records
* @param string $field for WHERE condition
* @param mixed $value matching value(s)
* @return array
*/
public function getCollectionBy($field, $value){
if (!is_array($value)){
$value = array($value);
}
2013-10-15 21:12:50 +03:00
$count = count($value);
if ($count===0){
return array();
} elseif ($count===1){
2013-09-27 18:43:10 +03:00
$result = $this->execute('SELECT * FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
} else {
$stmt = $this->buildInQuery($field, $value);
$result = $this->execute('SELECT * FROM ' . $this->tableName . ' WHERE '. $stmt , $value);
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
$data = $result->fetchAll();
if (!is_array($data)){
$data = array();
}
return $data;
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Get object data
* @return Array
*/
public function getData(){
return $this->data;
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Set object data
* @param array $data
*/
public function setData($data){
$this->data = $data;
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Check if there are any data in current object
2017-03-14 11:59:37 +01:00
* @return bool
2013-09-27 18:43:10 +03:00
*/
public function hasData(){
return count($this->data)>0;
}
2017-03-14 11:59:37 +01:00
2013-09-07 19:49:48 +03:00
/**
2017-03-14 11:59:37 +01:00
* Build placeholders for the query with variable input data
2013-09-27 18:43:10 +03:00
* @param string $field field name
2013-09-07 19:49:48 +03:00
* @param Array $array data
2013-09-27 18:43:10 +03:00
* @return String `field` IN (?, ?...) placeholders matching the number of elements in array
2013-09-07 19:49:48 +03:00
*/
2013-09-27 18:43:10 +03:00
protected function buildInQuery($field, $array){
2013-09-07 19:49:48 +03:00
$count = count($array);
$placeholders = array_fill(0, $count, '?');
$stmt = implode(', ', $placeholders);
2013-09-27 18:43:10 +03:00
return '`' . $field . '` IN (' . $stmt . ')';
}
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
/**
* Execute a query on database
* @param string $statement query to be executed
2017-03-14 11:59:37 +01:00
* @param mixed $args value(s) for the query.
2013-09-27 18:43:10 +03:00
* If omited the query will be run on the current object $data
* @return mixed (array/false)
*/
protected function execute($statement, $args = null){
$query = \OC::$server->getDatabaseConnection()->prepare($statement);
2017-03-14 11:59:37 +01:00
2013-09-27 18:43:10 +03:00
if (!is_null($args)){
$result = $query->execute($args);
} elseif (count($this->data)){
$result = $query->execute($this->data);
} else {
$result = $query->execute();
}
2017-03-14 11:59:37 +01:00
return $result ? $query : false;
2013-09-07 19:49:48 +03:00
}
2017-03-14 11:59:37 +01:00
2014-04-14 18:44:06 +03:00
public function __call($name, $arguments){
2014-04-11 23:12:23 +03:00
if (substr($name, 0, 3) === 'get'){
$requestedProperty = substr($name, 3);
$property = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $requestedProperty));
if (isset($this->data[$property])){
return $this->data[$property];
}
}
return null;
}
2013-09-27 18:43:10 +03:00
}