PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/adodb/adodb-pear.inc.php

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