PDO.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /** File PDO.class.php *
  3. * Porting of native PHP 5.1 PDO *
  4. * object usable with PHP 4.X.X *
  5. * and PHP 5.0.X version. *
  6. * ------------------------------------ *
  7. *(C) Andrea Giammarchi [2005/10/19] *
  8. * ____________________________________ */
  9. // check and preserve native PDO driver, for PHP4 or PHP 5.0 users
  10. if(!class_exists('PDO')) {
  11. // SUPPORTED STATIC ENVIROMENT VARIABLES
  12. define('PDO_ATTR_SERVER_VERSION', 4); // server version
  13. define('PDO_ATTR_CLIENT_VERSION', 5); // client version
  14. define('PDO_ATTR_SERVER_INFO', 6); // server informations
  15. define('PDO_ATTR_PERSISTENT', 12); // connection mode, persistent or normal
  16. // SUPPORTED STATIC PDO FETCH MODE VARIABLES
  17. define('PDO_FETCH_ASSOC', 2); // such mysql_fetch_assoc
  18. define('PDO_FETCH_NUM', 3); // such mysql_fetch_row
  19. define('PDO_FETCH_BOTH', 4); // such mysql_fetch_array
  20. define('PDO_FETCH_OBJ', 5); // such mysql_fetch_object
  21. // UNSUPPORTED STATIC PDO FETCH MODE VARIABLES
  22. define('PDO_FETCH_LAZY', 1); // usable but not supported, default is PDO_FETCH_BOTH and will be used
  23. define('PDO_FETCH_BOUND', 6); // usable but not supported, default is PDO_FETCH_BOTH and will be used
  24. /**
  25. * Class PDO
  26. * PostgreSQL, SQLITE and MYSQL PDO support for PHP 4.X.X or PHP 5.0.X users, compatible with PHP 5.1.0 (RC1).
  27. *
  28. * DESCRIPTION [directly from http://us2.php.net/manual/en/ref.pdo.php]
  29. * The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.
  30. * Each database driver that implements the PDO interface can expose database-specific features as regular extension functions.
  31. * Note that you cannot perform any database functions using the PDO extension by itself;
  32. * you must use a database-specific PDO driver to access a database server.
  33. *
  34. * HOW TO USE
  35. * To know how to use PDO driver and all its methods visit php.net wonderful documentation.
  36. * http://us2.php.net/manual/en/ref.pdo.php
  37. * In this class some methods are not available and actually this porting is only for MySQL, SQLITE and PostgreSQL.
  38. *
  39. * LIMITS
  40. * For some reasons ( time and php used version with this class ) some PDO methods are not availables and
  41. * someother are not totally supported.
  42. *
  43. * PDO :: UNSUPPORTED METHODS:
  44. * - beginTransaction [ mysql 3 has not transaction and manage them is possible only with a direct BEGIN
  45. * or COMMIT query ]
  46. * - commit
  47. * - rollback
  48. *
  49. * PDO :: NOT TOTALLY SUPPORTED METHODS:
  50. * - getAttribute [ accepts only PDO_ATTR_SERVER_INFO, PDO_ATTR_SERVER_VERSION,
  51. * PDO_ATTR_CLIENT_VERSION and PDO_ATTR_PERSISTENT attributes ]
  52. * - setAttribute [ supports only PDO_ATTR_PERSISTENT modification ]
  53. * - lastInsertId [ only fo PostgreSQL , returns only pg_last_oid ]
  54. *
  55. * - - - - - - - - - - - - - - - - - - - -
  56. *
  57. * PDOStatement :: UNSUPPORTED METHODS:
  58. * - bindColumn [ is not possible to undeclare a variable and using global scope is not
  59. * really a good idea ]
  60. *
  61. * PDOStatement :: NOT TOTALLY SUPPORTED METHODS:
  62. * - getAttribute [ accepts only PDO_ATTR_SERVER_INFO, PDO_ATTR_SERVER_VERSION,
  63. * PDO_ATTR_CLIENT_VERSION and PDO_ATTR_PERSISTENT attributes ]
  64. * - setAttribute [ supports only PDO_ATTR_PERSISTENT modification ]
  65. * - setFetchMode [ supports only PDO_FETCH_NUM, PDO_FETCH_ASSOC, PDO_FETCH_OBJ and
  66. * PDO_FETCH_BOTH database reading mode ]
  67. * ---------------------------------------------
  68. * @Compatibility >= PHP 4
  69. * @Dependencies PDO_mysql.class.php
  70. * PDO_sqlite.class.php
  71. * PDOStatement_mysql.class.php
  72. * PDOStatement_sqlite.class.php
  73. * @Author Andrea Giammarchi
  74. * @Site http://www.devpro.it/
  75. * @Mail andrea [ at ] 3site [ dot ] it
  76. * @Date 2005/10/13
  77. * @LastModified 2005/12/01 21:40
  78. * @Version 0.1b - tested, supports only PostgreSQL, MySQL or SQLITE databases
  79. */
  80. class PDO {
  81. /** Modified on 2005/12/01 to support new PDO constants on PHP 5.1.X */
  82. const FETCH_ASSOC = PDO_FETCH_ASSOC;
  83. const FETCH_NUM = PDO_FETCH_NUM;
  84. const FETCH_BOTH = PDO_FETCH_BOTH;
  85. const FETCH_OBJ = PDO_FETCH_OBJ;
  86. const FETCH_LAZY = PDO_FETCH_LAZY;
  87. const FETCH_BOUND = PDO_FETCH_BOUND;
  88. const ATTR_SERVER_VERSION = PDO_ATTR_SERVER_VERSION;
  89. const ATTR_CLIENT_VERSION = PDO_ATTR_CLIENT_VERSION;
  90. const ATTR_SERVER_INFO = PDO_ATTR_SERVER_INFO;
  91. const ATTR_PERSISTENT = PDO_ATTR_PERSISTENT;
  92. /**
  93. * 'Private' variables:
  94. * __driver:PDO_* Dedicated PDO database class
  95. */
  96. var $__driver;
  97. /**
  98. * Public constructor
  99. * http://us2.php.net/manual/en/function.pdo-construct.php
  100. */
  101. function PDO($string_dsn, $string_username = '', $string_password = '', $array_driver_options = null) {
  102. $con = &$this->__getDNS($string_dsn);
  103. if($con['dbtype'] === 'mysql') {
  104. require_once('PDO_mysql.class.php');
  105. if(isset($con['port']))
  106. $con['host'] .= ':'.$con['port'];
  107. $this->__driver = new PDO_mysql(
  108. $con['host'],
  109. $con['dbname'],
  110. $string_username,
  111. $string_password
  112. );
  113. }
  114. elseif($con['dbtype'] === 'sqlite2' || $con['dbtype'] === 'sqlite') {
  115. require_once('PDO_sqlite.class.php');
  116. $this->__driver = new PDO_sqlite($con['dbname']);
  117. }
  118. elseif($con['dbtype'] === 'pgsql') {
  119. require_once('PDO_pgsql.class.php');
  120. $string_dsn = "host={$con['host']} dbname={$con['dbname']} user={$string_username} password={$string_password}";
  121. if(isset($con['port']))
  122. $string_dsn .= " port={$con['port']}";
  123. $this->__driver = new PDO_pgsql($string_dsn);
  124. }
  125. }
  126. /** UNSUPPORTED
  127. * Public method
  128. * http://us2.php.net/manual/en/function.pdo-begintransaction.php
  129. */
  130. function beginTransaction() {
  131. $this->__driver->beginTransaction();
  132. }
  133. /** NOT NATIVE BUT MAYBE USEFULL FOR PHP < 5.1 PDO DRIVER
  134. * Public method
  135. * Calls database_close function.
  136. * this->close( Void ):Boolean
  137. * @Return Boolean True on success, false otherwise
  138. */
  139. function close() {
  140. return $this->__driver->close();
  141. }
  142. /** UNSUPPORTED
  143. * Public method
  144. * http://us2.php.net/manual/en/function.pdo-commit.php
  145. */
  146. function commit() {
  147. $this->__driver->commit();
  148. }
  149. /**
  150. * Public method
  151. * http://us2.php.net/manual/en/function.pdo-exec.php
  152. */
  153. function exec($query) {
  154. return $this->__driver->exec($query);
  155. }
  156. /**
  157. * Public method
  158. * http://us2.php.net/manual/en/function.pdo-errorcode.php
  159. */
  160. function errorCode() {
  161. return $this->__driver->errorCode();
  162. }
  163. /**
  164. * Public method
  165. * http://us2.php.net/manual/en/function.pdo-errorinfo.php
  166. */
  167. function errorInfo() {
  168. return $this->__driver->errorInfo();
  169. }
  170. /** NOT TOTALLY UNSUPPORTED
  171. * Public method
  172. * http://us2.php.net/manual/en/function.pdo-getattribute.php
  173. */
  174. function getAttribute($attribute) {
  175. return $this->__driver->getAttribute($attribute);
  176. }
  177. /**
  178. * Public method
  179. * http://us2.php.net/manual/en/function.pdo-lastinsertid.php
  180. */
  181. function lastInsertId() {
  182. return $this->__driver->lastInsertId();
  183. }
  184. /**
  185. * Public method
  186. * http://us2.php.net/manual/en/function.pdo-prepare.php
  187. */
  188. function prepare($query, $array = Array()) {
  189. return $this->__driver->prepare($query, $array = Array());
  190. }
  191. /**
  192. * Public method
  193. * http://us2.php.net/manual/en/function.pdo-query.php
  194. */
  195. function query($query) {
  196. return $this->__driver->query($query);
  197. }
  198. /**
  199. * Public method
  200. * http://us2.php.net/manual/en/function.pdo-quote.php
  201. */
  202. function quote($string) {
  203. return $this->__driver->quote($string);
  204. }
  205. /** UNSUPPORTED
  206. * Public method
  207. * http://us2.php.net/manual/en/function.pdo-rollback.php
  208. */
  209. function rollBack() {
  210. $this->__driver->rollBack();
  211. }
  212. /** NOT TOTALLY UNSUPPORTED
  213. * Public method
  214. * http://us2.php.net/manual/en/function.pdo-setattribute.php
  215. */
  216. function setAttribute($attribute, $mixed) {
  217. return $this->__driver->setAttribute($attribute, $mixed);
  218. }
  219. // PRIVATE METHOD [uncommented]
  220. function __getDNS(&$string) {
  221. $result = array();
  222. $pos = strpos($string, ':');
  223. $parameters = explode(';', substr($string, ($pos + 1)));
  224. $result['dbtype'] = strtolower(substr($string, 0, $pos));
  225. for($a = 0, $b = count($parameters); $a < $b; $a++) {
  226. $tmp = explode('=', $parameters[$a]);
  227. if(count($tmp) == 2)
  228. $result[$tmp[0]] = $tmp[1];
  229. else
  230. $result['dbname'] = $parameters[$a];
  231. }
  232. return $result;
  233. }
  234. }
  235. }
  236. // If you have PHP 5.1 but want to test this class, declare PDO variables as _PDO variables
  237. else {
  238. /**
  239. * Class _PDO
  240. * (C) Andrea Giammarchi
  241. * Please read PDO class comments to know more
  242. */
  243. class _PDO {
  244. const FETCH_ASSOC = PDO::FETCH_ASSOC;
  245. const FETCH_NUM = PDO::FETCH_NUM;
  246. const FETCH_BOTH = PDO::FETCH_BOTH;
  247. const FETCH_OBJ = PDO::FETCH_OBJ;
  248. const FETCH_LAZY = PDO::FETCH_LAZY;
  249. const FETCH_BOUND = PDO::FETCH_BOUND;
  250. const ATTR_SERVER_VERSION = PDO::ATTR_SERVER_VERSION;
  251. const ATTR_CLIENT_VERSION = PDO::ATTR_CLIENT_VERSION;
  252. const ATTR_SERVER_INFO = PDO::ATTR_SERVER_INFO;
  253. const ATTR_PERSISTENT = PDO::ATTR_PERSISTENT;
  254. var $__driver;
  255. function _PDO($string_dsn, $string_username = '', $string_password = '', $array_driver_options = null) {
  256. $con = &$this->__getDNS($string_dsn);
  257. if($con['dbtype'] === 'mysql') {
  258. require_once('PDO_mysql.class.php');
  259. if(isset($con['port']))
  260. $con['host'] .= ':'.$con['port'];
  261. $this->__driver = new PDO_mysql(
  262. $con['host'],
  263. $con['dbname'],
  264. $string_username,
  265. $string_password
  266. );
  267. }
  268. elseif($con['dbtype'] === 'sqlite2' || $con['dbtype'] === 'sqlite') {
  269. require_once('PDO_sqlite.class.php');
  270. $this->__driver = new PDO_sqlite($con['dbname']);
  271. }
  272. elseif($con['dbtype'] === 'pgsql') {
  273. require_once('PDO_pgsql.class.php');
  274. $string_dsn = "host={$con['host']} dbname={$con['dbname']} user={$string_username} password={$string_password}";
  275. if(isset($con['port']))
  276. $string_dsn .= " port={$con['port']}";
  277. $this->__driver = new PDO_pgsql($string_dsn);
  278. }
  279. }
  280. function beginTransaction() {
  281. $this->__driver->beginTransaction();
  282. }
  283. function close() {
  284. return $this->__driver->close();
  285. }
  286. function commit() {
  287. $this->__driver->commit();
  288. }
  289. function exec($query) {
  290. return $this->__driver->exec($query);
  291. }
  292. function errorCode() {
  293. return $this->__driver->errorCode();
  294. }
  295. function errorInfo() {
  296. return $this->__driver->errorInfo();
  297. }
  298. function getAttribute($attribute) {
  299. return $this->__driver->getAttribute($attribute);
  300. }
  301. function lastInsertId() {
  302. return $this->__driver->lastInsertId();
  303. }
  304. function prepare($query, $array = Array()) {
  305. return $this->__driver->prepare($query, $array = Array());
  306. }
  307. function query($query) {
  308. return $this->__driver->query($query);
  309. }
  310. function quote($string) {
  311. return $this->__driver->quote($string);
  312. }
  313. function rollBack() {
  314. $this->__driver->rollBack();
  315. }
  316. function setAttribute($attribute, $mixed) {
  317. return $this->__driver->setAttribute($attribute, $mixed);
  318. }
  319. function __getDNS(&$string) {
  320. $result = array();
  321. $pos = strpos($string, ':');
  322. $parameters = explode(';', substr($string, ($pos + 1)));
  323. $result['dbtype'] = strtolower(substr($string, 0, $pos));
  324. for($a = 0, $b = count($parameters); $a < $b; $a++) {
  325. $tmp = explode('=', $parameters[$a]);
  326. if(count($tmp) == 2)
  327. $result[$tmp[0]] = $tmp[1];
  328. else
  329. $result['dbname'] = $parameters[$a];
  330. }
  331. return $result;
  332. }
  333. }
  334. }
  335. ?>