return (bool)$this->query( $sql, $fname );
}
- /**
- * INSERT ... ON DUPLICATE KEY UPDATE wrapper, inserts an array into a
- * table, optionally updating if duplicate primary key found
- *
- * $rows may be a single associative array, or an array of these with
- * numeric keys, for multi-row insert.
- *
- * Usually aborts on failure. If errors are explicitly ignored, returns success.
- *
- * @param $table String: table name (prefix auto-added)
- * @param $rows Array: Array of rows to insert
- * @param $fname String: Calling function name (use __METHOD__) for logs/profiling
- * @param $onDupeUpdate Array: Associative array of fields to update on duplicate
- *
- * @return bool
- */
- function insertOrUpdate( $table, $rows, $fname = 'DatabaseBase::insertOrUpdate', $onDupeUpdate = array() ) {
- if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
- $keys = array_keys( $rows[0] );
- } else {
- $keys = array_keys( $rows );
- }
-
- // Get what is only to be set if inserted
- $where = array_diff( $rows, $onDupeUpdate );
-
- $res = $this->select(
- $table,
- $keys,
- $where,
- __METHOD__
- );
-
- if ( $res ) {
- // Where there is a different value to set if this is being
- // "updated", use the $onDupeUpdate value for that to replace the
- // original option (if it was an insert), and replace the column
- // name with the value read from the existing row
- foreach ( $where as $key => $unused ) {
- if ( isset( $onDupeUpdate[$key] ) ) {
- $options[$key] = str_replace( $key, $res[0]->{$key}, $onDupeUpdate[$key] );
- }
- }
- } else {
- // No results, it's just an insert
- $update = $where;
- }
-
- return (bool)$this->replace(
- $table,
- $update,
- array(),
- __METHOD__
- );
- }
-
/**
* Make UPDATE options for the DatabaseBase::update function
*
return false;
}
- /**
- * INSERT ... ON DUPE UPDATE wrapper, inserts an array into a table, optionally updating if
- * duplicate primary key found
- *
- * $a may be a single associative array, or an array of these with numeric keys, for
- * multi-row insert.
- *
- * Usually aborts on failure
- * If errors are explicitly ignored, returns success
- *
- * @param $table String: table name (prefix auto-added)
- * @param $a Array: Array of rows to insert
- * @param $fname String: Calling function name (use __METHOD__) for logs/profiling
- * @param $onDupeUpdate Array: Associative array of fields to update on duplicate
- *
- * @return bool
- */
- function insertOrUpdate( $table, $a, $fname = 'DatabaseBase::insertOrUpdate', $onDupeUpdate = array() ) {
- # No rows to insert, easy just return now
- if ( !count( $a ) ) {
- return true;
- }
-
- $table = $this->tableName( $table );
-
- if ( isset( $a[0] ) && is_array( $a[0] ) ) {
- $multi = true;
- $keys = array_keys( $a[0] );
- } else {
- $multi = false;
- $keys = array_keys( $a );
- }
-
- $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
-
- if ( $multi ) {
- $first = true;
- foreach ( $a as $row ) {
- if ( $first ) {
- $first = false;
- } else {
- $sql .= ',';
- }
- $sql .= '(' . $this->makeList( $row ) . ')';
- }
- } else {
- $sql .= '(' . $this->makeList( $a ) . ')';
- }
-
- if ( count( $onDupeUpdate ) ) {
- $sql .= ' ON DUPLICATE KEY UPDATE ' . $this->makeList( $onDupeUpdate, LIST_SET );
- }
-
- return (bool)$this->query( $sql, $fname );
- }
-
function getServerVersion() {
return mysql_get_server_info( $this->mConn );
}