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 AOOSMysqlInterface
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)
38 public function dataModelDefinition() {
42 private function _connect() {
43 if (!$this->_connection
= mysql_connect($this->_host
, $this->_username
, $this->_password
))
44 throw new AOOSException($this->core(), $this->tr("mysql_connect_fail"), $this->tr("error").": ".mysql_error());
45 if (!mysql_select_db($this->_database
))
46 throw new AOOSException($this->core(), $this->tr("mysql_select_db_fail"), $this->tr("error").": ".mysql_error());
49 public function __wakeup() {
51 $this->_queryText
= null;
54 private function _makeQuery($query)
56 $this->_queryText
= $query;
57 if (!($this->_query
= mysql_query($this->_queryText
)))
59 throw new AOOSException($this->core(), $this->tr("mysql_query_fail"), $this->tr("error").": ".mysql_error()."<br />Query: ".$query);
66 public function setTable($table)
68 $this->_table
= $table;
71 public function setQuery($query)
73 return $this->_makeQuery($query);
77 * @param string $field Field name
78 * @param string $value Value
81 public function insert($values)
83 $rawQuery = "INSERT INTO %s (%s) VALUES (%s)";
84 foreach ($values as $row) {
85 $fields = implode(",", array_keys($row));
86 $vals = implode(",", array_values($row));
87 $query = sprintf($rawQuery, $this->_table
, $fields, $vals);
89 if (!$this->_makeQuery($query)) {
97 * Selects a single row
99 public function select($fields, $where, $order, $limit)
101 $fields = implode(",", $fields);
102 $raw = "SELECT %s FROM %s";
103 $query = sprintf($raw, $fields, $this->_table
);
105 if ($w = $this->_arrayToWhere($where)) {
106 $query .= " WHERE ".$w;
109 if ($l = $this->_arrayToLimit($limit)) {
110 $query .= " LIMIT ".$l;
113 if ($o = $this->_arrayToOrder($order)) {
114 $query .= " ORDER BY ".$o;
117 if (!$this->_makeQuery($query)) {
121 while ($row = mysql_fetch_assoc($this->_query
)) {
124 mysql_free_result($this->_query
);
135 public function update($values, $where, $limit = null)
137 $raw = "UPDATE %s SET %s WHERE %s";
138 if (!($v = $this->_arrayToWhere($values))) {
141 if (!($w = $this->_arrayToWhere($where))) {
144 if ($l = $this->_arrayToLimit($limit)) {
148 $query = sprintf($raw, $this->_table
, $v, $w);
150 return $this->_makeQuery($query);
155 * @param string $where
158 public function remove($where, $limit = null)
160 $raw = "DELETE FROM %s WHERE %s";
161 if (!($w = $this->_arrayToWhere($where))) {
164 if ($l = $this->_arrayToLimit($limit)) {
165 $raw .= " LIMIT ".$l;
167 $query = sprintf($raw, $this->_table
, $w);
169 return $this->_makeQuery($query);
176 public function numRows($where = null)
178 $query = sprintf("SELECT COUNT(*) FROM %s ", $this->_table
);
179 if ($w = $this->_arrayToWhere($where)) {
180 $query .= "WHERE ".$w;
182 $this->_makeQuery($query);
183 $result = mysql_fetch_array($this->_query
);
188 * Creates a table in the database
190 public function create($fields, $props) {
192 $query = sprintf("CREATE TABLE IF NOT EXISTS %s (\n", $this->_table
);
193 foreach ($fields as $field => $type) {
194 if ($props[$field] & AOOSMODEL_PROP_UNIQUE
) {
195 $f[] = sprintf("PRIMARY KEY (%s)", $field);
200 case(AOOSMODEL_TYPE_STRING
):
203 case(AOOSMODEL_TYPE_INTEGER
):
205 if ($props[$field] & AOOSMODEL_PROP_DATA_INCREASING
) {
206 $t .= " AUTO_INCREMENT";
209 case(AOOSMODEL_TYPE_EMAIL
):
210 $t .= " VARCHAR(255)";
214 if ($props[$field] & AOOSMODEL_PROP_DATA_HASH
) {
215 $t = $field." VARCHAR(255)";
220 $query .= implode(",\n", $f);
223 return $this->_makeQuery($query);
226 private function _arrayToWhere($array) {
227 if (is_array($array)) {
229 foreach ($array as $key => $value) {
230 $a[] = $key."=".$value;
232 $str = count($a) > 1 ?
implode(" AND ", $a) : $a[0];
238 private function _arrayToLimit($a) {
240 if (count($a) == 1) {
241 $query = sprintf("%s", $a[0]);
244 $query = sprintf("%s", $a[0], $a[1]);
251 private function _arrayToOrder($a) {
253 $o = sprintf("%s,%s", $a[0], $a[1]);