/* Returns a condition string (WHERE <conditions...>) for the primary
key(s) depending on how it is/they are defined in static::$primary_key.
964810
private function primary_key_condition () {
		$statement = '';
		$values = V();

		if (is_array(static::$primary_key)) {
			$primary_keys_statement = [];

			foreach (static::$primary_key as $key) {
				array_push($primary_keys_statement, $key . ' = ?');
				$values->push($this[$key]);
			}

			$statement .= implode(' AND ', $primary_keys_statement);
		} else {
			$statement .= static::$primary_key . ' = ?';
			$values->push($this[static::$primary_key]);
		}

		return A([ 'statement' => $statement, 'values' => $values ]);
	}
1079518
public function where($key, $constraint = null, $value = null, $condition = 'AND')
    {
        $handle = $this->getHandle();

        $this->_internal_query->where($key, $constraint, $value, $condition);

        if (!isset($this->_parent_query->where[$handle])) {
            $this->_parent_query->where[$handle] = array('clause' => $this, 'condition' => $this->_condition);
        }

        return $this;
    }
1120321
public function pkValue()
    {
        if ($args = func_get_args()) {
            !is_array($args[0]) || $args = array_shift($args);
            $args = array_values($args);
            foreach ($args as $key=>$arg)
                $this->{$this->_properties['primary_key'][$key]} = $arg;

            return $this;
        }

        $pk = array_intersect_key($this->_schema['values'],
            array_fill_keys($this->_properties['primary_key'], null));

        return count($pk)==0?null:(count($pk)==1?array_shift($pk):$pk);
    }
855040
protected function buildPKConditions($pk, $_data){
        if(is_string($pk)){
            $pks = array($pk);
            if(!is_array($_data)){
                $_data = array($pk => $_data);
            }
        }
        else{
            $pks = $pk;
        }
        $params = array();
        foreach($pks as $k => $pk){
            $pks[$k] = $pk.'=:pk'.$k;
            $params[':pk'.$k] = $_data[$pk];
        }
        array_unshift($pks, 'AND');
        
        return array($pks, $params);
    }
312379
protected function findModelFromCondition($condition, $primaryKey, $modelClass, $relationContext)
    {
        $condition = [$primaryKey[0] => is_array($condition) ? array_values($condition) : $condition];

        // If an api user the internal find methods are used to find items.
        if (Yii::$app->adminuser->identity->is_api_user) {
            // api calls will always use the "original" find method which is based on yii2 guide the best approach to hide given data by default.
            $findModelInstance = $modelClass::find();
        } else {
            // if its an admin user which is browsing the ui the internal ngRestFind method is used.
            $findModelInstance = $modelClass::ngRestFind();
        }

        return $findModelInstance->andWhere($condition)->with($this->getWithRelation($relationContext))->one();
    }
607404
public function hasPrimaryKey()
    {
        if ($this->autoIncrement) {
            $empty = true;

            foreach ($this->keys as $key) {
                $empty = $empty && !$this->$key;
            }

            return !$empty;
        }

        $conditions = [];

        foreach ($this->getKeyName(true) as $key) {
            $conditions[$key] = $this->$key;
        }

        return $this->getDataMapper()->find($conditions);
    }
689552
public function getPrimaryKey()
    {
        if ($this->_primaryKey === null) {
            $key = (array)$this->getSchema()->primaryKey();
            if (count($key) === 1) {
                $key = $key[0];
            }
            $this->_primaryKey = $key;
        }

        return $this->_primaryKey;
    }
1032012
private function _select($table_name, $columns, $primary_key_value)
	{
		// Get the schema and primary key
		$table_schema      = self::_getTableSchema($table_name);
		$table_primary_key = self::_getTablePrimaryKey($table_name);

		// Get the columns
		$parameter_columns = Validation::validateTableColumns($table_name, $table_schema, $columns);

		// Escape the column names
		$parameter_columns = self::_escapeColumnNames($parameter_columns);

		// Construct the primary key requirement
		$primary_key_value = intval($primary_key_value);

		// Clean the table name
		$table_name = self::_getCleanTableName($table_name);

		// Construct the SELECT sql
		$sql = '
			SELECT ' . implode(', ', $parameter_columns) . '
			FROM ' . $table_name . '
			WHERE ' . $table_primary_key . ' = $1;
		';

		// Sanitize and run the query
		$rows = Utility::pgQueryParams($sql, array($primary_key_value));

		// Validate result, check that primary key is set
		if (empty($rows) || !array_key_exists('0', $rows) || !array_key_exists($table_primary_key, $rows[0]))
		{
			Utility::throwException('Record not found for ' . $table_name . ' with a primary key value of ' . $primary_key_value);
		}

		// Return the only record
		return $rows[0];
	}
385161
def conditions_code
      conditions = @table.options[:conditions]
      code = ''
      case conditions
      when Array
        case conditions[0]
        when String # SQL
          code << '[' + conditions.first.inspect
          code << conditions[1..-1].collect { |p| ', ' + sanitize_condition(p) }.join if conditions.size > 1
          code << ']'
        when Symbol # Method
          raise 'What?' # Amazingly explicit.
        # code << conditions.first.to_s + '('
        # code << conditions[1..-1].collect { |p| sanitize_condition(p) }.join(', ') if conditions.size > 1
        # code << ')'
        else
          raise ArgumentError, 'First element of an Array can only be String or Symbol.'
        end
      when Hash # SQL
        code << '{' + conditions.collect { |key, value| key.to_s + ': ' + sanitize_condition(value) }.join(',') + '}'
      when Symbol # Method
        code << conditions.to_s + '(options)'
      when CodeString
        code << '(' + conditions.gsub(/\s*\n\s*/, ';') + ')'
      when String
        code << conditions.inspect
      else
        raise ArgumentError, "Unsupported type for conditions: #{conditions.inspect}"
      end
      code
    end
