2 require_once("AOOSException.php");
3 require_once("AOOSModule.php");
7 * Provides an interface for handling data on database or file basis.
8 * @author Sebastian Skejø
10 class AOOSStorageDevice
extends AOOSModule
12 private $_storageObject = null;
13 private $_table = null;
14 private $_sort = null;
17 * Check wether $storageObject() is set
19 private function _checkDevice()
21 if ($this->storageObject() == null)
23 throw new AOOSException($this->core(), $this->tr("storage_type_not_set"));
30 * Check if we can use $array as an array
32 private function _checkArray($array)
34 // Is it actually an array?
35 if (!is_array($array))
37 throw new AOOSException($this->core(), $this->tr("not_array"));
41 // Check if length of array keys and array values match
42 if (count(array_keys($array)) != count(array_values($array)))
44 throw new AOOSException($this->core(), $this->tr("array_keys_value_different_length"));
51 * Check both $key and $value to see if we can use them as a value pair
53 private function _checkValue($key, $value)
55 // Are we dealing with strings?
56 if ((!is_string($key) && !is_int($key)) ||
(!is_string($key) && !is_int($value)))
58 throw new AOOSException($this->core(), $this->tr("not_string_or_int"), "<strong>Key:</strong>".$key."<br /><strong>Value:</strong> ".$value);
62 // Are the strings empty?
63 if (empty($key) ||
empty($value))
65 throw new AOOSException($this->core(), $this->tr("strings_empty"));
69 // Ok, we completed the tests - let's return true!
74 * Check if we have a valid model
76 private function _checkModel(&$model)
78 if (!$model instanceof AOOSModel
)
80 throw new AOOSException($this->core(), $this->tr("not_AOOSModel"));
87 * Check if the given where-clause can be used as a where clause
89 private function _checkWhere(&$where)
94 foreach ($where as $key => $value)
96 $tmp[] = sprintf("%s = %s", $key, $value);
98 $where = implode(" AND ", $tmp);
102 // Is it a string? We know it isn't an array
103 if (!is_string($where))
105 throw new AOOSException($this->core(), $this->tr("not_string_or_array"));
109 // Does it match a pattern?
110 if (!preg_match("/[[:alnum:]]+\s*=\s*[[:alnum:]]+/", $where))
112 throw new AOOSException($this->core(), $this->tr("where_not_valid"));
120 * Check if the given limit-clause is valid
122 private function _checkLimit($limit)
124 if (!is_string($limit))
126 throw new AOOSException($this->core(), $this->tr("not_string"));
130 // Does it match a limit-pattern?
131 if (!preg_match("/[0-9]+\s*,\s*[0-9]+/", $limit))
133 throw new AOOSException($this->core(), $this->tr("limit_not_valid"));
141 * Sets the table in which data should be added, updated or deleted from
142 * @param string $table Table name
145 public function setTable($table)
147 if (!$this->_checkDevice())
152 if (!is_string($table))
154 throw new AOOSException($this->core(), $this->tr("not_string"));
158 $this->_table
= $table;
159 $this->storageObject()->setTable($table);
164 * Sets type of storage device
165 * @param string $type Type of storage device
168 public function setStorageType($type)
170 if (!is_string($type))
172 throw new AOOSException($this->core(), $this->tr("not_string"));
176 $type = strtolower($type);
180 require_once("lib/AOOSStorageDeviceMySQL.php");
181 $this->_storageObject
= new AOOSStorageDeviceMySQL($this->core());
184 throw new AOOSException($this->core(), $this->tr("storage_type_not_supported"));
190 * Returns the storage object
191 * @return AOOSStorageDevice
193 public function storageObject()
195 return $this->_storageObject
;
199 * Inserts a model to $this->_table
200 * @param AOOSModel $model Model
203 public function insertModel(&$model)
205 if (!$this->_checkDevice() ||
!$this->_checkModel($model))
210 $data = $model->data(true);
212 $escape = $model->escape() ?
null : true;
213 foreach ($data as $row)
215 $result = $this->insertArray($row, $escape);
222 * Inserts an array to $this->_table
223 * @param array $a Array
226 public function insertArray($array, $escape = null)
228 if (!$this->_checkDevice() ||
!$this->_checkArray($array))
234 $result = $this->storageObject()->insert(array_keys($array), array_values($array), $escape);
240 * Inserts a single value to the field of name $key in $this->_table
241 * @param $key Fieldname
242 * @param $value Value
245 public function insertValue($key, $value, $escape = null)
247 if (!$this->_checkDevice() ||
!$this->_checkValue($key, $value))
252 return $this->storageObject()->insert(array($key), array($value), $escape);
256 * Removes a number of rows from $this->_table
257 * @param int $count Number of rows to delete
260 public function removeRows($count = 1)
262 if (!$this->_checkDevice())
269 throw new AOOSException($this->core(), $this->tr("not_integer"));
275 * Selects values from the table and puts them into a model
276 * @param array|* $fields Fields to select
277 * @param string $where A where-clause of the form \code field = 'value' \code
278 * @param string $limit A limit-clause of the from \code start, end \code
279 * @param AOOSModel $model If given a model the values will be put directly into that model.
282 public function selectModel($fields, $where = null, $limit = null, $order = null, &$model = null)
286 if (!$this->_checkDevice())
293 if (!$this->_checkArray($fields))
299 // Do we have a valid where and limit-clause?
300 if (($where != null && !$this->_checkWhere($where)) ||
($limit != null && !$this->_checkLimit($limit)))
305 // Make sure we have a model to put data into.
308 $model = new AOOSModel($this->core());
310 elseif (!$this->_checkModel($model))
315 while ($row = $this->storageObject()->select($fields, $where, $limit, $order, $this->_sort
))
319 $model->setColumnIndex(array_keys($row));
324 foreach($row as $key => $value)
326 $model->setData($value, -1, $key);
334 * Updates values in the table from the given model
335 * @param AOOSModel $model The model
336 * @param string|string $where A valid where clause. If "auto" is given where clauses will be made automatically.
339 public function updateFromModel(&$model, $where)
341 if (!$this->_checkDevice() ||
!$this->_checkModel($model))
346 if (strtolower($where) == "auto")
348 $f = $model->columnIndex();
350 $values = $this->selectModel(array($f[0]))->getColumn($f[0], 0, $model->rows()-1);
351 foreach ($values as $i)
359 if (!$this->_checkWhere($where) && !is_array($where))
364 if (!is_array($where))
366 $split = explode("=", $where);
367 $keys = array(trim(rtrim($split[0])));
368 $values = array(trim(rtrim($split[1])));
373 $keys = array_keys($where);
374 $values = array_values($where);
378 // We only want to loop as long as we have where-clauses
380 for ($i = 0; $i<$c; $i++
)
382 $key = array_shift($keys);
383 $val = array_shift($values);
384 $w = $key." = ".$val;
387 $this->updateFromArray($model->getRow($i), $w);
394 * Updates values in the table based on the given array.
395 * Each element in the array is translated into a 'key = value' pair based on the array keys and values.
396 * @param array $array The array
397 * @param string $where A valid where clause
400 public function updateFromArray(&$array, $where)
402 if (!$this->_checkDevice() ||
!$this->_checkArray($array))
407 return $this->storageObject()->update(array_keys($array), array_values($array), $where);
411 * Deletes each row from the table which is equal to a row in the model
412 * @param AOOSModel $model The model
413 * @param int $limit Maximum times a matching row can be deleted
416 public function deleteFromModel(&$model, $limit = null)
418 if (!$this->_checkDevice() ||
!$this->_checkModel($model))
423 for($i = 0; $i < $model->rows(); $i++
)
425 $this->deleteFromArray($model->getRow($i), $limit);
431 * 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.
432 * @param array $array The array
433 * @param int $limit Maximum times a matching row can be deleted
436 public function deleteFromArray(&$array, $limit = null)
438 if (!$this->_checkDevice() ||
!$this->_checkArray($array) ||
($limit != null && !is_int($limit)))
445 foreach($array as $key => $value)
447 $sep[] = $key." = ".$value;
449 $merged = implode(" AND ", $sep);
450 return $this->storageObject()->delete($merged, $limit);
454 * Sets a query if the selected storage type support it.
457 public function setQuery($query)
459 if (!is_string($query))
461 throw new AOOSException($this->core(), $this->tr("not_string"));
465 // Check if it supports it.
467 if (!$this->storageObject()->setQuery($query))
469 $str = $this->tr("query").": ".$query."<br />".
470 $this->tr("error").": ".mysql_error();
471 throw new AOOSException($this->core(), $this->tr("query_failed"), $str);
479 * @param string $where An optional, valid where clause
482 public function numRows($where = null)
484 if ($where != null && !$this->_checkWhere($where))
489 return $this->storageObject()->numRows($where);
492 public function setSort($sort) {
493 $sort = strtoupper($sort);
494 if ($sort != "ASC" ||
$sort != "DESC") {
495 throw new AOOSException($this->core(), $this->tr("not_ASC_or_DESC"), "", true, 1);
498 $this->_sort
= $sort;