| 1 | 
cvsjoko | 
1.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: Tomas V.V.Cox <cox@idecnet.com>                              | | 
| 17 | 
  | 
  | 
// +----------------------------------------------------------------------+ | 
| 18 | 
  | 
  | 
// | 
| 19 | 
  | 
  | 
// $Id: ifx.php,v 1.18 2002/02/28 08:27:09 sebastian Exp $ | 
| 20 | 
  | 
  | 
// | 
| 21 | 
  | 
  | 
// Database independent query interface definition for PHP's Informix | 
| 22 | 
  | 
  | 
// extension. | 
| 23 | 
  | 
  | 
// | 
| 24 | 
  | 
  | 
 | 
| 25 | 
  | 
  | 
// Legend: | 
| 26 | 
  | 
  | 
// For more info on Informix errors see: | 
| 27 | 
  | 
  | 
// http://www.informix.com/answers/english/ierrors.htm | 
| 28 | 
  | 
  | 
// | 
| 29 | 
  | 
  | 
// TODO: | 
| 30 | 
  | 
  | 
//  - set needed env Informix vars on connect | 
| 31 | 
  | 
  | 
//  - implement native prepare/execute | 
| 32 | 
  | 
  | 
 | 
| 33 | 
  | 
  | 
require_once 'DB/common.php'; | 
| 34 | 
  | 
  | 
 | 
| 35 | 
  | 
  | 
class DB_ifx extends DB_common | 
| 36 | 
  | 
  | 
{ | 
| 37 | 
  | 
  | 
    var $connection; | 
| 38 | 
  | 
  | 
    var $affected = 0; | 
| 39 | 
  | 
  | 
    var $dsn = array(); | 
| 40 | 
  | 
  | 
    var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */ | 
| 41 | 
  | 
  | 
 | 
| 42 | 
  | 
  | 
    function DB_ifx() | 
| 43 | 
  | 
  | 
    { | 
| 44 | 
  | 
  | 
        $this->phptype = 'ifx'; | 
| 45 | 
  | 
  | 
        $this->dbsyntax = 'ifx'; | 
| 46 | 
  | 
  | 
        $this->features = array( | 
| 47 | 
  | 
  | 
            'prepare' => false, | 
| 48 | 
  | 
  | 
            'pconnect' => true, | 
| 49 | 
  | 
  | 
            'transactions' => false, | 
| 50 | 
  | 
  | 
            'limit' => 'emulate' | 
| 51 | 
  | 
  | 
        ); | 
| 52 | 
  | 
  | 
        $this->errorcode_map = array( | 
| 53 | 
  | 
  | 
            '-201'    => DB_ERROR_SYNTAX, | 
| 54 | 
  | 
  | 
            '-206'    => DB_ERROR_NOSUCHTABLE, | 
| 55 | 
  | 
  | 
            '-217'    => DB_ERROR_NOSUCHFIELD, | 
| 56 | 
  | 
  | 
            '-329'    => DB_ERROR_NODBSELECTED, | 
| 57 | 
  | 
  | 
            '-1204'   => DB_ERROR_INVALID_DATE, | 
| 58 | 
  | 
  | 
            '-1205'   => DB_ERROR_INVALID_DATE, | 
| 59 | 
  | 
  | 
            '-1206'   => DB_ERROR_INVALID_DATE, | 
| 60 | 
  | 
  | 
            '-1209'   => DB_ERROR_INVALID_DATE, | 
| 61 | 
  | 
  | 
            '-1210'   => DB_ERROR_INVALID_DATE, | 
| 62 | 
  | 
  | 
            '-1212'   => DB_ERROR_INVALID_DATE | 
| 63 | 
  | 
  | 
       ); | 
| 64 | 
  | 
  | 
    } | 
| 65 | 
  | 
  | 
 | 
| 66 | 
  | 
  | 
    /** | 
| 67 | 
  | 
  | 
     * Connect to a database and log in as the specified user. | 
| 68 | 
  | 
  | 
     * | 
| 69 | 
  | 
  | 
     * @param $dsn the data source name (see DB::parseDSN for syntax) | 
| 70 | 
  | 
  | 
     * @param $persistent (optional) whether the connection should | 
| 71 | 
  | 
  | 
     *        be persistent | 
| 72 | 
  | 
  | 
     * | 
| 73 | 
  | 
  | 
     * @return int DB_OK on success, a DB error code on failure | 
| 74 | 
  | 
  | 
     */ | 
| 75 | 
  | 
  | 
    function connect(&$dsninfo, $persistent = false) | 
| 76 | 
  | 
  | 
    { | 
| 77 | 
  | 
  | 
        if (!DB::assertExtension('informix')) | 
| 78 | 
  | 
  | 
            return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND); | 
| 79 | 
  | 
  | 
 | 
| 80 | 
  | 
  | 
        $this->dsn = $dsninfo; | 
| 81 | 
  | 
  | 
        $dbhost = $dsninfo['hostspec'] ? '@' . $dsninfo['hostspec'] : ''; | 
| 82 | 
  | 
  | 
        $dbname = $dsninfo['database'] ? $dsninfo['database'] . $dbhost : ''; | 
| 83 | 
  | 
  | 
        $user = $dsninfo['username'] ? $dsninfo['username'] : ''; | 
| 84 | 
  | 
  | 
        $pw = $dsninfo['password'] ? $dsninfo['password'] : ''; | 
| 85 | 
  | 
  | 
 | 
| 86 | 
  | 
  | 
        $connect_function = $persistent ? 'ifx_pconnect' : 'ifx_connect'; | 
| 87 | 
  | 
  | 
 | 
| 88 | 
  | 
  | 
        $this->connection = @$connect_function($dbname, $user, $pw); | 
| 89 | 
  | 
  | 
        if (!is_resource($this->connection)) { | 
| 90 | 
  | 
  | 
            return $this->ifxraiseError(DB_ERROR_CONNECT_FAILED); | 
| 91 | 
  | 
  | 
        } | 
| 92 | 
  | 
  | 
        return DB_OK; | 
| 93 | 
  | 
  | 
    } | 
