1: <?php
2:
3: namespace Alchemy\core\query;
4: use Alchemy\core\Element;
5: use Alchemy\util\promise\IPromisable;
6:
7:
8: /**
9: * Represents a reference to a table in a SQL query
10: */
11: class TableRef extends Element implements IPromisable {
12:
13: protected $schema;
14:
15:
16: public static function list_promisable_methods() {
17: return array(
18: '__get' => "Alchemy\core\query\ColumnRef",
19: 'copy' => "Alchemy\core\query\TableRef",
20: 'schema' => "Alchemy\core\schema\Table");
21: }
22:
23:
24: /**
25: * Get a column reference by name
26: *
27: * @param string $name Column name
28: * @return ColumnRef reference to Column
29: */
30: public function __get($name) {
31: return $this->schema->getColumn($name)->getRef($this);
32: }
33:
34:
35: public function __construct($schema) {
36: $this->schema = $schema;
37:
38: $this->addTag('sql.compile', "TableRef");
39: }
40:
41:
42: public function columns() {
43: $columns = array();
44: foreach($this->schema->listColumns() as $column) {
45: $columns[$column->getName()] = $column->getRef($this);
46: }
47:
48: return $columns;
49: }
50:
51:
52: public function getDescription($maxdepth = 3, $curdepth = 0) {
53: $str = parent::getDescription($maxdepth, $curdepth);
54: return "$str ({$this->name()})";
55: }
56:
57:
58: /**
59: * Returns a Predicate for filtering rows of this table
60: * equal to the values of a map
61: *
62: * @param array $columns array('ColumnName' => Value)
63: * @return Predicate
64: */
65: public function equal(array $columns) {
66: $list = array();
67: foreach($columns as $name => $value) {
68: $list[] = $this->{$name}->equal($value);
69: }
70:
71: return count($list) > 1
72: ? Predicate::AND_($list)
73: : ($list ? $list[0] : null);
74: }
75:
76:
77: public function name() {
78: return $this->schema->getName();
79: }
80:
81:
82: public function schema() {
83: return $this->schema;
84: }
85: }