Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Solution: MS SQL Server not storing unicode characters in survey responses

  • dapster105
  • dapster105's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 3 months ago #223452 by dapster105
My LimeSurvey  production version is 3.14.9+180917 but likely applies to others. May have been solved in the repo since, but I can't verify that.

N.B. There are various other threads relating to this issue in the forum but they are all Locked unfortunately hence the new post. I would be grateful if anyone with access could add a ref to this potential solution to help others find it.

Like many others I've struggled / put up with the issue of Unicode characters not being stored properly in survey responses when using MS SQL Server (despite storing languages / translations just fine). I finally had a bit of time to debug this properly so I'm sharing my findings here in the hope that it saves someone else having to spend the same time again.

The root cause is that when UPDATEing survey responses, the LimeSurvey code does not use variable binding (as is used elsewhere and which works perfectly). Instead it generates the full UPDATE statement text dynamically. This can result in statements being executed like:
Code:
UPDATE [surveytable] SET [colname] = 'Zażółć gęślą jaźń' ...
As anyone who knows SQL Server will know, to treat a string literal like this as an nvarchar (unicode string type) you would need to prepend the opening single quote with N e.g.
Code:
UPDATE [surveytable] SET [colname] = N'Zażółć gęślą jaźń' ...

In generating the query text, use is made of the CDbConnection quoteValue() method which in turn calls quoteValue on the PDO driver, in this case pdo_sqlsrv.

If you have pdo_sqlsrv v5.8 or above, you should be able to change the default behaviour of the driver's quoteValue (and quoteValueWithType) methods by specifying the following in config.php :
Code:
...
    'components' => array(
        'db' => array(
            'connectionString' => 'sqlsrv:Server=****;Database=****',
            'username' => '****',
            'password' => '****',
            'charset' => 'utf8',
            'tablePrefix' => 'sirius_',
            'initSQLs'=>array('SET DATEFORMAT ymd;','SET QUOTED_IDENTIFIER ON;'),
            
            // STUFF BELOW _SHOULD_ MAKE UNICODE SAVEING WORK IF PDO_SQLSRV v 5.8+ IS INSTALLED
            'attributes' => array (
                PDO::ATTR_DEFAULT_STR_PARAM => PDO::PARAM_STR_NATL
                )
        )
 
    ),
...

If, like me, you're stuck with an older version of the PDO driver in your PHP stack, then you'll need to modify either CMssqlSqlsrvPdoAdapter, extending the quoteValue and quoteValueWithType methods to prepend the magic N, or modify directly in CDbConnection which is the route I took because, frankly, I'd spent enough time on it.

I hope this proves to be more helpful than the usual "you're not using SQL Server are you?!" responses [sucks-through-teeth].
The topic has been locked.
More
3 years 3 months ago #223453 by jelo
The question is if your old version of LimeSurvey (2018) hasn't changed till the current LimeSurvey v3.
If 3.14.9+180917 is really your production system, you might run into many other issues (e.g. browser issues).

Since there is not much testing around Limesurvey and MS SQL your post might be gold. I recommend to open a bug report about that issue and your solution. The response will be to check the issues with an uptodate version of LimeSurvey v3.

I don't use LimeSurvey with SQLServer. So I cannot comment or test your solution.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The following user(s) said Thank You: DenisChenu
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
3 years 3 months ago #223579 by DenisChenu

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.
The following user(s) said Thank You: dapster105
The topic has been locked.
  • dapster105
  • dapster105's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 3 months ago #223590 by dapster105
You're a legend @DenisChenu
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose