2009-08-27 12:48:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
2014-01-28 12:38:06 +00:00
|
|
|
/*
|
2021-07-24 19:48:30 +00:00
|
|
|
(C) Copyright 2009,2021 Max Pozdeev <maxpozdeev@gmail.com>
|
2014-01-28 12:38:06 +00:00
|
|
|
Licensed under the GNU GPL v2 license. See file COPYRIGHT for details.
|
|
|
|
|
*/
|
2009-10-22 08:29:01 +00:00
|
|
|
|
2009-08-27 12:48:09 +00:00
|
|
|
class DatabaseResult_Sqlite3
|
|
|
|
|
{
|
|
|
|
|
private $q;
|
2021-07-24 19:48:30 +00:00
|
|
|
private $affected;
|
2009-08-27 12:48:09 +00:00
|
|
|
|
2021-07-24 19:48:30 +00:00
|
|
|
function __construct($dbh, $query, $resultless = 0)
|
2009-08-27 12:48:09 +00:00
|
|
|
{
|
2021-07-24 19:48:30 +00:00
|
|
|
// use with DELETE, INSERT, UPDATE
|
|
|
|
|
if ($resultless)
|
2009-08-27 12:48:09 +00:00
|
|
|
{
|
2021-07-24 19:48:30 +00:00
|
|
|
$this->affected = $dbh->exec($query); //throws PDOException
|
2009-08-27 12:48:09 +00:00
|
|
|
}
|
2021-07-24 19:48:30 +00:00
|
|
|
// SELECT
|
2009-08-27 12:48:09 +00:00
|
|
|
else
|
|
|
|
|
{
|
2021-07-24 19:48:30 +00:00
|
|
|
$this->q = $dbh->query($query); //throws PDOException
|
|
|
|
|
$this->affected = $this->q->rowCount();
|
2009-08-27 12:48:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fetch_row()
|
|
|
|
|
{
|
|
|
|
|
return $this->q->fetch(PDO::FETCH_NUM);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fetch_assoc()
|
|
|
|
|
{
|
|
|
|
|
return $this->q->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 19:48:30 +00:00
|
|
|
function rowsAffected()
|
|
|
|
|
{
|
|
|
|
|
return $this->affected;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-27 12:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Database_Sqlite3
|
|
|
|
|
{
|
2021-07-24 19:48:30 +00:00
|
|
|
private $dbh;
|
|
|
|
|
private $affected = null;
|
2009-08-27 12:48:09 +00:00
|
|
|
var $lastQuery;
|
2021-07-26 14:34:48 +00:00
|
|
|
var $prefix = '';
|
2009-08-27 12:48:09 +00:00
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function connect($filename)
|
|
|
|
|
{
|
2021-07-24 19:48:30 +00:00
|
|
|
$options = array(
|
|
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
|
|
|
|
);
|
|
|
|
|
$this->dbh = new PDO("sqlite:$filename", null, null, $options); //throws PDOException
|
2009-08-27 12:48:09 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 19:48:30 +00:00
|
|
|
/*
|
|
|
|
|
SELECT queries for single row
|
|
|
|
|
*/
|
2009-08-27 12:48:09 +00:00
|
|
|
function sq($query, $p = NULL)
|
|
|
|
|
{
|
|
|
|
|
$q = $this->_dq($query, $p);
|
|
|
|
|
|
|
|
|
|
$res = $q->fetch_row();
|
2021-07-24 19:48:30 +00:00
|
|
|
if ($res === false) return NULL;
|
2009-08-27 12:48:09 +00:00
|
|
|
|
2021-07-24 19:48:30 +00:00
|
|
|
if (sizeof($res) > 1) return $res;
|
2009-08-27 12:48:09 +00:00
|
|
|
else return $res[0];
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 19:48:30 +00:00
|
|
|
/*
|
|
|
|
|
SELECT queries for single row
|
|
|
|
|
*/
|
2009-08-27 12:48:09 +00:00
|
|
|
function sqa($query, $p = NULL)
|
|
|
|
|
{
|
|
|
|
|
$q = $this->_dq($query, $p);
|
|
|
|
|
|
|
|
|
|
$res = $q->fetch_assoc();
|
2021-07-24 19:48:30 +00:00
|
|
|
if ($res === false) return NULL;
|
|
|
|
|
return $res;
|
2009-08-27 12:48:09 +00:00
|
|
|
}
|
2020-10-14 18:20:44 +00:00
|
|
|
|
2021-07-24 19:48:30 +00:00
|
|
|
/*
|
|
|
|
|
SELECT queries for multiple rows
|
|
|
|
|
*/
|
2009-08-27 12:48:09 +00:00
|
|
|
function dq($query, $p = NULL)
|
|
|
|
|
{
|
|
|
|
|
return $this->_dq($query, $p);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 18:20:44 +00:00
|
|
|
/*
|
2021-07-24 19:48:30 +00:00
|
|
|
for resultless queries like INSERT,UPDATE,DELETE
|
2009-08-27 12:48:09 +00:00
|
|
|
*/
|
|
|
|
|
function ex($query, $p = NULL)
|
|
|
|
|
{
|
2021-07-24 19:48:30 +00:00
|
|
|
$dbr = $this->_dq($query, $p, 1);
|
|
|
|
|
return $this->affected();
|
2009-08-27 12:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function _dq($query, $p = NULL, $resultless = 0)
|
|
|
|
|
{
|
2021-07-24 19:48:30 +00:00
|
|
|
if (!isset($p)) $p = array();
|
|
|
|
|
elseif (!is_array($p)) $p = array($p);
|
2009-08-27 12:48:09 +00:00
|
|
|
|
|
|
|
|
$m = explode('?', $query);
|
|
|
|
|
|
2021-07-24 19:48:30 +00:00
|
|
|
if (sizeof($p) > 0)
|
2009-08-27 12:48:09 +00:00
|
|
|
{
|
2021-07-24 19:48:30 +00:00
|
|
|
if (sizeof($m) < sizeof($p)+1) {
|
2009-08-27 12:48:09 +00:00
|
|
|
throw new Exception("params to set MORE than query params");
|
|
|
|
|
}
|
2021-07-24 19:48:30 +00:00
|
|
|
if (sizeof($m) > sizeof($p)+1) {
|
2009-08-27 12:48:09 +00:00
|
|
|
throw new Exception("params to set LESS than query params");
|
|
|
|
|
}
|
|
|
|
|
$query = "";
|
2021-07-24 19:48:30 +00:00
|
|
|
for ($i=0; $i<sizeof($m)-1; $i++) {
|
2010-08-14 17:02:35 +00:00
|
|
|
$query .= $m[$i]. (is_null($p[$i]) ? 'NULL' : $this->quote($p[$i]));
|
2009-08-27 12:48:09 +00:00
|
|
|
}
|
|
|
|
|
$query .= $m[$i];
|
|
|
|
|
}
|
2021-07-24 19:48:30 +00:00
|
|
|
$this->lastQuery = $query;
|
|
|
|
|
$dbr = new DatabaseResult_Sqlite3($this->dbh, $query, $resultless);
|
|
|
|
|
$this->affected = $dbr->rowsAffected();
|
|
|
|
|
return $dbr;
|
2009-08-27 12:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function affected()
|
|
|
|
|
{
|
|
|
|
|
return $this->affected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function quote($s)
|
|
|
|
|
{
|
|
|
|
|
return $this->dbh->quote($s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function quoteForLike($format, $s)
|
|
|
|
|
{
|
|
|
|
|
$s = str_replace(array('\\','%','_'), array('\\\\','\%','\_'), $s);
|
|
|
|
|
return $this->dbh->quote(sprintf($format, $s)). " ESCAPE '\'";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function last_insert_id()
|
|
|
|
|
{
|
|
|
|
|
return $this->dbh->lastInsertId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function table_exists($table)
|
|
|
|
|
{
|
2021-06-27 12:44:21 +00:00
|
|
|
$exists = $this->sq("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?", $table);
|
|
|
|
|
if ($exists == "1") {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$exists = $this->sq("SELECT 1 FROM sqlite_temp_master WHERE type='table' AND name=?", $table);
|
|
|
|
|
if ($exists == "1") {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2009-08-27 12:48:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|