| 
									
										
										
										
											2013-09-07 19:49:48 +03:00
										 |  |  | <?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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-26 22:55:17 +03:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Generic DB class  | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 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 | 
					
						
							| 
									
										
										
										
											2014-10-29 18:06:05 +03:00
										 |  |  | 	 * @param int $value primary key value | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 	 * @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); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * 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); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		$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; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-09-07 19:49:48 +03: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 . ')'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * 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; | 
					
						
							| 
									
										
										
										
											2013-09-07 19:49:48 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-04-11 23:12:23 +03: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
										 |  |  | } |