irm-1.6-b3/MDB/mysql.php
/**
     * 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 REPLACE query just updates its values instead of
     * inserting a new row.
     *
     * The REPLACE type of query does not make part of the SQL standards. Since
     * practically only MySQL implements it natively, this type of query is
     * emulated through this method for other DBMS using standard types of
     * queries inside a transaction to assure the atomicity of the operation.
     *
     * @access public
     *
     * @param string $table name of the table on which the REPLACE query will
     *  be executed.
     * @param array $fields 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 Metabase
     *          are supported except for clob and blob.
     *
     *    Default: no type conversion
     *
     *    Null
     *          Boolean 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
     *          Boolean 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 MDB_OK on success, a MDB error on failure
     */
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 REPLACE query just updates its values instead of
inserting a new row.

The REPLACE type of query does not make part of the SQL standards. Since
practically only MySQL implements it natively, this type of query is
emulated through this method for other DBMS using standard types of
queries inside a transaction to assure the atomicity of the operation.

@access public

@param string $table name of the table on which the REPLACE query will
be executed.
@param array $fields 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 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.

this property is required unless the Null property
is set to 1.

Type
Name of the type of the field. Currently, all types Metabase
are supported except for clob and blob.

no type conversion

Null
Boolean 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.

0

Key
Boolean 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.

0

@return mixed MDB_OK on success, a MDB error on failure
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 REPLACE query just updates its values instead of inserting a new row . The REPLACE type of query does not make part of the SQL standards . Since practically only MySQL implements it natively this type of query is emulated through this method for other DBMS using standard types of queries inside a transaction to assure the atomicity of the operation . @access public @param string $table name of the table on which the REPLACE query will be executed . @param array $fields 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 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 . this property is required unless the Null property is set to 1 . Type Name of the type of the field . Currently all types Metabase are supported except for clob and blob . no type conversion Null Boolean 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 . 0 Key Boolean 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 . 0 @return mixed MDB_OK on success a MDB error on failure
function replace($table, $fields)
    {
        $count = count($fields);
        for($keys = 0, $query = $values = '',reset($fields), $field = 0;
            $field<$count;
            next($fields), $field++)
        {
            $name = key($fields);
            if ($field > 0) {
                $query .= ',';
                $values .= ',';
            }
            $query .= $name;
            if (isset($fields[$name]['Null']) && $fields[$name]['Null']) {
                $value = 'NULL';
            } else {
                if(isset($fields[$name]['Type'])) {
                    switch ($fields[$name]['Type']) {
                        case 'text':
                            $value = $this->getTextValue($fields[$name]['Value']);
                            break;
                        case 'boolean':
                            $value = $this->getBooleanValue($fields[$name]['Value']);
                            break;
                        case 'integer':
                            $value = $this->getIntegerValue($fields[$name]['Value']);
                            break;
                        case 'decimal':
                            $value = $this->getDecimalValue($fields[$name]['Value']);
                            break;
                        case 'float':
                            $value = $this->getFloatValue($fields[$name]['Value']);
                            break;
                        case 'date':
                            $value = $this->getDateValue($fields[$name]['Value']);
                            break;
                        case 'time':
                            $value = $this->getTimeValue($fields[$name]['Value']);
                            break;
                        case 'timestamp':
                            $value = $this->getTimestampValue($fields[$name]['Value']);
                            break;
                        default:
                            return($this->raiseError(MDB_ERROR_CANNOT_REPLACE, NULL, NULL,
                                'no supported type for field "' . $name . '" specified'));
                    }
                } else {
                    $value = $fields[$name]['Value'];
                }
            }
            $values .= $value;
            if (isset($fields[$name]['Key']) && $fields[$name]['Key']) {
                if ($value === 'NULL') {
                    return($this->raiseError(MDB_ERROR_CANNOT_REPLACE, NULL, NULL,
                        $name.': key values may not be NULL'));
                }
                $keys++;
            }
        }
        if ($keys == 0) {
            return($this->raiseError(MDB_ERROR_CANNOT_REPLACE, NULL, NULL,
                'not specified which fields are keys'));
        }
        return($this->query("REPLACE INTO $table ($query) VALUES ($values)"));
    }
function replace ( $ table , $ fields ) { $ count = count ( $ fields ) ; for ( $ keys = 0 , $ query = $ values = '' , reset ( $ fields ) , $ field = 0 ; $ field < $ count ; next ( $ fields ) , $ field ++ ) { $ name = key ( $ fields ) ; if ( $ field > 0 ) { $ query .= ',' ; $ values .= ',' ; } $ query .= $ name ; if ( isset ( $ fields [ $ name ] [ 'Null' ] ) && $ fields [ $ name ] [ 'Null' ] ) { $ value = 'NULL' ; } else { if ( isset ( $ fields [ $ name ] [ 'Type' ] ) ) { switch ( $ fields [ $ name ] [ 'Type' ] ) { case 'text' : $ value = $ this -> getTextValue ( $ fields [ $ name ] [ 'Value' ] ) ; break ; case 'boolean' : $ value = $ this -> getBooleanValue ( $ fields [ $ name ] [ 'Value' ] ) ; break ; case 'integer' : $ value = $ this -> getIntegerValue ( $ fields [ $ name ] [ 'Value' ] ) ; break ; case 'decimal' : $ value = $ this -> getDecimalValue ( $ fields [ $ name ] [ 'Value' ] ) ; break ; case 'float' : $ value = $ this -> getFloatValue ( $ fields [ $ name ] [ 'Value' ] ) ; break ; case 'date' : $ value = $ this -> getDateValue ( $ fields [ $ name ] [ 'Value' ] ) ; break ; case 'time' : $ value = $ this -> getTimeValue ( $ fields [ $ name ] [ 'Value' ] ) ; break ; case 'timestamp' : $ value = $ this -> getTimestampValue ( $ fields [ $ name ] [ 'Value' ] ) ; break ; default : return ( $ this -> raiseError ( MDB_ERROR_CANNOT_REPLACE , NULL , NULL , 'no supported type for field "' . $ name . '" specified' ) ) ; } } else { $ value = $ fields [ $ name ] [ 'Value' ] ; } } $ values .= $ value ; if ( isset ( $ fields [ $ name ] [ 'Key' ] ) && $ fields [ $ name ] [ 'Key' ] ) { if ( $ value === 'NULL' ) { return ( $ this -> raiseError ( MDB_ERROR_CANNOT_REPLACE , NULL , NULL , $ name . ': key values may not be NULL' ) ) ; } $ keys ++ ; } } if ( $ keys == 0 ) { return ( $ this -> raiseError ( MDB_ERROR_CANNOT_REPLACE , NULL , NULL , 'not specified which fields are keys' ) ) ; } return ( $ this -> query ( " REPLACE INTO  $ table  ( $ query ) VALUES ( $ values ) " ) ) ; }
Execute a SQL REPLACE query.
Execute a SQL REPLACE query .