| 94 | 
  | 
  | 
 | 
| 95 | 
  | 
  | 
    /** | 
| 96 | 
  | 
  | 
     * Log out and disconnect from the database. | 
| 97 | 
  | 
  | 
     * | 
| 98 | 
  | 
  | 
     * @return bool TRUE on success, FALSE if not connected. | 
| 99 | 
  | 
  | 
     */ | 
| 100 | 
  | 
  | 
    function disconnect() | 
| 101 | 
  | 
  | 
    { | 
| 102 | 
  | 
  | 
        $ret = @ifx_close($this->connection); | 
| 103 | 
  | 
  | 
        $this->connection = null; | 
| 104 | 
  | 
  | 
        return $ret; | 
| 105 | 
  | 
  | 
    } | 
| 106 | 
  | 
  | 
 | 
| 107 | 
  | 
  | 
    /** | 
| 108 | 
  | 
  | 
     * Send a query to Informix and return the results as a | 
| 109 | 
  | 
  | 
     * Informix resource identifier. | 
| 110 | 
  | 
  | 
     * | 
| 111 | 
  | 
  | 
     * @param $query the SQL query | 
| 112 | 
  | 
  | 
     * | 
| 113 | 
  | 
  | 
     * @return int returns a valid Informix result for successful SELECT | 
| 114 | 
  | 
  | 
     * queries, DB_OK for other successful queries.  A DB error code | 
| 115 | 
  | 
  | 
     * is returned on failure. | 
| 116 | 
  | 
  | 
     */ | 
| 117 | 
  | 
  | 
    function simpleQuery($query) | 
| 118 | 
  | 
  | 
    { | 
| 119 | 
  | 
  | 
        $this->last_query = $query; | 
| 120 | 
  | 
  | 
        if (preg_match('/(SELECT)/i', $query)) {    //TESTME: Use !DB::isManip()? | 
| 121 | 
  | 
  | 
            // the scroll is needed for fetching absolute row numbers | 
| 122 | 
  | 
  | 
            // in a select query result | 
| 123 | 
  | 
  | 
            $result = @ifx_query($query, $this->connection, IFX_SCROLL); | 
| 124 | 
  | 
  | 
        } else { | 
| 125 | 
  | 
  | 
            $result = @ifx_query($query, $this->connection); | 
| 126 | 
  | 
  | 
        } | 
| 127 | 
  | 
  | 
        if (!$result) { | 
| 128 | 
  | 
  | 
            return $this->ifxraiseError(); | 
| 129 | 
  | 
  | 
        } | 
| 130 | 
  | 
  | 
        $this->affected = ifx_affected_rows ($result); | 
| 131 | 
  | 
  | 
        // Determine which queries that should return data, and which | 
| 132 | 
  | 
  | 
        // should return an error code only. | 
| 133 | 
  | 
  | 
        if (preg_match('/(SELECT)/i', $query)) { | 
| 134 | 
  | 
  | 
            return $result; | 
| 135 | 
  | 
  | 
        } | 
| 136 | 
  | 
  | 
        return DB_OK; | 
| 137 | 
  | 
  | 
    } | 
