Skip to content

Commit

Permalink
use query builder for better db vendor support
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Jan 24, 2015
1 parent 98f274c commit b9ba316
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
24 changes: 17 additions & 7 deletions library/PSX/Cache/Handler/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 11 additions & 11 deletions library/PSX/Session/Handler/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down

0 comments on commit b9ba316

Please sign in to comment.