dbase.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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.9.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. public $phptype = 'dbase';
  51. /**
  52. * The database syntax variant to be used (db2, access, etc.), if any
  53. * @var string
  54. */
  55. public $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. public $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. public $errorcode_map = array(
  83. );
  84. /**
  85. * The raw database connection created by PHP
  86. * @var resource
  87. */
  88. public $connection;
  89. /**
  90. * The DSN information for connecting to a database
  91. * @var array
  92. */
  93. public $dsn = array();
  94. /**
  95. * A means of emulating result resources
  96. * @var array
  97. */
  98. public $res_row = array();
  99. /**
  100. * The quantity of results so far
  101. *
  102. * For emulating result resources.
  103. *
  104. * @var integer
  105. */
  106. public $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. public $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>parent::__construct()</kbd>
  127. *
  128. * @return void
  129. */
  130. public function __construct()
  131. {
  132. parent::__construct();
  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. public 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(
  202. DB_ERROR_CONNECT_FAILED,
  203. null,
  204. null,
  205. null,
  206. 'the dbase file does not exist and '
  207. . 'it could not be created because '
  208. . 'the "fields" element of the DSN '
  209. . 'is not properly set'
  210. );
  211. }
  212. $this->connection = @dbase_create(
  213. $dsn['database'],
  214. $dsn['fields']
  215. );
  216. if (!$this->connection) {
  217. return $this->raiseError(
  218. DB_ERROR_CONNECT_FAILED,
  219. null,
  220. null,
  221. null,
  222. 'the dbase file does not exist and '
  223. . 'the attempt to create it failed: '
  224. . $php_errormsg
  225. );
  226. }
  227. } else {
  228. if (!isset($this->dsn['mode'])) {
  229. $this->dsn['mode'] = 0;
  230. }
  231. $this->connection = @dbase_open(
  232. $dsn['database'],
  233. $this->dsn['mode']
  234. );
  235. if (!$this->connection) {
  236. return $this->raiseError(
  237. DB_ERROR_CONNECT_FAILED,
  238. null,
  239. null,
  240. null,
  241. $php_errormsg
  242. );
  243. }
  244. }
  245. return DB_OK;
  246. }
  247. // }}}
  248. // {{{ disconnect()
  249. /**
  250. * Disconnects from the database server
  251. *
  252. * @return bool TRUE on success, FALSE on failure
  253. */
  254. public function disconnect()
  255. {
  256. $ret = @dbase_close($this->connection);
  257. $this->connection = null;
  258. return $ret;
  259. }
  260. // }}}
  261. // {{{ &query()
  262. public function &query($query = null)
  263. {
  264. // emulate result resources
  265. $this->res_row[(int)$this->result] = 0;
  266. $tmp = new DB_result($this, $this->result++);
  267. return $tmp;
  268. }
  269. // }}}
  270. // {{{ fetchInto()
  271. /**
  272. * Places a row from the result set into the given array
  273. *
  274. * Formating of the array and the data therein are configurable.
  275. * See DB_result::fetchInto() for more information.
  276. *
  277. * This method is not meant to be called directly. Use
  278. * DB_result::fetchInto() instead. It can't be declared "protected"
  279. * because DB_result is a separate object.
  280. *
  281. * @param resource $result the query result resource
  282. * @param array $arr the referenced array to put the data in
  283. * @param int $fetchmode how the resulting array should be indexed
  284. * @param int $rownum the row number to fetch (0 = first row)
  285. *
  286. * @return mixed DB_OK on success, NULL when the end of a result set is
  287. * reached or on failure
  288. *
  289. * @see DB_result::fetchInto()
  290. */
  291. public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  292. {
  293. if ($rownum === null) {
  294. $rownum = $this->res_row[(int)$result]++;
  295. }
  296. if ($fetchmode & DB_FETCHMODE_ASSOC) {
  297. $arr = @dbase_get_record_with_names($this->connection, $rownum);
  298. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
  299. $arr = array_change_key_case($arr, CASE_LOWER);
  300. }
  301. } else {
  302. $arr = @dbase_get_record($this->connection, $rownum);
  303. }
  304. if (!$arr) {
  305. return null;
  306. }
  307. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  308. $this->_rtrimArrayValues($arr);
  309. }
  310. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  311. $this->_convertNullArrayValuesToEmpty($arr);
  312. }
  313. return DB_OK;
  314. }
  315. // }}}
  316. // {{{ freeResult()
  317. /**
  318. * Deletes the result set and frees the memory occupied by the result set.
  319. *
  320. * This method is a no-op for dbase, as there aren't result resources in
  321. * the same sense as most other database backends.
  322. *
  323. * @param resource $result PHP's query result resource
  324. *
  325. * @return bool TRUE on success, FALSE if $result is invalid
  326. *
  327. * @see DB_result::free()
  328. */
  329. public function freeResult($result)
  330. {
  331. return true;
  332. }
  333. // }}}
  334. // {{{ numCols()
  335. /**
  336. * Gets the number of columns in a result set
  337. *
  338. * This method is not meant to be called directly. Use
  339. * DB_result::numCols() instead. It can't be declared "protected"
  340. * because DB_result is a separate object.
  341. *
  342. * @param resource $result PHP's query result resource
  343. *
  344. * @return int the number of columns. A DB_Error object on failure.
  345. *
  346. * @see DB_result::numCols()
  347. */
  348. public function numCols($foo)
  349. {
  350. return @dbase_numfields($this->connection);
  351. }
  352. // }}}
  353. // {{{ numRows()
  354. /**
  355. * Gets the number of rows in a result set
  356. *
  357. * This method is not meant to be called directly. Use
  358. * DB_result::numRows() instead. It can't be declared "protected"
  359. * because DB_result is a separate object.
  360. *
  361. * @param resource $result PHP's query result resource
  362. *
  363. * @return int the number of rows. A DB_Error object on failure.
  364. *
  365. * @see DB_result::numRows()
  366. */
  367. public function numRows($foo)
  368. {
  369. return @dbase_numrecords($this->connection);
  370. }
  371. // }}}
  372. // {{{ quoteBoolean()
  373. /**
  374. * Formats a boolean value for use within a query in a locale-independent
  375. * manner.
  376. *
  377. * @param boolean the boolean value to be quoted.
  378. * @return string the quoted string.
  379. * @see DB_common::quoteSmart()
  380. * @since Method available since release 1.7.8.
  381. */
  382. public function quoteBoolean($boolean)
  383. {
  384. return $boolean ? 'T' : 'F';
  385. }
  386. // }}}
  387. // {{{ tableInfo()
  388. /**
  389. * Returns information about the current database
  390. *
  391. * @param mixed $result THIS IS UNUSED IN DBASE. The current database
  392. * is examined regardless of what is provided here.
  393. * @param int $mode a valid tableInfo mode
  394. *
  395. * @return array an associative array with the information requested.
  396. * A DB_Error object on failure.
  397. *
  398. * @see DB_common::tableInfo()
  399. * @since Method available since Release 1.7.0
  400. */
  401. public function tableInfo($result = null, $mode = null)
  402. {
  403. if (function_exists('dbase_get_header_info')) {
  404. $id = @dbase_get_header_info($this->connection);
  405. if (!$id && $php_errormsg) {
  406. return $this->raiseError(
  407. DB_ERROR,
  408. null,
  409. null,
  410. null,
  411. $php_errormsg
  412. );
  413. }
  414. } else {
  415. /*
  416. * This segment for PHP 4 is loosely based on code by
  417. * Hadi Rusiah <deegos@yahoo.com> in the comments on
  418. * the dBase reference page in the PHP manual.
  419. */
  420. $db = @fopen($this->dsn['database'], 'r');
  421. if (!$db) {
  422. return $this->raiseError(
  423. DB_ERROR_CONNECT_FAILED,
  424. null,
  425. null,
  426. null,
  427. $php_errormsg
  428. );
  429. }
  430. $id = array();
  431. $i = 0;
  432. $line = fread($db, 32);
  433. while (!feof($db)) {
  434. $line = fread($db, 32);
  435. if (substr($line, 0, 1) == chr(13)) {
  436. break;
  437. } else {
  438. $pos = strpos(substr($line, 0, 10), chr(0));
  439. $pos = ($pos == 0 ? 10 : $pos);
  440. $id[$i] = array(
  441. 'name' => substr($line, 0, $pos),
  442. 'type' => $this->types[substr($line, 11, 1)],
  443. 'length' => ord(substr($line, 16, 1)),
  444. 'precision' => ord(substr($line, 17, 1)),
  445. );
  446. }
  447. $i++;
  448. }
  449. fclose($db);
  450. }
  451. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  452. $case_func = 'strtolower';
  453. } else {
  454. $case_func = 'strval';
  455. }
  456. $res = array();
  457. $count = count($id);
  458. if ($mode) {
  459. $res['num_fields'] = $count;
  460. }
  461. for ($i = 0; $i < $count; $i++) {
  462. $res[$i] = array(
  463. 'table' => $this->dsn['database'],
  464. 'name' => $case_func($id[$i]['name']),
  465. 'type' => $id[$i]['type'],
  466. 'len' => $id[$i]['length'],
  467. 'flags' => ''
  468. );
  469. if ($mode & DB_TABLEINFO_ORDER) {
  470. $res['order'][$res[$i]['name']] = $i;
  471. }
  472. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  473. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  474. }
  475. }
  476. return $res;
  477. }
  478. // }}}
  479. }
  480. /*
  481. * Local variables:
  482. * tab-width: 4
  483. * c-basic-offset: 4
  484. * End:
  485. */