lithium\data\source\Database::_constraint()

protected method

Build a SQL column constraint

Parameters

  • string $name

    The name of the meta to build

  • mixed $value

    The value used for building the meta

  • object $schema

    A Schema instance.

Returns

string

The SQL meta string

Source

	protected function _constraint($name, $value, $schema = null) {
		$value += ['options' => []];
		$meta = isset($this->_constraints[$name]) ? $this->_constraints[$name] : null;
		$template = isset($meta['template']) ? $meta['template'] : null;
		if (!$template) {
			return;
		}

		$data = [];
		foreach ($value as $name => $value) {
			switch ($name) {
				case 'key':
				case 'index':
					if (isset($meta[$name])) {
						$data['index'] = $meta[$name];
					}
				break;
				case 'to':
					$data[$name] = $this->name($value);
				break;
				case 'on':
					$data[$name] = "ON {$value}";
				break;
				case 'expr':
					if (is_array($value)) {
						$result = [];
						$context = new Query(['type' => 'none']);
						foreach ($value as $key => $val) {
							$return = $this->_processConditions($key, $val, $context, $schema);
							if ($return) {
								$result[] = $return;
							}
						}
						$data[$name] = join(" AND ", $result);
					} else {
						$data[$name] = $value;
					}
				break;
				case 'toColumn':
				case 'column':
					$data[$name] = join(', ', array_map([$this, 'name'], (array) $value));
				break;
			}
		}

		return trim(Text::insert($template, $data, ['clean' => true]));
	}