Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions core/DB/SQLitePDO.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Aleksey S. Denisov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* SQLitePDO DB connector.
*
* @see http://www.sqlite.org/
* @see http://www.php.net/manual/en/ref.pdo-sqlite.php
*
* @ingroup DB
**/
final class SQLitePDO extends Sequenceless
{

const ERROR_CONSTRAINT = 19;
/**
* @var PDO
*/
protected $link = null;

/**
* @return LiteDialect
**/
public static function getDialect()
{
return LiteDialect::me();
}

/**
* @return SQLitePDO
**/
public function connect()
{
try {
$this->link = new PDO(
"sqlite:{$this->basename}",
'',
'',
array(PDO::ATTR_PERSISTENT => $this->persistent)
);
} catch (PDOException $e) {
throw new DatabaseException(
'can not open SQLitePDO base: '
.$e->getMessage()
);
}

return $this;
}

/**
* @return SQLitePDO
**/
public function disconnect()
{
if ($this->link) {
$this->link = null;
}

return $this;
}

public function isConnected()
{
return $this->link !== null;
}

/**
* misc
**/
public function setDbEncoding()
{
throw new UnsupportedMethodException();
}

/**
* query methods
**/
public function queryRaw($queryString)
{
try {
$res = $this->link->query($queryString);
return $res;
} catch (PDOException $e) {
$code = $e->getCode();

if ($code == self::ERROR_CONSTRAINT)
$e = 'DuplicateObjectException';
else
$e = 'DatabaseException';

throw new $e(
$e->getMessage(),
$code
);
}
}

/**
* Same as query, but returns number of affected rows
* Returns number of affected rows in insert/update queries
**/
public function queryCount(Query $query)
{
$res = $this->queryNull($query);
/* @var $res PDOStatement */

return $res->rowCount();
}

public function queryRow(Query $query)
{
$res = $this->query($query);
/* @var $res PDOStatement */

$array = $res->fetchAll(PDO::FETCH_ASSOC);
if (count($array) > 1)
throw new TooManyRowsException(
'query returned too many rows (we need only one)'
);
elseif (count($array) == 1)
return reset($array);
else
return null;
}

public function queryColumn(Query $query)
{
$res = $this->query($query);
/* @var $res PDOStatement */

$resArray = $res->fetchAll(PDO::FETCH_ASSOC);
if ($resArray) {
$array = array();
foreach ($resArray as $row) {
$array[] = reset($row);
}

return $array;
} else
return null;
}

public function querySet(Query $query)
{
$res = $this->query($query);
/* @var $res PDOStatement */

return $res->fetchAll(PDO::FETCH_ASSOC) ?: null;
}

public function hasQueue()
{
return false;
}

public function getTableInfo($table)
{
throw new UnimplementedFeatureException();
}

protected function getInsertId()
{
return $this->link->lastInsertId();
}
}
?>