Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
36 kaklik 1
<?php
2
/** 
3
 * @version V4.80 8 Mar 2006 (c) 2000-2006 John Lim (jlim@natsoft.com.my). All rights reserved.
4
 * Released under both BSD license and Lesser GPL library license. 
5
 * Whenever there is any discrepancy between the two licenses, 
6
 * the BSD license will take precedence. 
7
 *
8
 * Set tabs to 4 for best viewing.
9
 * 
10
 * PEAR DB Emulation Layer for ADODB.
11
 *
12
 * The following code is modelled on PEAR DB code by Stig Bakken <ssb@fast.no>								   |
13
 * and Tomas V.V.Cox <cox@idecnet.com>.	Portions (c)1997-2002 The PHP Group.
14
 */
15
 
16
 /*
17
 We support:
18
 
19
 DB_Common
20
 ---------
21
 	query - returns PEAR_Error on error
22
	limitQuery - return PEAR_Error on error
23
	prepare - does not return PEAR_Error on error
24
	execute - does not return PEAR_Error on error
25
	setFetchMode - supports ASSOC and ORDERED
26
	errorNative
27
	quote
28
	nextID
29
	disconnect
30
 
31
	getOne
32
	getAssoc
33
	getRow
34
	getCol
35
	getAll
36
 
37
 DB_Result
38
 ---------
39
 	numRows - returns -1 if not supported
40
	numCols
41
	fetchInto - does not support passing of fetchmode
42
	fetchRows - does not support passing of fetchmode
43
	free
44
 */
45
 
46
define('ADODB_PEAR',dirname(__FILE__));
47
include_once "PEAR.php";
48
include_once ADODB_PEAR."/adodb-errorpear.inc.php";
49
include_once ADODB_PEAR."/adodb.inc.php";
50
 
51
if (!defined('DB_OK')) {
52
define("DB_OK",	1);
53
define("DB_ERROR",-1);
54
 
55
// autoExecute constants
56
define('DB_AUTOQUERY_INSERT', 1);
57
define('DB_AUTOQUERY_UPDATE', 2);
58
 
59
/**
60
 * This is a special constant that tells DB the user hasn't specified
61
 * any particular get mode, so the default should be used.
62
 */
63
 
64
define('DB_FETCHMODE_DEFAULT', 0);
65
 
66
/**
67
 * Column data indexed by numbers, ordered from 0 and up
68
 */
69
 
70
define('DB_FETCHMODE_ORDERED', 1);
71
 
72
/**
73
 * Column data indexed by column names
74
 */
75
 
76
define('DB_FETCHMODE_ASSOC', 2);
77
 
78
/* for compatibility */
79
 
80
define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED);
81
define('DB_GETMODE_ASSOC',   DB_FETCHMODE_ASSOC);
82
 
83
/**
84
 * these are constants for the tableInfo-function
85
 * they are bitwised or'ed. so if there are more constants to be defined
86
 * in the future, adjust DB_TABLEINFO_FULL accordingly
87
 */
88
 
89
define('DB_TABLEINFO_ORDER', 1);
90
define('DB_TABLEINFO_ORDERTABLE', 2);
91
define('DB_TABLEINFO_FULL', 3);
92
}
93
 
94
/**
95
 * The main "DB" class is simply a container class with some static
96
 * methods for creating DB objects as well as some utility functions
97
 * common to all parts of DB.
98
 *
99
 */
100
 
101
class DB
102
{
103
	/**
104
	 * Create a new DB object for the specified database type
105
	 *
106
	 * @param $type string database type, for example "mysql"
107
	 *
108
	 * @return object a newly created DB object, or a DB error code on
109
	 * error
110
	 */
111
 
112
	function &factory($type)
113
	{
114
		include_once(ADODB_DIR."/drivers/adodb-$type.inc.php");
115
		$obj = &NewADOConnection($type);
116
		if (!is_object($obj)) $obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1);
117
		return $obj;
118
	}
119
 
120
	/**
121
	 * Create a new DB object and connect to the specified database
122
	 *
123
	 * @param $dsn mixed "data source name", see the DB::parseDSN
124
	 * method for a description of the dsn format.  Can also be
125
	 * specified as an array of the format returned by DB::parseDSN.
126
	 *
127
	 * @param $options mixed if boolean (or scalar), tells whether
128
	 * this connection should be persistent (for backends that support
129
	 * this).  This parameter can also be an array of options, see
130
	 * DB_common::setOption for more information on connection
131
	 * options.
132
	 *
133
	 * @return object a newly created DB connection object, or a DB
134
	 * error object on error
135
	 *
136
	 * @see DB::parseDSN
137
	 * @see DB::isError
138
	 */
