Skip to content
This repository has been archived by the owner on Mar 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from nrocco/feature/cache-schema-info
Browse files Browse the repository at this point in the history
Cache schema information to a json file to avoid expensive lookups.
  • Loading branch information
nrocco committed Oct 19, 2015
2 parents 6720494 + f1d6f90 commit d0235c4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
80 changes: 52 additions & 28 deletions src/RestApi/RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class RestApi
];

protected $database;
protected $schemaMetaData;
protected $user;
protected $storage;

public function __construct(Connection $database)
{
$this->database = $database;
$this->cache = sys_get_temp_dir().'/schema.cache';
}

public function setUser($user)
Expand Down Expand Up @@ -422,48 +424,70 @@ protected function response($body, $code = 200, $headers = [])

protected function getTables()
{
$schemaManager = $this->database->getSchemaManager();
$resources = [];
$schema = $this->getSchemaMetaData();

foreach ($schemaManager->listTables() as $table) {
$resources[] = $table->getName();
}
foreach ($schemaManager->listViews() as $view) {
$resources[] = $view->getShortestName('public');
}

sort($resources);

return $resources;
return array_keys($schema);
}

protected function getTableColumns($table)
{
$schemaManager = $this->database->getSchemaManager();
$columns = [];
$schema = $this->getSchemaMetaData();

foreach ($schemaManager->listTableColumns($table) as $column) {
$columns[] = $column->getName();
}

return $columns;
return $schema[$table]['columns'];
}

protected function getPrimaryKeyField($table)
{
$schemaManager = $this->database->getSchemaManager();
$details = $schemaManager->listTableDetails($table);
$schema = $this->getSchemaMetaData();

if (false === $details->hasPrimaryKey()) {
return;
}
return $schema[$table]['pk'];
}

$pkColumns = $details->getPrimaryKeyColumns();
if (count($pkColumns) > 1) {
throw new \RuntimeException("Resource {$table} uses a composite primary key which is not supported");
protected function getSchemaMetaData()
{
if (!$this->schemaMetaData) {
if (!file_exists($this->cache)) {
$schemaManager = $this->database->getSchemaManager();
$resources = [];

foreach ($schemaManager->listTables() as $table) {
$resources[$table->getName()] = [];
}
foreach ($schemaManager->listViews() as $view) {
$resources[$view->getShortestName('public')] = [];
}

foreach ($resources as $table => &$tableDetails) {
$details = $schemaManager->listTableDetails($table);
$tableDetails['name'] = $details->getName();

if (true === $details->hasPrimaryKey()) {
$pkColumns = $details->getPrimaryKeyColumns();
if (count($pkColumns) > 1) {
throw new \RuntimeException(
"Resource {$table} uses a composite primary key which is not supported"
);
}
$tableDetails['pk'] = reset($pkColumns);
} else {
$tableDetails['pk'] = null;
}

$tableDetails['columns'] = [];
foreach ($details->getColumns() as $column) {
$tableDetails['columns'][] = $column->getName();
}
}

file_put_contents($this->cache, json_encode($resources));

$this->schemaMetaData = $resources;
} else {
$this->schemaMetaData = json_decode(file_get_contents($this->cache), true);
}
}

return reset($pkColumns);
return $this->schemaMetaData;
}

public function addWhere($key, $value)
Expand Down
4 changes: 4 additions & 0 deletions src/RestApi/Tests/RestApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class RestApiTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
if (file_exists(sys_get_temp_dir().'/schema.cache')) {
unlink(sys_get_temp_dir().'/schema.cache');
}

$this->database = DriverManager::getConnection(
['url' => 'sqlite:///:memory:'],
new Configuration()
Expand Down

0 comments on commit d0235c4

Please sign in to comment.