You are here

function _messaging_query_where in Messaging 6.4

Query builder: Build where conditions and arguments using schema data

Using schema data get 'field = [placeholder]' and args arrays

Parameters

$table: Table name (to be prefixed)

$params: Array of field => value conditions

$table_alias: Optional table alias to prefix fields in conditions

Return value

array() Array with 'where' (array of where conditions) and 'args' (array of query arguments)

2 calls to _messaging_query_where()
Messaging_Store::query_fields in includes/messaging_store.class.inc
Build parameters for where clause
_messaging_query_conditions in ./messaging.module
Helper function for query builders.

File

./messaging.module, line 870

Code

function _messaging_query_where($table, $params, $table_alias = NULL) {
  $schema = drupal_get_schema($table);
  $conditions = $args = array();
  foreach ($params as $field => $value) {

    // If not a field we ignore the parameter
    if (isset($schema['fields'][$field])) {
      $type = $schema['fields'][$field]['type'];
      $field_name = $table_alias ? "{$table_alias}.{$field}" : $field;

      // For array values, build IN conditions
      if (is_array($value)) {
        $conditions[] = $field_name . ' IN (' . db_placeholders($value, $type) . ')';
        $args = array_merge($args, $value);
      }
      elseif (is_null($value)) {
        $condtions[] = $field_name . ' IS NULL';
      }
      else {
        $conditions[] = $field_name . ' = ' . db_type_placeholder($type);
        $args[] = $value;
      }
    }
  }

  // Return arrwy with conditions and arguments. Also full where clause.
  return array(
    'where' => $conditions,
    'args' => $args,
  );
}