PageRenderTime 35ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/adodb_lite/adodb.inc.php

http://pacercms.googlecode.com/
PHP | 386 lines | 229 code | 53 blank | 104 comment | 26 complexity | fe39a35b68f76a6ee88b316a93c823fb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * ADOdb Lite is a PHP class to encapsulate multiple database APIs and is compatible with
  4. * a subset of the ADODB Command Syntax.
  5. * Currently supports Frontbase, MaxDB, miniSQL, MSSQL, MSSQL Pro, MySQLi, MySQLt, MySQL, PostgresSQL,
  6. * PostgresSQL64, PostgresSQL7, PostgresSQL8, SqLite, SqLite Pro, Sybase and Sybase ASE.
  7. *
  8. */
  9. if (!defined('_ADODB_LAYER'))
  10. define('_ADODB_LAYER',1);
  11. if (!defined('ADODB_DIR'))
  12. define('ADODB_DIR', dirname(__FILE__));
  13. $ADODB_vers = 'V1.30 ADOdb Lite 1 September 2006 (c) 2005, 2006 Mark Dickenson. All rights reserved. Released LGPL.';
  14. define('ADODB_FETCH_DEFAULT',0);
  15. define('ADODB_FETCH_NUM',1);
  16. define('ADODB_FETCH_ASSOC',2);
  17. define('ADODB_FETCH_BOTH',3);
  18. GLOBAL $ADODB_FETCH_MODE;
  19. $ADODB_FETCH_MODE = ADODB_FETCH_DEFAULT; // DEFAULT, NUM, ASSOC or BOTH. Default follows native driver default...
  20. /**
  21. * Database connection
  22. * Usage: $db = new ADONewConnection('dbtype');
  23. *
  24. * @access public
  25. * @param string $dbtype
  26. */
  27. function &ADONewConnection( $dbtype = 'mysql', $modules = '' )
  28. {
  29. global $ADODB_FETCH_MODE;
  30. $false = false;
  31. @include( ADODB_DIR . '/adodb.config.php' );
  32. if (strpos($dbtype,'://')) {
  33. $dsn_array = @parse_url(rawurldecode($dbtype));
  34. if (!$dsn_array || !$dsn_array['scheme'])
  35. return $false;
  36. $dbtype = $dsn_array['scheme'];
  37. $modules = (!empty($dsn_array['fragment'])) ? $dsn_array['fragment'] : $modules;
  38. } else $dsn_array = array('scheme'=>'');
  39. $dbtype = strtolower($dbtype);
  40. include_once ADODB_DIR . '/adodbSQL_drivers/' . $dbtype . '/' . $dbtype . '_driver.inc';
  41. $last_module = $dbtype . '_' . 'driver';
  42. if(!empty($modules))
  43. {
  44. $module_list = explode(":", strtolower($modules));
  45. $generic_modules = array();
  46. foreach($module_list as $mod) {
  47. $mod = trim($mod);
  48. if(is_file(ADODB_DIR . '/generic_modules/' . $mod . '_module.inc'))
  49. {
  50. $generic_modules[] = $mod;
  51. }
  52. else
  53. {
  54. include_once ADODB_DIR . '/adodbSQL_drivers/' . $dbtype . '/' . $dbtype . '_' . $mod . '_module.inc';
  55. $last_module = $dbtype . '_' . $mod;
  56. }
  57. }
  58. if(count($generic_modules))
  59. {
  60. foreach($generic_modules as $mod) {
  61. include_once ADODB_DIR . '/generic_modules/' . $mod . '_module.inc';
  62. $last_module = $mod;
  63. }
  64. }
  65. }
  66. $extention = $last_module . '_ADOConnection';
  67. $object = new $extention();
  68. $object->last_module_name = $last_module;
  69. $object->raiseErrorFn = (defined('ADODB_ERROR_HANDLER')) ? ADODB_ERROR_HANDLER : false;
  70. $object->query_count = 0;
  71. $object->query_time_total = 0;
  72. if(!empty($dsn_array['scheme']))
  73. {
  74. if (isset($dsn_array['port'])) $object->port = $dsn_array['port'];
  75. $persistent = false;
  76. $forcenew = false;
  77. if (isset($dsn_array['query'])) {
  78. $option_array = explode('&', $dsn_array['query']);
  79. foreach($option_array as $element => $value) {
  80. $array = explode('=', $value);
  81. $data = isset($array[1]) ? $array[1] : 1;
  82. switch(strtolower($array[0])) {
  83. case 'persist':
  84. case 'persistent':
  85. $persistent = $data;
  86. break;
  87. case 'debug':
  88. $object->debug = (integer) $data;
  89. break;
  90. case 'fetchmode':
  91. $ADODB_FETCH_MODE = constant($data);
  92. break;
  93. case 'clientflags':
  94. $object->clientFlags = $data;
  95. break;
  96. case 'port':
  97. $object->port = $data;
  98. break;
  99. case 'socket':
  100. $object->socket = $data;
  101. break;
  102. case 'forcenew':
  103. $forcenew = $data;
  104. break;
  105. }
  106. }
  107. }
  108. $dsn_array['host'] = isset($dsn_array['host']) ? $dsn_array['host'] : '';
  109. $dsn_array['user'] = isset($dsn_array['user']) ? $dsn_array['user'] : '';
  110. $dsn_array['pass'] = isset($dsn_array['pass']) ? $dsn_array['pass'] : '';
  111. $dsn_array['path'] = isset($dsn_array['path']) ? substr($dsn_array['path'], 1) : '';
  112. $result = $object->_connect($dsn_array['host'], $dsn_array['user'], $dsn_array['pass'], $dsn_array['path'], $persistent, $forcenew);
  113. if (!$result) return $false;
  114. }
  115. return $object;
  116. }
  117. /**
  118. * Alternative Database connection
  119. * Usage: $db = new NewADOConnection('dbtype');
  120. *
  121. * @access public
  122. * @param string $dbtype
  123. */
  124. function &NewADOConnection($dbtype='', $module = '' )
  125. {
  126. $tmp =& ADONewConnection($dbtype, $module);
  127. return $tmp;
  128. }
  129. function &NewDataDictionary(&$connection, $dbtype=false)
  130. {
  131. if(!$dbtype)
  132. $dbtype = $connection->dbtype;
  133. include_once ADODB_DIR . '/adodb-datadict.inc.php';
  134. include_once ADODB_DIR . '/adodbSQL_drivers/' . $dbtype . '/' . $dbtype . '_datadict.inc';
  135. $class = "ADODB2_$dbtype";
  136. $dict = new $class();
  137. $dict->connection = &$connection;
  138. $dict->upperName = strtoupper($dbtype);
  139. $dict->quote = $connection->nameQuote;
  140. $dict->debug_echo = $connection->debug_echo;
  141. return $dict;
  142. }
  143. /**
  144. * Backwards compatible with ADOdb usage of NewPerfMonitor
  145. * Change to module basis for PerfMon mean we need only return a reference to $connection object.
  146. * Usage: $perf =& NewPerfMonitor($conn); - $perf is a reference to $conn
  147. *
  148. * @access public
  149. * @param ADOConnection $connection
  150. * @param string $dbtype This is an optional parameter with no actual use in ADOdb-Lite; for BC only.
  151. */
  152. function &NewPerfMonitor(&$connection, $dbtype=false)
  153. {
  154. return $connection;
  155. }
  156. class ADOConnection
  157. {
  158. var $connectionId = false;
  159. var $record_set = false;
  160. var $database;
  161. var $dbtype;
  162. var $host;
  163. var $open;
  164. var $password;
  165. var $username;
  166. var $persistent;
  167. var $debug = false;
  168. var $debug_echo = true;
  169. var $debug_output;
  170. var $forcenewconnection = false;
  171. var $createdatabase = false;
  172. var $last_module_name;
  173. var $socket = false;
  174. var $port = false;
  175. var $clientFlags = 0;
  176. var $nameQuote = '"';
  177. var $sysDate = false; /// name of function that returns the current date
  178. var $sysTimeStamp = false; /// name of function that returns the current timestamp
  179. var $sql;
  180. var $raiseErrorFn = false;
  181. var $query_count = 0;
  182. var $query_time_total = 0;
  183. function ADOConnection()
  184. {
  185. }
  186. /**
  187. * Returns floating point version number of ADOdb Lite
  188. * Usage: $db->Version();
  189. *
  190. * @access public
  191. */
  192. function Version()
  193. {
  194. global $ADODB_vers;
  195. return (float) substr($ADODB_vers,1);
  196. }
  197. /**
  198. * Returns true if connected to database
  199. * Usage: $db->IsConnected();
  200. *
  201. * @access public
  202. */
  203. function IsConnected()
  204. {
  205. if($this->connectionId === false || $this->connectionId == false)
  206. return false;
  207. else return true;
  208. }
  209. /**
  210. * Normal Database connection
  211. * Usage: $result = $db->Connect('host', 'username', 'password', 'database');
  212. *
  213. * @access public
  214. * @param string $database
  215. * @param string $host
  216. * @param string $password
  217. * @param string $username
  218. * @param string $forcenew // private
  219. */
  220. function Connect( $host = "", $username = "", $password = "", $database = "", $forcenew = false)
  221. {
  222. return $this->_connect($host, $username, $password, $database, false, $forcenew);
  223. }
  224. /**
  225. * Persistent Database connection
  226. * Usage: $result = $db->PConnect('host', 'username', 'password', 'database');
  227. *
  228. * @access public
  229. * @param string $database
  230. * @param string $host
  231. * @param string $password
  232. * @param string $username
  233. */
  234. function PConnect( $host = "", $username = "", $password = "", $database = "")
  235. {
  236. return $this->_connect($host, $username, $password, $database, true, false);
  237. }
  238. /**
  239. * Force New Database connection
  240. * Usage: $result = $db->NConnect('host', 'username', 'password', 'database');
  241. *
  242. * @access public
  243. * @param string $database
  244. * @param string $host
  245. * @param string $password
  246. * @param string $username
  247. */
  248. function NConnect( $host = "", $username = "", $password = "", $database = "")
  249. {
  250. return $this->_connect($host, $username, $password, $database, false, true);
  251. }
  252. /**
  253. * Returns SQL query and instantiates sql statement & resultset driver
  254. * Usage: $linkId =& $db->execute( 'SELECT * FROM foo ORDER BY id' );
  255. *
  256. * @access public
  257. * @param string $sql
  258. * @return mixed Resource ID, Array
  259. */
  260. function &Execute( $sql, $inputarr = false )
  261. {
  262. // adodb_log_sql will time the query execution and log the sql query
  263. // note: the later $this->do_query() should not run since adodb_log_sql() independently executes the query itself.
  264. if($this->_logsql === true)
  265. {
  266. $ret =& adodb_log_sql($this, $sql, $inputarr);
  267. if (isset($ret)) return $ret;
  268. }
  269. $rs =& $this->do_query($sql, -1, -1, $inputarr);
  270. return $rs;
  271. }
  272. /**
  273. * Returns SQL query and instantiates sql statement & resultset driver
  274. * Usage: $linkId =& $db->SelectLimit( 'SELECT * FROM foo ORDER BY id', $nrows, $offset );
  275. * $nrows and $offset are optional
  276. *
  277. * @access public
  278. * @param string $sql
  279. * @param string $nrows
  280. * @param string $offset
  281. * @return mixed Resource ID, Array
  282. */
  283. function &SelectLimit( $sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0 )
  284. {
  285. $rs =& $this->do_query( $sql, $offset, $nrows, $inputarr);
  286. return $rs;
  287. }
  288. /**
  289. * Display debug output and database error.
  290. *
  291. * @access private
  292. */
  293. function outp($text, $newline = true)
  294. {
  295. global $ADODB_OUTP;
  296. $this->debug_output = "<br>\n(" . $this->dbtype . "): ".htmlspecialchars($text)."<br>\n Error (" . $this->ErrorNo() .'): '. $this->ErrorMsg() . "<br>\n";
  297. if(defined('ADODB_OUTP'))
  298. {
  299. $fn = ADODB_OUTP;
  300. } else if(isset($ADODB_OUTP))
  301. {
  302. $fn = $ADODB_OUTP;
  303. }
  304. if(defined('ADODB_OUTP') || isset($ADODB_OUTP))
  305. {
  306. $fn($this->debug_output, $newline);
  307. return;
  308. }
  309. if($this->debug_echo)
  310. echo $this->debug_output;
  311. }
  312. }
  313. /**
  314. * Empty result record set for updates, inserts, ect
  315. *
  316. * @access private
  317. */
  318. class ADORecordSet_empty
  319. {
  320. var $fields = false;
  321. var $EOF = true;
  322. function MoveNext() {return;}
  323. function RecordCount() {return 0;}
  324. function FieldCount() {return 0;}
  325. function EOF(){return TRUE;}
  326. function Close(){return true;}
  327. }
  328. class ADOFieldObject {
  329. var $name = '';
  330. var $max_length=0;
  331. var $type="";
  332. }
  333. ?>