| 138 | 
  | 
  | 
 | 
| 139 | 
  | 
  | 
    // {{{ nextResult() | 
| 140 | 
  | 
  | 
 | 
| 141 | 
  | 
  | 
    /** | 
| 142 | 
  | 
  | 
     * Move the internal ifx result pointer to the next available result | 
| 143 | 
  | 
  | 
     * | 
| 144 | 
  | 
  | 
     * @param a valid fbsql result resource | 
| 145 | 
  | 
  | 
     * | 
| 146 | 
  | 
  | 
     * @access public | 
| 147 | 
  | 
  | 
     * | 
| 148 | 
  | 
  | 
     * @return true if a result is available otherwise return false | 
| 149 | 
  | 
  | 
     */ | 
| 150 | 
  | 
  | 
    function nextResult($result) | 
| 151 | 
  | 
  | 
    { | 
| 152 | 
  | 
  | 
        return false; | 
| 153 | 
  | 
  | 
    } | 
| 154 | 
  | 
  | 
 | 
| 155 | 
  | 
  | 
    // }}} | 
| 156 | 
  | 
  | 
 | 
| 157 | 
  | 
  | 
    /** | 
| 158 | 
  | 
  | 
     * Gets the number of rows affected by the last query. | 
| 159 | 
  | 
  | 
     * if the last query was a select, returns an _estimate_ value. | 
| 160 | 
  | 
  | 
     * | 
| 161 | 
  | 
  | 
     * @return number of rows affected by the last query | 
| 162 | 
  | 
  | 
     */ | 
| 163 | 
  | 
  | 
    function affectedRows() | 
| 164 | 
  | 
  | 
    { | 
| 165 | 
  | 
  | 
        return $this->affected; | 
| 166 | 
  | 
  | 
    } | 
| 167 | 
  | 
  | 
 | 
| 168 | 
  | 
  | 
    /** | 
| 169 | 
  | 
  | 
     * Fetch and return a row of data (it uses fetchInto for that) | 
| 170 | 
  | 
  | 
     * @param   $result             Informix result identifier | 
| 171 | 
  | 
  | 
     * @param   $fetchmode     format of fetched row array | 
| 172 | 
  | 
  | 
     * @param   $rownum        the absolute row number to fetch | 
| 173 | 
  | 
  | 
     * | 
| 174 | 
  | 
  | 
     * @return  array   a row of data, or false on error | 
| 175 | 
  | 
  | 
     */ | 
| 176 | 
  | 
  | 
    function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) | 
| 177 | 
  | 
  | 
    { | 
| 178 | 
  | 
  | 
        if ($fetchmode == DB_FETCHMODE_DEFAULT) { | 
| 179 | 
  | 
  | 
            $fetchmode = $this->fetchmode; | 
| 180 | 
  | 
  | 
        } | 
| 181 | 
  | 
  | 
        $res = $this->fetchInto ($result, $arr, $fetchmode, $rownum); | 
| 182 | 
  | 
  | 
        if ($res !== DB_OK) { | 
| 183 | 
  | 
  | 
            return $res; | 
| 184 | 
  | 
  | 
        } | 
| 185 | 
  | 
  | 
        return $arr; | 
| 186 | 
  | 
  | 
    } | 
| 187 | 
  | 
  | 
 | 
| 188 | 
  | 
  | 
    /** | 
| 189 | 
  | 
  | 
     * Fetch a row and return as array. | 
| 190 | 
  | 
  | 
     * | 
| 191 | 
  | 
  | 
     * @param   $result     Informix result identifier | 
| 192 | 
  | 
  | 
     * @param   $row         (reference) array where data from the row is stored | 
| 193 | 
  | 
  | 
     * @param   $fetchmode      how the resulting array should be indexed | 
| 194 | 
  | 
  | 
     * @param   $rownum     the row number to fetch | 
| 195 | 
  | 
  | 
     * | 
| 196 | 
  | 
  | 
     * @return int an array on success, a DB error code on failure, NULL | 
| 197 | 
  | 
  | 
     *             if there is no more data | 
| 198 | 
  | 
  | 
     */ | 
