2 // @title MySQL DB Connection class
3 // @author Matt Todd <matt@matttoddphoto.com>
5 // @desc Used to connect and interact with MySQL databases. Inherits
6 // basic functionality and structure from the DBAbstract class.
8 include_once 'library/database.php';
9 include_once 'library/stdexception.php';
11 class mysql
extends database
{
12 // $config, $handle, $is_connected; // state and configuration // defined in 'database' class
15 const select
= 'SELECT :cache :columns FROM :table :join :where :group_by :having :order_by :limit :offset;';
16 const delete
= 'DELETE FROM :table :where :order_by :limit;';
17 const insert
= 'INSERT INTO :table SET :values;';
18 const update
= 'UPDATE :table SET :values :where :order_by :limit;';
19 // transactional statements
20 const begin
= 'BEGIN;';
21 const commit
= 'COMMIT;';
22 const rollback
= 'ROLLBACK;';
23 // query templates' parts
24 const cache
= 'SQL_CACHE';
25 const join
= 'LEFT JOIN :table ON :on :join';
26 const where
= 'WHERE :where';
27 const group_by
= 'GROUP BY :group_by';
28 const having
= 'HAVING :having';
29 const order_by
= 'ORDER BY :order_by';
30 const limit
= 'LIMIT :limit';
31 const offset
= 'OFFSET :offset';
33 const named_entity
= ':name AS :alias';
34 const equality
= '"{:1}" = "{:2}"';
35 const value
= '{:1} = "{:2}"';
37 // constructor // there's magic working here, so don't overwrite what's already in here unless
38 // you are willing to clean up after yourself
39 public function __construct($params) {
40 parent
::__construct($params);
41 parent
::$adapter_class = get_class();
45 public function connect() {
46 // establish connection
47 if(!($this->handle
= @mysql_pconnect
($this->config
['host'], $this->config
['username'], $this->config
['password'])))
48 throw new DBConnectionException(mysql_errno(), mysql_error(), (__FILE__
. ', line ' . __LINE__
));
49 parent
::$static_handle = $this->handle
;
50 $this->is_connected
= true;
54 public function disconnect() {
55 if(mysql_close($this->db
)) {
56 $this->is_connected
= false;
64 public function select_db($database = null) {
65 if(!empty($database)) $this->config
['database'] = $database; else $database = $this->config
['database'];
66 if($this->is_connected
) {
67 if(!@mysql_select_db
($database, $this->handle
)) throw new CouldNotSelectDB(mysql_errno(), mysql_error(), (__FILE__
. ', line ' . __LINE__
));
70 throw new CouldNotSelectDB("0002", "Not connected to the Database", (__FILE__
. ', line ' . __LINE__
));
74 protected function query($query) {
75 @mysql_free_result
($this->result
);
77 Debug
::log("Executed '{$query}'", 'sql', 'info', 'adapter::MySQL');
80 // if(!($this->result = @mysql_query($query, $this->handle))) throw new DBQueryException(mysql_errno(), mysql_error(), (__FILE__ . ', line ' . __LINE__));
81 // } catch(Exception $e) {
82 // print_r($e); die();
85 // execute the query and return the result
86 $this->result
= @mysql_query
($query, $this->handle
);
91 public function iterate($result) {
92 // iterate through the results
93 return @mysql_fetch_assoc
($result);
97 public function rows_found() {
98 return mysql_num_rows($this->result
);
100 public function affected_rows() {
101 return mysql_affected_rows();
103 public function last_id() {
104 return mysql_insert_id();
107 public function id() {
108 return $this->last_id();
112 public static function sanitize($value) {
113 return addslashes(stripslashes($value));
116 // get table column names (to make sure 'updates' and 'inserts' don't try to update a column that doesn't exist)
117 public static function columns($table) {
119 $query = "EXPLAIN {$table};";
122 $result = @mysql_query
($query, parent
::$static_handle);
124 // loop through details
125 while($row = @mysql_fetch_assoc
($result)) {
126 $columns[] = $row['Field'];