2 require_once("AOOSException.php");
3 require_once("AOOSModule.php");
4 require_once("AOOSInterfaces.php");
7 * The mysql-part of AOOSStorageDevice
8 * @author Sebastian Skejø
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);
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);
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
77 public function insert($field, $value, $escape = null)
79 $rawQuery = "INSERT INTO %s (%s) VALUES (%s)";
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))
98 * Selects a single row
100 public function select($fields, $where = null, $limit = null)
103 if (is_array($fields))
105 $fields = implode(", ", $fields);
108 $raw = "SELECT %s FROM %s";
109 $query = sprintf($raw, $fields, $this->_table
);
113 $query .= sprintf(" WHERE %s", $where);
118 $query .= sprintf(" LIMIT %s", $limit);
121 if (!$this->_makeQuery($query))
126 return mysql_fetch_assoc($this->_query
);
131 * @param array $fields Fields
132 * @param array $values Values
133 * @param string $where Valid where-clause
136 public function update($fields, $values, $where)
138 $raw = "UPDATE %s SET %s WHERE %s";
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))
159 * Deletes a row. See AOOSStorageDevice::deleteFromArray.
160 * @param string $where
163 public function delete($where, $limit = null)
165 $raw = "DELETE FROM %s WHERE %s";
166 $query = sprintf($raw, $this->_table
, $where);
170 $query .= " LIMIT ".$limit;
173 if (!$this->_makeQuery($query))
185 public function numRows($where = null)
187 $query = sprintf("SELECT COUNT(*) FROM %s ", $this->_table
);
190 $query .= "WHERE ".$where;
192 $this->_makeQuery($query);
193 $result = mysql_fetch_array($this->_query
);
201 private function _escape($array)
203 $array = $this->_strip($array);
204 foreach ($array as &$a)
206 $a = mysql_real_escape_string($a);
212 * Strip slashes from array values
214 private function _strip($array)
216 foreach ($array as &$a)
218 $a = stripslashes($a);
223 private function _quote(&$array)
225 foreach ($array as &$a)
227 $s = substr($a, 0,1);
228 if ($s != "\"" && $s != "'")
232 $e = substr($a, strlen($a)-1, strlen($a));
233 if ($e != "\"" && $e != "'")