/** * 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; }
/** * Establish a new database connection * * @param string $type * @param string $hostname * @param int $port * @param string $username * @param string $password * @param string $key * * @return Connection */ public function establishConnection($type, $hostname, $port, $username, $password, &$key) { $key = NULL; $connection = Connection::create($type, $hostname, $port, $username, $password, $key); $driver = $this->drivers->getDriver($type); // Test the connection if (!$driver->testConnection($connection)) { return null; } // Success, build and store the connection $store = $this->getStore(); $store->add($connection); // Lock (encrypt) the credentials and return the private key $connection->lock(); return $connection; }
/** * 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; }
public function getSchema(Connection $connection, $db, $table) { if (isset($schemaCache["{$connection->getId()}:{$db}:{$table}"])) { return $schemaCache["{$connection->getId()}:{$db}:{$table}"]; } $rows = $this->rawQuery($connection, $db, 'SHOW FULL COLUMNS FROM ' . $this->quoteSchema($table)); $columns = array(); foreach ($rows as $row) { $row = (array) $row; $description = (object) array('name' => $row['Field'], 'type' => $row['Type'], 'nullable' => $row['Null'] == 'YES', 'default' => $row['Default'], 'encoding' => $row['Collation'], 'comment' => $row['Comment'], 'isPrimary' => $row['Key'] == 'PRI', 'isIndexed' => $row['Key'] ? true : false, 'auto' => strpos($row['Extra'], 'auto_increment')); $columns[] = $description; } return $schemaCache["{$connection->getId()}:{$db}:{$table}"] = $columns; }