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

Database: renamed rowCount() to getRowCount(). Additonal error message for...

Database: renamed rowCount() to getRowCount(). Additonal error message for sql(), New: dbLog(). prepareExecute will log all fired queries.
parent aec82404
......@@ -13,6 +13,7 @@ use qfq\Store;
use qfq\CodeException;
use qfq\UserException;
use qfq\DbException;
use qfq\Support;
require_once(__DIR__ . '/../qfq/exceptions/UserException.php');
......@@ -20,6 +21,7 @@ require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
require_once(__DIR__ . '/../qfq/exceptions/DbException.php');
require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/helper/Support.php');
/**
* Class Database
......@@ -83,7 +85,7 @@ class Database {
* @return mixed Number of rows returned by last call to execute(). If Database::execute()
* has never been called prior a call to this method, false is returned.
*/
public function rowCount() {
public function getRowCount() {
if ($this->stmt == null) {
return false;
}
......@@ -176,11 +178,21 @@ class Database {
* ROW_EMPTY_IS_OK return result set as assoc array. It has to be 0 or one row selected. If not: throw Exception,
*
* @param $sql
* @param $mode
* @param string $mode
* @param array $parameterArray
* @param string $specificMessage
* @return mixed|null
* @throws \qfq\CodeException
* @throws \qfq\DbException
*/
public function sql($sql, $mode = ROW_REGULAR, array $parameterArray = array(), $specificMessage='') {
public function sql($sql, $mode = ROW_REGULAR, array $parameterArray = array(), $specificMessage = '') {
// CR often forgets to specify the $mode and use prepared statement with parameter instead.
if (is_array($mode))
throw new CodeException("Probably a parameter forgotten: \$mode ?");
// for error reporting in exception
if($specificMessage)
if ($specificMessage)
$specificMessage .= " ";
$count = $this->prepareExecute($sql, $parameterArray);
......@@ -205,31 +217,33 @@ class Database {
}
if ($count > 1) {
throw new DbException($specificMessage ."Expected one row, got $count rows: $sql", ERROR_DB_TOO_MANY_ROWS);
throw new DbException($specificMessage . "Expected one row, got $count rows: $sql", ERROR_DB_TOO_MANY_ROWS);
}
// ROW_EXACT_1
return $this->fetchOne();
}
/**
* Execute a prepared SQL statement.
*
* Execute the given SQL statement as prepared statement. It requires a parameter array which can be empty.
* Execute a prepared SQL statement like SELECT, INSERT, UPDATE, DELETE, SHOW, ...
*
* The result of the query can be retrieved by using one of the fetch methods.
*
* Subsequent calls to this method will overwrite the result of previous calls.
*
* @param string $sql SQL statement with prepared statement variable.
* @param array $parameterArray parameter array for prepared statement execution.
* @return mixed
* @throws \qfq\DbException
* @throws \qfq\UserException
*/
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);
// Logfile
$this->dbLog($sql, $parameterArray);
$this->stmt = $this->pdo->prepare($sql);
if (false === $this->stmt->execute($parameterArray)) {
throw new DbException($this->stmt->errorInfo()[2], ERROR_DB_EXECUTE);
......@@ -238,9 +252,52 @@ class Database {
$count = $this->stmt->rowCount();
$this->store->setVar(SYSTEM_SQL_COUNT, $count, STORE_SYSTEM);
// Logfile
$msg = (substr($sql, 0, 7) === 'INSERT ') ? 'ID: ' . $this->getLastInsertId() : 'Affected rows: ' . $count;
$this->dbLog($msg);
return $count;
}
/**
* @param $sql
* @param $parameterArray
* @throws \qfq\CodeException
*/
private function dbLog($sql, $parameterArray = array()) {
$msg = '[' . date('r') . '][';
if (count($parameterArray) === 0) {
$msg .= $sql;
} else {
$sqlArray = explode('?', $sql);
$ii = 0;
foreach ($parameterArray as $value) {
if (isset($sqlArray[$ii])) {
$msg .= $sqlArray[$ii++] . "'" . $value . "'";
} else {
$msg = '?';
}
}
if (isset($sqlArray[$ii]))
$msg .= $sqlArray[$ii];
}
$msg .= ']';
Support::log($msg);
}
/**
* Returns lastInsertId
*
* @return string
*/
public function getLastInsertId() {
return $this->pdo->lastInsertId();
}
/**
* Fetch all rows of the result as associative array.
*
......@@ -289,11 +346,4 @@ class Database {
return $this->stmt->fetch(\PDO::FETCH_ASSOC);
}
/**
* @return mixed
*/
public function getLastInsertId() {
return $this->pdo->lastInsertId();
}
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->db->prepareExecute('SELECT * FROM Person');
$this->assertEquals(2, $this->db->rowCount());
$this->assertEquals(2, $this->db->getRowCount());
}
/**
......@@ -95,7 +95,7 @@ class DatabaseTest extends AbstractDatabaseTest {
// Check read rows
$dataArray = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 2');
// Check count
$this->assertEquals(2, $this->db->rowCount());
$this->assertEquals(2, $this->db->getRowCount());
$this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
// Compare rows
$this->assertSame($expected, $dataArray);
......@@ -103,25 +103,25 @@ class DatabaseTest extends AbstractDatabaseTest {
// Same as above, but specify 'ROW_REGULAR'
$dataArray = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 2', ROW_REGULAR);
$this->assertEquals(2, $this->db->rowCount());
$this->assertEquals(2, $this->db->getRowCount());
// Check rows
$this->assertSame($expected, $dataArray);
// ROW_EXACT_1
$dataArray = $this->db->sql('SELECT * FROM Person WHERE id=1', ROW_EXACT_1);
$this->assertEquals(1, $this->db->rowCount());
$this->assertEquals(1, $this->db->getRowCount());
// Check rows
$this->assertSame($expected[0], $dataArray);
// ROW_EMPTY_IS_OK - one record
$dataArray = $this->db->sql('SELECT * FROM Person WHERE id=1', ROW_EMPTY_IS_OK);
$this->assertEquals(1, $this->db->rowCount());
$this->assertEquals(1, $this->db->getRowCount());
// Check rows
$this->assertSame($expected[0], $dataArray);
// ROW_EMPTY_IS_OK - no record
$dataArray = $this->db->sql('SELECT * FROM Person WHERE id=0', ROW_EMPTY_IS_OK);
$this->assertEquals(0, $this->db->rowCount());
$this->assertEquals(0, $this->db->getRowCount());
// Check rows
$this->assertSame(array(), $dataArray);
......@@ -145,13 +145,13 @@ class DatabaseTest extends AbstractDatabaseTest {
// Parameter Susbstitution by '?'
$dataArray = $this->db->sql('SELECT * FROM Person WHERE name LIKE ? ORDER BY id', ROW_REGULAR, ['Smith']);
// Check count
$this->assertEquals(1, $this->db->rowCount());
$this->assertEquals(1, $this->db->getRowCount());
// Parameter Susbstitution by name
$dataArray = $this->db->sql('SELECT * FROM Person WHERE name LIKE :valueOfName ORDER BY id ', ROW_REGULAR, [':valueOfName' => 'Smith']);
// Check count
$this->assertEquals(1, $this->db->rowCount());
$this->assertEquals(1, $this->db->getRowCount());
}
......@@ -168,8 +168,7 @@ class DatabaseTest extends AbstractDatabaseTest {
*/
public function testParameters() {
$this->db->prepareExecute('SELECT * FROM Person WHERE id = :valueOfA', [':valueOfA' => 2]);
$this->assertEquals(1, $this->db->rowCount());
$this->assertEquals(1, $this->db->getRowCount());
}
/**
......@@ -211,6 +210,7 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->assertEquals($expected, $this->db->getTableDefinition('Person'));
}
/**
* @throws Exception
*/
......
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