Skip to content

Commit f30278b

Browse files
committed
- Documented classes, methods and constants in the source
- Defined DB_ERROR_NOSUCHTABLE and DB_ERROR_NOSUCHFIELD - Renamed DB_common::capableOf to DB_common::provides - Cleaned up the error code mapping - Added simpleQuery method in DB_mysql - Changed getXxx methods in DB_common to use simpleQuery - Fixed bug in DB_common::getAssoc
1 parent 1627a57 commit f30278b

File tree

1 file changed

+97
-22
lines changed

1 file changed

+97
-22
lines changed

pear/DB.php

Lines changed: 97 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717
// | |
1818
// +----------------------------------------------------------------------+
1919
//
20+
// $Id$
21+
//
2022
// Database independent query interface.
2123
//
2224

2325
// {{{ Database independent error codes.
2426

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+
*/
2534
define("DB_OK", 0);
2635
define("DB_ERROR", -1);
2736
define("DB_ERROR_SYNTAX", -2);
@@ -40,39 +49,80 @@
4049
define("DB_ERROR_CANNOT_CREATE", -15);
4150
define("DB_ERROR_CANNOT_DELETE", -16);
4251
define("DB_ERROR_CANNOT_DROP", -17);
52+
define("DB_ERROR_NOSUCHTABLE", -18);
53+
define("DB_ERROR_NOSUCHFIELD", -19);
4354

4455
// }}}
4556
// {{{ Prepare/execute parameter types
4657

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+
*/
4768
define("DB_PARAM_SCALAR", 1);
4869
define("DB_PARAM_OPAQUE", 2);
4970

5071
// }}}
5172
// {{{ Binary data modes
5273

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+
*/
5385
define("DB_BINMODE_PASSTHRU", 1);
5486
define("DB_BINMODE_RETURN", 2);
5587
define("DB_BINMODE_CONVERT", 3);
5688

5789
// }}}
5890

59-
// {{{ class DB
60-
6191
/**
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.
6495
*
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
66113
* @author Stig Bakken <ssb@fast.no>
67-
* @since 4.0b4
114+
* @since PHP 4.0
68115
*/
69116
class DB {
70117
// {{{ factory()
71118

72119
/**
73120
* Create a new DB object for the specified database type.
121+
*
74122
* @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
76126
*/
77127
function factory($type) {
78128
global $USED_PACKAGES;
@@ -93,6 +143,19 @@ function factory($type) {
93143
// }}}
94144
// {{{ connect()
95145

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+
*/
96159
function connect($dsn, $persistent = false) {
97160
global $USED_PACKAGES;
98161

@@ -109,7 +172,10 @@ function connect($dsn, $persistent = false) {
109172
}
110173
$classname = 'DB_' . $type;
111174
$obj = new $classname;
112-
$obj->connect(&$dsninfo, $persistent);
175+
$err = $obj->connect(&$dsninfo, $persistent);
176+
if (DB::isError($err)) {
177+
return $err;
178+
}
113179
return $obj; // XXX ADDREF
114180
}
115181

@@ -118,19 +184,22 @@ function connect($dsn, $persistent = false) {
118184

119185
/**
120186
* Return the DB API version.
121-
* @return int the DB API version number
187+
*
188+
* @return double the DB API version number
122189
*/
123190
function apiVersion() {
124-
return 100;
191+
return 1.00;
125192
}
126193

127194
// }}}
128195
// {{{ isError()
129196

130197
/**
131198
* 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
134203
*/
135204
function isError($code) {
136205
return is_int($code) && ($code < 0);
@@ -140,9 +209,12 @@ function isError($code) {
140209
// {{{ errorMessage()
141210

142211
/**
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
146218
*/
147219
function errorMessage($code) {
148220
if (!is_array($errorMessages)) {
@@ -160,7 +232,12 @@ function errorMessage($code) {
160232
DB_ERROR_INVALID_NUMBER => "invalid number",
161233
DB_ERROR_INVALID_DATE => "invalid date or time",
162234
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"
164241
);
165242
}
166243
return $errorMessages[$code];
@@ -260,18 +337,18 @@ function parseDSN($dsn) {
260337
// }}}
261338
}
262339

263-
// }}}
264-
// {{{ class DB_result
265-
266340
/**
267341
* This class implements a wrapper for a DB result set.
268342
* A new instance of this class will be returned by the DB implementation
269343
* after processing a query that returns data.
270344
*/
271345
class DB_result {
346+
// {{{ properties
347+
272348
var $dbh;
273349
var $result;
274350

351+
// }}}
275352
// {{{ DB_result()
276353

277354
/**
@@ -324,7 +401,7 @@ function numCols() {
324401
// {{{ free()
325402

326403
/**
327-
* Frees the resource for this result and reset ourselves.
404+
* Frees the resources allocated for this result set.
328405
* @return int error code
329406
*/
330407
function free() {
@@ -339,8 +416,6 @@ function free() {
339416
// }}}
340417
}
341418

342-
// }}}
343-
344419
// Local variables:
345420
// tab-width: 4
346421
// c-basic-offset: 4

0 commit comments

Comments
 (0)