summaryrefslogtreecommitdiffstats
path: root/modules/database/classes/db.php
blob: 0d9ef94ddd53e385daee4a2ccb990276fe51c799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

/**
 * Database related functions. Creates connections,
 * executes queries and returns results. It is also the
 * generic connection class used by drivers.
 * @package Database
 */
abstract class DB {

    /**
     * An associative array of connections to databases
     * @var array   
     * @access private 
     * @static 
     */
	private static $_instances=array();

    /**
     * Executes a prepared statement query
     * 
     * @param string   $query  A prepared statement query
     * @param array     $params Parameters for the query
	 * @return Result_Database
     * @access public    
	 * @see Result_Database
     */
	public abstract function execute($query, $params = array());

    /**
     * Builds a new Query to the database
     * 
     * @param string $type Query type. Available types: select, update, insert, delete, count
	 * @return Result_Database
     * @access public  
	 * @see Query_Database
     */
	public abstract function build_query($type);

    /**
     * Gets the id of the last inserted row.
     * 
	 * @return mixed The id of the last inserted row
     * @access public 
     */
	public abstract function get_insert_id();
	
	/**
     * Gets column names for the specified table
     * 
	 * @param string $table Name of the table to get columns from
     * @return array Array of column names
     * @access public 
     */
	public abstract function list_columns($table);

    /**
     * Executes a named query where parameters are passed as an associative array
	 * Example:
	 * <code>
	 * $result=$db->namedQuery("SELECT * FROM fairies where name = :name",array('name'=>'Tinkerbell'));
	 * </code>
     * 
     * @param string $query  A named query
     * @param array   $params Associative array of parameters
     * @return Result_Database   Current drivers implementation of Result_Database
     * @access public  
     */
	public function namedQuery($query, $params=array()) {
		$bind = array();
		preg_match_all('#:(\w+)#is', $query, $matches,PREG_SET_ORDER);
		foreach($matches as $match)
			if(isset($params[$match[1]])){
				$query = preg_replace("#{$match[0]}#", '?', $query, 1);
				$bind[] = $params[$match[1]];
			}
		return $this->execute($query,$bind);
	}

    /**
     * Returns an Expression_Database representation of the value.
	 * Values wrapped inside Expression_Database are not escaped in queries
     * 
     * @param mixed $value Value to be wrapped
     * @return Expression_Database  Raw value that will not be escaped during query building
     * @access public  
     * @static 
     */
	public static function expr($value){
		return new Expression_Database($value);
	}
	
    /**
     * Builds a query for specified connection.
     * 
     * @param string $type   Query type. Available types: select,update,insert,delete,count
     * @param string  $config Configuration name of the connection.
	 *                        Defaults to  'default'.
     * @return Query_Database  Driver implementation of the Query_Database class.
     * @access public  
     * @static 
     */
	public static function query($type,$config = 'default') {
		return DB::instance($config)->build_query($type);
	}

    /**
     * Gets the id of the last inserted row
     * 
     * @param string  $config Configuration name of the connection.
	 *                        Defaults to  'default'.
     * @return mixed Id of the last inserted row
     * @access public 
     * @static 
     */
	public static function insert_id($config = 'default') {
		return DB::instance($config)->get_insert_id();
	}

    /**
     * Gets an instance of a connection to the database
     * 
     * @param string  $config Configuration name of the connection.
	 *                        Defaults to  'default'.
     * @return DB  Driver implementation of the DB class.
     * @access public 
     * @static 
     */
	public static function instance($config='default'){
		if (!isset(DB::$_instances[$config])) {
			$driver = Config::get("database.{$config}.driver");
			$driver="DB_{$driver}_Driver";
			DB::$_instances[$config] = new $driver($config);
		}
		return DB::$_instances[$config];
	}

}