Skip to content
Snippets Groups Projects
SimpleWhereClause.php 3.19 KiB
Newer Older
jaapjansma's avatar
jaapjansma committed
<?php
/**
 * @author Jaap Jansma <jaap.jansma@civicoop.org>
 * @license AGPL-3.0
 */

namespace Civi\DataProcessor\DataFlow\SqlDataFlow;

class SimpleWhereClause implements WhereClauseInterface {

  protected $table_alias;

  protected $field;

  protected $operator;

  protected $value;

  protected $isJoinClause = FALSE;

  public function __construct($table_alias, $field, $operator, $value, $valueType = 'String', $isJoinClause=FALSE) {
    if (is_array($value)) {
      switch ($operator) {
        case '=':
          $operator = 'IN';
          break;
        case '!=':
          $operator = 'NOT IN';
          break;
mattwire's avatar
mattwire committed

        case 'IS NULL':
          $operator = 'IS NULL';
          break;
        case 'IS NOT NULL':
          $operator = 'IS NOT NULL';
          break;
    $this->isJoinClause = $isJoinClause;
jaapjansma's avatar
jaapjansma committed
    $this->table_alias = $table_alias;
    $this->field = $field;
    $this->operator = $operator;
    if ($operator == 'IS NULL' || $operator == 'IS NOT NULL') {
mattwire's avatar
mattwire committed
      $this->value = NULL;
      return;
    }

jaapjansma's avatar
jaapjansma committed
    if (is_array($value)) {
jaapjansma's avatar
jaapjansma committed
      $esacpedValues = array();
      foreach($value as $val) {
        switch ($valueType) {
          case 'String':
          case 'Text':
          case 'Memo':
            $esacpedValues[] = "'" . \CRM_Utils_Type::escape($val, $valueType) . "'";
            break;
          default:
            $esacpedValues[] = \CRM_Utils_Type::escape($val, $valueType);
            break;
        }
jaapjansma's avatar
jaapjansma committed
      }
jaapjansma's avatar
jaapjansma committed
      if ($operator == 'BETWEEN' || $operator == 'NOT BETWEEN') {
        $this->value = implode(" AND ", $esacpedValues);
jaapjansma's avatar
jaapjansma committed
        $this->value = "(" . implode(", ", $esacpedValues) . ")";
      }
jaapjansma's avatar
jaapjansma committed
    } else {
      switch ($valueType) {
        case 'String':
        case 'Text':
        case 'Memo':
          $this->value = "'" . \CRM_Utils_Type::escape($value, $valueType) . "'";
          break;
        default:
          $this->value = \CRM_Utils_Type::escape($value, $valueType);
          break;
      }
jaapjansma's avatar
jaapjansma committed
    }
jaapjansma's avatar
jaapjansma committed
  }

  /**
   * Returns true when this where clause can be added to the
   * join or whether this clause should be propagated to the where part of the query
   *
   * @return bool
   */
  public function isJoinClause() {
    return $this->isJoinClause;
  }

jaapjansma's avatar
jaapjansma committed
  /**
   * Returns the where clause
   * E.g. contact_type = 'Individual'
   *
   * @return string
   */
  public function getWhereClause() {
jaapjansma's avatar
jaapjansma committed
    if ($this->isJoinClause()) {
      return "`{$this->table_alias}`.`{$this->field}` {$this->operator} {$this->value}";
    }
    switch ($this->operator) {
      case 'NOT IN':
      case 'NOT LIKE':
      case '!=':
        return "(`{$this->table_alias}`.`{$this->field}` {$this->operator} {$this->value} OR `{$this->table_alias}`.`{$this->field}` IS NULL)";
        break;
jaapjansma's avatar
jaapjansma committed
      case 'IS NULL':
        return "(`{$this->table_alias}`.`{$this->field}` {$this->operator} OR {$this->table_alias}`.`{$this->field} != '')";
        break;
jaapjansma's avatar
jaapjansma committed
      case 'IS NOT NULL':
        return "(`{$this->table_alias}`.`{$this->field}` {$this->operator} AND `{$this->table_alias}`.`{$this->field}` != '')";
jaapjansma's avatar
jaapjansma committed
        break;
jaapjansma's avatar
jaapjansma committed
      default:
        return "`{$this->table_alias}`.`{$this->field}` {$this->operator} {$this->value}";
        break;
jaapjansma's avatar
jaapjansma committed
    }
jaapjansma's avatar
jaapjansma committed
  }