| 199 | 
  | 
  | 
    function fetchInto($result, &$row, $fetchmode, $rownum=null) | 
| 200 | 
  | 
  | 
    { | 
| 201 | 
  | 
  | 
        if (($rownum !== null) && ($rownum < 0)) { | 
| 202 | 
  | 
  | 
            return null; | 
| 203 | 
  | 
  | 
        } | 
| 204 | 
  | 
  | 
        // if $rownum is null, fetch row will return the next row | 
| 205 | 
  | 
  | 
        if (!$row = @ifx_fetch_row($result, $rownum)) { | 
| 206 | 
  | 
  | 
            return null; | 
| 207 | 
  | 
  | 
        } | 
| 208 | 
  | 
  | 
        if ($fetchmode !== DB_FETCHMODE_ASSOC) { | 
| 209 | 
  | 
  | 
            $i=0; | 
| 210 | 
  | 
  | 
            $order = array(); | 
| 211 | 
  | 
  | 
            foreach ($row as $key => $val) { | 
| 212 | 
  | 
  | 
                $order[$i++] = $val; | 
| 213 | 
  | 
  | 
            } | 
| 214 | 
  | 
  | 
            $row = $order; | 
| 215 | 
  | 
  | 
        } | 
| 216 | 
  | 
  | 
        return DB_OK; | 
| 217 | 
  | 
  | 
    } | 
| 218 | 
  | 
  | 
 | 
| 219 | 
  | 
  | 
    function numRows($result) | 
| 220 | 
  | 
  | 
    { | 
| 221 | 
  | 
  | 
        return $this->raiseError(DB_ERROR_NOT_CAPABLE); | 
| 222 | 
  | 
  | 
    } | 
| 223 | 
  | 
  | 
 | 
| 224 | 
  | 
  | 
    /** | 
| 225 | 
  | 
  | 
     * Get the number of columns in a result set. | 
| 226 | 
  | 
  | 
     * | 
| 227 | 
  | 
  | 
     * @param $result Informix result identifier | 
| 228 | 
  | 
  | 
     * | 
| 229 | 
  | 
  | 
     * @return int the number of columns per row in $result | 
| 230 | 
  | 
  | 
     */ | 
| 231 | 
  | 
  | 
    function numCols($result) | 
| 232 | 
  | 
  | 
    { | 
| 233 | 
  | 
  | 
        if (!$cols = @ifx_num_fields($result)) { | 
| 234 | 
  | 
  | 
            return $this->ifxraiseError(); | 
| 235 | 
  | 
  | 
        } | 
| 236 | 
  | 
  | 
        return $cols; | 
| 237 | 
  | 
  | 
    } | 
| 238 | 
  | 
  | 
 | 
| 239 | 
  | 
  | 
    /** | 
| 240 | 
  | 
  | 
     * Free the internal resources associated with $result. | 
| 241 | 
  | 
  | 
     * | 
| 242 | 
  | 
  | 
     * @param $result Informix result identifier | 
| 243 | 
  | 
  | 
     * | 
| 244 | 
  | 
  | 
     * @return bool TRUE on success, DB_error on error | 
| 245 | 
  | 
  | 
     */ | 
| 246 | 
  | 
  | 
    function freeResult($result) | 
| 247 | 
  | 
  | 
    { | 
| 248 | 
  | 
  | 
        if (is_resource($result)) { | 
| 249 | 
  | 
  | 
            if (!@ifx_free_result($result)) { | 
| 250 | 
  | 
  | 
                return $this->ifxraiseError(); | 
| 251 | 
  | 
  | 
            } | 
| 252 | 
  | 
  | 
            return true; | 
| 253 | 
  | 
  | 
        } | 
| 254 | 
  | 
  | 
        if (!isset($this->prepare_tokens[(int)$result])) { | 
| 255 | 
  | 
  | 
            return false; | 
| 256 | 
  | 
  | 
        } | 
| 257 | 
  | 
  | 
        unset($this->prepare_tokens[(int)$result]); | 
| 258 | 
  | 
  | 
        unset($this->prepare_types[(int)$result]); | 
| 259 | 
  | 
  | 
        return true; | 
| 260 | 
  | 
  | 
    } | 
