dbase.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * The PEAR DB driver for PHP's dbase extension
  5. * for interacting with dBase databases
  6. *
  7. * PHP version 5
  8. *
  9. * LICENSE: This source file is subject to version 3.0 of the PHP license
  10. * that is available through the world-wide-web at the following URI:
  11. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  12. * the PHP License and are unable to obtain it through the web, please
  13. * send a note to license@php.net so we can mail you a copy immediately.
  14. *
  15. * @category Database
  16. * @package DB
  17. * @author Tomas V.V. Cox <cox@idecnet.com>
  18. * @author Daniel Convissor <danielc@php.net>
  19. * @copyright 1997-2007 The PHP Group
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version CVS: $Id$
  22. * @link http://pear.php.net/package/DB
  23. */
  24. /**
  25. * Obtain the DB_common class so it can be extended from
  26. */
  27. require_once 'DB/common.php';
  28. /**
  29. * The methods PEAR DB uses to interact with PHP's dbase extension
  30. * for interacting with dBase databases
  31. *
  32. * These methods overload the ones declared in DB_common.
  33. *
  34. * @category Database
  35. * @package DB
  36. * @author Tomas V.V. Cox <cox@idecnet.com>
  37. * @author Daniel Convissor <danielc@php.net>
  38. * @copyright 1997-2007 The PHP Group
  39. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  40. * @version Release: 1.8.2
  41. * @link http://pear.php.net/package/DB
  42. */
  43. class DB_dbase extends DB_common
  44. {
  45. // {{{ properties
  46. /**
  47. * The DB driver type (mysql, oci8, odbc, etc.)
  48. * @var string
  49. */
  50. var $phptype = 'dbase';
  51. /**
  52. * The database syntax variant to be used (db2, access, etc.), if any
  53. * @var string
  54. */
  55. var $dbsyntax = 'dbase';
  56. /**
  57. * The capabilities of this DB implementation
  58. *
  59. * The 'new_link' element contains the PHP version that first provided
  60. * new_link support for this DBMS. Contains false if it's unsupported.
  61. *
  62. * Meaning of the 'limit' element:
  63. * + 'emulate' = emulate with fetch row by number
  64. * + 'alter' = alter the query
  65. * + false = skip rows
  66. *
  67. * @var array
  68. */
  69. var $features = array(
  70. 'limit' => false,
  71. 'new_link' => false,
  72. 'numrows' => true,
  73. 'pconnect' => false,
  74. 'prepare' => false,
  75. 'ssl' => false,
  76. 'transactions' => false,
  77. );
  78. /**
  79. * A mapping of native error codes to DB error codes
  80. * @var array
  81. */
  82. var $errorcode_map = array(
  83. );
  84. /**
  85. * The raw database connection created by PHP
  86. * @var resource
  87. */
  88. var $connection;
  89. /**
  90. * The DSN information for connecting to a database
  91. * @var array
  92. */
  93. var $dsn = array();
  94. /**
  95. * A means of emulating result resources
  96. * @var array
  97. */
  98. var $res_row = array();
  99. /**
  100. * The quantity of results so far
  101. *
  102. * For emulating result resources.
  103. *
  104. * @var integer
  105. */
  106. var $result = 0;
  107. /**
  108. * Maps dbase data type id's to human readable strings
  109. *
  110. * The human readable values are based on the output of PHP's
  111. * dbase_get_header_info() function.
  112. *
  113. * @var array
  114. * @since Property available since Release 1.7.0
  115. */
  116. var $types = array(
  117. 'C' => 'character',
  118. 'D' => 'date',
  119. 'L' => 'boolean',
  120. 'M' => 'memo',
  121. 'N' => 'number',
  122. );
  123. // }}}
  124. // {{{ constructor
  125. /**
  126. * This constructor calls <kbd>$this->DB_common()</kbd>
  127. *
  128. * @return void
  129. */
  130. function DB_dbase()
  131. {
  132. $this->DB_common();
  133. }
  134. // }}}
  135. // {{{ connect()
  136. /**
  137. * Connect to the database and create it if it doesn't exist
  138. *
  139. * Don't call this method directly. Use DB::connect() instead.
  140. *
  141. * PEAR DB's dbase driver supports the following extra DSN options:
  142. * + mode An integer specifying the read/write mode to use
  143. * (0 = read only, 1 = write only, 2 = read/write).
  144. * Available since PEAR DB 1.7.0.
  145. * + fields An array of arrays that PHP's dbase_create() function needs
  146. * to create a new database. This information is used if the
  147. * dBase file specified in the "database" segment of the DSN
  148. * does not exist. For more info, see the PHP manual's
  149. * {@link http://php.net/dbase_create dbase_create()} page.
  150. * Available since PEAR DB 1.7.0.
  151. *
  152. * Example of how to connect and establish a new dBase file if necessary:
  153. * <code>
  154. * require_once 'DB.php';
  155. *
  156. * $dsn = array(
  157. * 'phptype' => 'dbase',
  158. * 'database' => '/path/and/name/of/dbase/file',
  159. * 'mode' => 2,
  160. * 'fields' => array(
  161. * array('a', 'N', 5, 0),
  162. * array('b', 'C', 40),
  163. * array('c', 'C', 255),
  164. * array('d', 'C', 20),
  165. * ),
  166. * );
  167. * $options = array(
  168. * 'debug' => 2,
  169. * 'portability' => DB_PORTABILITY_ALL,
  170. * );
  171. *
  172. * $db = DB::connect($dsn, $options);
  173. * if (PEAR::isError($db)) {
  174. * die($db->getMessage());
  175. * }
  176. * </code>
  177. *
  178. * @param array $dsn the data source name
  179. * @param bool $persistent should the connection be persistent?
  180. *
  181. * @return int DB_OK on success. A DB_Error object on failure.
  182. */
  183. function connect($dsn, $persistent = false)
  184. {
  185. if (!PEAR::loadExtension('dbase')) {
  186. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  187. }
  188. $this->dsn = $dsn;
  189. if ($dsn['dbsyntax']) {
  190. $this->dbsyntax = $dsn['dbsyntax'];
  191. }
  192. /*
  193. * Turn track_errors on for entire script since $php_errormsg
  194. * is the only way to find errors from the dbase extension.
  195. */
  196. @ini_set('track_errors', 1);
  197. $php_errormsg = '';
  198. if (!file_exists($dsn['database'])) {
  199. $this->dsn['mode'] = 2;
  200. if (empty($dsn['fields']) || !is_array($dsn['fields'])) {
  201. return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  202. null, null, null,
  203. 'the dbase file does not exist and '
  204. . 'it could not be created because '
  205. . 'the "fields" element of the DSN '
  206. . 'is not properly set');
  207. }
  208. $this->connection = @dbase_create($dsn['database'],
  209. $dsn['fields']);
  210. if (!$this->connection) {
  211. return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  212. null, null, null,
  213. 'the dbase file does not exist and '
  214. . 'the attempt to create it failed: '
  215. . $php_errormsg);
  216. }
  217. } else {
  218. if (!isset($this->dsn['mode'])) {
  219. $this->dsn['mode'] = 0;
  220. }
  221. $this->connection = @dbase_open($dsn['database'],
  222. $this->dsn['mode']);
  223. if (!$this->connection) {
  224. return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  225. null, null, null,
  226. $php_errormsg);
  227. }
  228. }
  229. return DB_OK;
  230. }
  231. // }}}
  232. // {{{ disconnect()
  233. /**
  234. * Disconnects from the database server
  235. *
  236. * @return bool TRUE on success, FALSE on failure
  237. */
  238. function disconnect()
  239. {
  240. $ret = @dbase_close($this->connection);
  241. $this->connection = null;
  242. return $ret;
  243. }
  244. // }}}
  245. // {{{ &query()
  246. function &query($query = null)
  247. {
  248. // emulate result resources
  249. $this->res_row[(int)$this->result] = 0;
  250. $tmp = new DB_result($this, $this->result++);
  251. return $tmp;
  252. }
  253. // }}}
  254. // {{{ fetchInto()
  255. /**
  256. * Places a row from the result set into the given array
  257. *
  258. * Formating of the array and the data therein are configurable.
  259. * See DB_result::fetchInto() for more information.
  260. *
  261. * This method is not meant to be called directly. Use
  262. * DB_result::fetchInto() instead. It can't be declared "protected"
  263. * because DB_result is a separate object.
  264. *
  265. * @param resource $result the query result resource
  266. * @param array $arr the referenced array to put the data in
  267. * @param int $fetchmode how the resulting array should be indexed
  268. * @param int $rownum the row number to fetch (0 = first row)
  269. *
  270. * @return mixed DB_OK on success, NULL when the end of a result set is
  271. * reached or on failure
  272. *
  273. * @see DB_result::fetchInto()
  274. */
  275. function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  276. {
  277. if ($rownum === null) {
  278. $rownum = $this->res_row[(int)$result]++;
  279. }
  280. if ($fetchmode & DB_FETCHMODE_ASSOC) {
  281. $arr = @dbase_get_record_with_names($this->connection, $rownum);
  282. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
  283. $arr = array_change_key_case($arr, CASE_LOWER);
  284. }
  285. } else {
  286. $arr = @dbase_get_record($this->connection, $rownum);
  287. }
  288. if (!$arr) {
  289. return null;
  290. }
  291. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  292. $this->_rtrimArrayValues($arr);
  293. }
  294. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  295. $this->_convertNullArrayValuesToEmpty($arr);
  296. }
  297. return DB_OK;
  298. }
  299. // }}}
  300. // {{{ freeResult()
  301. /**
  302. * Deletes the result set and frees the memory occupied by the result set.
  303. *
  304. * This method is a no-op for dbase, as there aren't result resources in
  305. * the same sense as most other database backends.
  306. *
  307. * @param resource $result PHP's query result resource
  308. *
  309. * @return bool TRUE on success, FALSE if $result is invalid
  310. *
  311. * @see DB_result::free()
  312. */
  313. function freeResult($result)
  314. {
  315. return true;
  316. }
  317. // }}}
  318. // {{{ numCols()
  319. /**
  320. * Gets the number of columns in a result set
  321. *
  322. * This method is not meant to be called directly. Use
  323. * DB_result::numCols() instead. It can't be declared "protected"
  324. * because DB_result is a separate object.
  325. *
  326. * @param resource $result PHP's query result resource
  327. *
  328. * @return int the number of columns. A DB_Error object on failure.
  329. *
  330. * @see DB_result::numCols()
  331. */
  332. function numCols($foo)
  333. {
  334. return @dbase_numfields($this->connection);
  335. }
  336. // }}}
  337. // {{{ numRows()
  338. /**
  339. * Gets the number of rows in a result set
  340. *
  341. * This method is not meant to be called directly. Use
  342. * DB_result::numRows() instead. It can't be declared "protected"
  343. * because DB_result is a separate object.
  344. *
  345. * @param resource $result PHP's query result resource
  346. *
  347. * @return int the number of rows. A DB_Error object on failure.
  348. *
  349. * @see DB_result::numRows()
  350. */
  351. function numRows($foo)
  352. {
  353. return @dbase_numrecords($this->connection);
  354. }
  355. // }}}
  356. // {{{ quoteBoolean()
  357. /**
  358. * Formats a boolean value for use within a query in a locale-independent
  359. * manner.
  360. *
  361. * @param boolean the boolean value to be quoted.
  362. * @return string the quoted string.
  363. * @see DB_common::quoteSmart()
  364. * @since Method available since release 1.7.8.
  365. */
  366. function quoteBoolean($boolean) {
  367. return $boolean ? 'T' : 'F';
  368. }
  369. // }}}
  370. // {{{ tableInfo()
  371. /**
  372. * Returns information about the current database
  373. *
  374. * @param mixed $result THIS IS UNUSED IN DBASE. The current database
  375. * is examined regardless of what is provided here.
  376. * @param int $mode a valid tableInfo mode
  377. *
  378. * @return array an associative array with the information requested.
  379. * A DB_Error object on failure.
  380. *
  381. * @see DB_common::tableInfo()
  382. * @since Method available since Release 1.7.0
  383. */
  384. function tableInfo($result = null, $mode = null)
  385. {
  386. if (function_exists('dbase_get_header_info')) {
  387. $id = @dbase_get_header_info($this->connection);
  388. if (!$id && $php_errormsg) {
  389. return $this->raiseError(DB_ERROR,
  390. null, null, null,
  391. $php_errormsg);
  392. }
  393. } else {
  394. /*
  395. * This segment for PHP 4 is loosely based on code by
  396. * Hadi Rusiah <deegos@yahoo.com> in the comments on
  397. * the dBase reference page in the PHP manual.
  398. */
  399. $db = @fopen($this->dsn['database'], 'r');
  400. if (!$db) {
  401. return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  402. null, null, null,
  403. $php_errormsg);
  404. }
  405. $id = array();
  406. $i = 0;
  407. $line = fread($db, 32);
  408. while (!feof($db)) {
  409. $line = fread($db, 32);
  410. if (substr($line, 0, 1) == chr(13)) {
  411. break;
  412. } else {
  413. $pos = strpos(substr($line, 0, 10), chr(0));
  414. $pos = ($pos == 0 ? 10 : $pos);
  415. $id[$i] = array(
  416. 'name' => substr($line, 0, $pos),
  417. 'type' => $this->types[substr($line, 11, 1)],
  418. 'length' => ord(substr($line, 16, 1)),
  419. 'precision' => ord(substr($line, 17, 1)),
  420. );
  421. }
  422. $i++;
  423. }
  424. fclose($db);
  425. }
  426. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  427. $case_func = 'strtolower';
  428. } else {
  429. $case_func = 'strval';
  430. }
  431. $res = array();
  432. $count = count($id);
  433. if ($mode) {
  434. $res['num_fields'] = $count;
  435. }
  436. for ($i = 0; $i < $count; $i++) {
  437. $res[$i] = array(
  438. 'table' => $this->dsn['database'],
  439. 'name' => $case_func($id[$i]['name']),
  440. 'type' => $id[$i]['type'],
  441. 'len' => $id[$i]['length'],
  442. 'flags' => ''
  443. );
  444. if ($mode & DB_TABLEINFO_ORDER) {
  445. $res['order'][$res[$i]['name']] = $i;
  446. }
  447. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  448. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  449. }
  450. }
  451. return $res;
  452. }
  453. // }}}
  454. }
  455. /*
  456. * Local variables:
  457. * tab-width: 4
  458. * c-basic-offset: 4
  459. * End:
  460. */
  461. ?>