]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pear/DB/msql.php
Activated Id substitution for Subversion
[SourceForge/phpwiki.git] / lib / pear / DB / msql.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: Sterling Hughes <sterling@php.net>                           |
17 // | Maintainer: Daniel Convissor <danielc@php.net>                       |
18 // +----------------------------------------------------------------------+
19 //
20 // $Id$
21
22 require_once 'DB/common.php';
23
24 /**
25  * Database independent query interface definition for PHP's Mini-SQL
26  * extension.
27  *
28  * @package  DB
29  * @version  $Id$
30  * @category Database
31  * @author   Sterling Hughes <sterling@php.net>
32  */
33 class DB_msql extends DB_common
34 {
35     // {{{ properties
36
37     var $connection;
38     var $phptype, $dbsyntax;
39     var $prepare_tokens = array();
40     var $prepare_types = array();
41
42     // }}}
43     // {{{ constructor
44
45     function DB_msql()
46     {
47         $this->DB_common();
48         $this->phptype = 'msql';
49         $this->dbsyntax = 'msql';
50         $this->features = array(
51             'prepare' => false,
52             'pconnect' => true,
53             'transactions' => false,
54             'limit' => 'emulate'
55         );
56     }
57
58     // }}}
59     // {{{ connect()
60
61     function connect($dsninfo, $persistent = false)
62     {
63         if (!DB::assertExtension('msql')) {
64             return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
65         }
66
67         $this->dsn = $dsninfo;
68         $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
69
70         $connect_function = $persistent ? 'msql_pconnect' : 'msql_connect';
71
72         if ($dbhost && $dsninfo['username'] && $dsninfo['password']) {
73             $conn = $connect_function($dbhost, $dsninfo['username'],
74                                       $dsninfo['password']);
75         } elseif ($dbhost && $dsninfo['username']) {
76             $conn = $connect_function($dbhost, $dsninfo['username']);
77         } else {
78             $conn = $connect_function($dbhost);
79         }
80         if (!$conn) {
81             $this->raiseError(DB_ERROR_CONNECT_FAILED);
82         }
83         if (!@msql_select_db($dsninfo['database'], $conn)){
84             return $this->raiseError(DB_ERROR_NODBSELECTED);
85         }
86         $this->connection = $conn;
87         return DB_OK;
88     }
89
90     // }}}
91     // {{{ disconnect()
92
93     function disconnect()
94     {
95         $ret = @msql_close($this->connection);
96         $this->connection = null;
97         return $ret;
98     }
99
100     // }}}
101     // {{{ simpleQuery()
102
103     function simpleQuery($query)
104     {
105         $this->last_query = $query;
106         $query = $this->modifyQuery($query);
107         $result = @msql_query($query, $this->connection);
108         if (!$result) {
109             return $this->raiseError();
110         }
111         // Determine which queries that should return data, and which
112         // should return an error code only.
113         return DB::isManip($query) ? DB_OK : $result;
114     }
115
116
117     // }}}
118     // {{{ nextResult()
119
120     /**
121      * Move the internal msql result pointer to the next available result
122      *
123      * @param a valid fbsql result resource
124      *
125      * @access public
126      *
127      * @return true if a result is available otherwise return false
128      */
129     function nextResult($result)
130     {
131         return false;
132     }
133
134     // }}}
135     // {{{ fetchInto()
136
137     /**
138      * Fetch a row and insert the data into an existing array.
139      *
140      * Formating of the array and the data therein are configurable.
141      * See DB_result::fetchInto() for more information.
142      *
143      * @param resource $result    query result identifier
144      * @param array    $arr       (reference) array where data from the row
145      *                            should be placed
146      * @param int      $fetchmode how the resulting array should be indexed
147      * @param int      $rownum    the row number to fetch
148      *
149      * @return mixed DB_OK on success, null when end of result set is
150      *               reached or on failure
151      *
152      * @see DB_result::fetchInto()
153      * @access private
154      */
155     function fetchInto($result, &$arr, $fetchmode, $rownum=null)
156     {
157         if ($rownum !== null) {
158             if (!@msql_data_seek($result, $rownum)) {
159                 return null;
160             }
161         }
162         if ($fetchmode & DB_FETCHMODE_ASSOC) {
163             $arr = @msql_fetch_array($result, MSQL_ASSOC);
164             if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
165                 $arr = array_change_key_case($arr, CASE_LOWER);
166             }
167         } else {
168             $arr = @msql_fetch_row($result);
169         }
170         if (!$arr) {
171             if ($error = @msql_error()) {
172                 return $this->raiseError($error);
173             } else {
174                 return null;
175             }
176         }
177         if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
178             $this->_rtrimArrayValues($arr);
179         }
180         if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
181             $this->_convertNullArrayValuesToEmpty($arr);
182         }
183         return DB_OK;
184     }
185
186     // }}}
187     // {{{ freeResult()
188
189     function freeResult($result)
190     {
191         return @msql_free_result($result);
192     }
193
194     // }}}
195     // {{{ numCols()
196
197     function numCols($result)
198     {
199         $cols = @msql_num_fields($result);
200         if (!$cols) {
201             return $this->raiseError();
202         }
203         return $cols;
204     }
205
206     // }}}
207     // {{{ numRows()
208
209     function numRows($result)
210     {
211         $rows = @msql_num_rows($result);
212         if (!$rows) {
213             return $this->raiseError();
214         }
215         return $rows;
216     }
217
218     // }}}
219     // {{{ affected()
220
221     /**
222      * Gets the number of rows affected by a query.
223      *
224      * @return number of rows affected by the last query
225      */
226     function affectedRows()
227     {
228         return @msql_affected_rows($this->connection);
229     }
230
231     // }}}
232
233 }
234
235 /*
236  * Local variables:
237  * tab-width: 4
238  * c-basic-offset: 4
239  * End:
240  */
241
242 ?>