1
<?php
2
/**
3
* Base Database Access Object
4
* Members must be public in order to be called in data object
5
* The following databases are supported:
6
*
7
* - [http://mysql.com MySQL]
8
* - [http://postgresql.org PostgreSQL]
9
* - [http://sqlite.org SQLite]
10
*
11
* @author Zhou Yuan <yuanzhou19@gmail.com>
12
* @link http://www.infopotato.com/
13
* Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)}
14
* @copyright Copyright © 2009-2011 Zhou Yuan
15
* @license http://www.opensource.org/licenses/mit-license.php MIT Licence
16
*/
17
18
19
// Must define the constants out of the class
20
// Can not define them as class const
21
define('FETCH_OBJ', 'FETCH_OBJ');
22
define('FETCH_ASSOC', 'FETCH_ASSOC');
23
define('FETCH_NUM', 'FETCH_NUM');
24
25
class Base_DAO {
26
/**
27
* Query result
28
*
29
* @var array
30
*/
31
public $query_result = array();
32
33
/**
34
* Gets the ID generated by the last INSERT query
35
*
36
* @var integer
37
*/
38
public $last_insert_id;
39
40
/**
41
* Constructor
42
*
43
* Closing isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.
44
* Allow the user to perform a connect at the same time as initialising the this class
45
*/
46
public function __construct(array $config = NULL) {}
47
48
/**
49
* Escapes special characters in a string for use in an SQL statement,
50
* taking into account the current charset of the connection
51
*/
52
public function escape($string) {}
53
54
/**
55
* Overridden by specific DB class
56
* USAGE: prepare( string $query [, array $params ] )
57
* $query - SQL query WITHOUT any user-entered parameters. Replace parameters with "?"
58
* e.g. $query = "SELECT date from history WHERE login = ?"
59
* $params - array of parameters
60
*
61
* Example:
62
* prepare( "SELECT secret FROM db WHERE login = ?", array($login) );
63
* prepare( "SELECT secret FROM db WHERE login = ? AND password = ?", array($login, $password) );
64
* That will result safe query to RDBMS with escaped $login and $password.
65
*/
66
public function prepare($query, array $params = NULL) {}
67
68
/**
69
* Perform SQL query and try to determine result value
70
* Overridden by specific DB class
71
* The query string should not end with a semicolon
72
*
73
* @return int Number of rows affected/selected or false on error
74
*/
75
public function exec_query($query) {}
76
77
/**
78
* Gets one single data cell from the database
79
*
80
* This function is very useful for evaluating query results within logic statements such as if or switch.
81
* If the query generates more than one row the first row will always be used by default.
82
* If the query generates more than one column the leftmost column will always be used by default.
83
* Even so, the full resultset will be available within the array $db->query_result should you wish to use them.
84
*
85
* @param string $query SQL query. If null, use the result from the previous query.
86
* @param int $x (optional) Column of value to return. Indexed from 0.
87
* @param int $y (optional) Row of value to return. Indexed from 0.
88
* @return string Database query result
89
*/
90
public function get_cell($query, $x = 0, $y = 0) {
91
$return_val = '';
92
93
$this->exec_query($query);
94
95
if ($this->query_result !== array()) {
96
if ($y >= count($this->query_result)) {
97
halt('A System Error Was Encountered', 'The offset y you specified overflows', 'sys_error');
98
}
99
100
// Returns all the values from the input array and indexes numerically the array
101
$values = array_values(get_object_vars($this->query_result[$y]));
102
if ($x >= count($values)) {
103
halt('A System Error Was Encountered', 'The offset x you specified overflows', 'sys_error');
104
}
105
106
if ($values[$x] !== '') {
107
$return_val = $values[$x];
108
}
109
}
110
111
return $return_val;
112
}
113
114
/**
115
* Gets a single row from the database
116
* If the query returns more than one row and no row offset is supplied the first row within the results set will be returned by default.
117
*
118
* @param string $query SQL query.
119
* @param string $output (optional) one of FETCH_ASSOC | FETCH_NUM | FETCH_OBJ constants.
120
* @param int $y (optional) Row to return if the query returns more than one row. Indexed from 0.
121
* @return mixed Database query result in format specifed by $output
122
*/
123
public function get_row($query, $output = FETCH_OBJ, $y = 0) {
124
$return_val = NULL;
125
126
$this->exec_query($query);
127
128
if ($this->query_result !== array()) {
129
if ($y >= count($this->query_result)) {
130
halt('A System Error Was Encountered', 'The offset y you specified overflows', 'sys_error');
131
}
132
133
if ($output == FETCH_OBJ) {
134
$return_val = $this->query_result[$y];
135
} elseif ($output == FETCH_ASSOC) {
136
$return_val = get_object_vars($this->query_result[$y]);
137
} elseif ($output == FETCH_NUM) {
138
$return_val = array_values(get_object_vars($this->query_result[$y]));
139
} else {
140
halt('A System Error Was Encountered', " \$db->get_row() -- Output type must be one of: FETCH_OBJ, FETCH_ASSOC, FETCH_NUM", 'sys_error');
141
}
142
}
143
144
return $return_val;
145
}
146
147
/**
148
* Extracts one column as one dimensional array based on a column offset.
149
*
150
* If no offset is supplied the offset will defualt to column 0. I.E the first column.
151
* If a null query is supplied the previous query results are used.
152
*
153
* @param string $query SQL query. If null, use the result from the previous query.
154
* @param int $x Column to return. Indexed from 0.
155
* @return array Database query result. Array indexed from 0 by SQL result row number.
156
*/
157
public function get_col($query, $x = 0) {
158
$return_val = array();
159
160
$this->exec_query($query);
161
162
if ($this->query_result !== array()) {
163
// Extract the column values
164
$cnt = count($this->query_result);
165
166
if ($x >= count(get_object_vars($this->query_result[0]))) {
167
halt('A System Error Was Encountered', 'The offset x you specified overflows', 'sys_error');
168
}
169
170
for ($i = 0; $i < $cnt; $i++) {
171
// Returns all the values from the input array and indexes numerically the array
172
$values = array_values(get_object_vars($this->query_result[$i]));
173
if ($values[$x] !== '') {
174
$return_val[$i] = $values[$x];
175
}
176
}
177
}
178
179
return $return_val;
180
}
181
182
183
/**
184
* Gets multiple rows of results from the database based on query and returns them as a multi dimensional array.
185
*
186
* Each element of the array contains one row of results and can be specified to be either an object, associative array or numerical array.
187
* If no results are found then the function returns false enabling you to use the function within logic statements such as if.
188
*
189
* @param string $query SQL query.
190
* @param string $output (optional) ane of FETCH_ASSOC | FETCH_ASSOC | FETCH_OBJ constants.
191
* @return mixed Database query results
192
*/
193
public function get_all($query, $output = FETCH_OBJ) {
194
$return_val = array();
195
196
$this->exec_query($query);
197
198
// Send back array of objects. Each row is an object
199
if ($output == FETCH_OBJ) {
200
$return_val = $this->query_result;
201
} elseif ($output == FETCH_ASSOC || $output == FETCH_NUM) {
202
if ($this->query_result) {
203
$i = 0;
204
// $row is object
205
foreach($this->query_result as $row) {
206
$return_val[$i] = get_object_vars($row);
207
208
if ($output == FETCH_NUM) {
209
$return_val[$i] = array_values($return_val[$i]);
210
}
211
$i++;
212
}
213
}
214
}
215
216
return $return_val;
217
}
218
219
/**
220
* Begin Transaction
221
*
222
* @return bool
223
*/
224
public function trans_begin() {}
225
226
/**
227
* Commit Transaction
228
*
229
* @return bool
230
*/
231
public function trans_commit() {}
232
233
/**
234
* Rollback Transaction
235
*
236
* @return bool
237
*/
238
public function trans_rollback() {}
239
240
}
241
242
// End of file: ./system/core/base_dao.php
Page URI: http://www.infopotato.com/index.php/code/core/base_dao/
