This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/database.class.php

306 lines
7.7 KiB
PHP
Raw Normal View History

<?php
require_once "compat.inc.php";
$ADODB_CACHE_DIR=sys_get_temp_dir();
require_once "lib/adodb/adodb.inc.php";
2009-01-04 14:34:27 +00:00
require_once "lib/adodb/adodb-exceptions.inc.php";
/* Querylet {{{
* A fragment of a query, used to build large search queries
*/
class Querylet {
var $sql;
var $variables;
2009-01-04 19:18:37 +00:00
public function Querylet($sql, $variables=array()) {
$this->sql = $sql;
$this->variables = $variables;
}
public function append($querylet) {
assert(!is_null($querylet));
$this->sql .= $querylet->sql;
$this->variables = array_merge($this->variables, $querylet->variables);
}
public function append_sql($sql) {
$this->sql .= $sql;
}
public function add_variable($var) {
$this->variables[] = $var;
}
}
class TagQuerylet {
var $tag;
var $positive;
public function TagQuerylet($tag, $positive) {
$this->tag = $tag;
$this->positive = $positive;
}
}
class ImgQuerylet {
var $qlet;
var $positive;
public function ImgQuerylet($qlet, $positive) {
$this->qlet = $qlet;
$this->positive = $positive;
}
}
// }}}
2009-01-19 18:18:23 +00:00
// {{{ db engines
class DBEngine {
var $name = null;
2009-01-22 12:05:55 +00:00
2009-01-22 13:53:30 +00:00
public function init($db) {}
2009-01-22 12:05:55 +00:00
public function create_table_sql($name, $data) {
return "CREATE TABLE $name ($data)";
}
}
class MySQL extends DBEngine {
var $name = "mysql";
2009-01-22 12:05:55 +00:00
public function init($db) {
$db->Execute("SET NAMES utf8;");
}
2009-01-22 12:05:55 +00:00
public function create_table_sql($name, $data) {
$data = str_replace("SCORE_AIPK", "INTEGER PRIMARY KEY auto_increment", $data);
$data = str_replace("SCORE_INET", "CHAR(15)", $data);
$data = str_replace("SCORE_BOOL_Y", "'Y'", $data);
$data = str_replace("SCORE_BOOL_N", "'N'", $data);
2009-01-22 12:14:38 +00:00
$data = str_replace("SCORE_BOOL", "ENUM('Y', 'N')", $data);
2009-01-22 12:12:15 +00:00
$data = str_replace("SCORE_NOW", "\"1970-01-01\"", $data);
2009-01-22 12:05:55 +00:00
$ctes = "TYPE=InnoDB DEFAULT CHARSET='utf8'";
return "CREATE TABLE $name ($data) $ctes";
}
}
class PostgreSQL extends DBEngine {
var $name = "pgsql";
2009-01-22 12:05:55 +00:00
public function create_table_sql($name, $data) {
$data = str_replace("SCORE_AIPK", "SERIAL PRIMARY KEY", $data);
$data = str_replace("SCORE_INET", "INET", $data);
$data = str_replace("SCORE_BOOL_Y", "'t'", $data);
$data = str_replace("SCORE_BOOL_N", "'f'", $data);
2009-01-22 12:14:38 +00:00
$data = str_replace("SCORE_BOOL", "BOOL", $data);
2009-01-22 13:53:30 +00:00
$data = str_replace("SCORE_NOW", "current_time", $data);
return "CREATE TABLE $name ($data)";
}
}
// shimmie functions for export to sqlite
function _unix_timestamp($date) { return strtotime($date); }
function _now() { return date("Y-m-d h:i:s"); }
function _floor($a) { return floor($a); }
function _log1($a) { return log($a); }
function _log2($a, $b) { return log($a, $b); }
function _isnull($a) { return is_null($a); }
function _md5($a) { return md5($a); }
function _concat($a, $b) { return $a . $b; }
function _lower($a) { return strtolower($a); }
2009-01-22 13:53:30 +00:00
class SQLite extends DBEngine {
var $name = "sqlite";
2009-01-22 15:08:37 +00:00
public function init($db) {
ini_set('sqlite.assoc_case', 0);
@sqlite_create_function($db->_connectionID, 'UNIX_TIMESTAMP', '_unix_timestamp', 1);
@sqlite_create_function($db->_connectionID, 'now', '_now', 0);
@sqlite_create_function($db->_connectionID, 'floor', '_floor', 1);
@sqlite_create_function($db->_connectionID, 'log', '_log1', 1);
@sqlite_create_function($db->_connectionID, 'log', '_log2', 2);
@sqlite_create_function($db->_connectionID, 'isnull', '_isnull', 1);
@sqlite_create_function($db->_connectionID, 'md5', '_md5', 1);
@sqlite_create_function($db->_connectionID, 'concat', '_concat', 2);
@sqlite_create_function($db->_connectionID, 'lower', '_lower', 1);
2009-01-22 15:08:37 +00:00
}
2009-01-22 13:53:30 +00:00
public function create_table_sql($name, $data) {
$data = str_replace("SCORE_AIPK", "INTEGER PRIMARY KEY", $data);
2009-01-22 15:08:37 +00:00
$data = str_replace("SCORE_INET", "VARCHAR(15)", $data);
2009-01-22 13:53:30 +00:00
$data = str_replace("SCORE_BOOL_Y", "'Y'", $data);
$data = str_replace("SCORE_BOOL_N", "'N'", $data);
2009-01-22 15:08:37 +00:00
$data = str_replace("SCORE_BOOL", "CHAR(1)", $data);
$data = str_replace("SCORE_NOW", "\"1970-01-01\"", $data);
$cols = array();
$extras = "";
foreach(explode(",", $data) as $bit) {
$matches = array();
2009-01-22 15:51:50 +00:00
if(preg_match("/INDEX\s*\((.*)\)/", $bit, $matches)) {
2009-01-22 15:08:37 +00:00
$col = $matches[1];
$extras .= "CREATE INDEX {$name}_{$col} on $name($col);";
}
else {
$cols[] = $bit;
}
}
$cols_redone = implode(", ", $cols);
return "CREATE TABLE $name ($cols_redone); $extras";
}
}
2009-01-19 18:18:23 +00:00
// }}}
// {{{ cache engines
interface CacheEngine {
2009-01-20 10:47:20 +00:00
public function get($key);
2009-01-20 11:54:43 +00:00
public function set($key, $val, $time=0);
2009-01-20 10:47:20 +00:00
public function delete($key);
2009-01-20 11:54:43 +00:00
public function get_hits();
public function get_misses();
2009-01-20 10:47:20 +00:00
}
class NoCache implements CacheEngine {
public function get($key) {return false;}
2009-01-20 11:54:43 +00:00
public function set($key, $val, $time=0) {}
2009-01-20 10:47:20 +00:00
public function delete($key) {}
2009-01-20 11:54:43 +00:00
public function get_hits() {return 0;}
public function get_misses() {return 0;}
2009-01-19 18:18:23 +00:00
}
class MemCache implements CacheEngine {
2009-01-20 11:54:43 +00:00
var $hits=0, $misses=0;
2009-01-20 10:47:20 +00:00
public function __construct($args) {
$this->memcache = new Memcache;
$this->memcache->pconnect('localhost', 11211) or ($this->use_memcache = false);
2009-01-19 18:18:23 +00:00
}
2009-01-20 10:47:20 +00:00
public function get($key) {
2009-01-19 18:18:23 +00:00
assert(!is_null($key));
2009-01-20 10:47:20 +00:00
$val = $this->memcache->get($key);
2009-01-19 18:18:23 +00:00
if($val) {
2009-01-20 10:47:20 +00:00
$this->hits++;
2009-01-19 18:18:23 +00:00
return $val;
}
else {
2009-01-20 10:47:20 +00:00
$this->misses++;
2009-01-19 18:18:23 +00:00
return false;
}
}
2009-01-20 10:47:20 +00:00
public function set($key, $val, $time=0) {
2009-01-19 18:18:23 +00:00
assert(!is_null($key));
2009-01-20 10:47:20 +00:00
$this->memcache->set($key, $val, false, $time);
2009-01-19 18:18:23 +00:00
}
2009-01-20 10:47:20 +00:00
public function delete($key) {
2009-01-19 18:18:23 +00:00
assert(!is_null($key));
2009-01-20 10:47:20 +00:00
$this->memcache->delete($key);
2009-01-19 18:18:23 +00:00
}
2009-01-20 11:54:43 +00:00
public function get_hits() {return $this->hits;}
public function get_misses() {return $this->misses;}
2009-01-19 18:18:23 +00:00
}
// }}}
/*
2009-01-03 20:32:57 +00:00
* A class for controlled database access
*/
class Database {
var $db;
var $extensions;
var $engine = null;
2009-01-19 18:18:23 +00:00
var $cache = null;
/*
* Create a new database object using connection info
* stored in config.php in the root shimmie folder
*/
public function Database() {
if(is_readable("config.php")) {
require_once "config.php";
2009-01-22 15:08:37 +00:00
if(substr($database_dsn, 0, 5) == "mysql") {
$this->engine = new MySQL();
}
else if(substr($database_dsn, 0, 5) == "pgsql") {
$this->engine = new PostgreSQL();
}
else if(substr($database_dsn, 0, 6) == "sqlite") {
$this->engine = new SQLite();
}
$this->db = @NewADOConnection($database_dsn);
2009-01-19 18:18:23 +00:00
if(isset($cache)) {
2009-01-20 10:47:20 +00:00
$matches = array();
preg_match("#(memcache)://(.*)#", $cache, $matches);
if($matches[1] == "memcache") {
$this->cache = new MemCache($matches[2]);
}
}
else {
$this->cache = new NoCache();
2009-01-19 18:18:23 +00:00
}
if($this->db) {
$this->db->SetFetchMode(ADODB_FETCH_ASSOC);
$this->engine->init($this->db);
}
else {
$version = VERSION;
print "
<html>
<head>
<title>Internal error - Shimmie-$version</title>
</head>
<body>
Internal error: Could not connect to database
</body>
</html>
";
exit;
}
}
else {
header("Location: install.php");
exit;
}
}
public function execute($query, $args=array()) {
$result = $this->db->Execute($query, $args);
if($result === False) {
print "SQL Error: " . $this->db->ErrorMsg();
print "<br>Query: $query";
print "<br>Args: "; print_r($args);
exit;
}
return $result;
}
public function get_all($query, $args=array()) {
$result = $this->db->GetAll($query, $args);
if($result === False) {
print "SQL Error: " . $this->db->ErrorMsg();
print "<br>Query: $query";
print "<br>Args: "; print_r($args);
exit;
}
return $result;
}
public function get_row($query, $args=array()) {
$result = $this->db->GetRow($query, $args);
if($result === False) {
print "SQL Error: " . $this->db->ErrorMsg();
print "<br>Query: $query";
print "<br>Args: "; print_r($args);
exit;
}
if(count($result) == 0) {
return null;
}
else {
return $result;
}
}
2009-01-22 11:22:55 +00:00
public function create_table($name, $data) {
2009-01-22 12:05:55 +00:00
$this->execute($this->engine->create_table_sql($name, $data));
2009-01-22 11:22:55 +00:00
}
}
?>