Skip to content
Snippets Groups Projects
MDB2.php 140 KiB
Newer Older
Robin Appelman's avatar
Robin Appelman committed
                            'named parameter name must match "bindname_format" option', __FUNCTION__);
                        return $err;
                    }
                    $positions[$p_position] = $parameter;
                    $query = substr_replace($query, '?', $position, strlen($parameter)+1);
                    // use parameter name in type array
                    if (isset($count) && isset($types_tmp[++$count])) {
                        $types[$parameter] = $types_tmp[$count];
                    }
                } else {
                    $positions[$p_position] = count($positions);
                }
                $position = $p_position + 1;
            } else {
                $position = $p_position;
            }
        }
        $class_name = 'MDB2_Statement_'.$this->phptype;
        $statement = null;
        $obj = new $class_name($this, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
        $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
        return $obj;
    }

    // }}}
    // {{{ function _skipDelimitedStrings($query, $position, $p_position)
    
    /**
     * Utility method, used by prepare() to avoid replacing placeholders within delimited strings.
     * Check if the placeholder is contained within a delimited string.
     * If so, skip it and advance the position, otherwise return the current position,
     * which is valid
     *
     * @param string $query
     * @param integer $position current string cursor position
     * @param integer $p_position placeholder position
     *
     * @return mixed integer $new_position on success
     *               MDB2_Error on failure
     *
     * @access  protected
     */
    function _skipDelimitedStrings($query, $position, $p_position)
    {
        $ignores = $this->string_quoting;
        $ignores[] = $this->identifier_quoting;
        $ignores = array_merge($ignores, $this->sql_comments);
        
        foreach ($ignores as $ignore) {
            if (!empty($ignore['start'])) {
                if (is_int($start_quote = strpos($query, $ignore['start'], $position)) && $start_quote < $p_position) {
                    $end_quote = $start_quote;
                    do {
                        if (!is_int($end_quote = strpos($query, $ignore['end'], $end_quote + 1))) {
                            if ($ignore['end'] === "\n") {
                                $end_quote = strlen($query) - 1;
                            } else {
                                $err =$this->raiseError(MDB2_ERROR_SYNTAX, null, null,
Robin Appelman's avatar
Robin Appelman committed
3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
                                    'query with an unterminated text string specified', __FUNCTION__);
                                return $err;
                            }
                        }
                    } while ($ignore['escape']
                        && $end_quote-1 != $start_quote
                        && $query[($end_quote - 1)] == $ignore['escape']
                        && (   $ignore['escape_pattern'] !== $ignore['escape']
                            || $query[($end_quote - 2)] != $ignore['escape'])
                    );

                    $position = $end_quote + 1;
                    return $position;
                }
            }
        }
        return $position;
    }
    
    // }}}
    // {{{ function quote($value, $type = null, $quote = true)

    /**
     * Convert a text value into a DBMS specific format that is suitable to
     * compose query statements.
     *
     * @param   string  text string value that is intended to be converted.
     * @param   string  type to which the value should be converted to
     * @param   bool    quote
     * @param   bool    escape wildcards
     *
     * @return  string  text string that represents the given argument value in
     *       a DBMS specific format.
     *
     * @access  public
     */
    function quote($value, $type = null, $quote = true, $escape_wildcards = false)
    {
        $result = $this->loadModule('Datatype', null, true);
        if (PEAR::isError($result)) {
            return $result;
        }

        return $this->datatype->quote($value, $type, $quote, $escape_wildcards);
    }

    // }}}
    // {{{ function getDeclaration($type, $name, $field)

    /**
     * Obtain DBMS specific SQL code portion needed to declare
     * of the given type
     *
     * @param   string  type to which the value should be converted to
     * @param   string  name the field to be declared.
     * @param   string  definition of the field
     *
     * @return  string  DBMS specific SQL code portion that should be used to
     *                 declare the specified field.
     *
     * @access  public
     */
    function getDeclaration($type, $name, $field)
    {
        $result = $this->loadModule('Datatype', null, true);
        if (PEAR::isError($result)) {
            return $result;
        }
        return $this->datatype->getDeclaration($type, $name, $field);
    }

    // }}}
    // {{{ function compareDefinition($current, $previous)

    /**
     * Obtain an array of changes that may need to applied
     *
     * @param   array   new definition
     * @param   array   old definition
     *
     * @return  array   containing all changes that will need to be applied
     *
     * @access  public
     */
    function compareDefinition($current, $previous)
    {
        $result = $this->loadModule('Datatype', null, true);
        if (PEAR::isError($result)) {
            return $result;
        }
        return $this->datatype->compareDefinition($current, $previous);
    }

    // }}}
    // {{{ function supports($feature)

    /**
     * Tell whether a DB implementation or its backend extension
     * supports a given feature.
     *
     * @param   string  name of the feature (see the MDB2 class doc)
     *
     * @return  bool|string if this DB implementation supports a given feature
     *                      false means no, true means native,
     *                      'emulated' means emulated
     *
     * @access  public
     */
    function supports($feature)
    {
        if (array_key_exists($feature, $this->supported)) {
            return $this->supported[$feature];
        }
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            "unknown support feature $feature", __FUNCTION__);
    }

    // }}}
    // {{{ function getSequenceName($sqn)

    /**
     * adds sequence name formatting to a sequence name
     *
     * @param   string  name of the sequence
     *
     * @return  string  formatted sequence name
     *
     * @access  public
     */
    function getSequenceName($sqn)
    {
        return sprintf($this->options['seqname_format'],
            preg_replace('/[^a-z0-9_\-\$.]/i', '_', $sqn));
    }

    // }}}
    // {{{ function getIndexName($idx)

    /**
     * adds index name formatting to a index name
     *
     * @param   string  name of the index
     *
     * @return  string  formatted index name
     *
     * @access  public
     */
    function getIndexName($idx)
    {
        return sprintf($this->options['idxname_format'],
            preg_replace('/[^a-z0-9_\-\$.]/i', '_', $idx));
    }

    // }}}
    // {{{ function nextID($seq_name, $ondemand = true)

    /**
     * Returns the next free id of a sequence
     *
     * @param   string  name of the sequence
     * @param   bool    when true missing sequences are automatic created
     *
     * @return  mixed   MDB2 Error Object or id
     *
     * @access  public
     */
    function nextID($seq_name, $ondemand = true)
    {
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ function lastInsertID($table = null, $field = null)

    /**
     * Returns the autoincrement ID if supported or $id or fetches the current
     * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
     *
     * @param   string  name of the table into which a new row was inserted
     * @param   string  name of the field into which a new row was inserted
     *
     * @return  mixed   MDB2 Error Object or id
     *
     * @access  public
     */
    function lastInsertID($table = null, $field = null)
    {
        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ function currID($seq_name)

    /**
     * Returns the current id of a sequence
     *
     * @param   string  name of the sequence
     *
     * @return  mixed   MDB2 Error Object or id
     *
     * @access  public
     */
    function currID($seq_name)
    {
        $this->warnings[] = 'database does not support getting current
            sequence value, the sequence value was incremented';
        return $this->nextID($seq_name);
    }

    // }}}
    // {{{ function queryOne($query, $type = null, $colnum = 0)

    /**
     * Execute the specified query, fetch the value from the first column of
     * the first row of the result set and then frees
     * the result set.
     *
     * @param string $query  the SELECT query statement to be executed.
     * @param string $type   optional argument that specifies the expected
     *                       datatype of the result set field, so that an eventual
     *                       conversion may be performed. The default datatype is
     *                       text, meaning that no conversion is performed
     * @param mixed  $colnum the column number (or name) to fetch
     *
     * @return  mixed   MDB2_OK or field value on success, a MDB2 error on failure
     *
     * @access  public
     */
    function queryOne($query, $type = null, $colnum = 0)
    {
        $result = $this->query($query, $type);
        if (!MDB2::isResultCommon($result)) {
            return $result;
        }

        $one = $result->fetchOne($colnum);
        $result->free();
        return $one;
    }

    // }}}
    // {{{ function queryRow($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)

    /**
     * Execute the specified query, fetch the values from the first
     * row of the result set into an array and then frees
     * the result set.
     *
     * @param   string  the SELECT query statement to be executed.
     * @param   array   optional array argument that specifies a list of
     *       expected datatypes of the result set columns, so that the eventual
     *       conversions may be performed. The default list of datatypes is
     *       empty, meaning that no conversion is performed.
     * @param   int     how the array data should be indexed
     *
     * @return  mixed   MDB2_OK or data array on success, a MDB2 error on failure
     *
     * @access  public
     */
    function queryRow($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)
    {
        $result = $this->query($query, $types);
        if (!MDB2::isResultCommon($result)) {
            return $result;
        }

        $row = $result->fetchRow($fetchmode);
        $result->free();
        return $row;
    }

    // }}}
    // {{{ function queryCol($query, $type = null, $colnum = 0)

    /**
     * Execute the specified query, fetch the value from the first column of
     * each row of the result set into an array and then frees the result set.
     *
     * @param string $query  the SELECT query statement to be executed.
     * @param string $type   optional argument that specifies the expected
     *                       datatype of the result set field, so that an eventual
     *                       conversion may be performed. The default datatype is text,
     *                       meaning that no conversion is performed
     * @param mixed  $colnum the column number (or name) to fetch
     *
     * @return  mixed   MDB2_OK or data array on success, a MDB2 error on failure
     * @access  public
     */
    function queryCol($query, $type = null, $colnum = 0)
    {
        $result = $this->query($query, $type);
        if (!MDB2::isResultCommon($result)) {
            return $result;
        }

        $col = $result->fetchCol($colnum);
        $result->free();
        return $col;
    }

    // }}}
    // {{{ function queryAll($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false, $force_array = false, $group = false)

    /**
     * Execute the specified query, fetch all the rows of the result set into
     * a two dimensional array and then frees the result set.
     *
     * @param   string  the SELECT query statement to be executed.
     * @param   array   optional array argument that specifies a list of
     *       expected datatypes of the result set columns, so that the eventual
     *       conversions may be performed. The default list of datatypes is
     *       empty, meaning that no conversion is performed.
     * @param   int     how the array data should be indexed
     * @param   bool    if set to true, the $all will have the first
     *       column as its first dimension
     * @param   bool    used only when the query returns exactly
     *       two columns. If true, the values of the returned array will be
     *       one-element arrays instead of scalars.
     * @param   bool    if true, the values of the returned array is
     *       wrapped in another array.  If the same key value (in the first
     *       column) repeats itself, the values will be appended to this array
     *       instead of overwriting the existing values.
     *
     * @return  mixed   MDB2_OK or data array on success, a MDB2 error on failure
     *
     * @access  public
     */
    function queryAll($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT,
        $rekey = false, $force_array = false, $group = false)
    {
        $result = $this->query($query, $types);
        if (!MDB2::isResultCommon($result)) {
            return $result;
        }

        $all = $result->fetchAll($fetchmode, $rekey, $force_array, $group);
        $result->free();
        return $all;
    }

    // }}}
}

// }}}
// {{{ class MDB2_Result

/**
 * The dummy class that all user space result classes should extend from
 *
 * @package     MDB2
 * @category    Database
 * @author      Lukas Smith <smith@pooteeweet.org>
 */
class MDB2_Result
{
}

// }}}
// {{{ class MDB2_Result_Common extends MDB2_Result

/**
 * The common result class for MDB2 result objects
 *
 * @package     MDB2
 * @category    Database
 * @author      Lukas Smith <smith@pooteeweet.org>
 */
class MDB2_Result_Common extends MDB2_Result
{
    // {{{ Variables (Properties)

    var $db;
    var $result;
    var $rownum = -1;
    var $types = array();
    var $values = array();
    var $offset;
    var $offset_count = 0;
    var $limit;
    var $column_names;

    // }}}
    // {{{ constructor: function __construct(&$db, &$result, $limit = 0, $offset = 0)

    /**
     * Constructor
     */
    function __construct(&$db, &$result, $limit = 0, $offset = 0)
    {
        $this->db =$db;
        $this->result =$result;
Robin Appelman's avatar
Robin Appelman committed
        $this->offset = $offset;
        $this->limit = max(0, $limit - 1);
    }


    // }}}
    // {{{ function setResultTypes($types)

    /**
     * Define the list of types to be associated with the columns of a given
     * result set.
     *
     * This function may be called before invoking fetchRow(), fetchOne(),
     * fetchCol() and fetchAll() so that the necessary data type
     * conversions are performed on the data to be retrieved by them. If this
     * function is not called, the type of all result set columns is assumed
     * to be text, thus leading to not perform any conversions.
     *
     * @param   array   variable that lists the
     *       data types to be expected in the result set columns. If this array
     *       contains less types than the number of columns that are returned
     *       in the result set, the remaining columns are assumed to be of the
     *       type text. Currently, the types clob and blob are not fully
     *       supported.
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function setResultTypes($types)
    {
        $load = $this->db->loadModule('Datatype', null, true);
        if (PEAR::isError($load)) {
            return $load;
        }
        $types = $this->db->datatype->checkResultTypes($types);
        if (PEAR::isError($types)) {
            return $types;
        }
        $this->types = $types;
        return MDB2_OK;
    }

    // }}}
    // {{{ function seek($rownum = 0)

    /**
     * Seek to a specific row in a result set
     *
     * @param   int     number of the row where the data can be found
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function seek($rownum = 0)
    {
        $target_rownum = $rownum - 1;
        if ($this->rownum > $target_rownum) {
            return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
                'seeking to previous rows not implemented', __FUNCTION__);
        }
        while ($this->rownum < $target_rownum) {
            $this->fetchRow();
        }
        return MDB2_OK;
    }

    // }}}
    // {{{ function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)

    /**
     * Fetch and return a row of data
     *
     * @param   int     how the array data should be indexed
     * @param   int     number of the row where the data can be found
     *
     * @return  int     data array on success, a MDB2 error on failure
     *
     * @access  public
     */
    function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
    {
        $err =$this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
Robin Appelman's avatar
Robin Appelman committed
3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839
            'method not implemented', __FUNCTION__);
        return $err;
    }

    // }}}
    // {{{ function fetchOne($colnum = 0)

    /**
     * fetch single column from the next row from a result set
     *
     * @param int|string the column number (or name) to fetch
     * @param int        number of the row where the data can be found
     *
     * @return string data on success, a MDB2 error on failure
     * @access  public
     */
    function fetchOne($colnum = 0, $rownum = null)
    {
        $fetchmode = is_numeric($colnum) ? MDB2_FETCHMODE_ORDERED : MDB2_FETCHMODE_ASSOC;
        $row = $this->fetchRow($fetchmode, $rownum);
        if (!is_array($row) || PEAR::isError($row)) {
            return $row;
        }
        if (!array_key_exists($colnum, $row)) {
            return $this->db->raiseError(MDB2_ERROR_TRUNCATED, null, null,
                'column is not defined in the result set: '.$colnum, __FUNCTION__);
        }
        return $row[$colnum];
    }

    // }}}
    // {{{ function fetchCol($colnum = 0)

    /**
     * Fetch and return a column from the current row pointer position
     *
     * @param int|string the column number (or name) to fetch
     *
     * @return  mixed data array on success, a MDB2 error on failure
     * @access  public
     */
    function fetchCol($colnum = 0)
    {
        $column = array();
        $fetchmode = is_numeric($colnum) ? MDB2_FETCHMODE_ORDERED : MDB2_FETCHMODE_ASSOC;
        $row = $this->fetchRow($fetchmode);
        if (is_array($row)) {
            if (!array_key_exists($colnum, $row)) {
                return $this->db->raiseError(MDB2_ERROR_TRUNCATED, null, null,
                    'column is not defined in the result set: '.$colnum, __FUNCTION__);
            }
            do {
                $column[] = $row[$colnum];
            } while (is_array($row = $this->fetchRow($fetchmode)));
        }
        if (PEAR::isError($row)) {
            return $row;
        }
        return $column;
    }

    // }}}
    // {{{ function fetchAll($fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false, $force_array = false, $group = false)

    /**
     * Fetch and return all rows from the current row pointer position
     *
     * @param   int     $fetchmode  the fetch mode to use:
     *                            + MDB2_FETCHMODE_ORDERED
     *                            + MDB2_FETCHMODE_ASSOC
     *                            + MDB2_FETCHMODE_ORDERED | MDB2_FETCHMODE_FLIPPED
     *                            + MDB2_FETCHMODE_ASSOC | MDB2_FETCHMODE_FLIPPED
     * @param   bool    if set to true, the $all will have the first
     *       column as its first dimension
     * @param   bool    used only when the query returns exactly
     *       two columns. If true, the values of the returned array will be
     *       one-element arrays instead of scalars.
     * @param   bool    if true, the values of the returned array is
     *       wrapped in another array.  If the same key value (in the first
     *       column) repeats itself, the values will be appended to this array
     *       instead of overwriting the existing values.
     *
     * @return  mixed   data array on success, a MDB2 error on failure
     *
     * @access  public
     * @see     getAssoc()
     */
    function fetchAll($fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false,
        $force_array = false, $group = false)
    {
        $all = array();
        $row = $this->fetchRow($fetchmode);
        if (PEAR::isError($row)) {
            return $row;
        } elseif (!$row) {
            return $all;
        }

        $shift_array = $rekey ? false : null;
        if (!is_null($shift_array)) {
            if (is_object($row)) {
                $colnum = count(get_object_vars($row));
            } else {
                $colnum = count($row);
            }
            if ($colnum < 2) {
                return $this->db->raiseError(MDB2_ERROR_TRUNCATED, null, null,
                    'rekey feature requires atleast 2 column', __FUNCTION__);
            }
            $shift_array = (!$force_array && $colnum == 2);
        }

        if ($rekey) {
            do {
                if (is_object($row)) {
                    $arr = get_object_vars($row);
                    $key = reset($arr);
                    unset($row->{$key});
                } else {
                    if ($fetchmode & MDB2_FETCHMODE_ASSOC) {
                        $key = reset($row);
                        unset($row[key($row)]);
                    } else {
                        $key = array_shift($row);
                    }
                    if ($shift_array) {
                        $row = array_shift($row);
                    }
                }
                if ($group) {
                    $all[$key][] = $row;
                } else {
                    $all[$key] = $row;
                }
            } while (($row = $this->fetchRow($fetchmode)));
        } elseif ($fetchmode & MDB2_FETCHMODE_FLIPPED) {
            do {
                foreach ($row as $key => $val) {
                    $all[$key][] = $val;
                }
            } while (($row = $this->fetchRow($fetchmode)));
        } else {
            do {
                $all[] = $row;
            } while (($row = $this->fetchRow($fetchmode)));
        }

        return $all;
    }

    // }}}
    // {{{ function rowCount()
    /**
     * Returns the actual row number that was last fetched (count from 0)
     * @return  int
     *
     * @access  public
     */
    function rowCount()
    {
        return $this->rownum + 1;
    }

    // }}}
    // {{{ function numRows()

    /**
     * Returns the number of rows in a result object
     *
     * @return  mixed   MDB2 Error Object or the number of rows
     *
     * @access  public
     */
    function numRows()
    {
        return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ function nextResult()

    /**
     * Move the internal result pointer to the next available result
     *
     * @return  true on success, false if there is no more result set or an error object on failure
     *
     * @access  public
     */
    function nextResult()
    {
        return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ function getColumnNames()

    /**
     * Retrieve the names of columns returned by the DBMS in a query result or
     * from the cache.
     *
     * @param   bool    If set to true the values are the column names,
     *                  otherwise the names of the columns are the keys.
     * @return  mixed   Array variable that holds the names of columns or an
     *                  MDB2 error on failure.
     *                  Some DBMS may not return any columns when the result set
     *                  does not contain any rows.
     *
     * @access  public
     */
    function getColumnNames($flip = false)
    {
        if (!isset($this->column_names)) {
            $result = $this->_getColumnNames();
            if (PEAR::isError($result)) {
                return $result;
            }
            $this->column_names = $result;
        }
        if ($flip) {
            return array_flip($this->column_names);
        }
        return $this->column_names;
    }

    // }}}
    // {{{ function _getColumnNames()

    /**
     * Retrieve the names of columns returned by the DBMS in a query result.
     *
     * @return  mixed   Array variable that holds the names of columns as keys
     *                  or an MDB2 error on failure.
     *                  Some DBMS may not return any columns when the result set
     *                  does not contain any rows.
     *
     * @access  private
     */
    function _getColumnNames()
    {
        return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ function numCols()

    /**
     * Count the number of columns returned by the DBMS in a query result.
     *
     * @return  mixed   integer value with the number of columns, a MDB2 error
     *       on failure
     *
     * @access  public
     */
    function numCols()
    {
        return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    // }}}
    // {{{ function getResource()

    /**
     * return the resource associated with the result object
     *
     * @return  resource
     *
     * @access  public
     */
    function getResource()
    {
        return $this->result;
    }

    // }}}
    // {{{ function bindColumn($column, &$value, $type = null)

    /**
     * Set bind variable to a column.
     *
     * @param   int     column number or name
     * @param   mixed   variable reference
     * @param   string  specifies the type of the field
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  public
     */
    function bindColumn($column, &$value, $type = null)
    {
        if (!is_numeric($column)) {
            $column_names = $this->getColumnNames();
            if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
                if ($this->db->options['field_case'] == CASE_LOWER) {
                    $column = strtolower($column);
                } else {
                    $column = strtoupper($column);
                }
            }
            $column = $column_names[$column];
        }
        $this->values[$column] =$value;
Robin Appelman's avatar
Robin Appelman committed
        if (!is_null($type)) {
            $this->types[$column] = $type;
        }
        return MDB2_OK;
    }

    // }}}
    // {{{ function _assignBindColumns($row)

    /**
     * Bind a variable to a value in the result row.
     *
     * @param   array   row data
     *
     * @return  mixed   MDB2_OK on success, a MDB2 error on failure
     *
     * @access  private
     */
    function _assignBindColumns($row)
    {
        $row = array_values($row);
        foreach ($row as $column => $value) {
            if (array_key_exists($column, $this->values)) {
                $this->values[$column] = $value;
            }
        }
        return MDB2_OK;
    }

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

    /**
     * Free the internal resources associated with result.
     *
     * @return  bool    true on success, false if result is invalid
     *
     * @access  public
     */
    function free()
    {
        $this->result = false;
        return MDB2_OK;
    }

    // }}}
}

// }}}
// {{{ class MDB2_Row

/**
 * The simple class that accepts row data as an array
 *
 * @package     MDB2
 * @category    Database
 * @author      Lukas Smith <smith@pooteeweet.org>
 */
class MDB2_Row
{
    // {{{ constructor: function __construct(&$row)

    /**
     * constructor
     *
     * @param   resource    row data as array
     */
    function __construct(&$row)
    {
        foreach ($row as $key => $value) {
            $this->$key = &$row[$key];
        }
    }
}

// }}}
// {{{ class MDB2_Statement_Common

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

    var $db;
    var $statement;
    var $query;
    var $result_types;
    var $types;
    var $values = array();
    var $limit;
    var $offset;
    var $is_manip;

    // }}}
    // {{{ constructor: function __construct(&$db, &$statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)

    /**
     * Constructor
     */
    function __construct(&$db, &$statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
    {
        $this->db =$db;
        $this->statement =$statement;
Robin Appelman's avatar
Robin Appelman committed
        $this->positions = $positions;
        $this->query = $query;
        $this->types = (array)$types;
        $this->result_types = (array)$result_types;
        $this->limit = $limit;
        $this->is_manip = $is_manip;
        $this->offset = $offset;
    }


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

    /**
     * Set the value of 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   value that is meant to be assigned 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 bindValue($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;
        if (!is_null($type)) {
            $this->types[$parameter] = $type;
        }
        return MDB2_OK;
    }

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

    /**
     * Set the values of multiple a parameter of a prepared query in bulk.
     *
     * @param   array   specifies all necessary information
     *       for bindValue() the array elements must use keys corresponding to
     *       the number of the position of the parameter.