You are here

function _eloqua_db_insert_update in Eloqua 7.2

Same name and namespace in other branches
  1. 7 eloqua_webform/eloqua_webform.inc \_eloqua_db_insert_update()

Query the Database and mimic a INSERT ... ON DUPLICATE KEY UPDATE.

Because there isn't a "Drupal" way to do an INSERT ... ON DUPLICATE KEY UPDATE using the abstraction layer they provided.

Also, when using InnoDB tables, you cannot actually retrieve the last-insert-id reliably. So, all in all, this just returns a TRUE/FALSE now, as opposed to the Drupal 6 Version.

Parameters

array $index: The node ID to update.

array $fields: The fields to update.

Return value

bool The result of the update.

1 call to _eloqua_db_insert_update()
eloqua_webform_create in eloqua_webform/eloqua_webform.inc
Creates webform settings from the database.

File

eloqua_webform/eloqua_webform.inc, line 414
Eloqua Helper functions and constants

Code

function _eloqua_db_insert_update($index, $fields) {
  $insert_fields = $index + $fields;
  $do_update = FALSE;
  $result = FALSE;
  try {
    $result = db_insert('eloqua_webform')
      ->fields($insert_fields)
      ->execute();

    // No longer able to return the last inserted ID.
    if ($result !== FALSE) {
      $result = TRUE;
    }
  } catch (PDOException $e) {

    // Constraint error.
    if ($e
      ->getCode() == '23000') {
      $do_update = TRUE;
    }
  }

  // Attempt update.
  if ($do_update) {
    $query = db_update('eloqua_webform')
      ->fields($fields);
    foreach ($index as $key => $value) {
      $query
        ->condition($key, $value);
    }
    try {
      $result = $query
        ->execute();
    } catch (PDOException $e) {
      return FALSE;
    }

    // Rows updated.
    if (is_numeric($result)) {
      $result = (bool) $result;
    }
    else {

      // Probably failed.
      $result = FALSE;
    }
  }
  return $result;
}