| 261 | 
  | 
  | 
 | 
| 262 | 
  | 
  | 
    function ifxraiseError($errno = null) | 
| 263 | 
  | 
  | 
    { | 
| 264 | 
  | 
  | 
        if ($errno === null) { | 
| 265 | 
  | 
  | 
            $errno = $this->errorCode(ifx_error()); | 
| 266 | 
  | 
  | 
        } | 
| 267 | 
  | 
  | 
 | 
| 268 | 
  | 
  | 
        return $this->raiseError($errno, null, null, null, | 
| 269 | 
  | 
  | 
                            $this->errorNative()); | 
| 270 | 
  | 
  | 
    } | 
| 271 | 
  | 
  | 
 | 
| 272 | 
  | 
  | 
    /** | 
| 273 | 
  | 
  | 
     * Map native error codes to DB's portable ones.  Requires that | 
| 274 | 
  | 
  | 
     * the DB implementation's constructor fills in the $errorcode_map | 
| 275 | 
  | 
  | 
     * property. | 
| 276 | 
  | 
  | 
     * | 
| 277 | 
  | 
  | 
     * @return int a portable DB error code, or DB_ERROR if this DB | 
| 278 | 
  | 
  | 
     * implementation has no mapping for the given error code. | 
| 279 | 
  | 
  | 
     */ | 
| 280 | 
  | 
  | 
 | 
| 281 | 
  | 
  | 
    function errorCode($nativecode) | 
| 282 | 
  | 
  | 
    { | 
| 283 | 
  | 
  | 
        if (ereg('SQLCODE=(.*)]', $nativecode, $match)) { | 
| 284 | 
  | 
  | 
            $code = $match[1]; | 
| 285 | 
  | 
  | 
            if (isset($this->errorcode_map[$code])) { | 
| 286 | 
  | 
  | 
                return $this->errorcode_map[$code]; | 
| 287 | 
  | 
  | 
            } | 
| 288 | 
  | 
  | 
        } | 
| 289 | 
  | 
  | 
        return DB_ERROR; | 
| 290 | 
  | 
  | 
    } | 
| 291 | 
  | 
  | 
 | 
| 292 | 
  | 
  | 
    /** | 
| 293 | 
  | 
  | 
     * Get the native error message of the last error (if any) that | 
| 294 | 
  | 
  | 
     * occured on the current connection. | 
| 295 | 
  | 
  | 
     * | 
| 296 | 
  | 
  | 
     * @return int native Informix error code | 
| 297 | 
  | 
  | 
     */ | 
| 298 | 
  | 
  | 
    function errorNative() | 
| 299 | 
  | 
  | 
    { | 
| 300 | 
  | 
  | 
        return ifx_error() . ' ' . ifx_errormsg(); | 
| 301 | 
  | 
  | 
    } | 
| 302 | 
  | 
  | 
 | 
| 303 | 
  | 
  | 
    // {{{ getSpecialQuery() | 
| 304 | 
  | 
  | 
 | 
| 305 | 
  | 
  | 
    /** | 
| 306 | 
  | 
  | 
    * Returns the query needed to get some backend info | 
| 307 | 
  | 
  | 
    * @param string $type What kind of info you want to retrieve | 
| 308 | 
  | 
  | 
    * @return string The SQL query string | 
| 309 | 
  | 
  | 
    */ | 
| 310 | 
  | 
  | 
    function getSpecialQuery($type) | 
| 311 | 
  | 
  | 
    { | 
| 312 | 
  | 
  | 
        switch ($type) { | 
| 313 | 
  | 
  | 
            case 'tables': | 
| 314 | 
  | 
  | 
            default: | 
| 315 | 
  | 
  | 
                return null; | 
| 316 | 
  | 
  | 
        } | 
| 317 | 
  | 
  | 
        return $sql; | 
| 318 | 
  | 
  | 
    } | 
| 319 | 
  | 
  | 
 | 
| 320 | 
  | 
  | 
    // }}} | 
| 321 | 
  | 
  | 
} | 
| 322 | 
  | 
  | 
 | 
| 323 | 
  | 
  | 
?> |