Skip to content
Snippets Groups Projects
MDB2.php 140 KiB
Newer Older
Robin Appelman's avatar
Robin Appelman committed
     * @param   array   specifies the types of the fields
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     * @see     bindParam()
     */
    function bindValueArray($values, $types = null)
    {
        $types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
        $parameters = array_keys($values);
        foreach ($parameters as $key => $parameter) {
            $this->db->pushErrorHandling(PEAR_ERROR_RETURN);
            $this->db->expectError(MDB2_ERROR_NOT_FOUND);
            $err = $this->bindValue($parameter, $values[$parameter], $types[$key]);
            $this->db->popExpect();
            $this->db->popErrorHandling();
            if (PEAR::isError($err)) {
                if ($err->getCode() == MDB2_ERROR_NOT_FOUND) {
                    //ignore (extra value for missing placeholder)
                    continue;
                }
                return $err;
            }
        }
        return MDB2_OK;
    }

    // }}}
    // {{{ function bindParam($parameter, &$value, $type = null)

    /**
     * Bind a variable to a parameter of a prepared query.
     *
     * @param   int     the order number of the parameter in the query
     *       statement. The order number of the first parameter is 1.
     * @param   mixed   variable that is meant to be bound to specified
     *       parameter. The type of the value depends on the $type argument.
     * @param   string  specifies the type of the field
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function bindParam($parameter, &$value, $type = null)
    {
        if (!is_numeric($parameter)) {
            $parameter = preg_replace('/^:(.*)$/', '\\1', $parameter);
        }
        if (!in_array($parameter, $this->positions)) {
            return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
        }
        $this->values[$parameter] =$value;
Robin Appelman's avatar
Robin Appelman committed
        if (!is_null($type)) {
            $this->types[$parameter] = $type;
        }
        return MDB2_OK;
    }

    // }}}
    // {{{ function bindParamArray(&$values, $types = null)

    /**
     * Bind the variables of multiple a parameter of a prepared query in bulk.
     *
     * @param   array   specifies all necessary information
     *       for bindParam() the array elements must use keys corresponding to
     *       the number of the position of the parameter.
     * @param   array   specifies the types of the fields
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     * @see     bindParam()
     */
    function bindParamArray(&$values, $types = null)
    {
        $types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
        $parameters = array_keys($values);
        foreach ($parameters as $key => $parameter) {
            $err = $this->bindParam($parameter, $values[$parameter], $types[$key]);
            if (PEAR::isError($err)) {
                return $err;
            }
        }
        return MDB2_OK;
    }

    // }}}
    // {{{ function &execute($values = null, $result_class = true, $result_wrap_class = false)

    /**
     * Execute a prepared query statement.
     *
     * @param array specifies all necessary information
     *              for bindParam() the array elements must use keys corresponding
     *              to the number of the position of the parameter.
     * @param mixed specifies which result class to use
     * @param mixed specifies which class to wrap results in
     *
     * @return mixed MDB2_Result or integer (affected rows) on success,
     *               a MDB2 error on failure
     * @access public
     */
    function &execute($values = null, $result_class = true, $result_wrap_class = false)
    {
        if (is_null($this->positions)) {
            return $this->db->raiseError(MDB2_ERROR, null, null,
                'Prepared statement has already been freed', __FUNCTION__);
        }

        $values = (array)$values;
        if (!empty($values)) {
            $err = $this->bindValueArray($values);
            if (PEAR::isError($err)) {
                return $this->db->raiseError(MDB2_ERROR, null, null,
                                            'Binding Values failed with message: ' . $err->getMessage(), __FUNCTION__);
            }
        }
        $result =$this->_execute($result_class, $result_wrap_class);
Robin Appelman's avatar
Robin Appelman committed
        return $result;
    }

    // }}}
    // {{{ function &_execute($result_class = true, $result_wrap_class = false)

    /**
     * Execute a prepared query statement helper method.
     *
     * @param   mixed   specifies which result class to use
     * @param   mixed   specifies which class to wrap results in
     *
     * @return mixed MDB2_Result or integer (affected rows) on success,
     *               a MDB2 error on failure
     * @access  private
     */
    function &_execute($result_class = true, $result_wrap_class = false)
    {
        $this->last_query = $this->query;
        $query = '';
        $last_position = 0;
        foreach ($this->positions as $current_position => $parameter) {
            if (!array_key_exists($parameter, $this->values)) {
                return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                    'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
            }
            $value = $this->values[$parameter];
            $query.= substr($this->query, $last_position, $current_position - $last_position);
            if (!isset($value)) {
                $value_quoted = 'NULL';
            } else {
                $type = !empty($this->types[$parameter]) ? $this->types[$parameter] : null;
                $value_quoted = $this->db->quote($value, $type);
                if (PEAR::isError($value_quoted)) {
                    return $value_quoted;
                }
            }
            $query.= $value_quoted;
            $last_position = $current_position + 1;
        }
        $query.= substr($this->query, $last_position);

        $this->db->offset = $this->offset;
        $this->db->limit = $this->limit;
        if ($this->is_manip) {
            $result = $this->db->exec($query);
        } else {
            $result =$this->db->query($query, $this->result_types, $result_class, $result_wrap_class);
Robin Appelman's avatar
Robin Appelman committed
        }
        return $result;
    }

    // }}}
    // {{{ function free()

    /**
     * Release resources allocated for the specified prepared query.
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function free()
    {
        if (is_null($this->positions)) {
            return $this->db->raiseError(MDB2_ERROR, null, null,
                'Prepared statement has already been freed', __FUNCTION__);
        }

        $this->statement = null;
        $this->positions = null;
        $this->query = null;
        $this->types = null;
        $this->result_types = null;
        $this->limit = null;
        $this->is_manip = null;
        $this->offset = null;
        $this->values = null;

        return MDB2_OK;
    }

    // }}}
}

// }}}
// {{{ class MDB2_Module_Common

/**
 * The common modules class for MDB2 module objects
 *
 * @package     MDB2
 * @category    Database
 * @author      Lukas Smith <smith@pooteeweet.org>
 */
