several SQLite compatability improvements, as suggested by naikoto on the forums

git-svn-id: file:///home/shish/svn/shimmie2/trunk@680 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2008-01-02 21:49:12 +00:00
parent bcca76969e
commit 19d80a244f
5 changed files with 90 additions and 119 deletions

View file

@ -16,6 +16,7 @@ class Image {
public function Image($row=null) { public function Image($row=null) {
if(!is_null($row)) { if(!is_null($row)) {
foreach($row as $name => $value) { foreach($row as $name => $value) {
// FIXME: some databases use table.name rather than name
$this->$name = $value; // hax $this->$name = $value; // hax
} }
} }
@ -30,7 +31,7 @@ class Image {
if(!isset($this->tag_array)) { if(!isset($this->tag_array)) {
global $database; global $database;
$this->tag_array = Array(); $this->tag_array = Array();
$row = $database->Execute("SELECT * FROM image_tags JOIN tags ON image_tags.tag_id = tags.id WHERE image_id=? ORDER BY tag", array($this->id)); $row = $database->Execute("SELECT tag FROM image_tags JOIN tags ON image_tags.tag_id = tags.id WHERE image_id=? ORDER BY tag", array($this->id));
while(!$row->EOF) { while(!$row->EOF) {
$this->tag_array[] = $row->fields['tag']; $this->tag_array[] = $row->fields['tag'];
$row->MoveNext(); $row->MoveNext();

View file

@ -167,7 +167,7 @@ class TagList extends Extension {
global $config; global $config;
$query = " $query = "
SELECT COUNT(it3.image_id) as count, t3.tag SELECT COUNT(it3.image_id) as count, t3.tag AS tag
FROM FROM
image_tags AS it1, image_tags AS it1,
image_tags AS it2, image_tags AS it2,
@ -183,7 +183,7 @@ class TagList extends Extension {
AND t1.id = it1.tag_id AND t1.id = it1.tag_id
AND t3.id = it3.tag_id AND t3.id = it3.tag_id
GROUP BY it3.tag_id GROUP BY it3.tag_id
ORDER BY count DESC ORDER BY count(it3.image_id) DESC
LIMIT ? LIMIT ?
"; ";
$args = array($image->id, $config->get_int('tag_list_length')); $args = array($image->id, $config->get_int('tag_list_length'));
@ -230,7 +230,7 @@ class TagList extends Extension {
if($tags_ok) { if($tags_ok) {
$query = " $query = "
SELECT t2.tag, COUNT(it2.image_id) AS count SELECT t2.tag AS tag, COUNT(it2.image_id) AS count
FROM FROM
image_tags AS it1, image_tags AS it1,
image_tags AS it2, image_tags AS it2,
@ -242,7 +242,7 @@ class TagList extends Extension {
AND it1.tag_id = t1.id AND it1.tag_id = t1.id
AND it2.tag_id = t2.id AND it2.tag_id = t2.id
GROUP BY t2.tag GROUP BY t2.tag
ORDER BY count ORDER BY count(it2.image_id)
DESC LIMIT ? DESC LIMIT ?
"; ";
$args = array($config->get_int('tag_list_length')); $args = array($config->get_int('tag_list_length'));

View file

@ -62,4 +62,8 @@ $user = _get_user();
send_event(new InitExtEvent()); send_event(new InitExtEvent());
send_event(_get_page_request($page, $user)); send_event(_get_page_request($page, $user));
$page->display(); $page->display();
// for databases which support transactions
$database->db->CommitTrans(true);
?> ?>

View file

@ -492,145 +492,92 @@ function move_data($old_dsn, $new_dsn, $old_data) {
* Note: try and keep this as ANSI SQL compliant as possible, * Note: try and keep this as ANSI SQL compliant as possible,
* so that we can (in theory) support other databases * so that we can (in theory) support other databases
*/ */
function create_tables_mysql($db) { function create_tables_common($db, $auto_incrementing_id, $boolean, $true, $false, $ip) {
$db->StartTrans();
$db->Execute("SET NAMES utf8"); // FIXME: mysql-specific :(
$db->Execute("CREATE TABLE aliases ( $db->Execute("CREATE TABLE aliases (
oldtag varchar(255) NOT NULL, oldtag VARCHAR(255) NOT NULL PRIMARY KEY,
newtag varchar(255) NOT NULL, newtag VARCHAR(255) NOT NULL
PRIMARY KEY (oldtag)
)"); )");
$db->Execute("CREATE TABLE config ( $db->Execute("CREATE TABLE config (
name varchar(255) NOT NULL, name VARCHAR(255) NOT NULL PRIMARY KEY,
value text, value TEXT
PRIMARY KEY (name)
)"); )");
$db->Execute("CREATE TABLE images ( $db->Execute("CREATE TABLE images (
id int(11) NOT NULL auto_increment, id $auto_incrementing_id,
owner_id int(11) NOT NULL default '0', owner_id INTEGER NOT NULL,
owner_ip char(16) default NULL, owner_ip $ip,
filename varchar(64) NOT NULL default '', filename VARCHAR(64) NOT NULL DEFAULT '',
filesize int(11) NOT NULL default '0', filesize INTEGER NOT NULL,
hash char(32) NOT NULL default '', hash CHAR(32) NOT NULL UNIQUE,
ext char(4) NOT NULL default '', ext CHAR(4) NOT NULL,
source varchar(255), source VARCHAR(255),
width int(11) NOT NULL, width INTEGER NOT NULL,
height int(11) NOT NULL, height INTEGER NOT NULL,
posted datetime NOT NULL, posted TIMESTAMP NOT NULL
PRIMARY KEY (id),
UNIQUE (hash)
)");
$db->Execute("CREATE TABLE tags (
id int not null auto_increment primary key,
tag varchar(64) not null unique,
count int not null default 0,
KEY tags_count(count)
)");
$db->Execute("CREATE TABLE image_tags (
image_id int NOT NULL default 0,
tag_id int NOT NULL default 0,
UNIQUE KEY image_id_tag_id (image_id,tag_id),
KEY tags_tag_id (tag_id),
KEY tags_image_id (image_id)
)"); )");
$db->Execute("CREATE TABLE users ( $db->Execute("CREATE TABLE users (
id int(11) NOT NULL auto_increment, id $auto_incrementing_id,
name varchar(32) NOT NULL, name VARCHAR(32) NOT NULL UNIQUE,
pass char(32) default NULL, pass CHAR(32),
joindate datetime NOT NULL, joindate DATETIME NOT NULL,
enabled enum('N','Y') NOT NULL default 'Y', enabled $boolean NOT NULL DEFAULT $true,
admin enum('N','Y') NOT NULL default 'N', admin $boolean NOT NULL DEFAULT $false,
email varchar(255) default NULL, email VARCHAR(255)
PRIMARY KEY (id),
UNIQUE (name)
)"); )");
$db->Execute("CREATE TABLE layout ( $db->Execute("CREATE TABLE layout (
title varchar(64) primary key not null, title VARCHAR(64) PRIMARY KEY NOT NULL,
section varchar(32) not null default \"left\", section VARCHAR(32) NOT NULL DEFAULT 'left',
position int not null default 50, position INTEGER NOT NULL DEFAULT 50,
visible enum('Y', 'N') default 'Y' not null visible $boolean DEFAULT $true
)");
$db->Execute("INSERT INTO config(name, value) VALUES(?, ?)", Array('db_version', 5));
return $db->CommitTrans();
}
function create_tables_pgsql($db) {
$db->StartTrans();
$db->Execute("CREATE TABLE aliases (
oldtag varchar(255) NOT NULL,
newtag varchar(255) NOT NULL,
PRIMARY KEY (oldtag)
)");
$db->Execute("CREATE TABLE config (
name varchar(255) NOT NULL,
value text,
PRIMARY KEY (name)
)");
$db->Execute("CREATE TABLE images (
id SERIAL NOT NULL,
owner_id integer NOT NULL default '0',
owner_ip char(16) default NULL,
filename varchar(64) NOT NULL default '',
filesize integer NOT NULL default '0',
hash char(32) NOT NULL default '',
ext char(4) NOT NULL default '',
source varchar(255),
width integer NOT NULL,
height integer NOT NULL,
posted timestamp NOT NULL,
PRIMARY KEY (id),
UNIQUE (hash)
)"); )");
$db->Execute("CREATE TABLE tags ( $db->Execute("CREATE TABLE tags (
id SERIAL NOT NULL, id $auto_incrementing_id,
tag varchar(64) not null unique, tag VARCHAR(64) NOT NULL UNIQUE,
count int not null default 0, count INTEGER NOT NULL DEFAULT 0
PRIMARY KEY(id)
)"); )");
$db->Execute("CREATE INDEX tags__count ON tags(count)"); $db->Execute("CREATE INDEX tags__count ON tags(count)");
$db->Execute("CREATE TABLE image_tags ( $db->Execute("CREATE TABLE image_tags (
image_id int NOT NULL default 0, image_id INTEGER NOT NULL DEFAULT 0,
tag_id int NOT NULL default 0, tag_id INTEGER NOT NULL DEFAULT 0,
UNIQUE (image_id,tag_id) UNIQUE (image_id, tag_id)
)"); )");
$db->Execute("CREATE INDEX image_tags__tag_id ON image_tags(tag_id)"); $db->Execute("CREATE INDEX image_tags__tag_id ON image_tags(tag_id)");
$db->Execute("CREATE INDEX image_tags__image_id ON image_tags(image_id)"); $db->Execute("CREATE INDEX image_tags__image_id ON image_tags(image_id)");
$db->Execute("CREATE TABLE users (
id SERIAL NOT NULL,
name varchar(32) NOT NULL,
pass char(32) default NULL,
joindate timestamp NOT NULL,
enabled char(1) NOT NULL default 'Y',
admin char(1) NOT NULL default 'N',
email varchar(255) default NULL,
PRIMARY KEY (id),
UNIQUE (name)
)");
$db->Execute("CREATE TABLE layout (
title varchar(64) primary key not null,
section varchar(32) not null default 'left',
position int not null default 50,
visible char(1) default 'Y' not null
)");
$db->Execute("INSERT INTO config(name, value) VALUES(?, ?)", Array('db_version', 5)); $db->Execute("INSERT INTO config(name, value) VALUES(?, ?)", Array('db_version', 5));
}
function create_tables_mysql($db) {
$db->StartTrans();
$db->Execute("SET NAMES utf8");
create_tables_common($db,
"INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY",
"ENUM('Y', 'N')", "'Y'", "'N'",
"CHAR(15)"
);
return $db->CommitTrans();
}
function create_tables_pgsql($db) {
$db->StartTrans();
create_tables_common($db,
"SERIAL NOT NULL PRIMARY KEY",
"BOOLEAN", "True", "False",
"INET"
);
return $db->CommitTrans();
}
function create_tables_sqlite($db) {
$db->StartTrans();
create_tables_common($db,
"INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL",
"CHAR(1)", "'Y'", "'N'",
"CHAR(15)"
);
return $db->CommitTrans(); return $db->CommitTrans();
} }
// }}} // }}}

View file

@ -152,6 +152,15 @@ class ADODB_sqlite extends ADOConnection {
{ {
@sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1); @sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1);
@sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2); @sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2);
// XXX: shimmie customisation, as suggested by naikoto on the forums
@sqlite_create_function($this->_connectionID, 'UNIX_TIMESTAMP', 'UNIX_TIMESTAMP', 1);
@sqlite_create_function($this->_connectionID, 'now', 'now', 0);
@sqlite_create_function($this->_connectionID, 'floor', 'mfloor', 1);
@sqlite_create_function($this->_connectionID, 'log', 'mlog', 1);
@sqlite_create_function($this->_connectionID, 'isnull', 'fisnull', 1);
@sqlite_create_function($this->_connectionID, 'md5', 'fmd5', 1);
@sqlite_create_function($this->_connectionID, 'concat', 'fconcat', 2);
} }
@ -395,4 +404,14 @@ class ADORecordset_sqlite extends ADORecordSet {
} }
} }
?>
// shimmie functions
ini_set ( 'sqlite.assoc_case' , 0 );
function UNIX_TIMESTAMP($date) { return strtotime($date); }
function now() { return date("Y-m-d h:i:s"); }
function mfloor($a) { return floor($a); }
function mlog($a) { return log($a); }
function fisnull($a) { return is_null($a); }
function fmd5($a) { return md5($a); }
function fconcat($a, $b) { return $a . $b; }
?>