From b9ba316075843da8b6f092dcbb2c39c168b74c0a Mon Sep 17 00:00:00 2001 From: Christoph Kappestein Date: Sat, 24 Jan 2015 22:32:59 +0100 Subject: [PATCH] use query builder for better db vendor support --- library/PSX/Cache/Handler/Sql.php | 24 +++++++++++++++++------- library/PSX/Session/Handler/Sql.php | 22 +++++++++++----------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/library/PSX/Cache/Handler/Sql.php b/library/PSX/Cache/Handler/Sql.php index 07f4cf06..9deb3b69 100644 --- a/library/PSX/Cache/Handler/Sql.php +++ b/library/PSX/Cache/Handler/Sql.php @@ -67,16 +67,23 @@ public function __construct(Connection $connection, $tableName, ColumnAllocation public function load($key) { + $columnId = $this->allocation->get(self::COLUMN_ID); $columnContent = $this->allocation->get(self::COLUMN_CONTENT); $columnDate = $this->allocation->get(self::COLUMN_DATE); - $condition = new Condition(); - $condition->add($this->allocation->get(self::COLUMN_ID), '=', $key, 'AND'); - $condition->add($this->allocation->get(self::COLUMN_DATE), 'IS', 'NULL', 'OR', Condition::TYPE_RAW); - $condition->add($this->allocation->get(self::COLUMN_DATE), '>=', date(DateTime::SQL)); + $builder = $this->connection->createQueryBuilder(); + $builder = $builder->select(array($columnContent, $columnDate)) + ->from($this->tableName) + ->where($builder->expr()->eq($columnId, ':id')) + ->andWhere($builder->expr()->orX( + $builder->expr()->isNull($columnDate), + $builder->expr()->gte($columnDate, ':now') + )); - $sql = 'SELECT ' . $columnContent . ', ' . $columnDate . ' FROM ' . $this->tableName . ' ' . $condition->getStatment(); - $row = $this->connection->fetchAssoc($sql, $condition->getValues()); + $row = $this->connection->fetchAssoc($builder->getSQL(), array( + 'id' => $key, + 'now' => date(DateTime::SQL)) + ); if(!empty($row)) { @@ -115,7 +122,10 @@ public function remove($key) public function removeAll() { - $this->connection->query('DELETE FROM ' . $this->tableName . ' WHERE 1'); + $builder = $this->connection->createQueryBuilder() + ->delete($this->tableName); + + $this->connection->executeUpdate($builder->getSQL()); return true; } diff --git a/library/PSX/Session/Handler/Sql.php b/library/PSX/Session/Handler/Sql.php index b742f000..b0c4e2a4 100644 --- a/library/PSX/Session/Handler/Sql.php +++ b/library/PSX/Session/Handler/Sql.php @@ -66,13 +66,12 @@ public function close() public function read($id) { - $columnId = $this->allocation->get(self::COLUMN_ID); - $columnContent = $this->allocation->get(self::COLUMN_CONTENT); + $builder = $this->connection->createQueryBuilder() + ->select($this->allocation->get(self::COLUMN_CONTENT)) + ->from($this->tableName) + ->where($this->allocation->get(self::COLUMN_ID) . ' = :id'); - $sql = 'SELECT `' . $columnContent . '` FROM `' . $this->tableName . '` WHERE `' . $columnId . '` = :id'; - $content = $this->connection->fetchColumn($sql, array('id' => $id)); - - return $content; + return $this->connection->fetchColumn($builder->getSQL(), array('id' => $id)); } public function write($id, $data) @@ -97,12 +96,13 @@ public function destroy($id) public function gc($maxTime) { - $columnDate = $this->allocation->get(self::COLUMN_DATE); - - $maxTime = (int) $maxTime; - $sql = 'DELETE FROM `' . $this->tableName . '` WHERE DATE_ADD(`' . $columnDate . '`, INTERVAL :maxTime SECOND) < NOW()'; + $dateAdd = $this->connection->getDatabasePlatform()->getDateAddSecondsExpression($this->allocation->get(self::COLUMN_DATE), (int) $maxTime); + $now = $this->connection->getDatabasePlatform()->getNowExpression(); + $builder = $this->connection->createQueryBuilder() + ->delete($this->tableName) + ->where($dateAdd . ' < ' . $now); - $this->connection->executeUpdate($sql, array('maxTime' => $maxTime)); + $this->connection->executeUpdate($builder->getSQL(), array('maxTime' => $maxTime)); return true; }