Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Fix QueryTable error when getting metadata for an empty SQLite table #205

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/DataSet/QueryTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,7 @@ protected function createTableMetaData()
// get column names from data
$columns = \array_keys($this->data[0]);
} else {
// if no rows found, get column names from database
$pdoStatement = $this->databaseConnection->getConnection()->prepare('SELECT column_name FROM information_schema.COLUMNS WHERE table_schema=:schema AND table_name=:table');
$pdoStatement->execute([
'table' => $this->tableName,
'schema' => $this->databaseConnection->getSchema()
]);

$columns = $pdoStatement->fetchAll(PDO::FETCH_COLUMN, 0);
$columns = $this->databaseConnection->getMetaData()->getTableColumns($this->tableName);
}
// create metadata
$this->tableMetaData = new DefaultTableMetadata($this->tableName, $columns);
Expand Down
29 changes: 29 additions & 0 deletions tests/DataSet/QueryTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ public static function providerTestGetValue()
];
}

public function testGetEmptyTableMetaData()
{
$metaData = new DefaultTableMetadata('table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']);

$conn = new PDO('sqlite::memory:');
$conn->exec(
'CREATE TABLE IF NOT EXISTS table1 (
table1_id INTEGER PRIMARY KEY AUTOINCREMENT,
column1 VARCHAR(20),
column2 INT(10),
column3 DECIMAL(6,2),
column4 TEXT
)'
);

$query = '
SELECT *
FROM table1
';

$empty_table = new QueryTable(
'table1',
$query,
new DefaultConnection($conn)
);

$this->assertEquals($metaData, $empty_table->getTableMetaData());
}

public function testGetTableMetaData()
{
$metaData = new DefaultTableMetadata('table1', ['col1', 'col2', 'col3']);
Expand Down