dbase.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. require_once 'common.php';
  29. /**
  30. * The methods PEAR DB uses to interact with PHP's dbase extension
  31. * for interacting with dBase databases
  32. *
  33. * These methods overload the ones declared in DB_common.
  34. *
  35. * @category Database
  36. * @package DB
  37. * @author Tomas V.V. Cox <cox@idecnet.com>
  38. * @author Daniel Convissor <danielc@php.net>
  39. * @copyright 1997-2007 The PHP Group
  40. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  41. * @version Release: 1.9.2
  42. * @link http://pear.php.net/package/DB
  43. */
  44. class DB_dbase extends DB_common
  45. {
  46. // {{{ properties
  47. /**
  48. * The DB driver type (mysql, oci8, odbc, etc.)
  49. * @var string
  50. */
  51. public $phptype = 'dbase';
  52. /**
  53. * The database syntax variant to be used (db2, access, etc.), if any
  54. * @var string
  55. */
  56. public $dbsyntax = 'dbase';
  57. /**
  58. * The capabilities of this DB implementation
  59. *
  60. * The 'new_link' element contains the PHP version that first provided
  61. * new_link support for this DBMS. Contains false if it's unsupported.
  62. *
  63. * Meaning of the 'limit' element:
  64. * + 'emulate' = emulate with fetch row by number
  65. * + 'alter' = alter the query
  66. * + false = skip rows
  67. *
  68. * @var array
  69. */
  70. public $features = array(
  71. 'limit' => false,
  72. 'new_link' => false,
  73. 'numrows' => true,
  74. 'pconnect' => false,
  75. 'prepare' => false,
  76. 'ssl' => false,
  77. 'transactions' => false,
  78. );
  79. /**
  80. * A mapping of native error codes to DB error codes
  81. * @var array
  82. */
  83. public $errorcode_map = array();
  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 ((new 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|object
  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 $foo
  343. * @return int the number of columns. A DB_Error object on failure.
  344. *
  345. * @see DB_result::numCols()
  346. */
  347. public function numCols($foo)
  348. {
  349. return @dbase_numfields($this->connection);
  350. }
  351. // }}}
  352. // {{{ numRows()
  353. /**
  354. * Gets the number of rows in a result set
  355. *
  356. * This method is not meant to be called directly. Use
  357. * DB_result::numRows() instead. It can't be declared "protected"
  358. * because DB_result is a separate object.
  359. *
  360. * @param $foo
  361. * @return int the number of rows. A DB_Error object on failure.
  362. *
  363. * @see DB_result::numRows()
  364. */
  365. public function numRows($foo)
  366. {
  367. return @dbase_numrecords($this->connection);
  368. }
  369. // }}}
  370. // {{{ quoteBoolean()
  371. /**
  372. * Formats a boolean value for use within a query in a locale-independent
  373. * manner.
  374. *
  375. * @param boolean the boolean value to be quoted.
  376. * @return string the quoted string.
  377. * @see DB_common::quoteSmart()
  378. * @since Method available since release 1.7.8.
  379. */
  380. public function quoteBoolean($boolean)
  381. {
  382. return $boolean ? 'T' : 'F';
  383. }
  384. // }}}
  385. // {{{ tableInfo()
  386. /**
  387. * Returns information about the current database
  388. *
  389. * @param mixed $result THIS IS UNUSED IN DBASE. The current database
  390. * is examined regardless of what is provided here.
  391. * @param int $mode a valid tableInfo mode
  392. *
  393. * @return array|object
  394. * A DB_Error object on failure.
  395. *
  396. * @see DB_common::tableInfo()
  397. * @since Method available since Release 1.7.0
  398. */
  399. public function tableInfo($result = null, $mode = null)
  400. {
  401. if (function_exists('dbase_get_header_info')) {
  402. $id = @dbase_get_header_info($this->connection);
  403. if (!$id && $php_errormsg) {
  404. return $this->raiseError(
  405. DB_ERROR,
  406. null,
  407. null,
  408. null,
  409. $php_errormsg
  410. );
  411. }
  412. } else {
  413. /*
  414. * This segment for PHP 4 is loosely based on code by
  415. * Hadi Rusiah <deegos@yahoo.com> in the comments on
  416. * the dBase reference page in the PHP manual.
  417. */
  418. $db = @fopen($this->dsn['database'], 'r');
  419. if (!$db) {
  420. return $this->raiseError(
  421. DB_ERROR_CONNECT_FAILED,
  422. null,
  423. null,
  424. null,
  425. $php_errormsg
  426. );
  427. }
  428. $id = array();
  429. $i = 0;
  430. $line = fread($db, 32);
  431. while (!feof($db)) {
  432. $line = fread($db, 32);
  433. if (substr($line, 0, 1) == chr(13)) {
  434. break;
  435. } else {
  436. $pos = strpos(substr($line, 0, 10), chr(0));
  437. $pos = ($pos == 0 ? 10 : $pos);
  438. $id[$i] = array(
  439. 'name' => substr($line, 0, $pos),
  440. 'type' => $this->types[substr($line, 11, 1)],
  441. 'length' => ord(substr($line, 16, 1)),
  442. 'precision' => ord(substr($line, 17, 1)),
  443. );
  444. }
  445. $i++;
  446. }
  447. fclose($db);
  448. }
  449. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  450. $case_func = 'strtolower';
  451. } else {
  452. $case_func = 'strval';
  453. }
  454. $res = array();
  455. $count = count($id);
  456. if ($mode) {
  457. $res['num_fields'] = $count;
  458. }
  459. for ($i = 0; $i < $count; $i++) {
  460. $res[$i] = array(
  461. 'table' => $this->dsn['database'],
  462. 'name' => $case_func($id[$i]['name']),
  463. 'type' => $id[$i]['type'],
  464. 'len' => $id[$i]['length'],
  465. 'flags' => ''
  466. );
  467. if ($mode & DB_TABLEINFO_ORDER) {
  468. $res['order'][$res[$i]['name']] = $i;
  469. }
  470. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  471. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  472. }
  473. }
  474. return $res;
  475. }
  476. // }}}
  477. }
  478. /*
  479. * Local variables:
  480. * tab-width: 4
  481. * c-basic-offset: 4
  482. * End:
  483. */