Hello all

Sorry if this has already been posted but I can't find any cases which resemble this. I'm using the latest -dev of Date. I accidentally installed 2.4 stable over an existing 2.x-dev install (stable version had been causing me issues for a while so was happily using -dev until I made the mistake of upgrading before my morning coffee ;o). To fix it I installed the latest -dev over the top of 2.4, doing all the necessaries like removing old code and updating the DB. I've even tried disabling all date-based modules and then re-enabling them with no luck.

The cause is that, somewhere between building the 'preview' SQL used in Views' preview function, and sending the query to the DB, the word "from" in the Date CCK field title is being capitalised. You don't see anything until you examine the event log, but - interestingly - the pagers are all working (so I see "< prev 1 2 3 next >" at the bottom of empty views - the error is occurring only in the query to fetch the data, but pager is ok.

An example of the broken SQL from the even log (tidied up a bit) - note the third line of the query:

Unknown column 'node_data_field_datetime_FROMto.field_datetime_fromto_value' in 'field list'
query: execute

SELECT node.nid AS nid,
   node.title AS node_title,
   node_data_field_datetime_FROMto.field_datetime_fromto_value AS node_data_field_datetime_fromto_field_datetime_fromto_value,
   node_data_field_datetime_fromto.field_datetime_fromto_value2 AS node_data_field_datetime_fromto_field_datetime_fromto_value2,
   node.type AS node_type,
   node.vid AS node_vid,
   location.name AS location_name,
   location.city AS location_city,
   location.province AS location_province,
   location.country AS location_country
 FROM drupal_node node 
 INNER JOIN drupal_term_node term_node ON node.vid = term_node.vid
 LEFT JOIN drupal_content_type_event node_data_field_datetime_fromto ON node.vid = node_data_field_datetime_fromto.vid
 LEFT JOIN drupal_location_instance location_instance ON node.vid = location_instance.vid
 LEFT JOIN drupal_location location ON location_instance.lid = location.lid
 WHERE ((term_node.tid = 338) AND (node.status <> 0) AND (node.type in ('event')))
    AND (DATE_FORMAT(ADDTIME(node_data_field_datetime_fromto.field_datetime_fromto_value, SEC_TO_TIME(3600)), '%Y-%m-%d\T%H:%i') >= '2009-10-13T11:15')
   ORDER BY node_data_field_datetime_fromto_field_datetime_fromto_value ASC
 LIMIT 0, 20

 in /var/....../sites/all/modules/views/includes/view.inc on line 755

This is how the query is being shown in the Views preview:

SELECT node.nid AS nid,
   node.title AS node_title,
   node_data_field_datetime_fromto.field_datetime_fromto_value AS node_data_field_datetime_fromto_field_datetime_fromto_value,
   node_data_field_datetime_fromto.field_datetime_fromto_value2 AS node_data_field_datetime_fromto_field_datetime_fromto_value2,
   node.type AS node_type,
   node.vid AS node_vid,
   location.name AS location_name,
   location.city AS location_city,
   location.province AS location_province,
   location.country AS location_country
 FROM drupal_node node 
 INNER JOIN drupal_term_node term_node ON node.vid = term_node.vid
 LEFT JOIN drupal_content_type_event node_data_field_datetime_fromto ON node.vid = node_data_field_datetime_fromto.vid
 LEFT JOIN drupal_location_instance location_instance ON node.vid = location_instance.vid
 LEFT JOIN drupal_location location ON location_instance.lid = location.lid
 WHERE ((term_node.tid = 338) AND (node.status <> 0) AND (node.type in ('event')))
    AND (DATE_FORMAT(ADDTIME(node_data_field_datetime_fromto.field_datetime_fromto_value, SEC_TO_TIME(3600)), '%Y-%m-%d\T%H:%i') >= '2009-10-13T11:14')
   ORDER BY node_data_field_datetime_fromto_field_datetime_fromto_value ASC

Other things I've checked:
- update.php has run
- this is the only (obvious) problem

If anyone could point me in the direction of where this code might be, I'd be happy to dig around and try finding out what's broken. Any help would be most gratefully appreciated!

Comments

alexharries’s picture

Title: 'Unknown column': column name of CCK 'from' date incorrectly capitalised in Views 2 when column name contains "FROM" » [TEMP FIX FOUND] db_rewrite_sql or hook_db_rewrite_sql in view.inc is corrupting Date queries where column name includes 'from'
Status: Active » Needs work

Right, I have narrowed this problem down to the views.inc file, so this may not be a Date issue after all - if it does turn out not to be a fault of Date, I apologise for jumping to the wrong conclusion!

I have narrowed the corruption down to the db_rewrite_sql call in the view.inc file, at line 685: column names are correct in the $this->build_info['query'] variable, but are corrupted ("from" becomes "FROM") in the resulting $query variable, when db_rewrite_sql is called here:

685: $query = db_rewrite_sql($this->build_info['query'], $this->base_table, $this->base_field, array('view' => &$this));

Having looked through the core code I can't see anything which looks like it's causing the error, but I did notice that there is a hook_db_rewrite_sql function which is documented at http://api.drupal.org/api/function/hook_db_rewrite_sql/6 - do either Date or Views (or a sub-module) use this hook? If so, I suspect that the problem might be originating there.

The following four lines of code have allowed me to get my calendars working again. This is a horrible, nasty fix which I wouldn't wish on my worst enemy, **BUT** it's working, which is better than nothing until the offending bug can be squashed, so I'm sharing it here in the hope that it can be useful for someone else suffering these problems:

File: ...../modules/views/includes/view.inc, line 684:

	$query = db_rewrite_sql($this->build_info['query'], $this->base_table, $this->base_field, array('view' => &$this));
	$count_query = db_rewrite_sql($this->build_info['count_query'], $this->base_table, $this->base_field, array('view' => &$this));
	// start nasty, dirty hack to fix incorrect capitalisation from hook_db_rewrite_sql - alex@greyhead.co.uk - 20091016
	$search_regex = '/([^\s])(FROM)([^\s])/';
	$replace_regex = '\1from\3';
	$query = preg_replace($search_regex,$replace_regex,$query);
	$count_query = preg_replace($search_regex,$replace_regex,$count_query);
	// end nasty, dirty hack to fix incorrect capitalisation from hook_db_rewrite_sql - alex@greyhead.co.uk - 20091016
	$args = $this->build_info['query_args'];

Although I know this isn't fixed by a country mile, I've also tentatively changed the status to "needs work" from "active" to try and make it more findable for people also in the same boat.

Please feel free to criticise or comment as you see fit, but please do bear in mind I'm aware this is very much a hack and not a solution.

Best regards,

Alex

alexharries’s picture

Should I assume no-one else has found this problem, since I haven't seen any other reports? :(

Still a problem with the latest version of Views and Date.

HGS’s picture

I've noticed the same problem. I've been stepping through the code and it looks like the regular expression in db_distinct_field on line 368 of database.mysqli.inc is assuming that the first match for 'from' is the SQL keyword.

I'm going to have a look around to see if this has been mentioned in another context.

alexharries’s picture

Title: [TEMP FIX FOUND] db_rewrite_sql or hook_db_rewrite_sql in view.inc is corrupting Date queries where column name includes 'from' » hook_db_rewrite_sql seems to be corrupting queries where 'from' is in the column name, e.g. {field_date_from}
Project: Date » Drupal core
Version: 6.x-2.x-dev » 6.x-dev
Component: Code » base system

I'm tentatively moving this to core as it looks like the bug might be occurring through - if not as a result of - a core call.

Apologies if it isn't, but I don't know how to find out where this corruption is occurring, so if anyone can help me I would be most grateful.

Thanks,

Al

dpearcefl’s picture

Priority: Critical » Normal
Status: Needs work » Postponed (maintainer needs more info)

Has this been fixed in the latest D6?

dpearcefl’s picture

Priority: Normal » Major
Status: Postponed (maintainer needs more info) » Needs work

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.