Commit 34127973 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Database.php: function renamed getTableDefinition

parent 0ac8d21f
......@@ -131,13 +131,13 @@ class Database {
*
* @param string $columnName name of the column
*
* @return array the definition of the column as retrieved by Database::getFieldsFromTable().
* @return array the definition of the column as retrieved by Database::getTableDefinition().
*
* @throws exceptions\UserException
*
*/
private function getFieldDefinitionFromTable($table, $columnName) {
$tableDefinition = $this->getFieldsFromTable($table);
$tableDefinition = $this->getTableDefinition($table);
foreach ($tableDefinition AS $row) {
if ($row["Field"] == $columnName) {
return $row;
......@@ -147,15 +147,22 @@ class Database {
}
/**
* Get all column definitions for a table
* Get all column definitions for a table. Return Assoc Array:
*
* Field Type Null Key Default Extra
* --------------------------------------------------------------------------
* id bigint(20) NO PRI NULL auto_increment
* name varchar(128) YES NULL
* firstname varchar(128) YES NULL
* gender enum('','male','female') NO male
* groups set('','a','b','c') NO a
*
* @param string $table table to retrieve column definition from
*
* @return array column definition of table as returned by SHOW FIELDS FROM as associative array.
*/
private function getFieldsFromTable($table) {
public function getTableDefinition($table) {
$statement = $this->pdo->query("SHOW FIELDS FROM `$table`");
return $statement->fetchAll(\PDO::FETCH_ASSOC);
}
......@@ -202,7 +209,7 @@ class Database {
}
if ($count > 1) {
throw new DbException("Expected one row, got $count: $sql", ERROR_DB_TOO_MANY_ROWS);
throw new DbException("Expected one row, got $count rows: $sql", ERROR_DB_TOO_MANY_ROWS);
}
return $this->fetchOne();
......
......@@ -8,6 +8,9 @@ require_once(__DIR__ . '/../../qfq/Database.php');
class DatabaseTest extends AbstractDatabaseTest {
/**
* @throws \qfq\exceptions\DbException
*/
public function testExecute() {
$this->db->prepareExecute('SELECT * FROM Person');
......@@ -15,12 +18,18 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->assertEquals(2, $this->db->rowCount());
}
/**
* @throws \qfq\exceptions\DbException
*/
public function testFetchOneRowAssocEmptyResult() {
$this->db->prepareExecute('SELECT * FROM Person WHERE id=0', []);
$this->assertEquals(array(), $this->db->fetchOne());
}
/**
* @throws \qfq\exceptions\DbException
*/
public function testFetchOne() {
$this->db->prepareExecute('SELECT * FROM Person');
......@@ -34,6 +43,9 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->assertEquals('John', $oneRow['firstname']);
}
/**
* @throws \qfq\exceptions\DbException
*/
public function testFetchAll() {
$this->db->prepareExecute('SELECT * FROM Person ORDER BY id LIMIT 2');
......@@ -44,12 +56,18 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->assertEquals(2, $allRows[1]['id']);
}
/**
* @throws \qfq\exceptions\DbException
*/
public function testFetchAllEmpty() {
$this->db->prepareExecute('SELECT * FROM Person WHERE id=0 ORDER BY id');
$this->assertEquals(array(), $this->db->fetchAll());
}
/**
* @throws \qfq\exceptions\DbException
*/
public function testQuerySimple() {
$expected = [
......@@ -115,6 +133,9 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->assertEquals('', $data);
}
/**
* @throws \qfq\exceptions\DbException
*/
public function testQuerySimpleParameter() {
// Parameter Susbstitution by '?'
$dataArray = $this->db->sql('SELECT * FROM Person WHERE name LIKE ? ORDER BY id', ROW_REGULAR, ['Smith']);
......@@ -137,12 +158,18 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->db->sql('some garbage');
}
/**
* @throws \qfq\exceptions\DbException
*/
public function testParameters() {
$this->db->prepareExecute('SELECT * FROM Person WHERE id = :valueOfA', [':valueOfA' => 2]);
$this->assertEquals(1, $this->db->rowCount());
}
/**
*
*/
public function testGetSetValueList() {
$valueList = $this->db->getEnumSetValueList('person', 'gender');
$expected = [0 => '', 1 => 'male', 2 => 'female'];
......@@ -153,6 +180,9 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->assertEquals($expected, $valueList);
}
/**
* @throws \qfq\exceptions\DbException
*/
public function testGetLastInsertId() {
$sql = "INSERT INTO Person (id, name, firstname, gender, groups) VALUES (NULL, 'Doe', 'Jonni', 'male','')";
......@@ -161,6 +191,24 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->assertEquals(3, $this->db->getLastInsertId());
}
/**
*
*/
public function testGetTableDefinition() {
$expected = [
['Field' => 'id', 'Type' => 'bigint(20)', 'Null' => 'NO', 'Key' => 'PRI', 'Default' => '', 'Extra' => 'auto_increment'],
['Field' => 'name', 'Type' => 'varchar(128)', 'Null' => 'YES', 'Key' => '', 'Default' => '', 'Extra' => ''],
['Field' => 'firstname', 'Type' => 'varchar(128)', 'Null' => 'YES', 'Key' => '', 'Default' => '', 'Extra' => ''],
['Field' => 'gender', 'Type' => "enum('','male','female')", 'Null' => 'NO', 'Key' => '', 'Default' => '', 'Extra' => ''],
['Field' => 'groups', 'Type' => "set('','a','b','c')", 'Null' => 'NO', 'Key' => '', 'Default' => '', 'Extra' => ''],
];
$this->assertEquals($expected, $this->db->getTableDefinition('person'));
}
/**
* @throws Exception
*/
protected function setUp() {
parent::setUp();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment