* @package Mammut\DB\Adapter\SQLite */ class SQLite extends \Mammut\DB\DB { const _VERSION_ = '1.1.0.0'; /** * @var SQLite3 */ protected $dbi = false; protected $file = false; /** * * @var iCache */ protected $cache; public function __construct($cstring, $user, $password, array $options = array(), iCache $cache = NULL) { if(!extension_loaded('sqlite3')) throw new \Mammut\Exception\ExtensionException('sqlite3 extension for sqlite missing'); $this->dialect = new \Mammut\DB\Sql\Dialect\Sqlite(); $this->file = $cstring; $this->cache = $cache; $this->dbi = new \SQLite3($cstring); if(empty($this->dbi)) throw new DBException('not connected'); } public function setDatabase($name) { throw new ImplementationException('not implemented'); } public function getDatabase() { return $this->file; } public function getSchema() { return $this->file; } /** * (non-PHPdoc) * * @see \Mammut\DB\DB::newQuery() */ public function newQuery($type) { switch($type) { case iQuery::Q_DELETE: return new \Mammut\DB\SQLite\Query\Delete(); case iQuery::Q_INSERT: return new \Mammut\DB\SQLite\Query\Insert(); case iQuery::Q_SELECT: return new \Mammut\DB\SQLite\Query\Select(); case iQuery::Q_UPDATE: return new \Mammut\DB\SQLite\Query\Update(); } throw new InvalidArgumentException($type); } public function query($query, $limit = -1, $skip = 0) { if(self::$LOGGER) self::$LOGGER->log(LOG_DEBUG, get_class($this) . ': sending new query: ' . $query); $r = $this->dbi->query($query); return new Result($r); } public function prepareStatement($query, $limit = -1, $skip = 0) { throw new ImplementationException('not implemented'); } public function getArray($sql) { throw new ImplementationException('not implemented'); } public function getRow($sql) { throw new ImplementationException('not implemented'); } public function getObject($sql, $class = false, $param = array()) { throw new ImplementationException('not implemented'); } public function getInsertId() { throw new ImplementationException('not implemented'); } public function getAffectedRowCount() { throw new ImplementationException('not implemented'); } public function createTable(TableInfo $info) { $sql = SQLiteUtil::createTableSQL($this, $info, $this->getSchema()); echo $sql; $result = $this->dbi->exec($sql); $this->qcount++; echo $this->dbi->lastErrorMsg(); if(!$result) throw new SQLException($this->dbi->lastErrorMsg(), $sql); $this->tables = false; } public function createView(\Mammut\DB\Model\ViewInfo $info) { $sql = $this->dialect->getDdl()->getCreateViewSQL($info); $this->query($sql); } public function table($table) { if($table instanceof \Mammut\DB\Model\TableInfo) $table = $table->getName(); if(array_search($table, $this->tableList()) === false) throw new DBException('table "' . $table . '" dosen\'t exist'); return new Table($this, $table); } public function tableList() { $result = false; $query = "SELECT * FROM sqlite_master WHERE type='table'"; $result = $this->dbi->query($query); $this->qcount++; if($this->dbi->lastErrorCode() != 0) throw new SQLException($this->dbi->lastErrorMsg(), $query); if($result) { $tables = array(); while($next = $result->fetchArray()) { $tables[] = $next[0]; } $result->finalize(); return $tables; } else return false; } public function getTableInfo($tablename) { throw new ImplementationException('not implemented'); } public function dateCol2uts($date) { throw new ImplementationException('not implemented'); } public function uts2dateCol($uts) { throw new ImplementationException('not implemented'); } public function datetimeCol2uts($datetime) { throw new ImplementationException('not implemented'); } public function uts2datetimeCol($uts) { throw new ImplementationException('not implemented'); } }