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 $e = new AOOSException($this->core(), $this->tr("mysql_query_fail"), $this->tr("error").": ".mysql_error()."<br />Query: ".$query, true, 0);
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;
118 $this->_makeQuery($query);
119 } catch (AOOSException
$e) {
123 while ($row = mysql_fetch_assoc($this->_query
)) {
126 mysql_free_result($this->_query
);
137 public function update($values, $where, $limit = null)
139 $raw = "UPDATE %s SET %s WHERE %s";
140 if (!($v = $this->_arrayToWhere($values))) {
143 if (!($w = $this->_arrayToWhere($where))) {
146 if ($l = $this->_arrayToLimit($limit)) {
150 $query = sprintf($raw, $this->_table
, $v, $w);
152 return $this->_makeQuery($query);
157 * @param string $where
160 public function remove($where, $limit = null)
162 $raw = "DELETE FROM %s WHERE %s";
163 if (!($w = $this->_arrayToWhere($where))) {
166 if ($l = $this->_arrayToLimit($limit)) {
167 $raw .= " LIMIT ".$l;
169 $query = sprintf($raw, $this->_table
, $w);
171 return $this->_makeQuery($query);
178 public function numRows($where = null)
180 $query = sprintf("SELECT COUNT(*) FROM %s ", $this->_table
);
181 if ($w = $this->_arrayToWhere($where)) {
182 $query .= "WHERE ".$w;
184 $this->_makeQuery($query);
185 $result = mysql_fetch_array($this->_query
);
190 * Creates a table in the database
192 public function create($fields, $props) {
195 $query = sprintf("CREATE TABLE IF NOT EXISTS %s (\n", $this->_table
);
196 foreach ($fields as $field => $type) {
197 if (!($props[$field] & AOOSMODEL_PROP_FROM_DATABASE
)) {
201 if ($props[$field] & AOOSMODEL_PROP_UNIQUE
) {
202 $f[] = sprintf("PRIMARY KEY (%s)", $field);
207 case(AOOSMODEL_TYPE_STRING
):
210 case(AOOSMODEL_TYPE_DATETIME
):
211 case(AOOSMODEL_TYPE_INTEGER
):
213 if ($props[$field] & AOOSMODEL_PROP_DATA_INCREASING
) {
214 $t .= " AUTO_INCREMENT";
217 case(AOOSMODEL_TYPE_EMAIL
):
218 $t .= " VARCHAR(255)";
222 if ($props[$field] & AOOSMODEL_PROP_DATA_HASH
) {
223 $t = $field." VARCHAR(255)";
228 $query .= implode(",\n", $f);
233 return $this->_makeQuery($query);
239 public function drop() {
240 $query = sprintf("DROP TABLE IF EXISTS %s", $this->_table
);
241 return $this->_makeQuery($query);
244 private function _arrayToWhere($array) {
245 if (is_array($array)) {
247 foreach ($array as $key => $value) {
248 $a[] = $key."=".$value;
250 $str = count($a) > 1 ?
implode(" AND ", $a) : $a[0];
256 private function _arrayToLimit($a) {
258 if (count($a) == 1) {
259 $query = sprintf("%s", $a[0]);
262 $query = sprintf("%s", $a[0], $a[1]);
269 private function _arrayToOrder($a) {
271 $o = sprintf("%s,%s", $a[0], $a[1]);