310247
public function where($key, $condition = null, $value = null)
    {
        if (!is_null($condition) && is_null($value)) {
            $value = $condition;
            $condition = '=';
        }

        if (count($this->_conditions) < 1) {
            array_push($this->_conditions, []);
        }
        return $this->makeWhere($key, $condition, $value);
    }
1178817
public static function getListQuery( ?array $condition = [], $key = null, $value = null, $indexBy = true, $orderBy = true, $alias = null )
	{
		
		if( !$alias && is_subclass_of( get_called_class(), ActiveRecord::class ) ) {
			$table = Yii::$app->db->schema->getRawTableName( ( get_called_class() )::tableName() );
		}
		else if( $alias ) {
			$table = $alias;
		}
		else {
			$table = '';
		}
		
		!$table ?: $table .= '.';
		
		$key   = $key ?? static::primaryKey()[0];
		$value = $value ?? $key;
		
		$query = static::find()
		               ->select( [
			               $table . $value,
			               $table . $key,
		               ] )
		               ->andFilterWhere( $condition ?? [] )
		;
		
		if( $orderBy === true ) { //
			$query->orderBy( $table . $value );
		}
		else if( $orderBy !== false ) { //
			$query->orderBy( $table . $orderBy );
		}
		
		if( $indexBy === true ) { //
			$query->indexBy( $key );
		}
		else if( $indexBy !== false ) { //
			$query->indexBy( $indexBy );
		}
		
		return $query;
	}
559361
protected function _tablePrimaryKey()
    {
        $alias = $this->_table->alias();
        $pk = $this->_table->primaryKey();

        if (!is_array($pk)) {
            $pk = [$pk];
        }

        $pk = array_map(function ($key) use ($alias) {
            return "{$alias}.{$key}";
        }, $pk);

        return $pk;
    }
1087729
private function _getPrimaryKeyFromInput($table_name, $keyed_array)
	{
		// Get the primary key
		$table_primary_key = self::_getTablePrimaryKey($table_name);

		// Find the primary key
		if (empty($keyed_array) || !array_key_exists($table_primary_key, $keyed_array))
		{
			Utility::throwException('Primary key value not found in input.');
		}

		return $keyed_array[$table_primary_key];
	}
1140653
function getCondition() {
  const type = NodeType.ConditionType;
  const token = tokens[pos];
  const line = token.ln;
  const column = token.col;
  let content = [];
  let s;
  let _pos;

  content.push(getAtkeyword());

  while (pos < tokensLength) {
    if (checkBlock(pos)) break;

    s = checkSC(pos);
    _pos = pos + s;

    if (!_checkCondition(_pos)) break;

    if (s) content = content.concat(getSC());
    content.push(_getCondition());
  }

  return newNode(type, content, line, column);
}
798773
function ( req ) {

    var pk = req.options.id || (req.options.where && req.options.where.id) || req.param('id');

    // TODO: make this smarter...
    // (e.g. look for actual primary key of model and look for it
    //  in the absence of `id`.)
    // See coercePK for reference (although be aware it is not currently in use)

    // exclude criteria on id field
    pk = _.isPlainObject(pk) ? undefined : pk;
    return pk;
  }
572526
public function get_unique_primary($table): ?string
  {
    if ( ($keys = $this->get_keys($table)) &&
      isset($keys['keys']['PRIMARY']) &&
      (\count($keys['keys']['PRIMARY']['columns']) === 1) ){
      return $keys['keys']['PRIMARY']['columns'][0];
    }
    return null;
  }
694032
public static function getConditionBuilder()
    {
        $result = null;

        switch (static::getDriver()) {
            default:
                $result = new \pwf\components\querybuilder\adapters\SQL\ConditionBuilder();
        }

        return $result;
    }