]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pear/DB/sybase.php
locking table specific for better databases
[SourceForge/phpwiki.git] / lib / pear / DB / sybase.php
1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 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 // +----------------------------------------------------------------------+
18 //
19 // $Id: sybase.php,v 1.2 2004-04-26 20:44:37 rurban Exp $
20 //
21 // Database independent query interface definition for PHP's Sybase
22 // extension.
23 //
24 // Based on DB 1.3 from the pear.php.net repository. 
25 // The only modifications made have been modification of the include paths. 
26 //
27 rcs_id('$Id: sybase.php,v 1.2 2004-04-26 20:44:37 rurban Exp $');
28 rcs_id('From Pear CVS: Id: sybase.php,v 1.3 2002/05/09 12:29:53 ssb Exp');
29
30 require_once 'DB/common.php';
31
32 class DB_sybase extends DB_common
33 {
34     // {{{ properties
35
36     var $connection;
37     var $phptype, $dbsyntax;
38     var $prepare_tokens = array();
39     var $prepare_types = array();
40
41     // }}}
42     // {{{ constructor
43
44     function DB_sybase()
45     {
46         $this->DB_common();
47         $this->phptype = 'sybase';
48         $this->dbsyntax = 'sybase';
49         $this->features = array(
50             'prepare' => false,
51             'pconnect' => true,
52             'transactions' => false,
53             'limit' => 'emulate'
54         );
55     }
56
57     // }}}
58     // {{{ connect()
59
60     function connect($dsninfo, $persistent = false)
61     {
62         if (!DB::assertExtension('sybase') && !DB::assertExtension('sybase_ct'))
63             return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
64
65         $this->dsn = $dsninfo;
66         $user = $dsninfo['username'];
67         $pw   = $dsninfo['password'];
68
69         $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
70         $connect_function = $persistent ? 'sybase_pconnect' : 'sybase_connect';
71
72         if ($dbhost && $user && $pw) {
73             $conn = $connect_function($dbhost, $user, $pw);
74         } elseif ($dbhost && $user) {
75             $conn = $connect_function($dbhost, $user);
76         } elseif ($dbhost) {
77             $conn = $connect_function($dbhost);
78         } else {
79             $conn = $connect_function();
80         }
81
82         if (!$conn) {
83             return $this->raiseError(DB_ERROR_CONNECT_FAILED);
84         }
85
86         if ($dsninfo['database']) {
87             if (!@sybase_select_db($dsninfo['database'], $conn)) {
88                 return $this->raiseError(DB_ERROR_NODBSELECTED);
89             }
90         }
91         $this->connection = $conn;
92         return DB_OK;
93     }
94
95     // }}}
96     // {{{ disconnect()
97
98     function disconnect()
99     {
100         $ret = @sybase_close($this->connection);
101         $this->connection = null;
102         return $ret;
103     }
104
105     // }}}
106     // {{{ simpleQuery()
107
108     function simpleQuery($query)
109     {
110         $this->last_query = $query;
111         $query = $this->modifyQuery($query);
112         $result = @sybase_query($query, $this->connection);
113         if (!$result) {
114             return $this->raiseError();
115         }
116         // Determine which queries that should return data, and which
117         // should return an error code only.
118         return DB::isManip($query) ? DB_OK : $result;
119     }
120
121     // }}}
122     // {{{ nextResult()
123
124     /**
125      * Move the internal sybase result pointer to the next available result
126      *
127      * @param a valid fbsql result resource
128      *
129      * @access public
130      *
131      * @return true if a result is available otherwise return false
132      */
133     function nextResult($result)
134     {
135         return false;
136     }
137
138     // }}}
139     // {{{ fetchRow()
140     function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
141     {
142         if ($fetchmode == DB_FETCHMODE_DEFAULT) {
143             $fetchmode = $this->fetchmode;
144         }
145         $res = $this->fetchInto ($result, $arr, $fetchmode, $rownum);
146         if ($res !== DB_OK) {
147             return $res;
148         }
149         return $arr;
150     }
151
152     // }}}
153     // {{{ fetchInto()
154
155     function fetchInto($result, &$ar, $fetchmode, $rownum=null)
156     {
157         if ($rownum !== null) {
158             if (!sybase_data_seek($result, $rownum)) {
159                 return $this->raiseError();
160             }
161         }
162         $ar = ($fetchmode & DB_FETCHMODE_ASSOC) ? @sybase_fetch_array($result) : @sybase_fetch_row($result);
163         if (!$ar) {
164             // reported not work as seems that sybase_get_last_message()
165             // always return a message here
166             //if ($errmsg = sybase_get_last_message()) {
167             //    return $this->raiseError($errmsg);
168             //} else {
169                 return null;
170             //}
171         }
172         return DB_OK;
173     }
174
175     // }}}
176     // {{{ freeResult()
177
178     function freeResult($result)
179     {
180         if (is_resource($result)) {
181             return @sybase_free_result($result);
182         }
183         if (!isset($this->prepare_tokens[(int)$result])) {
184             return false;
185         }
186         unset($this->prepare_tokens[(int)$result]);
187         unset($this->prepare_types[(int)$result]);
188         return true;
189     }
190
191     // }}}
192     // {{{ numCols()
193
194     function numCols($result)
195     {
196         $cols = @sybase_num_fields($result);
197         if (!$cols) {
198             return $this->raiseError();
199         }
200         return $cols;
201     }
202
203     // }}}
204     // {{{ affectedRows()
205
206     /**
207      * Gets the number of rows affected by the data manipulation
208      * query.  For other queries, this function returns 0.
209      *
210      * @return number of rows affected by the last query
211      */
212
213     function affectedRows()
214     {
215         if (DB::isManip($this->last_query)) {
216             $result = @sybase_affected_rows($this->connection);
217         } else {
218             $result = 0;
219         }
220         return $result;
221      }
222
223     // }}}
224     // {{{ getSpecialQuery()
225
226     /**
227     * Returns the query needed to get some backend info
228     * @param string $type What kind of info you want to retrieve
229     * @return string The SQL query string
230     */
231     function getSpecialQuery($type)
232     {
233         switch ($type) {
234             case 'tables':
235                 $sql = "select name from sysobjects where type = 'U' order by name";
236                 break;
237             case 'views':
238                 $sql = "select name from sysobjects where type = 'V'";
239                 break;
240             default:
241                 return null;
242         }
243         return $sql;
244     }
245
246     // }}}
247
248 }
249
250 /*
251  * Local variables:
252  * tab-width: 4
253  * c-basic-offset: 4
254  * End:
255  */
256 ?>