Updated the User-module.
[AOOS.git] / lib / AOOSStorageDeviceMySQL.php
blobd549f539dac16c02cc02aa6196029132152a251a
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)
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 (!$this->_makeQuery($query))
123 return false;
126 return mysql_fetch_assoc($this->_query);
130 * Update values
131 * @param array $fields Fields
132 * @param array $values Values
133 * @param string $where Valid where-clause
134 * @return bool
136 public function update($fields, $values, $where)
138 $raw = "UPDATE %s SET %s WHERE %s";
139 $vals = array();
140 $merged = "";
142 for ($i = 0; $i < count($fields); $i++)
144 $vals[] = $fields[$i]." = ".$values[$i];
146 $merged = implode(", ", $vals);
148 $query = sprintf($raw, $this->_table, $merged, $where);
150 if (!$this->_makeQuery($query))
152 return false;
155 return true;
159 * Deletes a row. See AOOSStorageDevice::deleteFromArray.
160 * @param string $where
161 * @return bool
163 public function delete($where, $limit = null)
165 $raw = "DELETE FROM %s WHERE %s";
166 $query = sprintf($raw, $this->_table, $where);
168 if ($limit)
170 $query .= " LIMIT ".$limit;
173 if (!$this->_makeQuery($query))
175 return false;
178 return true;
182 * Num rows
183 * @return int
185 public function numRows($where = null)
187 $query = sprintf("SELECT COUNT(*) FROM %s ", $this->_table);
188 if ($where)
190 $query .= "WHERE ".$where;
192 $this->_makeQuery($query);
193 $result = mysql_fetch_array($this->_query);
194 return $result[0];
199 * Escapes string
201 private function _escape($array)
203 $array = $this->_strip($array);
204 foreach ($array as &$a)
206 $a = mysql_real_escape_string($a);
208 return $array;
212 * Strip slashes from array values
214 private function _strip($array)
216 foreach ($array as &$a)
218 $a = stripslashes($a);
220 return $array;
223 private function _quote(&$array)
225 foreach ($array as &$a)
227 $s = substr($a, 0,1);
228 if ($s != "\"" && $s != "'")
230 $a = "'".$a;
232 $e = substr($a, strlen($a)-1, strlen($a));
233 if ($e != "\"" && $e != "'")
235 $a = $a."'";
238 return $array;