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