/** * Returns SQL query. * @return string */ public function getSql() { $join = $this->createJoins(implode(',', $this->conditions), TRUE) + $this->createJoins(implode(',', $this->select) . ",$this->group,$this->having," . implode(',', $this->order)); $cache = $this->connection->getCache(); if ($this->rows === NULL && $cache && !is_string($this->prevAccessed)) { $this->accessed = $this->prevAccessed = $cache->load(array(__CLASS__, $this->name, $this->conditions)); } $prefix = $join ? "$this->delimitedName." : ''; if ($this->select) { $cols = $this->removeExtraTables($this->tryDelimite(implode(', ', $this->select))); } elseif ($this->prevAccessed) { $cols = $prefix . implode(', ' . $prefix, array_map(array($this->connection->getSupplementalDriver(), 'delimite'), array_keys($this->prevAccessed))); } else { $cols = $prefix . '*'; } return "SELECT{$this->topString()} $cols FROM $this->delimitedName" . implode($join) . $this->whereString(); }
/** * Creates filtered table representation. * @param string database table name * @param NConnection */ public function __construct($table, NConnection $connection) { $this->name = $table; $this->connection = $connection; $reflection = $connection->getDatabaseReflection(); $this->primary = $reflection->getPrimary($table); $this->sqlBuilder = new NSqlBuilder($table, $connection, $reflection); $this->cache = $connection->getCache(); }