function tripal_core_chado_execute_prepared

1.x tripal_core_chado.api.inc tripal_core_chado_execute_prepared($statement_name, $sql, $values)

Execute a prepared statement with the supplied values

Parameters

$statement_name: The name of the prepared statement

$sql: The SQL to execute using chado query. Should be of the form EXECUTE [statement_name] ([arg1],[arg2]...[argn])

$values: An array of values in the execute sql statement

3 calls to tripal_core_chado_execute_prepared()
tripal_core_chado_insert in tripal_core/api/tripal_core_chado.api.inc
Provides a generic routine for inserting into any Chado table
tripal_core_chado_select in tripal_core/api/tripal_core_chado.api.inc
Provides a generic routine for selecting data from a Chado table
tripal_feature_load_gff3_fasta in tripal_feature/includes/gff_loader.inc

File

tripal_core/api/tripal_core_chado.api.inc, line 3090
The Tripal Core API

Code

function tripal_core_chado_execute_prepared($statement_name, $sql, $values) {
  global $prepared_statements;

  if (!tripal_core_is_sql_prepared($statement_name)) {
    watchdog('tripal_core', "tripal_core_chado_execute_prepared: Cannot execute an unprepared statement: '%name'", array('%name' => $statement_name), WATCHDOG_ERROR);
    return FALSE;
  }

  // Before Executing, Ensure that all the values are supplied
  $required_values = $prepared_statements[$statement_name]['prepared_args'];
  if (!$required_values) {
    watchdog('tripal_core', "tripal_core_chado_execute_prepared: missing prepare arguments for this statement: '%name'", array('%name' => $statement_name), WATCHDOG_ERROR);
    return FALSE;
  }

  if (sizeof($required_values) == sizeof($values)) {

    $error = FALSE;
    foreach ($values as $k => $v) {
      if (isset($required_values[$k])) {
        switch ($required_values[$k]) {
          case 'text':
            // anything can be converted to a string, so if the type is 'text' let's just convert it
            $values[$k] = (string) $v;
            /*
            $check = is_string($v);
            if ($v != '' and !$check) {
              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
                array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
              return FALSE;
            } */
            break;
          case 'int':
            $check = is_numeric($v);
            if (!$check) {
              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'", 
              array('%name' => $statement_name, '%k' => $k + 1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
              return FALSE;
            }
            break;
          case 'bool':
            if ($v != 'TRUE' and $v != 'FALSE') {
              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'", 
              array('%name' => $statement_name, '%k' => $k + 1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
              return FALSE;
            }
            break;
          case 'numeric':
            $check = is_numeric($v);
            if (!$check) {
              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'", 
              array('%name' => $statement_name, '%k' => $k + 1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
              return FALSE;
            }
            break;
          default:
            watchdog('tripal_core', "chado_execute_prepared: unsupported argument type (supplied for '%name' statement %type)", 

            array('%name' => $statement_name, '%type' => $required_values[$k]), WATCHDOG_WARNING);
            break;
        }
      }
      else {
        watchdog('tripal_core', "chado_execute_prepared: wrong number of arguments supplied for '%name' statement. Expected %required but recieved %values", 
        array('%name' => $statement_name, '%required' => print_r($required_values, TRUE), '%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
        return FALSE;
      }
    }

    // Since all values are supplied, execute
    $resource = chado_query($sql, $values);
    return $resource;
  }
  else {
    watchdog('tripal_core', "chado_execute_prepared: wrong number of arguments supplied for '%name' statement. ' .
      'Expected %required but recieved %values. Statement: %statement.", 
    array('%name' => $statement_name, '%required' => print_r($required_values, TRUE),
      '%values' => print_r($values, TRUE), '%statement' => $prepared_statements[$statement_name]['prepared_sql']), WATCHDOG_ERROR);
    return FALSE;
  }
}