From: Steinar H. Gunderson Date: Tue, 19 Jun 2007 13:15:53 +0000 (+0200) Subject: Make some sorely needed changes to the SQL schema, and update the code X-Git-Url: https://git.sesse.net/?p=pr0n;a=commitdiff_plain;h=6be9a26e71fc85e69cbbdbc95fc50d3569050921;ds=sidebyside Make some sorely needed changes to the SQL schema, and update the code accordingly (I really hope I fixed everything; I've done some testing, and it seems largely OK). The biggest change is that the event primary key is now (vhost,event) and not just (event), which is rippling through all the tables; this means the vhosts are finally truly independent. Also clean up to remove some old length restrictions from the MySQL days. --- diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index c34aa65..6fb5536 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -34,7 +34,7 @@ BEGIN { require Sesse::pr0n::Config_local; }; - $VERSION = "v2.30"; + $VERSION = "v2.40"; @ISA = qw(Exporter); @EXPORT = qw(&error &dberror); %EXPORT_TAGS = qw(); @@ -220,7 +220,7 @@ sub update_image_info { # update the last_picture cache as well (this should of course be done # via a trigger, but this is less complicated :-) ) - $dbh->do('UPDATE last_picture_cache SET last_picture=GREATEST(last_picture, ?) WHERE event=(SELECT event FROM images WHERE id=?)', + $dbh->do('UPDATE last_picture_cache SET last_picture=GREATEST(last_picture, ?) WHERE (vhost,event)=(SELECT vhost,event FROM images WHERE id=?)', undef, $datetime, $id) or die "Couldn't update last_picture in SQL: $!"; } @@ -573,11 +573,11 @@ sub add_new_event { return @errors; } - $dbh->do("INSERT INTO events (id,date,name,vhost) VALUES (?,?,?,?)", + $dbh->do("INSERT INTO events (event,date,name,vhost) VALUES (?,?,?,?)", undef, $id, $date, $desc, $vhost) or return ("Kunne ikke sette inn ny hendelse" . $dbh->errstr); - $dbh->do("INSERT INTO last_picture_cache (event,last_picture) VALUES (?,NULL)", - undef, $id) + $dbh->do("INSERT INTO last_picture_cache (vhost,event,last_picture) VALUES (?,?,NULL)", + undef, $vhost, $id) or return ("Kunne ikke sette inn ny cache-rad" . $dbh->errstr); return (); diff --git a/perl/Sesse/pr0n/Image.pm b/perl/Sesse/pr0n/Image.pm index e436428..d1743b4 100644 --- a/perl/Sesse/pr0n/Image.pm +++ b/perl/Sesse/pr0n/Image.pm @@ -46,8 +46,8 @@ sub handler { # or error($r, "Could not find $event", 404, "File not found"); # Look it up in the database - my $ref = $dbh->selectrow_hashref('SELECT id,width,height FROM images WHERE event=? AND filename=?', - undef, $event, $filename); + my $ref = $dbh->selectrow_hashref('SELECT id,width,height FROM images WHERE event=? AND vhost=? AND filename=?', + undef, $event, $r->get_server_name, $filename); error($r, "Could not find $event/$filename", 404, "File not found") unless (defined($ref)); $id = $ref->{'id'}; diff --git a/perl/Sesse/pr0n/Index.pm b/perl/Sesse/pr0n/Index.pm index e91d9c1..e331cc4 100644 --- a/perl/Sesse/pr0n/Index.pm +++ b/perl/Sesse/pr0n/Index.pm @@ -46,8 +46,8 @@ sub handler { ); # Any NEF files => default to processing - my $ref = $dbh->selectrow_hashref('SELECT * FROM images WHERE event=? AND LOWER(filename) LIKE \'%.nef\' LIMIT 1', - undef, $event) + my $ref = $dbh->selectrow_hashref('SELECT * FROM images WHERE event=? AND vhost=? AND LOWER(filename) LIKE \'%.nef\' LIMIT 1', + undef, $event, $r->get_server_name) and $defsettings{'xres'} = $defsettings{'yres'} = undef; # Reduce the front page load when in overload mode. @@ -82,7 +82,7 @@ sub handler { $num = undef; } - $ref = $dbh->selectrow_hashref('SELECT name,date,EXTRACT(EPOCH FROM last_update) AS last_update FROM events WHERE id=? AND vhost=?', + $ref = $dbh->selectrow_hashref('SELECT name,date,EXTRACT(EPOCH FROM last_update) AS last_update FROM events WHERE event=? AND vhost=?', undef, $event, $r->get_server_name) or error($r, "Could not find event $event", 404, "File not found"); @@ -96,16 +96,16 @@ sub handler { } # Count the number of selected images. - $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS num_selected FROM images WHERE event=? AND selected=\'t\'", undef, $event); + $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS num_selected FROM images WHERE event=? AND vhost=? AND selected=\'t\'", undef, $event, $r->get_server_name); my $num_selected = $ref->{'num_selected'}; # Find all images related to this event. my $where = ($all == 0) ? ' AND selected=\'t\'' : ''; my $limit = (defined($start) && defined($num) && !$settings{'fullscreen'}) ? (" LIMIT $num OFFSET " . ($start-1)) : ""; - my $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE event=? $where ORDER BY (date - INTERVAL '6 hours')::date,takenby,date,filename $limit") + my $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE event=? AND vhost=? $where ORDER BY (date - INTERVAL '6 hours')::date,takenby,date,filename $limit") or dberror($r, "prepare()"); - $q->execute($event) + $q->execute($event, $r->get_server_name) or dberror($r, "image enumeration"); # Print the page itself @@ -154,7 +154,7 @@ sub handler { TRIM(model.value) AS model, coalesce(TRIM(lens_spec.value), TRIM(lens.value)) AS lens, COUNT(*) AS num - FROM ( SELECT * FROM images WHERE event=? $where ORDER BY (date - INTERVAL '6 hours')::date,takenby,date,filename $limit ) i + FROM ( SELECT * FROM images WHERE event=? AND vhost=? $where ORDER BY (date - INTERVAL '6 hours')::date,takenby,date,filename $limit ) i LEFT JOIN exif_info model ON i.id=model.image LEFT JOIN ( SELECT * FROM exif_info WHERE tag='Lens' ) lens ON i.id=lens.image LEFT JOIN ( SELECT * FROM exif_info WHERE tag='LensSpec') lens_spec ON i.id=lens_spec.image @@ -162,7 +162,7 @@ sub handler { GROUP BY 1,2 ORDER BY 1,2") or die "Couldn't prepare to find equipment: $!"; - $eq->execute($event) + $eq->execute($event, $r->get_server_name) or die "Couldn't find equipment: $!"; my @equipment = (); @@ -441,8 +441,8 @@ sub print_nextprev { return unless (defined($start) && defined($num)); # determine total number - my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE event=? $where", - undef, $event) + my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE event=? AND vhost=? $where", + undef, $event, $r->get_server_name) or dberror($r, "image enumeration"); my $num_images = $ref->{'num_images'}; diff --git a/perl/Sesse/pr0n/Listing.pm b/perl/Sesse/pr0n/Listing.pm index 473914e..02ac273 100644 --- a/perl/Sesse/pr0n/Listing.pm +++ b/perl/Sesse/pr0n/Listing.pm @@ -18,7 +18,7 @@ sub handler { # my $q = $dbh->prepare('SELECT t1.id,t1.date,t1.name FROM events t1 LEFT JOIN images t2 ON t1.id=t2.event WHERE t1.vhost=? GROUP BY t1.id,t1.date,t1.name ORDER BY COALESCE(MAX(t2.date),\'1970-01-01 00:00:00\'),t1.id') or # dberror($r, "Couldn't list events"); - my $q = $dbh->prepare('SELECT id,date,name FROM events e JOIN last_picture_cache c ON e.id=c.event WHERE vhost=? ORDER BY last_picture DESC') + my $q = $dbh->prepare('SELECT event,date,name FROM events e JOIN last_picture_cache c USING (vhost,event) WHERE vhost=? ORDER BY last_picture DESC') or dberror($r, "Couldn't list events"); $q->execute($r->get_server_name) or dberror($r, "Couldn't get events"); @@ -29,7 +29,7 @@ sub handler { $r->print("