<?php
/*
 * Copyright (c) 2010, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

class WV_Filter_Column extends _WV_Filter_BaseColumn {
	public function __toString() {
		return $this->toSQL();
	}

	public function toSQL() {
		if (is_array($this->value) && empty($this->value)) {
			return '0';
		}

		if ($this->value === null) {
			$op = ($this->operation === '=' || $this->operation === 'IS') ? 'IS' : 'IS NOT';
			return $this->column.' '.$op.' NULL';
		}

		$sql = WV_SQL::getInstance();

		if (is_array($this->value)) {
			$values = array_map(array($sql, 'quote'), $this->value);
			return $this->column.' '.$this->operation.' ('.implode(', ', $values).')';
		}

		return $this->column.' '.$this->operation.' '.$sql->quote($this->value);
	}
}
