Db::getCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * ownCloud - Documents App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2013 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Documents;
13
14
/**
15
 * Generic DB class 
16
 */
17
18
abstract class Db {
19
	
20
	protected $data;
21
	
22
	protected $tableName;
23
	
24
	protected $insertStatement;
25
	
26
	protected $loadStatement;
27
	
28
	public function __construct($data = array()){
29
		$this->setData($data);
30
	}
31
	
32
	/**
33
	 * Insert current object data into database
34
	 * @return mixed
35
	 */
36
	public function insert(){
37
		$result = $this->execute($this->insertStatement);
38
		return $result;
39
	}
40
	
41
	/**
42
	 * Get id of the recently inserted record
43
	 * @return mixed
44
	 */
45
	public function getLastInsertId(){
46
		return \OC::$server->getDatabaseConnection()->lastInsertId($this->tableName);
47
	}
48
	
49
	/**
50
	 * Get single record by primary key
51
	 * @param int $value primary key value
52
	 * @return \OCA\Documents\Db
53
	 */
54
	public function load($value){
55
		if (!is_array($value)){
56
			$value = array($value);
57
		}
58
		
59
		$result = $this->execute($this->loadStatement, $value);
60
		$data = $result->fetch();
61
		if (!is_array($data)){
62
			$data = array();
63
		}
64
		$this->data = $data;
65
		return $this;
66
	}
67
	
68
	/**
69
	 * Get single record matching condition
70
	 * @param string $field for WHERE condition
71
	 * @param mixed $value matching value(s)
72
	 * @return \OCA\Documents\Db
73
	 * @throws Exception
74
	 */
75
	public function loadBy($field, $value){
76
		if (!is_array($value)){
77
			$value = array($value);
78
		}
79
		$result = $this->execute('SELECT * FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
80
		$data = $result->fetchAll();
81
		if (!is_array($data) || !count($data)){
82
			$this->data = array();
83
		} elseif (count($data)!=1) {
84
			throw new Exception('Duplicate ' . $value . ' for the filed ' . $field);
85
		} else {
86
			$this->data = $data[0];
87
		}
88
		
89
		return $this;
90
	}
91
92
	/**
93
	 * Delete records matching the condition
94
	 * @param string $field for WHERE condition
95
	 * @param mixed $value matching value(s)
96
	 */
97
	public function deleteBy($field, $value){
98
		if (!is_array($value)){
99
			$value = array($value);
100
		}
101
		$count = count($value);
102 View Code Duplication
		if ($count===0){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
			return;
104
		} elseif ($count===1){
105
			$this->execute('DELETE FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
106
		} else {
107
			$stmt = $this->buildInQuery($field, $value);
108
			$this->execute('DELETE FROM ' . $this->tableName . ' WHERE ' . $stmt, $value);
109
		}
110
	}
111
	
112
	/**
113
	 * Get all records from the table
114
	 * @return array
115
	 */
116
	public function getCollection(){
117
		$result = $this->execute('SELECT * FROM ' . $this->tableName);
118
		$data = $result->fetchAll();
119
		if (!is_array($data)){
120
			$data = array();
121
		}
122
		return $data;
123
	}
124
	
125
	/**
126
	 * Get array of matching records
127
	 * @param string $field for WHERE condition
128
	 * @param mixed $value matching value(s)
129
	 * @return array
130
	 */
131
	public function getCollectionBy($field, $value){
132
		if (!is_array($value)){
133
			$value = array($value);
134
		}
135
		$count = count($value);
136 View Code Duplication
		if ($count===0){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
			return array();
138
		} elseif ($count===1){
139
			$result = $this->execute('SELECT * FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
140
		} else {
141
			$stmt = $this->buildInQuery($field, $value);
142
			$result = $this->execute('SELECT * FROM ' . $this->tableName . ' WHERE '. $stmt , $value);
143
		}
144
		
145
		$data = $result->fetchAll();
146
		if (!is_array($data)){
147
			$data = array();
148
		}
149
		return $data;
150
	}
151
	
152
	/**
153
	 * Get object data
154
	 * @return Array
155
	 */
156
	public function getData(){
157
		return $this->data;
158
	}
159
	
160
	/**
161
	 * Set object data
162
	 * @param array $data
163
	 */
164
	public function setData($data){
165
		$this->data = $data;
166
	}
167
	
168
	/**
169
	 * Check if there are any data in current object
170
	 * @return bool 
171
	 */
172
	public function hasData(){
173
		return count($this->data)>0;
174
	}
175
	
176
	/**
177
	 * Build placeholders for the query with variable input data 
178
	 * @param string $field field name
179
	 * @param Array $array data
180
	 * @return String `field` IN (?, ?...) placeholders matching the number of elements in array
181
	 */
182
	protected function buildInQuery($field, $array){
183
		$count = count($array);
184
		$placeholders = array_fill(0, $count, '?');
185
		$stmt = implode(', ', $placeholders);
186
		return '`' . $field . '` IN ('  . $stmt . ')';
187
	}
188
	
189
	/**
190
	 * Execute a query on database
191
	 * @param string $statement query to be executed
192
	 * @param mixed $args value(s) for the query. 
193
	 * If omited the query will be run on the current object $data
194
	 * @return mixed (array/false)
195
	 */
196
	protected function execute($statement, $args = null){
197
		$query = \OC::$server->getDatabaseConnection()->prepare($statement);
198
		
199
		if (!is_null($args)){
200
			$result = $query->execute($args);
201
		} elseif (count($this->data)){
202
			$result = $query->execute($this->data);
203
		} else {
204
			$result = $query->execute();
205
		}
206
		
207
		return $result ? $query : false;
208
	}
209
	
210
	public function __call($name, $arguments){
211
		if (substr($name, 0, 3) === 'get'){
212
			$requestedProperty = substr($name, 3);
213
			$property = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $requestedProperty));
214
			if (isset($this->data[$property])){
215
				return $this->data[$property];
216
			}
217
		}
218
		return null;
219
	}
220
}
221