protected function getTableAlias($table) { foreach ($this->tables as $ta => $ttable) { if ($table == $ttable) { if (Number::is($ta)) { return $table; } return $ta; } } }
function _Set($k, $v) { if ($k === null || Number::is($k)) { if ($v instanceof IToSQL) { //Add(WhereAnd | WhereOr) -> Append //Add(Statement) -> WhereAnd -> Append $this->data[] = $v; } elseif (is_string($v)) { //Add(string) -> Statement -> Add $this->data[] = new WhereAND($v); } elseif (is_array($v)) { if (!$v) { return; //Empty array } if (Arr::is_assoc($v)) { //Add(array(array('field'=>'value'))) -> Statement -> Add foreach ($v as $k => $vv) { $this->_Set($k, $vv); } } elseif (isset($v[0]) && is_array($v[0])) { foreach ($v as $vv) { $this->_Set(null, $vv); } } else { //Add(array(expr1,comparison,expr2)) -> Statement -> Add $op = null; if (count($v) == 2) { $op = '='; } if (count($v) == 3) { $op = $v[1]; $v = array($v[0], $v[1]); } if ($op === null) { throw new \Exception('Invalid array format'); } $this->data[] = new Comparison($v[0], $v[1], $op, static::AUTO_NULL); } } else { throw new \Exception('Unknown format for add'); } } else { //Assosiative simple syntax $op = '='; if ($v instanceof IComparison) { $op = ''; } $this->data[] = WhereAND::fromAssign($k, $v, $op, static::AUTO_NULL); } }
function _Set($k, $v) { if ($k === null || \Radical\Basic\String\Number::is($k)) { if (is_array($v)) { if (Arr::is_assoc($v)) { foreach ($v as $k => $vv) { $this->_Add($k, $vv); } } else { foreach ($v as $k => $vv) { $this->_Add(null, $vv); } } } elseif ($v !== null) { //Add(expr) $this->data[] = $v; } } else { //Add(table,field) -> TableExpression $this->data[] = new TableExpression($v, $k); } }
function _Set($k, $order_by) { if ($k === null || \Radical\Basic\String\Number::is($k)) { if (is_string($order_by)) { $this->data[] = $order_by; } elseif ($order_by instanceof OrderBy) { //Add(OrderByPart) $this->data = $order_by; } elseif (is_array($order_by)) { if (Arr::is_assoc($order_by)) { foreach ($order_by as $key => $order) { $this->_Set($key, $order); } } else { foreach ($order_by as $o) { if (is_array($o)) { //Add(array(array(expr1,order1 = ASC),array(expr1,order2 = ASC))) if (count($o) == 2) { $this->_Set($o[0], $o[1]); } else { throw new \Exception('Unknown array format'); } } else { //Add(array(expr1,expr2)) $this->_Add(null, $o); } } } } else { throw new \Exception('Invalid order by call'); } } else { //Add(expr,order) $this->data[] = new OrderByPart($k, $order_by); } }
function toSQL() { $fields = $this->fields; array_walk($fields, function ($value, $key) use(&$fields) { if (!Number::is($key)) { $fields[$key] = $value . ' AS ' . $key; } }); $ret = 'SELECT ' . implode(', ', $fields) . ' ' . $this->from; if ($this->for_update) { $ret .= ' FOR UPDATE'; } return $ret; }