class MDB2_Module_Common
{
    // {{{ Variables (Properties)

    /**
     * contains the key to the global MDB2 instance array of the associated
     * MDB2 instance
     *
     * @var     int
     * @access  protected
     */
    var $db_index;

    // }}}
    // {{{ constructor: function __construct($db_index)

    /**
     * Constructor
     */
    function __construct($db_index)
    {
        $this->db_index = $db_index;
    }

    // }}}
    // {{{ function &getDBInstance()

    /**
     * Get the instance of MDB2 associated with the module instance
     *
     * @return  object  MDB2 instance or a MDB2 error on failure
     *
     * @access  public
     */
    function getDBInstance()
Robin Appelman's avatar
Robin Appelman committed
    {
        if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
            $result =$GLOBALS['_MDB2_databases'][$this->db_index];
Robin Appelman's avatar
Robin Appelman committed
        } else {
            $result =$this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
Robin Appelman's avatar
Robin Appelman committed
                'could not find MDB2 instance');
        }
        return $result;
    }

    // }}}
}

// }}}
// {{{ function MDB2_closeOpenTransactions()

/**
 * Close any open transactions form persistent connections
 *
 * @return  void
 *
 * @access  public
 */

function MDB2_closeOpenTransactions()
{
    reset($GLOBALS['_MDB2_databases']);
    while (next($GLOBALS['_MDB2_databases'])) {
        $key = key($GLOBALS['_MDB2_databases']);
        if ($GLOBALS['_MDB2_databases'][$key]->opened_persistent
            && $GLOBALS['_MDB2_databases'][$key]->in_transaction
        ) {
            $GLOBALS['_MDB2_databases'][$key]->rollback();
        }
    }
}

// }}}
// {{{ function MDB2_defaultDebugOutput(&$db, $scope, $message, $is_manip = null)

/**
 * default debug output handler
 *
 * @param   object  reference to an MDB2 database object
 * @param   string  usually the method name that triggered the debug call:
 *                  for example 'query', 'prepare', 'execute', 'parameters',
 *                  'beginTransaction', 'commit', 'rollback'
 * @param   string  message that should be appended to the debug variable
 * @param   array   contains context information about the debug() call
 *                  common keys are: is_manip, time, result etc.
 *
 * @return  void|string optionally return a modified message, this allows
 *                      rewriting a query before being issued or prepared
 *
 * @access  public
 */
function MDB2_defaultDebugOutput(&$db, $scope, $message, $context = array())
{
    $db->debug_output.= $scope.'('.$db->db_index.'): ';
    $db->debug_output.= $message.$db->getOption('log_line_break');
    return $message;
}

// }}}
Frank Karlitschek's avatar
Frank Karlitschek committed
?>