123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- <?php
- require_once 'common.php';
- class DB_dbase extends DB_common
- {
-
-
- public $phptype = 'dbase';
-
- public $dbsyntax = 'dbase';
-
- public $features = array(
- 'limit' => false,
- 'new_link' => false,
- 'numrows' => true,
- 'pconnect' => false,
- 'prepare' => false,
- 'ssl' => false,
- 'transactions' => false,
- );
-
- public $errorcode_map = array();
-
- public $connection;
-
- public $dsn = array();
-
- public $res_row = array();
-
- public $result = 0;
-
- public $types = array(
- 'C' => 'character',
- 'D' => 'date',
- 'L' => 'boolean',
- 'M' => 'memo',
- 'N' => 'number',
- );
-
-
-
- public function __construct()
- {
- parent::__construct();
- }
-
-
-
- public function connect($dsn, $persistent = false)
- {
- if (!PEAR::loadExtension('dbase')) {
- return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
- }
- $this->dsn = $dsn;
- if ($dsn['dbsyntax']) {
- $this->dbsyntax = $dsn['dbsyntax'];
- }
-
- @ini_set('track_errors', 1);
- $php_errormsg = '';
- if (!file_exists($dsn['database'])) {
- $this->dsn['mode'] = 2;
- if (empty($dsn['fields']) || !is_array($dsn['fields'])) {
- return $this->raiseError(
- DB_ERROR_CONNECT_FAILED,
- null,
- null,
- null,
- 'the dbase file does not exist and '
- . 'it could not be created because '
- . 'the "fields" element of the DSN '
- . 'is not properly set'
- );
- }
- $this->connection = @dbase_create(
- $dsn['database'],
- $dsn['fields']
- );
- if (!$this->connection) {
- return $this->raiseError(
- DB_ERROR_CONNECT_FAILED,
- null,
- null,
- null,
- 'the dbase file does not exist and '
- . 'the attempt to create it failed: '
- . $php_errormsg
- );
- }
- } else {
- if (!isset($this->dsn['mode'])) {
- $this->dsn['mode'] = 0;
- }
- $this->connection = @dbase_open(
- $dsn['database'],
- $this->dsn['mode']
- );
- if (!$this->connection) {
- return $this->raiseError(
- DB_ERROR_CONNECT_FAILED,
- null,
- null,
- null,
- $php_errormsg
- );
- }
- }
- return DB_OK;
- }
-
-
-
- public function disconnect()
- {
- $ret = @dbase_close($this->connection);
- $this->connection = null;
- return $ret;
- }
-
-
- public function &query($query = null)
- {
-
- $this->res_row[(int)$this->result] = 0;
- $tmp = new DB_result($this, $this->result++);
- return $tmp;
- }
-
-
-
- public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
- {
- if ($rownum === null) {
- $rownum = $this->res_row[(int)$result]++;
- }
- if ($fetchmode & DB_FETCHMODE_ASSOC) {
- $arr = @dbase_get_record_with_names($this->connection, $rownum);
- if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
- $arr = array_change_key_case($arr, CASE_LOWER);
- }
- } else {
- $arr = @dbase_get_record($this->connection, $rownum);
- }
- if (!$arr) {
- return null;
- }
- if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
- $this->_rtrimArrayValues($arr);
- }
- if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
- $this->_convertNullArrayValuesToEmpty($arr);
- }
- return DB_OK;
- }
-
-
-
- public function freeResult($result)
- {
- return true;
- }
-
-
-
- public function numCols($foo)
- {
- return @dbase_numfields($this->connection);
- }
-
-
-
- public function numRows($foo)
- {
- return @dbase_numrecords($this->connection);
- }
-
-
-
- public function quoteBoolean($boolean)
- {
- return $boolean ? 'T' : 'F';
- }
-
-
-
- public function tableInfo($result = null, $mode = null)
- {
- if (function_exists('dbase_get_header_info')) {
- $id = @dbase_get_header_info($this->connection);
- if (!$id && $php_errormsg) {
- return $this->raiseError(
- DB_ERROR,
- null,
- null,
- null,
- $php_errormsg
- );
- }
- } else {
-
- $db = @fopen($this->dsn['database'], 'r');
- if (!$db) {
- return $this->raiseError(
- DB_ERROR_CONNECT_FAILED,
- null,
- null,
- null,
- $php_errormsg
- );
- }
- $id = array();
- $i = 0;
- $line = fread($db, 32);
- while (!feof($db)) {
- $line = fread($db, 32);
- if (substr($line, 0, 1) == chr(13)) {
- break;
- } else {
- $pos = strpos(substr($line, 0, 10), chr(0));
- $pos = ($pos == 0 ? 10 : $pos);
- $id[$i] = array(
- 'name' => substr($line, 0, $pos),
- 'type' => $this->types[substr($line, 11, 1)],
- 'length' => ord(substr($line, 16, 1)),
- 'precision' => ord(substr($line, 17, 1)),
- );
- }
- $i++;
- }
- fclose($db);
- }
- if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
- $case_func = 'strtolower';
- } else {
- $case_func = 'strval';
- }
- $res = array();
- $count = count($id);
- if ($mode) {
- $res['num_fields'] = $count;
- }
- for ($i = 0; $i < $count; $i++) {
- $res[$i] = array(
- 'table' => $this->dsn['database'],
- 'name' => $case_func($id[$i]['name']),
- 'type' => $id[$i]['type'],
- 'len' => $id[$i]['length'],
- 'flags' => ''
- );
- if ($mode & DB_TABLEINFO_ORDER) {
- $res['order'][$res[$i]['name']] = $i;
- }
- if ($mode & DB_TABLEINFO_ORDERTABLE) {
- $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
- }
- }
- return $res;
- }
-
- }
|