139
	function &connect($dsn, $options = false)
140
	{
141
		if (is_array($dsn)) {
142
			$dsninfo = $dsn;
143
		} else {
144
			$dsninfo = DB::parseDSN($dsn);
145
		}
146
		switch ($dsninfo["phptype"]) {
147
			case 'pgsql': 	$type = 'postgres7'; break;
148
			case 'ifx':		$type = 'informix9'; break;
149
			default: 		$type = $dsninfo["phptype"]; break;
150
		}
151
 
152
		if (is_array($options) && isset($options["debug"]) &&
153
			$options["debug"] >= 2) {
154
			// expose php errors with sufficient debug level
155
			 @include_once("adodb-$type.inc.php");
156
		} else {
157
			 @include_once("adodb-$type.inc.php");
158
		}
159
 
160
		@$obj =& NewADOConnection($type);
161
		if (!is_object($obj)) {
162
			$obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1);
163
			return $obj;
164
		}
165
		if (is_array($options)) {
166
			foreach($options as $k => $v) {
167
				switch(strtolower($k)) {
168
				case 'persist':
169
				case 'persistent': 	$persist = $v; break;
170
				#ibase
171
				case 'dialect': 	$obj->dialect = $v; break;
172
				case 'charset':		$obj->charset = $v; break;
173
				case 'buffers':		$obj->buffers = $v; break;
174
				#ado
175
				case 'charpage':	$obj->charPage = $v; break;
176
				#mysql
177
				case 'clientflags': $obj->clientFlags = $v; break;
178
				}
179
			}
180
		} else {
181
		   	$persist = false;
182
		}
183
 
184
		if (isset($dsninfo['socket'])) $dsninfo['hostspec'] .= ':'.$dsninfo['socket'];
185
		else if (isset($dsninfo['port'])) $dsninfo['hostspec'] .= ':'.$dsninfo['port'];
186
 
187
		if($persist) $ok = $obj->PConnect($dsninfo['hostspec'], $dsninfo['username'],$dsninfo['password'],$dsninfo['database']);
188
		else  $ok = $obj->Connect($dsninfo['hostspec'], $dsninfo['username'],$dsninfo['password'],$dsninfo['database']);
189
 
190
		if (!$ok) $obj = ADODB_PEAR_Error();
191
		return $obj;
192
	}
193
 
194
	/**
195
	 * Return the DB API version
196
	 *
197
	 * @return int the DB API version number
198
	 */
199
	function apiVersion()
200
	{
201
		return 2;
202
	}
203
 
204
	/**
205
	 * Tell whether a result code from a DB method is an error
206
	 *
207
	 * @param $value int result code
208
	 *
209
	 * @return bool whether $value is an error
210
	 */
211
	function isError($value)
212
	{
213
		if (!is_object($value)) return false;
214
		$class = get_class($value);
215
		return $class == 'pear_error' || is_subclass_of($value, 'pear_error') || 
216
				$class == 'db_error' || is_subclass_of($value, 'db_error');
217
	}
218
 
219
 
220
	/**
221
	 * Tell whether a result code from a DB method is a warning.
222
	 * Warnings differ from errors in that they are generated by DB,
223
	 * and are not fatal.
224
	 *
225
	 * @param $value mixed result value
226
	 *
227
	 * @return bool whether $value is a warning
228
	 */
229
	function isWarning($value)
230
	{
231
		return false;
232
		/*
233
		return is_object($value) &&
234
			(get_class( $value ) == "db_warning" ||
235
			 is_subclass_of($value, "db_warning"));*/
236
	}
237
 
