]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pear/DB/dbase.php
extra_empty_lines
[SourceForge/phpwiki.git] / lib / pear / DB / dbase.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2004 The PHP Group                                |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license,      |
9 // | that is bundled with this package in the file LICENSE, and is        |
10 // | available at through the world-wide-web at                           |
11 // | http://www.php.net/license/2_02.txt.                                 |
12 // | If you did not receive a copy of the PHP license and are unable to   |
13 // | obtain it through the world-wide-web, please send a note to          |
14 // | license@php.net so we can mail you a copy immediately.               |
15 // +----------------------------------------------------------------------+
16 // | Author: Tomas V.V.Cox <cox@idecnet.com>                              |
17 // | Maintainer: Daniel Convissor <danielc@php.net>                       |
18 // +----------------------------------------------------------------------+
19 //
20 // $Id$
21
22 // XXX legend:
23 //  You have to compile your PHP with the --enable-dbase option
24
25 require_once 'DB/common.php';
26
27 /**
28  * Database independent query interface definition for PHP's dbase
29  * extension.
30  *
31  * @package  DB
32  * @version  $Id$
33  * @category Database
34  * @author   Stig Bakken <ssb@php.net>
35  */
36 class DB_dbase extends DB_common
37 {
38     // {{{ properties
39
40     var $connection;
41     var $phptype, $dbsyntax;
42     var $prepare_tokens = array();
43     var $prepare_types = array();
44     var $res_row = array();
45     var $result = 0;
46
47     // }}}
48     // {{{ constructor
49
50     /**
51      * DB_mysql constructor.
52      *
53      * @access public
54      */
55     function DB_dbase()
56     {
57         $this->DB_common();
58         $this->phptype = 'dbase';
59         $this->dbsyntax = 'dbase';
60         $this->features = array(
61             'prepare'       => false,
62             'pconnect'      => false,
63             'transactions'  => false,
64             'limit'         => false
65         );
66         $this->errorcode_map = array();
67     }
68
69     // }}}
70     // {{{ connect()
71
72     function connect($dsninfo, $persistent = false)
73     {
74         if (!DB::assertExtension('dbase')) {
75             return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
76         }
77         $this->dsn = $dsninfo;
78         ob_start();
79         $conn  = @dbase_open($dsninfo['database'], 0);
80         $error = ob_get_contents();
81         ob_end_clean();
82         if (!$conn) {
83             return $this->raiseError(DB_ERROR_CONNECT_FAILED, null,
84                                      null, null, strip_tags($error));
85         }
86         $this->connection = $conn;
87         return DB_OK;
88     }
89
90     // }}}
91     // {{{ disconnect()
92
93     function disconnect()
94     {
95         $ret = @dbase_close($this->connection);
96         $this->connection = null;
97         return $ret;
98     }
99
100     // }}}
101     // {{{ &query()
102
103     function &query($query = null)
104     {
105         // emulate result resources
106         $this->res_row[$this->result] = 0;
107         $tmp =& new DB_result($this, $this->result++);
108         return $tmp;
109     }
110
111     // }}}
112     // {{{ fetchInto()
113
114     /**
115      * Fetch a row and insert the data into an existing array.
116      *
117      * Formating of the array and the data therein are configurable.
118      * See DB_result::fetchInto() for more information.
119      *
120      * @param resource $result query result identifier
121      * @param array    $arr    (reference) array where data from the row
122      *                            should be placed
123      * @param int $fetchmode how the resulting array should be indexed
124      * @param int $rownum    the row number to fetch
125      *
126      * @return mixed DB_OK on success, null when end of result set is
127      *               reached or on failure
128      *
129      * @see DB_result::fetchInto()
130      * @access private
131      */
132     function fetchInto($result, &$arr, $fetchmode, $rownum=null)
133     {
134         if ($rownum === null) {
135             $rownum = $this->res_row[$result]++;
136         }
137         if ($fetchmode & DB_FETCHMODE_ASSOC) {
138             $arr = @dbase_get_record_with_names($this->connection, $rownum);
139             if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
140                 $arr = array_change_key_case($arr, CASE_LOWER);
141             }
142         } else {
143             $arr = @dbase_get_record($this->connection, $rownum);
144         }
145         if (!$arr) {
146             return null;
147         }
148         if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
149             $this->_rtrimArrayValues($arr);
150         }
151         if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
152             $this->_convertNullArrayValuesToEmpty($arr);
153         }
154         return DB_OK;
155     }
156
157     // }}}
158     // {{{ numCols()
159
160     function numCols($foo)
161     {
162         return @dbase_numfields($this->connection);
163     }
164
165     // }}}
166     // {{{ numRows()
167
168     function numRows($foo)
169     {
170         return @dbase_numrecords($this->connection);
171     }
172
173     // }}}
174     // {{{ quoteSmart()
175
176     /**
177      * Format input so it can be safely used in a query
178      *
179      * @param mixed $in data to be quoted
180      *
181      * @return mixed Submitted variable's type = returned value:
182      *               + null = the string <samp>NULL</samp>
183      *               + boolean = <samp>T</samp> if true or
184      *                 <samp>F</samp> if false.  Use the <kbd>Logical</kbd>
185      *                 data type.
186      *               + integer or double = the unquoted number
187      *               + other (including strings and numeric strings) =
188      *                 the data with single quotes escaped by preceeding
189      *                 single quotes then the whole string is encapsulated
190      *                 between single quotes
191      *
192      * @internal
193      */
194     function quoteSmart($in)
195     {
196         if (is_int($in) || is_double($in)) {
197             return $in;
198         } elseif (is_bool($in)) {
199             return $in ? 'T' : 'F';
200         } elseif (is_null($in)) {
201             return 'NULL';
202         } else {
203             return "'" . $this->escapeSimple($in) . "'";
204         }
205     }
206
207     // }}}
208
209 }
210
211 /*
212  * Local variables:
213  * tab-width: 4
214  * c-basic-offset: 4
215  * End:
216  */
217
218 ?>