Small bugfix within getSetting()
[AOOS.git] / AOOSStorageDevice.php
blobdad5655ba0e6f61588b430da17f83b3370c1d3c7
1 <?php
2 require_once("AOOSException.php");
3 require_once("AOOSModule.php");
5 /**
6 * AOOSStorageDevice
7 * Provides an interface for handling data on database or file basis.
8 * @author Sebastian Skejø
9 */
10 class AOOSStorageDevice extends AOOSModule
12 private $_storageObject = null;
13 private $_table = null;
15 /**
16 * Check wether $storageObject() is set
18 private function _checkDevice()
20 if ($this->storageObject() == null)
22 throw new AOOSException($this->core(), $this->tr("storage_type_not_set"));
23 return false;
25 return true;
28 /**
29 * Check if we can use $array as an array
31 private function _checkArray($array)
33 // Is it actually an array?
34 if (!is_array($array))
36 throw new AOOSException($this->core(), $this->tr("not_array"));
37 return false;
40 // Check if length of array keys and array values match
41 if (count(array_keys($array)) != count(array_values($array)))
43 throw new AOOSException($this->core(), $this->tr("array_keys_value_different_length"));
44 return false;
46 return true;
49 /**
50 * Check both $key and $value to see if we can use them as a value pair
52 private function _checkValue($key, $value)
54 // Are we dealing with strings?
55 if ((!is_string($key) && !is_int($key)) || (!is_string($key) && !is_int($value)))
57 throw new AOOSException($this->core(), $this->tr("not_string_or_int"), "<strong>Key:</strong>".$key."<br /><strong>Value:</strong> ".$value);
58 return false;
61 // Are the strings empty?
62 if (empty($key) || empty($value))
64 throw new AOOSException($this->core(), $this->tr("strings_empty"));
65 return false;
68 // Ok, we completed the tests - let's return true!
69 return true;
72 /**
73 * Check if we have a valid model
75 private function _checkModel(&$model)
77 if (!$model instanceof AOOSModel)
79 throw new AOOSException($this->core(), $this->tr("not_AOOSModel"));
80 return false;
82 return true;
85 /**
86 * Check if the given where-clause can be used as a where clause
88 private function _checkWhere($where)
90 // Is it a string?
91 if (!is_string($where))
93 throw new AOOSException($this->core(), $this->tr("not_string"));
94 return false;
97 // Does it match a pattern?
98 if (!preg_match("/[[:alnum:]]+\s*=\s*[[:alnum:]]+/", $where))
100 throw new AOOSException($this->core(), $this->tr("where_not_valid"));
101 return false;
104 return true;
108 * Check if the given limit-clause is valid
110 private function _checkLimit($limit)
112 if (!is_string($limit))
114 throw new AOOSException($this->core(), $this->tr("not_string"));
115 return false;
118 // Does it match a limit-pattern?
119 if (!preg_match("/[0-9]+\s*,\s*[0-9]+/", $limit))
121 throw new AOOSException($this->core(), $this->tr("limit_not_valid"));
122 return false;
125 return true;
129 * Sets the table in which data should be added, updated or deleted from
130 * @param string $table Table name
131 * @return bool
133 public function setTable($table)
135 if (!$this->_checkDevice())
137 return false;
140 if (!is_string($table))
142 throw new AOOSException($this->core(), $this->tr("not_string"));
143 return false;
146 $this->_table = $table;
147 $this->storageObject()->setTable($table);
148 return true;
152 * Sets type of storage device
153 * @param string $type Type of storage device
154 * @return bool
156 public function setStorageType($type)
158 if (!is_string($type))
160 throw new AOOSException($this->core(), $this->tr("not_string"));
161 return false;
164 $type = strtolower($type);
165 switch ($type)
167 case ("mysql"):
168 require_once("lib/AOOSStorageDeviceMySQL.php");
169 $this->_storageObject = new AOOSStorageDeviceMySQL($this->core());
170 break;
171 default:
172 throw new AOOSException($this->core(), $this->tr("storage_type_not_supported"));
173 return false;
178 * Returns the storage object
179 * @return AOOSStorageDevice
181 public function storageObject()
183 return $this->_storageObject;
187 * Inserts a model to $this->_table
188 * @param AOOSModel $model Model
189 * @return bool
191 public function insertModel(&$model)
193 if (!$this->_checkDevice() || !$this->_checkModel($model))
195 return false;
198 $data = $model->data(true);
199 $result = false;
200 $escape = $model->escape() ? null : true;
201 foreach ($data as $row)
203 $result = $this->insertArray($row, $escape);
206 return $result;
210 * Inserts an array to $this->_table
211 * @param array $a Array
212 * @return bool
214 public function insertArray($array, $escape = null)
216 if (!$this->_checkDevice() || !$this->_checkArray($array))
218 return false;
221 $result = false;
222 $result = $this->storageObject()->insert(array_keys($array), array_values($array), $escape);
224 return $result;
228 * Inserts a single value to the field of name $key in $this->_table
229 * @param $key Fieldname
230 * @param $value Value
231 * @return bool
233 public function insertValue($key, $value, $escape = null)
235 if (!$this->_checkDevice() || !$this->_checkValue($key, $value))
237 return false;
240 return $this->storageObject()->insert(array($key), array($value), $escape);
244 * Removes a number of rows from $this->_table
245 * @param int $count Number of rows to delete
246 * @return bool
248 public function removeRows($count = 1)
250 if (!$this->_checkDevice())
252 return false;
255 if(!is_int($count))
257 throw new AOOSException($this->core(), $this->tr("not_integer"));
258 return false;
263 * Selects values from the table and puts them into a model
264 * @param array|* $fields Fields to select
265 * @param string $where A where-clause of the form \code field = 'value' \code
266 * @param string $limit A limit-clause of the from \code start, end \code
267 * @param AOOSModel $model If given a model the values will be put directly into that model.
268 * @return AOOSModel
270 public function selectModel($fields, $where = null, $limit = null, &$model = null)
272 $first = true;
274 if (!$this->_checkDevice())
276 return false;
279 if ($fields != "*")
281 if (!$this->_checkArray($fields))
283 return false;
287 // Do we have a valid where and limit-clause?
288 if (($where != null && !$this->_checkWhere($where)) || ($limit != null && !$this->_checkLimit($limit)))
290 return false;
293 // Make sure we have a model to put data into.
294 if ($model == null)
296 $model = new AOOSModel($this->core());
298 elseif (!$this->_checkModel($model))
300 return false;
303 while ($row = $this->storageObject()->select($fields, $where, $limit))
305 if ($first)
307 $model->setColumnIndex(array_keys($row));
308 $first = false;
310 $model->addRow();
312 foreach($row as $key => $value)
314 $model->setData($value, -1, $key);
318 return $model;
322 * Updates values in the table from the given model
323 * @param AOOSModel $model The model
324 * @param string|string $where A valid where clause. If "auto" is given where clauses will be made automatically.
325 * @return bool
327 public function updateFromModel(&$model, $where)
329 if (!$this->_checkDevice() || !$this->_checkModel($model))
331 return false;
334 if (strtolower($where) == "auto")
336 $f = $model->columnIndex();
337 $keys = array();
338 $values = $this->selectModel(array($f[0]))->getColumn($f[0], 0, $model->rows()-1);
339 foreach ($values as $i)
341 $keys[] = $f[0];
343 unset($f);
345 else
347 if (!$this->_checkWhere($where) && !is_array($where))
349 return false;
352 if (!is_array($where))
354 $split = explode("=", $where);
355 $keys = array(trim(rtrim($split[0])));
356 $values = array(trim(rtrim($split[1])));
357 unset($split);
359 else
361 $keys = array_keys($where);
362 $values = array_values($where);
366 // We only want to loop as long as we have where-clauses
367 $c = count($keys);
368 for ($i = 0; $i<$c; $i++)
370 $key = array_shift($keys);
371 $val = array_shift($values);
372 $w = $key." = ".$val;
373 unset($key);
374 unset($val);
375 $this->updateFromArray($model->getRow($i), $w);
378 return true;
382 * Updates values in the table based on the given array.
383 * Each element in the array is translated into a 'key = value' pair based on the array keys and values.
384 * @param array $array The array
385 * @param string $where A valid where clause
386 * @return bool
388 public function updateFromArray(&$array, $where)
390 if (!$this->_checkDevice() || !$this->_checkArray($array))
392 return false;
395 return $this->storageObject()->update(array_keys($array), array_values($array), $where);
399 * Deletes each row from the table which is equal to a row in the model
400 * @param AOOSModel $model The model
401 * @param int $limit Maximum times a matching row can be deleted
402 * @return bool
404 public function deleteFromModel(&$model, $limit = null)
406 if (!$this->_checkDevice() || !$this->_checkModel($model))
408 return false;
411 for($i = 0; $i < $model->rows(); $i++)
413 $this->deleteFromArray($model->getRow($i), $limit);
415 return true;
419 * Deletes a row from the table if a row matches the array in a such way that array keys matches field names and the corresponding array values matches the corresponding field values.
420 * @param array $array The array
421 * @param int $limit Maximum times a matching row can be deleted
422 * @return bool
424 public function deleteFromArray(&$array, $limit = null)
426 if (!$this->_checkDevice() || !$this->_checkArray($array) || ($limit != null && !is_int($limit)))
428 return false;
431 $sep = array();
432 $merged = "";
433 foreach($array as $key => $value)
435 $sep[] = $key." = ".$value;
437 $merged = implode(" AND ", $sep);
438 return $this->storageObject()->delete($merged, $limit);
442 * Sets a query if the selected storage type support it.
443 * @return bool
445 public function setQuery($query)
447 if (!is_string($query))
449 throw new AOOSException($this->core(), $this->tr("not_string"));
450 return false;
453 // Check if it supports it.
455 if (!$this->storageObject()->setQuery($query))
457 $str = $this->tr("query").": ".$query."<br />".
458 $this->tr("error").": ".mysql_error();
459 throw new AOOSException($this->core(), $this->tr("query_failed"), $str);
460 return false;
462 return true;
466 * Returns row count
467 * @param string $where An optional, valid where clause
468 * @return int|false
470 public function numRows($where = null)
472 if ($where != null && !$this->_checkWhere($where))
474 return false;
477 return $this->storageObject()->numRows($where);