PostgreSQL errors

curtis - June 5, 2008 - 15:34
Project:Archive
Version:6.x-1.2
Component:Page
Category:bug report
Priority:critical
Assigned:Unassigned
Status:needs work
Description

The Archive module does not work properly with PostgreSQL for the following reasons.

Quoting: In PostgreSQL, double quotes signify a column name. Several queries in the Archive module use double quotes where single quotes should be used.

GROUP and ORDER: The archive pages use the following query, which fails in PostgreSQL:

$result = db_query(db_rewrite_sql("SELECT t.type, t.name, COUNT(n.nid) AS node_count FROM {node} n INNER JOIN {node_type} t ON t.type = n.type WHERE n.status = 1 AND t.type IN ('". join($types, "', '") ."') GROUP BY n.type ORDER BY n.created"))

I'm assuming that MySQL ORDERs the results before it GROUPs them. I can't comment as to whether this is correct from a standards standpoint, but this query fails in PostgreSQL.

You can get around this with PostgreSQL by using a sub-select. Here's code that works:

$result = db_query(db_rewrite_sql("SELECT r.type, r.name, COUNT(r.nid) AS node_count FROM (SELECT t.type, t.name, n.nid FROM {node} n INNER JOIN {node_type} t USING (type) WHERE n.status = 1 AND t.type IN ('". join($types, "', '") ."') ORDER BY n.created) AS r GROUP BY r.type, r.name"));

Patch: I'm attaching a patch that fixes both of these issues. Since I cannot test whether the sub-select will work in MySQL, I've created a simple test to see if $db_type = "pgsql" and if so, use the sub-select query. If that query works in MySQL as well, you can probably just use that one and get rid of the other version.

AttachmentSize
archive.module.patch502 bytes

#1

curtis - June 5, 2008 - 15:35
AttachmentSize
archive.pages_.inc_.patch 1.77 KB

#2

Susurrus - August 18, 2008 - 08:38
Status:active» needs review

It seems like standard SQL requires all fields in the ORDER BY clause to be returned in the data set. Can you try the following instead? I'd really like to minimize switching on DB_TYPE:

$result = db_query(db_rewrite_sql('SELECT t.type, t.name, COUNT(n.nid) AS node_count, n.type, n.created FROM {node} n INNER JOIN {node_type} t ON t.type = n.type WHERE n.status = 1 AND t.type IN ("'. join($types, '", "') .'") GROUP BY n.type ORDER BY n.created'));

#3

Susurrus - August 19, 2008 - 22:19

I've committed all the changes for the double-quotes for 5.x and 6.x.

I'd still like your input on my suggestion in #2, as I'd prefer not to branch on the database type, especially since this won't be an issue anymore in 7.x

#4

Susurrus - August 23, 2008 - 23:35
Status:needs review» needs work

#5

curtis - November 19, 2008 - 20:17

Sorry it's taken awhile to reply. Had to find time to play around with it.

You have the right idea, you just put the column names in the wrong place. This patch works in postgres without having to branch. Hopefully it doesn't break mysql!

AttachmentSize
archive.pages_.inc_.diff 1.38 KB
 
 

Drupal is a registered trademark of Dries Buytaert.