238
	/**
239
	 * Parse a data source name
240
	 *
241
	 * @param $dsn string Data Source Name to be parsed
242
	 *
243
	 * @return array an associative array with the following keys:
244
	 *
245
	 *  phptype: Database backend used in PHP (mysql, odbc etc.)
246
	 *  dbsyntax: Database used with regards to SQL syntax etc.
247
	 *  protocol: Communication protocol to use (tcp, unix etc.)
248
	 *  hostspec: Host specification (hostname[:port])
249
	 *  database: Database to use on the DBMS server
250
	 *  username: User name for login
251
	 *  password: Password for login
252
	 *
253
	 * The format of the supplied DSN is in its fullest form:
254
	 *
255
	 *  phptype(dbsyntax)://username:password@protocol+hostspec/database
256
	 *
257
	 * Most variations are allowed:
258
	 *
259
	 *  phptype://username:password@protocol+hostspec:110//usr/db_file.db
260
	 *  phptype://username:password@hostspec/database_name
261
	 *  phptype://username:password@hostspec
262
	 *  phptype://username@hostspec
263
	 *  phptype://hostspec/database
264
	 *  phptype://hostspec
265
	 *  phptype(dbsyntax)
266
	 *  phptype
267
	 *
268
	 * @author Tomas V.V.Cox <cox@idecnet.com>
269
	 */
270
	function parseDSN($dsn)
271
	{
272
		if (is_array($dsn)) {
273
			return $dsn;
274
		}
275
 
276
		$parsed = array(
277
			'phptype'  => false,
278
			'dbsyntax' => false,
279
			'protocol' => false,
280
			'hostspec' => false,
281
			'database' => false,
282
			'username' => false,
283
			'password' => false
284
		);
285
 
286
		// Find phptype and dbsyntax
287
		if (($pos = strpos($dsn, '://')) !== false) {
288
			$str = substr($dsn, 0, $pos);
289
			$dsn = substr($dsn, $pos + 3);
290
		} else {
291
			$str = $dsn;
292
			$dsn = NULL;
293
		}
294
 
295
		// Get phptype and dbsyntax
296
		// $str => phptype(dbsyntax)
297
		if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
298
			$parsed['phptype'] = $arr[1];
299
			$parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
300
		} else {
301
			$parsed['phptype'] = $str;
302
			$parsed['dbsyntax'] = $str;
303
		}
304
 
305
		if (empty($dsn)) {
306
			return $parsed;
307
		}
308
 
309
		// Get (if found): username and password
310
		// $dsn => username:password@protocol+hostspec/database
311
		if (($at = strpos($dsn,'@')) !== false) {
312
			$str = substr($dsn, 0, $at);
313
			$dsn = substr($dsn, $at + 1);
314
			if (($pos = strpos($str, ':')) !== false) {
315
				$parsed['username'] = urldecode(substr($str, 0, $pos));
316
				$parsed['password'] = urldecode(substr($str, $pos + 1));
317
			} else {
318
				$parsed['username'] = urldecode($str);
319
			}
320
		}
321
 
322
		// Find protocol and hostspec
323
		// $dsn => protocol+hostspec/database
324
		if (($pos=strpos($dsn, '/')) !== false) {
325
			$str = substr($dsn, 0, $pos);
326
			$dsn = substr($dsn, $pos + 1);
327
		} else {
328
			$str = $dsn;
329
			$dsn = NULL;
330
		}
331
 
332
		// Get protocol + hostspec
333
		// $str => protocol+hostspec
334
		if (($pos=strpos($str, '+')) !== false) {
335
			$parsed['protocol'] = substr($str, 0, $pos);
336
			$parsed['hostspec'] = urldecode(substr($str, $pos + 1));
337
		} else {
338
			$parsed['hostspec'] = urldecode($str);
339
		}
340
 
341
		// Get dabase if any
342
		// $dsn => database
343
		if (!empty($dsn)) {
344
			$parsed['database'] = $dsn;
345
		}
346
 
347
		return $parsed;
348
	}
349
 
350
	/**
351
	 * Load a PHP database extension if it is not loaded already.
352
	 *
353
	 * @access public
354
	 *
355
	 * @param $name the base name of the extension (without the .so or
356
	 * .dll suffix)
357
	 *
358
	 * @return bool true if the extension was already or successfully
359
	 * loaded, false if it could not be loaded
360
	 */
361
	function assertExtension($name)
362
	{
363
		if (!extension_loaded($name)) {
364
			$dlext = (strncmp(PHP_OS,'WIN',3) === 0) ? '.dll' : '.so';
365
			@dl($name . $dlext);
366
		}
367
		if (!extension_loaded($name)) {
368
			return false;
369
		}
370
		return true;
371
	}
372
}
373
 
374
?>