You are here

function notifications_query_sql in Notifications 6.2

Same name and namespace in other branches
  1. 6.4 includes/query.inc \notifications_query_sql()
  2. 6 notifications.module \notifications_query_sql()
  3. 6.3 notifications.module \notifications_query_sql()

Build the SQL statement from query elements

It will build INSERT + SELECT or SELECT queries from its elements

Return value

array() list($sql, $args);

5 calls to notifications_query_sql()
NotificationsBasicTests::testNotificationsQueryBuilder in tests/notifications_api.test
Test query builder
notifications_delete_subscriptions in ./notifications.module
Delete multiple subscriptions and clean up related data (pending notifications, fields).
notifications_get_subscriptions in ./notifications.module
Get subscriptions that fit a set of conditions.
notifications_queue in ./notifications.module
Queue events for notifications adding query conditions from plug-ins
notifications_user_get_subscriptions in ./notifications.module
Get subscription for a given user

File

./notifications.module, line 599
Notifications module

Code

function notifications_query_sql($query) {
  $sql = '';
  if (!empty($query['insert'])) {
    $sql .= 'INSERT INTO ' . $query['into'] . ' (' . implode(', ', $query['insert']) . ') ';
  }
  $sql .= !empty($query['distinct']) ? 'SELECT DISTINCT ' : 'SELECT ';
  $sql .= implode(', ', $query['select']);
  $sql .= ' FROM ' . implode(', ', $query['from']);
  if (!empty($query['join'])) {
    $sql .= ' ' . implode(' ', $query['join']);
  }

  // Where conditions come from 'where' and 'fields' elements
  // Field conditions are OR'd and added into the other conditions
  $where = !empty($query['where']) ? $query['where'] : array();
  if (!empty($query['fields'])) {
    $where[] = '(' . implode(') OR (', $query['fields']) . ')';
  }
  if ($where) {
    $sql .= ' WHERE (' . implode(') AND (', $where) . ')';
  }
  if (!empty($query['group'])) {
    $sql .= ' GROUP BY ' . implode(', ', $query['group']);
  }
  if (!empty($query['having'])) {
    $sql .= ' HAVING ' . implode(' AND ', $query['having']);
  }

  // Merge all args, start with generic ones for subscription queries, then other groups
  $args = !empty($query['args']) ? $query['args'] : array();
  foreach (array(
    'select',
    'join',
    'where',
    'fields',
    'having',
  ) as $key) {
    if (!empty($query[$key . ' args'])) {
      $args = array_merge($args, $query[$key . ' args']);
    }
  }
  return array(
    $sql,
    $args,
  );
}