Skip to content
Snippets Groups Projects
MDB2.php 140 KiB
Newer Older
Robin Appelman's avatar
Robin Appelman committed
    // {{{ function beginTransaction($savepoint = null)

    /**
     * Start a transaction or set a savepoint.
     *
     * @param   string  name of a savepoint to set
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function beginTransaction($savepoint = null)
    {
        $this->debug('Starting transaction', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'transactions are not supported', __FUNCTION__);
    }

    // }}}
    // {{{ function commit($savepoint = null)

    /**
     * Commit the database changes done during a transaction that is in
     * progress or release a savepoint. This function may only be called when
     * auto-committing is disabled, otherwise it will fail. Therefore, a new
     * transaction is implicitly started after committing the pending changes.
     *
     * @param   string  name of a savepoint to release
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function commit($savepoint = null)
    {
        $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'commiting transactions is not supported', __FUNCTION__);
    }

    // }}}
    // {{{ function rollback($savepoint = null)

    /**
     * Cancel any database changes done during a transaction or since a specific
     * savepoint that is in progress. This function may only be called when
     * auto-committing is disabled, otherwise it will fail. Therefore, a new
     * transaction is implicitly started after canceling the pending changes.
     *
     * @param   string  name of a savepoint to rollback to
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function rollback($savepoint = null)
    {
        $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'rolling back transactions is not supported', __FUNCTION__);
    }

    // }}}
    // {{{ function inTransaction($ignore_nested = false)

    /**
     * If a transaction is currently open.
     *
     * @param   bool    if the nested transaction count should be ignored
     * @return  int|bool    - an integer with the nesting depth is returned if a
     *                      nested transaction is open
     *                      - true is returned for a normal open transaction
     *                      - false is returned if no transaction is open
     *
     * @access  public
     */
    function inTransaction($ignore_nested = false)
    {
        if (!$ignore_nested && isset($this->nested_transaction_counter)) {
            return $this->nested_transaction_counter;
        }
        return $this->in_transaction;
    }

    // }}}
    // {{{ function setTransactionIsolation($isolation)

    /**
     * Set the transacton isolation level.
     *
     * @param   string  standard isolation level
     *                  READ UNCOMMITTED (allows dirty reads)
     *                  READ COMMITTED (prevents dirty reads)
     *                  REPEATABLE READ (prevents nonrepeatable reads)
     *                  SERIALIZABLE (prevents phantom reads)
     * @param   array some transaction options:
     *                  'wait' => 'WAIT' | 'NO WAIT'
     *                  'rw'   => 'READ WRITE' | 'READ ONLY'
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     * @since   2.1.1
     */
    static function setTransactionIsolation($isolation, $options = array())
Robin Appelman's avatar
Robin Appelman committed
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
    {
        $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'isolation level setting is not supported', __FUNCTION__);
    }

    // }}}
    // {{{ function beginNestedTransaction($savepoint = false)

    /**
     * Start a nested transaction.
     *
     * @return  mixed   MDB2_OK on success/savepoint name, a MDB2 error on failure
     *
     * @access  public
     * @since   2.1.1
     */
    function beginNestedTransaction()
    {
        if ($this->in_transaction) {
            ++$this->nested_transaction_counter;
            $savepoint = sprintf($this->options['savepoint_format'], $this->nested_transaction_counter);
            if ($this->supports('savepoints') && $savepoint) {
                return $this->beginTransaction($savepoint);
            }
            return MDB2_OK;
        }
        $this->has_transaction_error = false;
        $result = $this->beginTransaction();
        $this->nested_transaction_counter = 1;
        return $result;
    }

    // }}}
    // {{{ function completeNestedTransaction($force_rollback = false, $release = false)

    /**
     * Finish a nested transaction by rolling back if an error occured or
     * committing otherwise.
     *
     * @param   bool    if the transaction should be rolled back regardless
     *                  even if no error was set within the nested transaction
     * @return  mixed   MDB_OK on commit/counter decrementing, false on rollback
     *                  and a MDB2 error on failure
     *
     * @access  public
     * @since   2.1.1
     */
    function completeNestedTransaction($force_rollback = false)
    {
        if ($this->nested_transaction_counter > 1) {
            $savepoint = sprintf($this->options['savepoint_format'], $this->nested_transaction_counter);
            if ($this->supports('savepoints') && $savepoint) {
                if ($force_rollback || $this->has_transaction_error) {
                    $result = $this->rollback($savepoint);
                    if (!PEAR::isError($result)) {
                        $result = false;
                        $this->has_transaction_error = false;
                    }
                } else {
                    $result = $this->commit($savepoint);
                }
            } else {
                $result = MDB2_OK;
            }
            --$this->nested_transaction_counter;
            return $result;
        }

        $this->nested_transaction_counter = null;
        $result = MDB2_OK;

        // transaction has not yet been rolled back
        if ($this->in_transaction) {
            if ($force_rollback || $this->has_transaction_error) {
                $result = $this->rollback();
                if (!PEAR::isError($result)) {
                    $result = false;
                }
            } else {
                $result = $this->commit();
            }
        }
        $this->has_transaction_error = false;
        return $result;
    }

    // }}}
    // {{{ function failNestedTransaction($error = null, $immediately = false)

    /**
     * Force setting nested transaction to failed.
     *
     * @param   mixed   value to return in getNestededTransactionError()
     * @param   bool    if the transaction should be rolled back immediately
     * @return  bool    MDB2_OK
     *
     * @access  public
     * @since   2.1.1
     */
    function failNestedTransaction($error = null, $immediately = false)
    {
        if (is_null($error)) {
            $error = $this->has_transaction_error ? $this->has_transaction_error : true;
        } elseif (!$error) {
            $error = true;
        }
        $this->has_transaction_error = $error;
        if (!$immediately) {
            return MDB2_OK;
        }
        return $this->rollback();
    }

    // }}}
    // {{{ function getNestedTransactionError()

    /**
     * The first error that occured since the transaction start.
     *
     * @return  MDB2_Error|bool     MDB2 error object if an error occured or false.
     *
     * @access  public
     * @since   2.1.1
     */
    function getNestedTransactionError()
    {
        return $this->has_transaction_error;
    }

    // }}}
    // {{{ connect()

    /**
     * Connect to the database
     *
     * @return true on success, MDB2 Error Object on failure
     */
    function connect()
    {
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ databaseExists()

    /**
     * check if given database name is exists?
     *
     * @param string $name    name of the database that should be checked
     *
     * @return mixed true/false on success, a MDB2 error on failure
     * @access public
     */
    function databaseExists($name)
    {
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ setCharset($charset, $connection = null)

    /**
     * Set the charset on the current connection
     *
     * @param string    charset
     * @param resource  connection handle
     *
     * @return true on success, MDB2 Error Object on failure
     */
    function setCharset($charset, $connection = null)
    {
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ function disconnect($force = true)

    /**
     * Log out and disconnect from the database.
     *
     * @param boolean $force whether the disconnect should be forced even if the
     *                       connection is opened persistently
     *
     * @return mixed true on success, false if not connected and error object on error
     *
     * @access  public
     */
    function disconnect($force = true)
    {
        $this->connection = 0;
        $this->connected_dsn = array();
        $this->connected_database_name = '';
        $this->opened_persistent = null;
        $this->connected_server_info = '';
        $this->in_transaction = null;
        $this->nested_transaction_counter = null;
        return MDB2_OK;
    }

    // }}}
    // {{{ function setDatabase($name)

    /**
     * Select a different database
     *
     * @param   string  name of the database that should be selected
     *
     * @return  string  name of the database previously connected to
     *
     * @access  public
     */
    function setDatabase($name)
    {
        $previous_database_name = (isset($this->database_name)) ? $this->database_name : '';
        $this->database_name = $name;
        if (!empty($this->connected_database_name) && ($this->connected_database_name != $this->database_name)) {
            $this->disconnect(false);
        }
        return $previous_database_name;
    }

    // }}}
    // {{{ function getDatabase()

    /**
     * Get the current database
     *
     * @return  string  name of the database
     *
     * @access  public
     */
    function getDatabase()
    {
        return $this->database_name;
    }

    // }}}
    // {{{ function setDSN($dsn)

    /**
     * set the DSN
     *
     * @param   mixed   DSN string or array
     *
     * @return  MDB2_OK
     *
     * @access  public
     */
    function setDSN($dsn)
    {
        $dsn_default = $GLOBALS['_MDB2_dsninfo_default'];
        $dsn = MDB2::parseDSN($dsn);
        if (array_key_exists('database', $dsn)) {
            $this->database_name = $dsn['database'];
            unset($dsn['database']);
        }
        $this->dsn = array_merge($dsn_default, $dsn);
        return $this->disconnect(false);
    }

    // }}}
    // {{{ function getDSN($type = 'string', $hidepw = false)

    /**
     * return the DSN as a string
     *
     * @param   string  format to return ("array", "string")
     * @param   string  string to hide the password with
     *
     * @return  mixed   DSN in the chosen type
     *
     * @access  public
     */
    function getDSN($type = 'string', $hidepw = false)
    {
        $dsn = array_merge($GLOBALS['_MDB2_dsninfo_default'], $this->dsn);
        $dsn['phptype'] = $this->phptype;
        $dsn['database'] = $this->database_name;
        if ($hidepw) {
            $dsn['password'] = $hidepw;
        }
        switch ($type) {
        // expand to include all possible options
        case 'string':
           $dsn = $dsn['phptype'].
               ($dsn['dbsyntax'] ? ('('.$dsn['dbsyntax'].')') : '').
               '://'.$dsn['username'].':'.
                $dsn['password'].'@'.$dsn['hostspec'].
                ($dsn['port'] ? (':'.$dsn['port']) : '').
                '/'.$dsn['database'];
            break;
        case 'array':
        default:
            break;
        }
        return $dsn;
    }

    // }}}
    // {{{ _isNewLinkSet()

    /**
     * Check if the 'new_link' option is set
     *
     * @return boolean
     *
     * @access protected
     */
    function _isNewLinkSet()
    {
        return (isset($this->dsn['new_link'])
            && ($this->dsn['new_link'] === true
             || (is_string($this->dsn['new_link']) && preg_match('/^true$/i', $this->dsn['new_link']))
             || (is_numeric($this->dsn['new_link']) && 0 != (int)$this->dsn['new_link'])
            )
        );
    }

    // }}}
    // {{{ function &standaloneQuery($query, $types = null, $is_manip = false)

   /**
     * execute a query as database administrator
     *
     * @param   string  the SQL query
     * @param   mixed   array that contains the types of the columns in
     *                        the result set
     * @param   bool    if the query is a manipulation query
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function &standaloneQuery($query, $types = null, $is_manip = false)
    {
        $offset = $this->offset;
        $limit = $this->limit;
        $this->offset = $this->limit = 0;
        $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);

        $connection = $this->getConnection();
        if (PEAR::isError($connection)) {
            return $connection;
        }

        $result =$this->_doQuery($query, $is_manip, $connection, false);
Robin Appelman's avatar
Robin Appelman committed
        if (PEAR::isError($result)) {
            return $result;
        }

        if ($is_manip) {
            $affected_rows =  $this->_affectedRows($connection, $result);
            return $affected_rows;
        }
        $result =$this->_wrapResult($result, $types, true, false, $limit, $offset);
Robin Appelman's avatar
Robin Appelman committed
        return $result;
    }

    // }}}
    // {{{ function _modifyQuery($query, $is_manip, $limit, $offset)

    /**
     * Changes a query string for various DBMS specific reasons
     *
     * @param   string  query to modify
     * @param   bool    if it is a DML query
     * @param   int  limit the number of rows
     * @param   int  start reading from given offset
     *
     * @return  string  modified query
     *
     * @access  protected
     */
    function _modifyQuery($query, $is_manip, $limit, $offset)
    {
        return $query;
    }

    // }}}
    // {{{ function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)

    /**
     * Execute a query
     * @param   string  query
     * @param   bool    if the query is a manipulation query
     * @param   resource connection handle
     * @param   string  database name
     *
     * @return  result or error object
     *
     * @access  protected
     */
    function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
    {
        $this->last_query = $query;
        $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
        if ($result) {
            if (PEAR::isError($result)) {
                return $result;
            }
            $query = $result;
        }
        $err =$this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
Robin Appelman's avatar
Robin Appelman committed
            'method not implemented', __FUNCTION__);
        return $err;
    }

    // }}}
    // {{{ function _affectedRows($connection, $result = null)

    /**
     * Returns the number of rows affected
     *
     * @param   resource result handle
     * @param   resource connection handle
     *
     * @return  mixed   MDB2 Error Object or the number of rows affected
     *
     * @access  private
     */
    function _affectedRows($connection, $result = null)
    {
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ function &exec($query)

    /**
     * Execute a manipulation query to the database and return the number of affected rows
     *
     * @param   string  the SQL query
     *
     * @return  mixed   number of affected rows on success, a MDB2 error on failure
     *
     * @access  public
     */
    function &exec($query)
    {
        $offset = $this->offset;
        $limit = $this->limit;
        $this->offset = $this->limit = 0;
        $query = $this->_modifyQuery($query, true, $limit, $offset);

        $connection = $this->getConnection();
        if (PEAR::isError($connection)) {
            return $connection;
        }

        $result =$this->_doQuery($query, true, $connection, $this->database_name);
Robin Appelman's avatar
Robin Appelman committed
        if (PEAR::isError($result)) {
            return $result;
        }

        $affectedRows = $this->_affectedRows($connection, $result);
        return $affectedRows;
    }

    // }}}
    // {{{ function &query($query, $types = null, $result_class = true, $result_wrap_class = false)

    /**
     * Send a query to the database and return any results
     *
     * @param   string  the SQL query
     * @param   mixed   array that contains the types of the columns in
     *                        the result set
     * @param   mixed   string which specifies which result class to use
     * @param   mixed   string which specifies which class to wrap results in
     *
     * @return mixed   an MDB2_Result handle on success, a MDB2 error on failure
     *
     * @access  public
     */
    function &query($query, $types = null, $result_class = true, $result_wrap_class = false)
    {
        $offset = $this->offset;
        $limit = $this->limit;
        $this->offset = $this->limit = 0;
        $query = $this->_modifyQuery($query, false, $limit, $offset);

        $connection = $this->getConnection();
        if (PEAR::isError($connection)) {
            return $connection;
        }

        $result =$this->_doQuery($query, false, $connection, $this->database_name);
Robin Appelman's avatar
Robin Appelman committed
        if (PEAR::isError($result)) {
            return $result;
        }

        $result =$this->_wrapResult($result, $types, $result_class, $result_wrap_class, $limit, $offset);
Robin Appelman's avatar
Robin Appelman committed
        return $result;
    }

    // }}}
    // {{{ function &_wrapResult($result, $types = array(), $result_class = true, $result_wrap_class = false, $limit = null, $offset = null)

    /**
     * wrap a result set into the correct class
     *
     * @param   resource result handle
     * @param   mixed   array that contains the types of the columns in
     *                        the result set
     * @param   mixed   string which specifies which result class to use
     * @param   mixed   string which specifies which class to wrap results in
     * @param   string  number of rows to select
     * @param   string  first row to select
     *
     * @return mixed   an MDB2_Result, a MDB2 error on failure
     *
     * @access  protected
     */
    function &_wrapResult($result, $types = array(), $result_class = true,
        $result_wrap_class = false, $limit = null, $offset = null)
    {
        if ($types === true) {
            if ($this->supports('result_introspection')) {
                $this->loadModule('Reverse', null, true);
                $tableInfo = $this->reverse->tableInfo($result);
                if (PEAR::isError($tableInfo)) {
                    return $tableInfo;
                }
                $types = array();
                foreach ($tableInfo as $field) {
                    $types[] = $field['mdb2type'];
                }
            } else {
                $types = null;
            }
        }

        if ($result_class === true) {
            $result_class = $this->options['result_buffering']
                ? $this->options['buffered_result_class'] : $this->options['result_class'];
        }

        if ($result_class) {
            $class_name = sprintf($result_class, $this->phptype);
            if (!MDB2::classExists($class_name)) {
                $err =$this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
Robin Appelman's avatar
Robin Appelman committed
                    'result class does not exist '.$class_name, __FUNCTION__);
                return $err;
            }
            $result =new $class_name($this, $result, $limit, $offset);
Robin Appelman's avatar
Robin Appelman committed
            if (!MDB2::isResultCommon($result)) {
                $err =$this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
Robin Appelman's avatar
Robin Appelman committed
                    'result class is not extended from MDB2_Result_Common', __FUNCTION__);
                return $err;
            }
            if (!empty($types)) {
                $err = $result->setResultTypes($types);
                if (PEAR::isError($err)) {
                    $result->free();
                    return $err;
                }
            }
        }
        if ($result_wrap_class === true) {
            $result_wrap_class = $this->options['result_wrap_class'];
        }
        if ($result_wrap_class) {
            if (!MDB2::classExists($result_wrap_class)) {
                $err =$this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
Robin Appelman's avatar
Robin Appelman committed
                    'result wrap class does not exist '.$result_wrap_class, __FUNCTION__);
                return $err;
            }
            $result = new $result_wrap_class($result, $this->fetchmode);
        }
        return $result;
    }

    // }}}
    // {{{ function getServerVersion($native = false)

    /**
     * return version information about the server
     *
     * @param   bool    determines if the raw version string should be returned
     *
     * @return  mixed   array with version information or row string
     *
     * @access  public
     */
    function getServerVersion($native = false)
    {
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ function setLimit($limit, $offset = null)

    /**
     * set the range of the next query
     *
     * @param   string  number of rows to select
     * @param   string  first row to select
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function setLimit($limit, $offset = null)
    {
        if (!$this->supports('limit_queries')) {
            return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
                'limit is not supported by this driver', __FUNCTION__);
        }
        $limit = (int)$limit;
        if ($limit < 0) {
            return $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
                'it was not specified a valid selected range row limit', __FUNCTION__);
        }
        $this->limit = $limit;
        if (!is_null($offset)) {
            $offset = (int)$offset;
            if ($offset < 0) {
                return $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
                    'it was not specified a valid first selected range row', __FUNCTION__);
            }
            $this->offset = $offset;
        }
        return MDB2_OK;
    }

    // }}}
    // {{{ function subSelect($query, $type = false)

    /**
     * simple subselect emulation: leaves the query untouched for all RDBMS
     * that support subselects
     *
     * @param   string  the SQL query for the subselect that may only
     *                      return a column
     * @param   string  determines type of the field
     *
     * @return  string  the query
     *
     * @access  public
     */
    function subSelect($query, $type = false)
    {
        if ($this->supports('sub_selects') === true) {
            return $query;
        }

        if (!$this->supports('sub_selects')) {
            return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
                'method not implemented', __FUNCTION__);
        }

        $col = $this->queryCol($query, $type);
        if (PEAR::isError($col)) {
            return $col;
        }
        if (!is_array($col) || count($col) == 0) {
            return 'NULL';
        }
        if ($type) {
            $this->loadModule('Datatype', null, true);
            return $this->datatype->implodeArray($col, $type);
        }
        return implode(', ', $col);
    }

    // }}}
    // {{{ function replace($table, $fields)

    /**
     * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
     * query, except that if there is already a row in the table with the same
     * key field values, the old row is deleted before the new row is inserted.
     *
     * The REPLACE type of query does not make part of the SQL standards. Since
     * practically only MySQL and SQLite implement it natively, this type of
     * query isemulated through this method for other DBMS using standard types
     * of queries inside a transaction to assure the atomicity of the operation.
     *
     * @param   string  name of the table on which the REPLACE query will
     *       be executed.
     * @param   array   associative array   that describes the fields and the
     *       values that will be inserted or updated in the specified table. The
     *       indexes of the array are the names of all the fields of the table.
     *       The values of the array are also associative arrays that describe
     *       the values and other properties of the table fields.
     *
     *       Here follows a list of field properties that need to be specified:
     *
     *       value
     *           Value to be assigned to the specified field. This value may be
     *           of specified in database independent type format as this
     *           function can perform the necessary datatype conversions.
     *
     *           Default: this property is required unless the Null property is
     *           set to 1.
     *
     *       type
     *           Name of the type of the field. Currently, all types MDB2
     *           are supported except for clob and blob.
     *
     *           Default: no type conversion
     *
     *       null
     *           bool    property that indicates that the value for this field
     *           should be set to null.
     *
     *           The default value for fields missing in INSERT queries may be
     *           specified the definition of a table. Often, the default value
     *           is already null, but since the REPLACE may be emulated using
     *           an UPDATE query, make sure that all fields of the table are
     *           listed in this function argument array.
     *
     *           Default: 0
     *
     *       key
     *           bool    property that indicates that this field should be
     *           handled as a primary key or at least as part of the compound
     *           unique index of the table that will determine the row that will
     *           updated if it exists or inserted a new row otherwise.
     *
     *           This function will fail if no key field is specified or if the
     *           value of a key field is set to null because fields that are
     *           part of unique index they may not be null.
     *
     *           Default: 0
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function replace($table, $fields)
    {
        if (!$this->supports('replace')) {
            return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
                'replace query is not supported', __FUNCTION__);
        }
        $count = count($fields);
        $condition = $values = array();
        for ($colnum = 0, reset($fields); $colnum < $count; next($fields), $colnum++) {
            $name = key($fields);
            if (isset($fields[$name]['null']) && $fields[$name]['null']) {
                $value = 'NULL';
            } else {
                $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null;
                $value = $this->quote($fields[$name]['value'], $type);
            }
            $values[$name] = $value;
            if (isset($fields[$name]['key']) && $fields[$name]['key']) {
                if ($value === 'NULL') {
                    return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
                        'key value '.$name.' may not be NULL', __FUNCTION__);
                }
                $condition[] = $this->quoteIdentifier($name, true) . '=' . $value;
            }
        }
        if (empty($condition)) {
            return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
                'not specified which fields are keys', __FUNCTION__);
        }

        $result = null;
        $in_transaction = $this->in_transaction;
        if (!$in_transaction && PEAR::isError($result = $this->beginTransaction())) {
            return $result;
        }

        $connection = $this->getConnection();
        if (PEAR::isError($connection)) {
            return $connection;
        }

        $condition = ' WHERE '.implode(' AND ', $condition);
        $query = 'DELETE FROM ' . $this->quoteIdentifier($table, true) . $condition;
        $result =$this->_doQuery($query, true, $connection);
Robin Appelman's avatar
Robin Appelman committed
        if (!PEAR::isError($result)) {
            $affected_rows = $this->_affectedRows($connection, $result);
            $insert = '';
            foreach ($values as $key => $value) {
                $insert .= ($insert?', ':'') . $this->quoteIdentifier($key, true);
            }
            $values = implode(', ', $values);
            $query = 'INSERT INTO '. $this->quoteIdentifier($table, true) . "($insert) VALUES ($values)";
            $result =$this->_doQuery($query, true, $connection);
Robin Appelman's avatar
Robin Appelman committed
            if (!PEAR::isError($result)) {
                $affected_rows += $this->_affectedRows($connection, $result);;
            }
        }

        if (!$in_transaction) {
            if (PEAR::isError($result)) {
                $this->rollback();
            } else {
                $result = $this->commit();
            }
        }

        if (PEAR::isError($result)) {
            return $result;
        }

        return $affected_rows;
    }

    // }}}
    // {{{ function &prepare($query, $types = null, $result_types = null, $lobs = array())

    /**
     * Prepares a query for multiple execution with execute().
     * With some database backends, this is emulated.
     * prepare() requires a generic query as string like
     * 'INSERT INTO numbers VALUES(?,?)' or
     * 'INSERT INTO numbers VALUES(:foo,:bar)'.
     * The ? and :name and are placeholders which can be set using
     * bindParam() and the query can be sent off using the execute() method.
     * The allowed format for :name can be set with the 'bindname_format' option.
     *
     * @param   string  the query to prepare
     * @param   mixed   array that contains the types of the placeholders
     * @param   mixed   array that contains the types of the columns in
     *                        the result set or MDB2_PREPARE_RESULT, if set to
     *                        MDB2_PREPARE_MANIP the query is handled as a manipulation query
     * @param   mixed   key (field) value (parameter) pair for all lob placeholders
     *
     * @return  mixed   resource handle for the prepared query on success, 
     *                  a MDB2 error on failure
     *
     * @access  public
     * @see     bindParam, execute
     */
    function &prepare($query, $types = null, $result_types = null, $lobs = array())
    {
        $is_manip = ($result_types === MDB2_PREPARE_MANIP);
        $offset = $this->offset;
        $limit = $this->limit;
        $this->offset = $this->limit = 0;
        $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));
        if ($result) {
            if (PEAR::isError($result)) {
                return $result;
            }
            $query = $result;
        }
        $placeholder_type_guess = $placeholder_type = null;
        $question  = '?';
        $colon     = ':';
        $positions = array();
        $position  = 0;
        while ($position < strlen($query)) {
            $q_position = strpos($query, $question, $position);
            $c_position = strpos($query, $colon, $position);
            if ($q_position && $c_position) {
                $p_position = min($q_position, $c_position);
            } elseif ($q_position) {
                $p_position = $q_position;
            } elseif ($c_position) {
                $p_position = $c_position;
            } else {
                break;
            }
            if (is_null($placeholder_type)) {
                $placeholder_type_guess = $query[$p_position];
            }

            $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);
            if (PEAR::isError($new_pos)) {
                return $new_pos;
            }
            if ($new_pos != $position) {
                $position = $new_pos;
                continue; //evaluate again starting from the new position
            }

            if ($query[$position] == $placeholder_type_guess) {
                if (is_null($placeholder_type)) {
                    $placeholder_type = $query[$p_position];
                    $question = $colon = $placeholder_type;
                    if (!empty($types) && is_array($types)) {
                        if ($placeholder_type == ':') {
                            if (is_int(key($types))) {
                                $types_tmp = $types;
                                $types = array();
                                $count = -1;
                            }
                        } else {
                            $types = array_values($types);
                        }
                    }
                }
                if ($placeholder_type == ':') {
                    $regexp = '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
                    $parameter = preg_replace($regexp, '\\1', $query);
                    if ($parameter === '') {
                        $err =$this->raiseError(MDB2_ERROR_SYNTAX, null, null,