Commit 124d7232 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Database*.php:

- renamed querySemple() to sql().
- renamed execute() to prepareEXecute()
- ParamArray or prepared Statement will be display on exceptions.
- Testcases extended to ParamArray for sql()
parent c889d94d
......@@ -92,6 +92,7 @@ const SYSTEM_DBH = 'dataBaseHandle';
const SYSTEM_SQL_RAW = 'sqlRaw'; // Type: SANATIZE_ALL / String. SQL Query (before substitute). Useful for error reporting.
const SYSTEM_SQL_FINAL = 'sqlFinal'; // Type: SANATIZE_ALL / String. SQL Query (after substitute). Useful for error reporting.
const SYSTEM_SQL_COUNT = 'sqlCount'; // Type: SANATIZE_DIGIT / Int.Number of rows in SQL ResultSet. Useful for error reporting.
const SYSTEM_SQL_PARAM_ARRAY = 'sqlParamArray'; // Type: SANATIZE_ALL / Paramter of prepared SQL Statement. Useful for error reporting.
// FORM - copy from table 'form' of processed form
//const DEF_FORM_NAME = CLIENT_FORM;
......
......@@ -177,10 +177,10 @@ class Database {
* @param $sql
* @param $mode
*/
public function querySimple($sql, $mode = ROW_REGULAR) {
public function sql($sql, $mode = ROW_REGULAR, array $parameterArray = array()) {
// for error reporting in exception
$count = $this->execute($sql, array());
$count = $this->prepareExecute($sql, $parameterArray);
if ($count === false) {
throw new DbException("No idea why this error happens - please take some time and check this: $sql", ERROR_DB_GENERIC_CHECK);
}
......@@ -222,9 +222,10 @@ class Database {
* @param array $parameterArray parameter array for prepared statement execution.
* @return mixed
*/
public function execute($sql, array $parameterArray = array()) {
public function prepareExecute($sql, array $parameterArray = array()) {
$this->store->setVar(SYSTEM_SQL_FINAL, $sql, STORE_SYSTEM);
$this->store->setVar(SYSTEM_SQL_PARAM_ARRAY, $parameterArray, STORE_SYSTEM);
$this->stmt = $this->pdo->prepare($sql);
if (false === $this->stmt->execute($parameterArray)) {
......@@ -284,4 +285,5 @@ class Database {
return $this->stmt->fetch(\PDO::FETCH_ASSOC);
}
}
\ No newline at end of file
......@@ -10,19 +10,19 @@ class DatabaseTest extends AbstractDatabaseTest {
public function testExecute() {
$this->database->execute('SELECT * FROM person');
$this->database->prepareExecute('SELECT * FROM person');
$this->assertEquals(2, $this->database->rowCount());
}
public function testFetchOneRowAssocEmptyResult() {
$this->database->execute('SELECT * FROM person WHERE id=0', []);
$this->database->prepareExecute('SELECT * FROM person WHERE id=0', []);
$this->assertEquals(array(), $this->database->fetchOne());
}
public function testFetchOne() {
$this->database->execute('SELECT * FROM person');
$this->database->prepareExecute('SELECT * FROM person');
$oneRow = $this->database->fetchOne();
......@@ -35,7 +35,7 @@ class DatabaseTest extends AbstractDatabaseTest {
}
public function testFetchAll() {
$this->database->execute('SELECT * FROM person ORDER BY id LIMIT 2');
$this->database->prepareExecute('SELECT * FROM person ORDER BY id LIMIT 2');
$allRows = $this->database->fetchAll();
......@@ -45,7 +45,7 @@ class DatabaseTest extends AbstractDatabaseTest {
}
public function testFetchAllEmpty() {
$this->database->execute('SELECT * FROM person WHERE id=0 ORDER BY id');
$this->database->prepareExecute('SELECT * FROM person WHERE id=0 ORDER BY id');
$this->assertEquals(array(), $this->database->fetchAll());
}
......@@ -70,7 +70,7 @@ class DatabaseTest extends AbstractDatabaseTest {
];
// Check read rows
$dataArray = $this->database->querySimple('SELECT * FROM person ORDER BY id LIMIT 2');
$dataArray = $this->database->sql('SELECT * FROM person ORDER BY id LIMIT 2');
// Check count
$this->assertEquals(2, $this->database->rowCount());
$this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
......@@ -79,52 +79,66 @@ class DatabaseTest extends AbstractDatabaseTest {
// Check rows
// Same as above, but specify 'ROW_REGULAR'
$dataArray = $this->database->querySimple('SELECT * FROM person ORDER BY id LIMIT 2', ROW_REGULAR);
$dataArray = $this->database->sql('SELECT * FROM person ORDER BY id LIMIT 2', ROW_REGULAR);
$this->assertEquals(2, $this->database->rowCount());
// Check rows
$this->assertSame($expected, $dataArray);
// ROW_EXACT_1
$dataArray = $this->database->querySimple('SELECT * FROM person WHERE id=1', ROW_EXACT_1);
$dataArray = $this->database->sql('SELECT * FROM person WHERE id=1', ROW_EXACT_1);
$this->assertEquals(1, $this->database->rowCount());
// Check rows
$this->assertSame($expected[0], $dataArray);
// ROW_EMPTY_IS_OK - one record
$dataArray = $this->database->querySimple('SELECT * FROM person WHERE id=1', ROW_EMPTY_IS_OK);
$dataArray = $this->database->sql('SELECT * FROM person WHERE id=1', ROW_EMPTY_IS_OK);
$this->assertEquals(1, $this->database->rowCount());
// Check rows
$this->assertSame($expected[0], $dataArray);
// ROW_EMPTY_IS_OK - no record
$dataArray = $this->database->querySimple('SELECT * FROM person WHERE id=0', ROW_EMPTY_IS_OK);
$dataArray = $this->database->sql('SELECT * FROM person WHERE id=0', ROW_EMPTY_IS_OK);
$this->assertEquals(0, $this->database->rowCount());
// Check rows
$this->assertSame(array(), $dataArray);
// Check Implode: 2 records
$data = $this->database->querySimple('SELECT * FROM person ORDER BY id LIMIT 2', ROW_IMPLODE_ALL);
$data = $this->database->sql('SELECT * FROM person ORDER BY id LIMIT 2', ROW_IMPLODE_ALL);
$this->assertEquals(implode($expected[0]) . implode($expected[1]), $data);
// Check Implode: 1 record
$data = $this->database->querySimple('SELECT * FROM person ORDER BY id LIMIT 1', ROW_IMPLODE_ALL);
$data = $this->database->sql('SELECT * FROM person ORDER BY id LIMIT 1', ROW_IMPLODE_ALL);
$this->assertEquals(implode($expected[0]), $data);
// Check Implode 0 record
$data = $this->database->querySimple('SELECT * FROM person WHERE id=0 ORDER BY id LIMIT 2', ROW_IMPLODE_ALL);
$data = $this->database->sql('SELECT * FROM person WHERE id=0 ORDER BY id LIMIT 2', ROW_IMPLODE_ALL);
$this->assertEquals('', $data);
}
public function testQuerySimpleParameter() {
// Parameter Susbstitution by '?'
$dataArray = $this->database->sql('SELECT * FROM person WHERE name LIKE ? ORDER BY id', ROW_REGULAR, ['Smith']);
// Check count
$this->assertEquals(1, $this->database->rowCount());
// Parameter Susbstitution by name
$dataArray = $this->database->sql('SELECT * FROM person WHERE name LIKE :valueOfName ORDER BY id ', ROW_REGULAR, [':valueOfName' => 'Smith']);
// Check count
$this->assertEquals(1, $this->database->rowCount());
}
/**
* @expectedException \qfq\exceptions\DbException
*
*/
public function testSanatizeException() {
$this->database->querySimple('some garbage');
$this->database->sql('some garbage');
}
public function testParameters() {
$this->database->execute('SELECT * FROM person WHERE id = :valueOfA', [':valueOfA' => 2]);
$this->database->prepareExecute('SELECT * FROM person WHERE id = :valueOfA', [':valueOfA' => 2]);
$this->assertEquals(1, $this->database->rowCount());
}
......@@ -143,7 +157,7 @@ class DatabaseTest extends AbstractDatabaseTest {
$sql = "INSERT INTO person (id, name, firstname, gender, groups) VALUES (NULL, 'Doe', 'Jonni', 'male','')";
$this->database->execute($sql);
$this->database->prepareExecute($sql);
$this->assertEquals(3, $this->database->getLastInsertId());
}
......
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