Hi,
I'm unsure if this is the correct project to post this to but seeing as it appears to be SQL server specific I thought I would start here. It appears that after installing sqlsrv with Drupal 7, and then setting up the Views module (version 7.x-3.0-beta3), that some of the default Views do not work.
For example, enabling the 'Glossary' view causes the following:
Debug:
'Exception: SQLSTATE[07002]: [Microsoft][SQL Server Native Client 10.0]COUNT field incorrect or syntax error'
in views_plugin_query_default->execute() (line 1356 of C:\inetpub\sites\-\sites\all\modules\views\plugins\views_plugin_query_default.inc).
Error message
SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Argument data type nvarchar is invalid for argument 3 of substring function.
I've run the SQL profiler while this is happening and found the following query being created:
exec sp_executesql N'SELECT COUNT(*) AS expression
FROM
(SELECT 1 AS expression
FROM
drupal_node node
INNER JOIN drupal_users users ON node.uid = users.uid
WHERE ( (( (SUBSTRING(node.title, 1, @P1) = @P2) )) )) subquery',N'@P1 nvarchar(1),@P2 nvarchar(1)',N'1',N'A'
It's easy to see here what is wrong; @P1 is being declared as nvarchar(1) which is incompatible with the type expected for the third parameter of SUBSTRING - it should be a numerical value. Simply changing @P1 to type int and running the query manually works.
I've only been playing with Drupal for about a week so I'm not at all up to play with it's internals yet.. I've had a big of a dig through the code but the DB abstractions make it difficult to find why this is happening without knowing a whole lot more about the codebase. Is this likely to be a sqlsrv issue or a Views issue?
Any help or tips would be great, I'll keep looking too and report back if I find anything. Cheers.
Comments
Comment #1
CapBBeard commentedWell I found the following on MSDN: http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/thread/0f0...
This states that similar issues were seen on some versions of PHP (< 5.2.10, and 5.3.0), however I'm running on 5.3.6.
Anyway, I added a dirty fix into the Views module to always add a CAST(@P1 TO INT) in the last parameter of SUBSTRING, which has resolved the immediate issue (although it was more of a temporary quick fix for my own testing than an actual solution). However I'm now continuing to see problems.
It appears that the initial error message of
Is in fact related to another query, not the one I had initially thought. This particular query does not appear in the SQL profiler so I'm assuming it doesn't even hit the SQL server but is rejected by the SQL driver [? just a guess, I'm no expert]. Dumping the query out with the exception reveals it's contents:
After finding the following similar case here http://drupal.org/node/1057538 it seems that using the same placeholder twice causes this problem, due to it being technically invalid in PDO. So perhaps a Views bug?
Comment #2
damien tournoud commentedEw. This is unfortunate. We are expanding aliases in
GROUP BY, because SQL Server cowardly refuses to do that itself, and this causes the duplicate use of the placeholders.This is technically a bug in here. Triaging.
Comment #3
damien tournoud commentedRaising to critical. I want to fix this before the new release that needs to happen soon to support Drupal 7.4.
Comment #4
damien tournoud commentedOk, it seems that the core issue:
Might actually be an issue somewhere down the stack (PDO driver or SQL Server), so I have released the 1.1 version without that fix.
Comment #5
damien tournoud commentedReproduction test case:
Results in
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Argument data type nvarchar is invalid for argument 3 of substring function.'Comment #6
artusamakHere is a patch to fix this issue with the SUBSTRING() casting.
Here is a link for quick review:
http://drupalcode.org/sandbox/Artusamak/1254272.git/commitdiff/3217e1a4e...
To check the code here is the repository url:
Comment #7
artusamakCommited & fixed in http://drupalcode.org/project/sqlsrv.git/commit/ad3161c9f612b93dca6c7ff2...