/** * Analyze the given query, producing a semantic representation of the most important elements * as well as related schema information. * * @param \DataBundle\Connection $connection * @param type $db * @param type $query * @return QueryAnalysis */ public function analyzeQuery(Connection $connection, $db, $query) { $driver = $this->drivers->getDriver($connection->getType()); $analysis = $driver->analyzeQuery($query); $model = new QueryAnalysis(); $model->setValid($analysis->valid); if (!$analysis->valid) { return $model; } $selection = array(); if (isset($analysis->selection)) { foreach ($analysis->selection as $select) { $selection[] = new QuerySelection($select->column, $select->alias); } } $model->setSelection($selection); // Fetch the schemas $froms = array(); if (isset($analysis->from)) { foreach ($analysis->from as $source) { $from = new QuerySource(); $from->setName($source->name); $from->setColumns($this->schema->getTableSchema($connection, $db, $source->name)); $froms[] = $from; } } $model->setFrom($froms); return $model; }
/** * Retrieve a set of Columns representing the schema of the given table. * * @param \DataBundle\Connection $connection * @param string $db * @param string $tableName * @return Column[] */ public function getTableSchema(Connection $connection, $db, $tableName) { $driver = $this->drivers->getDriver($connection->getType()); $columns = array(); foreach ($driver->getSchema($connection, $db, $tableName) as $column) { $columnModel = $this->modelForColumn($column); $columns[] = $columnModel; } return $columns; }