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("insert_fail_mysql"), $this->tr("error").": ".mysql_error()."<br />Query: ".$query);
62 public function setTable($table)
64 $this->_table
= $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 $field = implode(",", $field);
86 $value = implode(",", $value);
87 $query = sprintf($rawQuery, $this->_table
, $field, $value);
89 if (!$this->_makeQuery($query))
97 * Selects a single row
99 public function select($fields, $where = null, $limit = null)
102 if (is_array($fields))
104 $fields = implode(", ", $fields);
107 $raw = "SELECT %s FROM %s";
108 $query = sprintf($raw, $fields, $this->_table
);
112 $query .= sprintf(" WHERE %s", $where);
117 $query .= sprintf(" LIMIT %s", $limit);
120 if (!$this->_makeQuery($query))
125 return mysql_fetch_assoc($this->_query
);
130 * @param array $fields Fields
131 * @param array $values Values
132 * @param string $where Valid where-clause
135 public function update($fields, $values, $where)
137 $raw = "UPDATE %s SET %s WHERE %s";
141 for ($i = 0; $i < count($fields); $i++
)
143 $vals[] = $fields[$i]." = ".$values[$i];
145 $merged = implode(", ", $vals);
147 $query = sprintf($raw, $this->_table
, $merged, $where);
149 if (!$this->_makeQuery($query))
158 * Deletes a row. See AOOSStorageDevice::deleteFromArray.
159 * @param string $where
162 public function delete($where, $limit = null)
164 $raw = "DELETE FROM %s WHERE %s";
165 $query = sprintf($raw, $this->_table
, $where);
169 $query .= " LIMIT ".$limit;
172 if (!$this->_makeQuery($query))
184 public function numRows($where = null)
186 $query = "SELECT COUNT(*)";
189 $query .= "WHERE ".$where;
191 return $this->_makeQuery($query);
198 private function _escape($array)
200 $array = $this->_strip($array);
201 foreach ($array as &$a)
203 $a = mysql_real_escape_string($a);
209 * Strip slashes from array values
211 private function _strip($array)
213 foreach ($array as &$a)
215 $a = stripslashes($a);