Commit 6c73ade5 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Database.php: Fixed bug that INSERT returns affected rows. Correct is...

Database.php: Fixed bug that INSERT returns affected rows. Correct is last_insert_id. Moved 'REPLACE' to behave like 'INSERT' instead of 'UPDATE. Extended Tests
parent 1892d0ba
......@@ -117,10 +117,12 @@ class Database {
* @param string $mode
* @param array $parameterArray
* @param string $specificMessage
* @return mixed|null
* SELECT | SHOW | DESCRIBE | EXPLAIN: If no record found, empty string ( ROW_EXPECT_0_1, ROW_EXPECT_1) or empty array (all other modes)
* INSERT: last_insert_id
* UPDATE | DELETE | REPLACE: affected rows
* @param array $keys
* @param array $stat DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
* @return array|int
* SELECT | SHOW | DESCRIBE | EXPLAIN: If no record found, empty string ( ROW_EXPECT_0_1, ROW_EXPECT_1) or empty array (all other modes)
* INSERT: last_insert_id
* UPDATE | DELETE | REPLACE: affected rows
* @throws \qfq\CodeException
* @throws \qfq\DbException
*/
......@@ -182,6 +184,8 @@ class Database {
default:
throw new DbException($specificMessage . "Unknown mode: $mode", ERROR_UNKNOWN_MODE);
}
} elseif ($queryType === QUERY_TYPE_INSERT) {
$result = $stat[DB_INSERT_ID];
} else {
$result = $count;
}
......@@ -219,7 +223,7 @@ class Database {
* @param string $sql SQL statement with prepared statement variable.
* @param array $parameterArray parameter array for prepared statement execution.
* @param string $queryType returns QUERY_TYPE_SELECT | QUERY_TYPE_UPDATE | QUERY_TYPE_INSERT, depending on the query.
* @param array $stat
* @param array $stat DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
* @return int|mixed
* @throws \qfq\CodeException
* @throws \qfq\DbException
......@@ -266,6 +270,7 @@ class Database {
$count = $stat[DB_NUM_ROWS];
$msg = 'Read rows: ' . $stat[DB_NUM_ROWS];
break;
case 'REPLACE':
case 'INSERT':
$queryType = QUERY_TYPE_INSERT;
$stat[DB_INSERT_ID] = $this->mysqli->insert_id;
......@@ -274,13 +279,13 @@ class Database {
$msg = 'ID: ' . $count;
break;
case 'UPDATE':
case 'REPLACE':
case 'DELETE':
$queryType = QUERY_TYPE_UPDATE;
$stat[DB_AFFECTED_ROWS] = $this->mysqli->affected_rows;
$count = $stat[DB_AFFECTED_ROWS];
$msg = 'Affected rows: ' . $count;
break;
case 'SET':
$queryType = QUERY_TYPE_CONTROL;
$stat[DB_AFFECTED_ROWS] = 0;
......
......@@ -228,14 +228,140 @@ class DatabaseTest extends AbstractDatabaseTest {
/**
* @throws \qfq\DbException
*/
public function testGetLastInsertId() {
public function testSelectReturn() {
$dummy = array();
$stat = array();
$sql = "SELECT name FROM Person ORDER BY id";
$rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
$this->assertEquals([0 => ['name' => 'Doe'], 1 => ['name' => 'Smith']], $rc);
// DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
$this->assertEquals(2, $stat[DB_NUM_ROWS]);
}
/**
* @throws \qfq\DbException
*/
public function testShowReturn() {
$dummy = array();
$stat = array();
$sql = "SHOW TABLES";
$rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
$this->assertEquals([
0 => ['Tables_in_crose_qfq_db_phpunit' => 'Address'],
1 => ['Tables_in_crose_qfq_db_phpunit' => 'Form'],
2 => ['Tables_in_crose_qfq_db_phpunit' => 'FormElement'],
3 => ['Tables_in_crose_qfq_db_phpunit' => 'Gruppe'],
4 => ['Tables_in_crose_qfq_db_phpunit' => 'Note'],
5 => ['Tables_in_crose_qfq_db_phpunit' => 'Person'],
6 => ['Tables_in_crose_qfq_db_phpunit' => 'note']
], $rc);
// DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
$this->assertEquals(7, $stat[DB_NUM_ROWS]);
}
/**
* @throws \qfq\DbException
*/
public function testExplainReturn() {
$dummy = array();
$stat = array();
$sql = "EXPLAIN Person";
$rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
// DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
$this->assertEquals(5, $stat[DB_NUM_ROWS]);
}
/**
* @throws \qfq\DbException
*/
public function testInsertReturn() {
$dummy = array();
$stat = array();
$sql = "INSERT INTO Person (id, name, firstname, gender, groups) VALUES (NULL, 'Doe', 'Jonni', 'male','')";
$this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
$rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
// DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
$this->assertEquals(3, $stat[DB_INSERT_ID]);
$this->assertEquals(1, $stat[DB_AFFECTED_ROWS]);
$this->assertEquals(3, $rc);
}
/**
* @throws \qfq\DbException
*/
public function testUpdateReturn() {
$dummy = array();
$stat = array();
$sql = "UPDATE Person SET groups = 'a'";
$rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
$this->assertEquals(2, $rc);
// DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
$this->assertEquals(2, $stat[DB_AFFECTED_ROWS]);
}
/**
* @throws \qfq\DbException
*/
public function testDeleteReturn() {
$dummy = array();
$stat = array();
$this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
$sql = "DELETE FROM Person WHERE id=2";
$rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
$this->assertEquals(1, $rc);
// DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
$this->assertEquals(1, $stat[DB_AFFECTED_ROWS]);
}
/**
* @throws \qfq\DbException
*/
public function testReplaceReturn() {
$dummy = array();
$stat = array();
$this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
$sql = "REPLACE INTO Person (id, name, firstname, gender, groups) VALUES (1, 'Doe', 'John', 'male','')";
$rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
// DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
$this->assertEquals(1, $stat[DB_INSERT_ID]);
$this->assertEquals(2, $stat[DB_AFFECTED_ROWS]);
$this->assertEquals(1, $rc);
$sql = "REPLACE INTO Person (id, name, firstname, gender, groups) VALUES (100, 'Lincoln', 'Abraham', 'male','')";
$rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
// DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
$this->assertEquals(100, $stat[DB_INSERT_ID]);
$this->assertEquals(1, $stat[DB_AFFECTED_ROWS]);
$this->assertEquals(100, $rc);
}
/**
......
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