Added Canvas 1.1.0, originally not under SCM so no historical development records...
[canvas.git] / library / adapters / mysql.php
blob9f17c07ae0be159d04c9c9ec237ae73b8691d3f7
1 <?php
2 // @title MySQL DB Connection class
3 // @author Matt Todd <matt@matttoddphoto.com>
4 // @created 2005-09-28
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
14 // query templates
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';
32 // meta templates
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();
44 // actions
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;
51 return true;
54 public function disconnect() {
55 if(mysql_close($this->db)) {
56 $this->is_connected = false;
57 $this->db = null;
58 return true;
59 } else {
60 return 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__));
68 return true;
69 } else {
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');
79 // try {
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();
83 // }
85 // execute the query and return the result
86 $this->result = @mysql_query($query, $this->handle);
88 return $this->result;
91 public function iterate($result) {
92 // iterate through the results
93 return @mysql_fetch_assoc($result);
96 // functions
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();
106 // alias
107 public function id() {
108 return $this->last_id();
111 // sanitize values
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) {
118 // query
119 $query = "EXPLAIN {$table};";
121 // get table details
122 $result = @mysql_query($query, parent::$static_handle);
124 // loop through details
125 while($row = @mysql_fetch_assoc($result)) {
126 $columns[] = $row['Field'];
129 return $columns;