Ordering supported by mysql-storage device
[AOOS.git] / lib / AOOSStorageDeviceMySQL.php
blob8ffa06991f1e9939bb31c66be569a2bf364386cd
1 <?php
2 require_once("AOOSException.php");
3 require_once("AOOSModule.php");
4 require_once("AOOSInterfaces.php");
6 /**
7 * The mysql-part of AOOSStorageDevice
8 * @author Sebastian Skejø
9 */
10 class AOOSStorageDeviceMySQL extends AOOSModule implements AOOSStorageInterface
12 private $_connection = null;
13 private $_username = null;
14 private $_password = null;
15 private $_database = null;
16 private $_host = null;
17 private $_table = null;
18 private $_query = null;
19 private $_queryText = null;
21 public function __construct($core)
23 parent::__construct($core);
25 try {
26 $this->_username = $core->getSetting("DBUsername");
27 $this->_password = $core->getSetting("DBPassword");
28 $this->_database = $core->getSetting("DBDatabase");
29 $this->_host = $core->getSetting("DBHost");
31 catch (AOOSException $e)
35 if (!$this->_connection = mysql_connect($this->_host, $this->_username, $this->_password))
36 throw new AOOSException($this->core(), $this->tr("mysql_connect_fail"), $this->tr("error").": ".mysql_error());
37 if (!mysql_select_db($this->_database))
38 throw new AOOSException($this->core(), $this->tr("mysql_select_db_fail"), $this->tr("error").": ".mysql_error());
41 public function __destruct()
43 if (!mysql_close($this->_connection))
44 throw new AOOSException($this->core(), $this->tr("mysql_close_fail"), $this->tr("error").": ".mysql_error(), true, 1);
47 private function _makeQuery($query)
49 if ($this->_queryText != $query)
51 $this->_queryText = $query;
52 if (!($this->_query = mysql_query($this->_queryText)))
54 throw new AOOSException($this->core(), $this->tr("query_fail_mysql"), $this->tr("error").": ".mysql_error()."<br />Query: ".$query);
55 return false;
59 return true;
62 public function setTable($table)
64 $this->_table = $this->core()->getSetting("DBPrefix").$table;
67 public function setQuery($query)
69 return $this->_makeQuery($query);
72 /** Inserts a single value
73 * @param string $field Field name
74 * @param string $value Value
75 * @return bool
77 public function insert($field, $value, $escape = null)
79 $rawQuery = "INSERT INTO %s (%s) VALUES (%s)";
80 if ($escape)
82 $field = $this->_escape($field);
83 $value = $this->_escape($value);
85 $this->_quote($value);
86 $field = implode(",", $field);
87 $value = implode(",", $value);
88 $query = sprintf($rawQuery, $this->_table, $field, $value);
90 if (!$this->_makeQuery($query))
92 return false;
94 return true;
97 /**
98 * Selects a single row
100 public function select($fields, $where = null, $limit = null, $order = null, $sort = null)
102 // Fix fields
103 if (is_array($fields))
105 $fields = implode(", ", $fields);
108 $raw = "SELECT %s FROM %s";
109 $query = sprintf($raw, $fields, $this->_table);
111 if ($where)
113 $query .= sprintf(" WHERE %s", $where);
116 if ($limit)
118 $query .= sprintf(" LIMIT %s", $limit);
121 if ($order)
122 $query .= sprintf(" ORDER BY %s", $order);
124 if ($sort && $order)
125 $query .= $sort;
127 if (!$this->_makeQuery($query))
129 return false;
132 return mysql_fetch_assoc($this->_query);
136 * Update values
137 * @param array $fields Fields
138 * @param array $values Values
139 * @param string $where Valid where-clause
140 * @return bool
142 public function update($fields, $values, $where)
144 $raw = "UPDATE %s SET %s WHERE %s";
145 $vals = array();
146 $merged = "";
148 for ($i = 0; $i < count($fields); $i++)
150 $vals[] = $fields[$i]." = ".$values[$i];
152 $merged = implode(", ", $vals);
154 $query = sprintf($raw, $this->_table, $merged, $where);
156 if (!$this->_makeQuery($query))
158 return false;
161 return true;
165 * Deletes a row. See AOOSStorageDevice::deleteFromArray.
166 * @param string $where
167 * @return bool
169 public function delete($where, $limit = null)
171 $raw = "DELETE FROM %s WHERE %s";
172 $query = sprintf($raw, $this->_table, $where);
174 if ($limit)
176 $query .= " LIMIT ".$limit;
179 if (!$this->_makeQuery($query))
181 return false;
184 return true;
188 * Num rows
189 * @return int
191 public function numRows($where = null)
193 $query = sprintf("SELECT COUNT(*) FROM %s ", $this->_table);
194 if ($where)
196 $query .= "WHERE ".$where;
198 $this->_makeQuery($query);
199 $result = mysql_fetch_array($this->_query);
200 return $result[0];
205 * Escapes string
207 private function _escape($array)
209 $array = $this->_strip($array);
210 foreach ($array as &$a)
212 $a = mysql_real_escape_string($a);
214 return $array;
218 * Strip slashes from array values
220 private function _strip($array)
222 foreach ($array as &$a)
224 $a = stripslashes($a);
226 return $array;
229 private function _quote(&$array)
231 foreach ($array as &$a)
233 $s = substr($a, 0,1);
234 if ($s != "\"" && $s != "'")
236 $a = "'".$a;
238 $e = substr($a, strlen($a)-1, strlen($a));
239 if ($e != "\"" && $e != "'")
241 $a = $a."'";
244 return $array;