Skip to content

Commit

Permalink
allow sqlite to use ERRMODE_SILENT
Browse files Browse the repository at this point in the history
  • Loading branch information
TRPB committed Dec 22, 2017
1 parent 2bab24f commit b310fed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
9 changes: 5 additions & 4 deletions maphper/datasource/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,18 @@ public function save($data, $tryagain = true) {
try {
$result = $this->insert($this->table, $this->primaryKey, $data);

if ($tryagain === false && $result->errorCode() === '00000' && $result->rowCount() === 0) {
//If there was an error but PDO is silent, trigger the catch block anyway
if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);

if ($tryagain === false && $result->rowCount() === 0) {

$updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data);

$matched = $this->findByField($updateWhere->getArgs());

if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
}
//If there was an error but PDO is silent, trigger the catch block anyway
if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);


}
catch (\Exception $e) {
if ($tryagain) {
Expand Down
4 changes: 4 additions & 0 deletions maphper/datasource/sqliteadapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function query(\Maphper\Lib\Query $query) {
else return $stmt;
}
}
//Handle SQLite when PDO_ERRMODE is set to SILENT
else {
throw new \Exception('Invalid query');
}
}

public function lastInsertId() {
Expand Down
2 changes: 1 addition & 1 deletion tests/SqliteDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct() {
protected function setUp() {
parent::setUp ();
$this->pdo = new PDO('sqlite:./test.db');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
}

protected function tearDown() {
Expand Down

0 comments on commit b310fed

Please sign in to comment.