type_cast = Pluf_DB_defaultTypecast(); $this->debug = $debug; $this->pfx = $pfx; $this->debug('* SQLITE OPEN'); $this->type_cast['Pluf_DB_Field_Compressed'] = array('Pluf_DB_CompressedFromDb', 'Pluf_DB_SQLite_CompressedToDb'); // Connect and let the Exception be thrown in case of problem try { $this->con_id = new PDO('sqlite:'.$dbname); } catch (PDOException $e) { throw $e; } } /** * Get the version of the SQLite library. * * @return string Version string */ function getServerInfo() { return $this->con_id->getAttribute(PDO::ATTR_SERVER_INFO); } /** * Log the queries. Keep track of the last query and if in debug mode * keep track of all the queries in * $GLOBALS['_PX_debug_data']['sql_queries'] * * @param string Query to keep track * @return bool true */ function debug($query) { $this->lastquery = $query; if (!$this->debug) return true; if (!isset($GLOBALS['_PX_debug_data']['sql_queries'])) $GLOBALS['_PX_debug_data']['sql_queries'] = array(); $GLOBALS['_PX_debug_data']['sql_queries'][] = $query; return true; } function close() { $this->con_id = null; return true; } function select($query) { $this->debug($query); if (false === ($cur = $this->con_id->query($query))) { throw new Exception($this->getError()); } return $cur->fetchAll(PDO::FETCH_ASSOC); } function execute($query) { $this->debug($query); if (false === ($cur = $this->con_id->exec($query))) { throw new Exception($this->getError()); } return $cur; } function getLastID() { $this->debug('* GET LAST ID'); return (int) $this->con_id->lastInsertId(); } /** * Returns a string ready to be used in the exception. * * @return string Error string */ function getError() { $err = $this->con_id->errorInfo(); $err[] = $this->lastquery; return implode(' - ', $err); } function esc($str) { return $this->con_id->quote($str); } /** * Quote the column name. * * @param string Name of the column * @return string Escaped name */ function qn($col) { return '"'.$col.'"'; } /** * Start a transaction. */ function begin() { $this->execute('BEGIN'); } /** * Commit a transaction. */ function commit() { $this->execute('COMMIT'); } /** * Rollback a transaction. */ function rollback() { $this->execute('ROLLBACK'); } function __toString() { return 'con_id.')>'; } } function Pluf_DB_SQLite_CompressedToDb($val, $con) { if (is_null($val)) { return 'NULL'; } return 'X'.$con->esc(bin2hex(gzdeflate($val, 9))); }