Blame view
sources/apps/documents/lib/db.php
4.95 KB
|
d1bafeea1
|
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 |
<?php
/**
* ownCloud - Documents App
*
* @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\Documents;
/**
* Generic DB class
*/
abstract class Db {
protected $data;
protected $tableName;
protected $insertStatement;
protected $loadStatement;
public function __construct($data = array()){
$this->setData($data);
}
/**
* Insert current object data into database
* @return mixed
*/
public function insert(){
$result = $this->execute($this->insertStatement);
return $result;
}
/**
* Get id of the recently inserted record
* @return mixed
*/
public function getLastInsertId(){
return \OCP\DB::insertid($this->tableName);
}
/**
* Get single record by primary key
* @param type $value primary key value
* @return \OCA\Documents\Db
*/
public function load($value){
if (!is_array($value)){
$value = array($value);
}
$result = $this->execute($this->loadStatement, $value);
$data = $result->fetchRow();
if (!is_array($data)){
$data = array();
}
$this->data = $data;
return $this;
}
/**
* Get single record matching condition
* @param string $field for WHERE condition
* @param mixed $value matching value(s)
* @return \OCA\Documents\Db
* @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();
} elseif (count($data)!=1) {
throw new Exception('Duplicate ' . $value . ' for the filed ' . $field);
} else {
$this->data = $data[0];
}
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);
}
$count = count($value);
if ($count===0){
|
|
6d9380f96
|
103 |
return; |
|
d1bafeea1
|
104 |
} elseif ($count===1){
|
|
6d9380f96
|
105 |
$this->execute('DELETE FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
|
|
d1bafeea1
|
106 107 |
} else {
$stmt = $this->buildInQuery($field, $value);
|
|
6d9380f96
|
108 |
$this->execute('DELETE FROM ' . $this->tableName . ' WHERE ' . $stmt, $value);
|
|
d1bafeea1
|
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 208 209 210 211 212 213 214 215 216 217 218 219 220 |
}
}
/**
* 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;
}
/**
* 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);
}
$count = count($value);
if ($count===0){
return array();
} elseif ($count===1){
$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);
}
$data = $result->fetchAll();
if (!is_array($data)){
$data = array();
}
return $data;
}
/**
* Get object data
* @return Array
*/
public function getData(){
return $this->data;
}
/**
* Set object data
* @param array $data
*/
public function setData($data){
$this->data = $data;
}
/**
* Check if there are any data in current object
* @return bool
*/
public function hasData(){
return count($this->data)>0;
}
/**
* Build placeholders for the query with variable input data
* @param string $field field name
* @param Array $array data
* @return String `field` IN (?, ?...) placeholders matching the number of elements in array
*/
protected function buildInQuery($field, $array){
$count = count($array);
$placeholders = array_fill(0, $count, '?');
$stmt = implode(', ', $placeholders);
return '`' . $field . '` IN (' . $stmt . ')';
}
/**
* Execute a query on database
* @param string $statement query to be executed
* @param mixed $args value(s) for the query.
* If omited the query will be run on the current object $data
* @return mixed (array/false)
*/
protected function execute($statement, $args = null){
$query = \OCP\DB::prepare($statement);
if (!is_null($args)){
$result = $query->execute($args);
} elseif (count($this->data)){
$result = $query->execute($this->data);
} else {
$result = $query->execute();
}
return $result;
}
public function __call($name, $arguments){
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;
}
}
|