Skip to content

Commit

Permalink
Merge a879499 into 67e51e7
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasikturbo committed Jun 14, 2017
2 parents 67e51e7 + a879499 commit 8afe846
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/AbstractPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,17 @@ public function save($data, $key, array $tags=null, $ttl=null)
? implode(', ', $tags)
: null;

// upsert
$sql = $this->getSql('update');
$nb = $this->exec($sql, $values)->rowCount();
if ($nb == 0) {
$sql = $this->getSql('insert');
if (!empty($this->sql_definitions['upsert'])) {
$sql = $this->getSql('upsert');
$nb = $this->exec($sql, $values)->rowCount();
} else {
// upsert
$sql = $this->getSql('update');
$nb = $this->exec($sql, $values)->rowCount();
if ($nb == 0) {
$sql = $this->getSql('insert');
$nb = $this->exec($sql, $values)->rowCount();
}
}

return (boolean) $nb;
Expand Down
6 changes: 4 additions & 2 deletions src/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class Mysql extends AbstractPdo
(`expire` IS NULL OR `expire` > :now);',
'update' => 'UPDATE `%s` SET `data`=:data, `tags`=:tags, `expire`=:exp,
`dated`=:dated WHERE `key`=:key;',
'insert' => 'INSERT INTO `%s` (`key`, `data`, `tags`, `expire`, `dated`)
VALUES (:key, :data, :tags, :exp, :dated);',
'insert' => 'INSERT INTO `%s` (`key`, `data`, `tags`, `expire`,
`dated`) VALUES (:key, :data, :tags, :exp, :dated)
ON DUPLICATE KEY UPDATE `data`=VALUES(`data`),
`tags`=VALUES(`tags`), `expire`=VALUES(`expire`);',
'delete' => 'DELETE FROM `%s` WHERE `key`=?;',
'clean' => 'DELETE FROM `%s` WHERE %s;', // %s 'clean_like' iterated
'clean_like'=> 'tags LIKE ?',
Expand Down
15 changes: 15 additions & 0 deletions tests/PdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,19 @@ public function testGetDriverName()
}
}

/**
* Regression test for pull request GH#28
*
* @link https://github.com/frqnck/apix-cache/pull/28
* PDOException: SQLSTATE[23000]: Integrity constraint violation:
* 1062 Duplicate entry 'apix-cache-key:same_key' for key 'PRIMARY'
* @group pr
*/
public function testPullRequest28()
{
$this->cache->save('same_data', 'same_key');
$this->cache->save('same_data', 'same_key');

$this->assertEquals('same_data', $this->cache->load('same_key'));
}
}

0 comments on commit 8afe846

Please sign in to comment.