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
))) {
58 $e = new AOOSException($this->core(), $this->tr("mysql_query_fail"), $this->tr("error").": ".mysql_error()."<br />Query: ".$query, true, 0);
65 public function setTable($table)
67 $this->_table
= $table;
70 public function setQuery($query)
72 return $this->_makeQuery($query);
76 * @param string $field Field name
77 * @param string $value Value
80 public function insert($values)
82 $rawQuery = "INSERT INTO %s (%s) VALUES (%s)";
85 foreach ($values as $key => $val) {
86 if ($val && $val !== "''") {
91 $fields = implode(",", $fields);
92 $vals = implode(",", $vals);
93 $query = sprintf($rawQuery, $this->_table
, $fields, $vals);
95 if (!$this->_makeQuery($query)) {
102 * Selects a single row
104 public function select($fields, $where, $order, $limit)
106 $fields = implode(",", $fields);
107 $raw = "SELECT %s FROM %s";
108 $query = sprintf($raw, $fields, $this->_table
);
110 if ($w = $this->_arrayToWhere($where)) {
111 $query .= " WHERE ".$w;
114 if ($l = $this->_arrayToLimit($limit)) {
115 $query .= " LIMIT ".$l;
118 if ($o = $this->_arrayToOrder($order)) {
119 $query .= " ORDER BY ".$o;
123 $this->_makeQuery($query);
124 } catch (AOOSException
$e) {
128 while ($row = mysql_fetch_assoc($this->_query
)) {
131 mysql_free_result($this->_query
);
142 public function update($values, $where, $limit = null)
144 $raw = "UPDATE %s SET %s WHERE %s";
145 if (!($v = $this->_arrayToWhere($values))) {
148 $v = str_replace("AND", ",", $v);
149 if (!($w = $this->_arrayToWhere($where))) {
152 if ($l = $this->_arrayToLimit($limit)) {
156 $query = sprintf($raw, $this->_table
, $v, $w);
157 return $this->_makeQuery($query);
162 * @param string $where
165 public function remove($where, $limit = null)
167 $raw = "DELETE FROM %s WHERE %s";
168 if (!($w = $this->_arrayToWhere($where))) {
171 if ($l = $this->_arrayToLimit($limit)) {
172 $raw .= " LIMIT ".$l;
174 $query = sprintf($raw, $this->_table
, $w);
176 return $this->_makeQuery($query);
183 public function numRows($where = null)
185 $query = sprintf("SELECT COUNT(*) FROM %s ", $this->_table
);
186 if ($w = $this->_arrayToWhere($where)) {
187 $query .= "WHERE ".$w;
189 $this->_makeQuery($query);
190 $result = mysql_fetch_array($this->_query
);
195 * Creates a table in the database
197 public function create($fields, $flags) {
200 $query = sprintf("CREATE TABLE IF NOT EXISTS %s (\n", $this->_table
);
201 foreach ($fields as $field => $type) {
203 if ($flags[$field] & AOOSMODEL_FLAG_PRIMARY_KEY
) {
204 $f[] = sprintf("PRIMARY KEY (%s)", $field);
209 case(AOOSMODEL_TYPE_STRING
):
210 $t .= " VARCHAR(255)";
212 case(AOOSMODEL_TYPE_INTEGER
):
215 case(AOOSMODEL_TYPE_TEXT
):
218 case(AOOSMODEL_TYPE_BOOLEAN
):
225 $query .= implode(",\n", $f);
230 return $this->_makeQuery($query);
236 public function drop() {
237 $query = sprintf("DROP TABLE IF EXISTS %s", $this->_table
);
238 return $this->_makeQuery($query);
241 private function _arrayToWhere($array) {
242 if (is_array($array)) {
244 foreach ($array as $key => $value) {
245 if ($value && $value != "''") {
246 $a[] = $key."=".$value;
249 $str = count($a) > 1 ?
implode(" AND ", $a) : $a[0];
255 private function _arrayToLimit($a) {
257 if (count($a) == 1) {
258 $query = sprintf("%s", $a[0]);
261 $query = sprintf("%s", $a[0], $a[1]);
268 private function _arrayToOrder($a) {
270 $o = sprintf("%s %s", $a[0], $a[1]);