Added .htaccess, config.php, lang.php.
[AOOS.git] / lib / AOOSStorageDeviceMySQL.php
blob0d858bdd08fef006c51515132c1d1c1e6683918f
1 <?php
2 require_once("AOOSException.php");
3 require_once("AOOSModule.php");
4 require_once("AOOSInterfaces.php");
6 /**
7 * The mysql-part of AOOSStorageDevice
8 * @author Sebastian Skejø
9 */
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);
25 try {
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);
55 return false;
59 return true;
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
75 * @return bool
77 public function insert($field, $value, $escape = null)
79 $rawQuery = "INSERT INTO %s (%s) VALUES (%s)";
80 if ($escape)
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))
91 return false;
93 return true;
96 /**
97 * Selects a single row
99 public function select($fields, $where = null, $limit = null)
101 // Fix fields
102 if (is_array($fields))
104 $fields = implode(", ", $fields);
107 $raw = "SELECT %s FROM %s";
108 $query = sprintf($raw, $fields, $this->_table);
110 if ($where)
112 $query .= sprintf(" WHERE %s", $where);
115 if ($limit)
117 $query .= sprintf(" LIMIT %s", $limit);
120 if (!$this->_makeQuery($query))
122 return false;
125 return mysql_fetch_assoc($this->_query);
129 * Update values
130 * @param array $fields Fields
131 * @param array $values Values
132 * @param string $where Valid where-clause
133 * @return bool
135 public function update($fields, $values, $where)
137 $raw = "UPDATE %s SET %s WHERE %s";
138 $vals = array();
139 $merged = "";
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))
151 return false;
154 return true;
158 * Deletes a row. See AOOSStorageDevice::deleteFromArray.
159 * @param string $where
160 * @return bool
162 public function delete($where, $limit = null)
164 $raw = "DELETE FROM %s WHERE %s";
165 $query = sprintf($raw, $this->_table, $where);
167 if ($limit)
169 $query .= " LIMIT ".$limit;
172 if (!$this->_makeQuery($query))
174 return false;
177 return true;
181 * Num rows
182 * @return int
184 public function numRows($where = null)
186 $query = "SELECT COUNT(*)";
187 if ($where)
189 $query .= "WHERE ".$where;
191 return $this->_makeQuery($query);
196 * Escapes string
198 private function _escape($array)
200 $array = $this->_strip($array);
201 foreach ($array as &$a)
203 $a = mysql_real_escape_string($a);
205 return $array;
209 * Strip slashes from array values
211 private function _strip($array)
213 foreach ($array as &$a)
215 $a = stripslashes($a);
217 return $array;