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