17
17
// | |
18
18
// +----------------------------------------------------------------------+
19
19
//
20
+ // $Id$
21
+ //
20
22
// Database independent query interface.
21
23
//
22
24
23
25
// {{{ Database independent error codes.
24
26
27
+ /*
28
+ * The method mapErrorCode in each DB_dbtype implementation maps
29
+ * native error codes to one of these.
30
+ *
31
+ * If you add an error code here, make sure you also add a textual
32
+ * version of it in DB::errorMessage().
33
+ */
25
34
define ("DB_OK " , 0 );
26
35
define ("DB_ERROR " , -1 );
27
36
define ("DB_ERROR_SYNTAX " , -2 );
40
49
define ("DB_ERROR_CANNOT_CREATE " , -15 );
41
50
define ("DB_ERROR_CANNOT_DELETE " , -16 );
42
51
define ("DB_ERROR_CANNOT_DROP " , -17 );
52
+ define ("DB_ERROR_NOSUCHTABLE " , -18 );
53
+ define ("DB_ERROR_NOSUCHFIELD " , -19 );
43
54
44
55
// }}}
45
56
// {{{ Prepare/execute parameter types
46
57
58
+ /*
59
+ * These constants are used when storing information about prepared
60
+ * statements (using the "prepare" method in DB_dbtype).
61
+ *
62
+ * The prepare/execute model in DB is borrowed from the ODBC extension,
63
+ * in a query the "?" character means a scalar parameter, and a "*"
64
+ * character means an opaque parameter. An opaque parameter is simply
65
+ * a file name, the real data are in that file (useful for large blob
66
+ * operations).
67
+ */
47
68
define ("DB_PARAM_SCALAR " , 1 );
48
69
define ("DB_PARAM_OPAQUE " , 2 );
49
70
50
71
// }}}
51
72
// {{{ Binary data modes
52
73
74
+ /*
75
+ * These constants define different ways of returning binary data
76
+ * from queries. Again, this model has been borrowed from the ODBC
77
+ * extension.
78
+ *
79
+ * DB_BINMODE_PASSTHRU sends the data directly through to the browser
80
+ * when data is fetched from the database.
81
+ * DB_BINMODE_RETURN lets you return data as usual.
82
+ * DB_BINMODE_CONVERT returns data as well, only it is converted to
83
+ * hex format, for example the string "123" would become "313233".
84
+ */
53
85
define ("DB_BINMODE_PASSTHRU " , 1 );
54
86
define ("DB_BINMODE_RETURN " , 2 );
55
87
define ("DB_BINMODE_CONVERT " , 3 );
56
88
57
89
// }}}
58
90
59
- // {{{ class DB
60
-
61
91
/**
62
- * This class implements a factory method for creating DB objects,
63
- * as well as some "static methods".
92
+ * The main "DB" class is simply a container class with some static
93
+ * methods for creating DB objects as well as some utility functions
94
+ * common to all parts of DB.
64
95
*
65
- * @version 100
96
+ * The object model of DB is as follows (indentation means inheritance):
97
+ *
98
+ * DB The main DB class. This is simply a utility class
99
+ * with some "static" methods for creating DB objects as
100
+ * well as common utility functions for other DB classes.
101
+ *
102
+ * DB_common The base for each DB implementation. Provides default
103
+ * | implementations (some would say virtual methods) for
104
+ * | the actual DB implementations as well as a bunch of
105
+ * | query utility functions.
106
+ * |
107
+ * +-DB_mysql The DB implementation for MySQL. Inherits DB_common.
108
+ * When calling DB::factory or DB::connect for MySQL
109
+ * connections, the object returned is an instance of this
110
+ * class.
111
+ *
112
+ * @version 1.00
66
113
* @author Stig Bakken <ssb@fast.no>
67
- * @since 4.0b4
114
+ * @since PHP 4.0
68
115
*/
69
116
class DB {
70
117
// {{{ factory()
71
118
72
119
/**
73
120
* Create a new DB object for the specified database type.
121
+ *
74
122
* @param $type database type
75
- * @return object a newly created DB object, or false on error
123
+ *
124
+ * @return object a newly created DB object, or a DB error code on
125
+ * error
76
126
*/
77
127
function factory ($ type ) {
78
128
global $ USED_PACKAGES ;
@@ -93,6 +143,19 @@ function factory($type) {
93
143
// }}}
94
144
// {{{ connect()
95
145
146
+ /**
147
+ * Create a new DB object and connect to the specified database.
148
+ *
149
+ * @param $dsn "data source name", see the parseDSN method for a
150
+ * description of the dsn format.
151
+ *
152
+ * @param $persistent (optional) whether this connection should be
153
+ * persistent. Ignored if the backend extension does not support
154
+ * persistent connections.
155
+ *
156
+ * @return object a newly created DB object, or a DB error code on
157
+ * error
158
+ */
96
159
function connect ($ dsn , $ persistent = false ) {
97
160
global $ USED_PACKAGES ;
98
161
@@ -109,7 +172,10 @@ function connect($dsn, $persistent = false) {
109
172
}
110
173
$ classname = 'DB_ ' . $ type ;
111
174
$ obj = new $ classname ;
112
- $ obj ->connect (&$ dsninfo , $ persistent );
175
+ $ err = $ obj ->connect (&$ dsninfo , $ persistent );
176
+ if (DB ::isError ($ err )) {
177
+ return $ err ;
178
+ }
113
179
return $ obj ; // XXX ADDREF
114
180
}
115
181
@@ -118,19 +184,22 @@ function connect($dsn, $persistent = false) {
118
184
119
185
/**
120
186
* Return the DB API version.
121
- * @return int the DB API version number
187
+ *
188
+ * @return double the DB API version number
122
189
*/
123
190
function apiVersion () {
124
- return 100 ;
191
+ return 1.00 ;
125
192
}
126
193
127
194
// }}}
128
195
// {{{ isError()
129
196
130
197
/**
131
198
* Tell whether a result code from a DB method is an error.
132
- * @param $code result code
133
- * @return bool whether $code is an error
199
+ *
200
+ * @param $code result code
201
+ *
202
+ * @return bool whether $code is an error
134
203
*/
135
204
function isError ($ code ) {
136
205
return is_int ($ code ) && ($ code < 0 );
@@ -140,9 +209,12 @@ function isError($code) {
140
209
// {{{ errorMessage()
141
210
142
211
/**
143
- * Return a textual error message for an error code.
144
- * @param $code error code
145
- * @return string error message
212
+ * Return a textual error message for a DB error code.
213
+ *
214
+ * @param $code error code
215
+ *
216
+ * @return string error message, or false if the error code was
217
+ * not recognized
146
218
*/
147
219
function errorMessage ($ code ) {
148
220
if (!is_array ($ errorMessages )) {
@@ -160,7 +232,12 @@ function errorMessage($code) {
160
232
DB_ERROR_INVALID_NUMBER => "invalid number " ,
161
233
DB_ERROR_INVALID_DATE => "invalid date or time " ,
162
234
DB_ERROR_DIVZERO => "division by zero " ,
163
- DB_ERROR_NODBSELECTED => "no database selected "
235
+ DB_ERROR_NODBSELECTED => "no database selected " ,
236
+ DB_ERROR_CANNOT_CREATE => "can not create " ,
237
+ DB_ERROR_CANNOT_DELETE => "can not delete " ,
238
+ DB_ERROR_CANNOT_DROP => "can not drop " ,
239
+ DB_ERROR_NOSUCHTABLE => "no such table " ,
240
+ DB_ERROR_NOSUCHFIELD => "no such field "
164
241
);
165
242
}
166
243
return $ errorMessages [$ code ];
@@ -260,18 +337,18 @@ function parseDSN($dsn) {
260
337
// }}}
261
338
}
262
339
263
- // }}}
264
- // {{{ class DB_result
265
-
266
340
/**
267
341
* This class implements a wrapper for a DB result set.
268
342
* A new instance of this class will be returned by the DB implementation
269
343
* after processing a query that returns data.
270
344
*/
271
345
class DB_result {
346
+ // {{{ properties
347
+
272
348
var $ dbh ;
273
349
var $ result ;
274
350
351
+ // }}}
275
352
// {{{ DB_result()
276
353
277
354
/**
@@ -324,7 +401,7 @@ function numCols() {
324
401
// {{{ free()
325
402
326
403
/**
327
- * Frees the resource for this result and reset ourselves .
404
+ * Frees the resources allocated for this result set .
328
405
* @return int error code
329
406
*/
330
407
function free () {
@@ -339,8 +416,6 @@ function free() {
339
416
// }}}
340
417
}
341
418
342
- // }}}
343
-
344
419
// Local variables:
345
420
// tab-width: 4
346
421
// c-basic-offset: 4
0 commit comments