From fe02accb42d5308139950dfaa674f14715ebaa3e Mon Sep 17 00:00:00 2001 From: ianchanning Date: Sat, 8 Oct 2011 19:06:00 +0300 Subject: [PATCH 1/6] Code to handle UTF-8 characters. Changed $columns['string'] to default to nvarchar(3000). Changed value function to return N'$data' if it is a string. --- models/datasources/dbo/dbo_sqlsrv.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/models/datasources/dbo/dbo_sqlsrv.php b/models/datasources/dbo/dbo_sqlsrv.php index 82dd466..44d9b6f 100755 --- a/models/datasources/dbo/dbo_sqlsrv.php +++ b/models/datasources/dbo/dbo_sqlsrv.php @@ -77,7 +77,7 @@ class DboSqlsrv extends DboSource { */ var $columns = array( 'primary_key' => array('name' => 'IDENTITY (1, 1) NOT NULL'), - 'string' => array('name' => 'varchar', 'limit' => '255'), + 'string' => array('name' => 'nvarchar', 'limit' => '6000'), // ICC this limit doesn't seem to matter, I set it to 6000 anyway which is equivalent to nvarchar(3000) 'text' => array('name' => 'text'), 'integer' => array('name' => 'int', 'formatter' => 'intval'), 'float' => array('name' => 'numeric', 'formatter' => 'floatval'), @@ -296,6 +296,9 @@ function value($data, $column = null, $safe = false) { if (in_array($column, array('integer', 'float', 'binary')) && is_numeric($data)) { return $data; + } else if ($column == 'string') { + // ICC we are assuming all string data is unicode now + return "N'" . $data . "'"; } return "'" . $data . "'"; } From b2dc42d1c7535a0084f6e63c0f24101adca808eb Mon Sep 17 00:00:00 2001 From: ianchanning Date: Sat, 8 Oct 2011 19:07:25 +0300 Subject: [PATCH 2/6] whitespace change --- models/datasources/dbo/dbo_sqlsrv.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/datasources/dbo/dbo_sqlsrv.php b/models/datasources/dbo/dbo_sqlsrv.php index 44d9b6f..80099fc 100755 --- a/models/datasources/dbo/dbo_sqlsrv.php +++ b/models/datasources/dbo/dbo_sqlsrv.php @@ -77,7 +77,7 @@ class DboSqlsrv extends DboSource { */ var $columns = array( 'primary_key' => array('name' => 'IDENTITY (1, 1) NOT NULL'), - 'string' => array('name' => 'nvarchar', 'limit' => '6000'), // ICC this limit doesn't seem to matter, I set it to 6000 anyway which is equivalent to nvarchar(3000) + 'string' => array('name' => 'nvarchar', 'limit' => '6000'), // ICC this limit doesn't seem to matter, I set it to 6000 anyway which is equivalent to nvarchar(3000) 'text' => array('name' => 'text'), 'integer' => array('name' => 'int', 'formatter' => 'intval'), 'float' => array('name' => 'numeric', 'formatter' => 'floatval'), From ecbf30640e7223e9ab1205ae79f6eafa2f4c41f4 Mon Sep 17 00:00:00 2001 From: ianchanning Date: Wed, 12 Oct 2011 16:36:01 +0300 Subject: [PATCH 3/6] Remove check to force integer, float and binary empty quotes ('') to be NULL. This is to put the code the back to the same as mssql. --- models/datasources/dbo/dbo_sqlsrv.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/models/datasources/dbo/dbo_sqlsrv.php b/models/datasources/dbo/dbo_sqlsrv.php index 80099fc..762f062 100755 --- a/models/datasources/dbo/dbo_sqlsrv.php +++ b/models/datasources/dbo/dbo_sqlsrv.php @@ -274,9 +274,6 @@ function value($data, $column = null, $safe = false) { if ($data === null) { return 'NULL'; } - if (in_array($column, array('integer', 'float', 'binary')) && $data === '') { - return 'NULL'; - } if ($data === '') { return "''"; } From f3f61580c639906cc0eab57e3740f602a83ffd13 Mon Sep 17 00:00:00 2001 From: ianchanning Date: Wed, 12 Oct 2011 16:42:16 +0300 Subject: [PATCH 4/6] Added note about sqlsrv UTF-8 and CakePHP 1.2 --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 8f36b70..f6be8c9 100644 --- a/readme.md +++ b/readme.md @@ -3,6 +3,8 @@ This plugin contains various datasources contributed by the core CakePHP team and the community. The datasources plugin is compatible with CakePHP 1.3+. +ICC - for this plugin I have modifed the sqlsrv driver to allow UTF-8 characters and make it more compatible with CakePHP v1.2. + ### Using the datasources plugin First download the repository and place it in `app/plugins/datasources` or on one of your plugin paths. You can then import and use the datasources in your App classes. From 5e0c9ad3254026cad4a8e881bd2bc4e7b41522c8 Mon Sep 17 00:00:00 2001 From: ianchanning Date: Wed, 12 Oct 2011 18:54:42 +0300 Subject: [PATCH 5/6] Put back in the code to make this file CakePHP v1.3 compatible --- models/datasources/dbo/dbo_sqlsrv.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/models/datasources/dbo/dbo_sqlsrv.php b/models/datasources/dbo/dbo_sqlsrv.php index 762f062..80099fc 100755 --- a/models/datasources/dbo/dbo_sqlsrv.php +++ b/models/datasources/dbo/dbo_sqlsrv.php @@ -274,6 +274,9 @@ function value($data, $column = null, $safe = false) { if ($data === null) { return 'NULL'; } + if (in_array($column, array('integer', 'float', 'binary')) && $data === '') { + return 'NULL'; + } if ($data === '') { return "''"; } From b8353e877e6f03891e65da83412f7b3d2b00905c Mon Sep 17 00:00:00 2001 From: ianchanning Date: Wed, 12 Oct 2011 20:04:40 +0300 Subject: [PATCH 6/6] Convert DateTime objects to strings in the fetchResult function. Fix suggested by @FinchPowers in this [issue](https://github.com/revathskumar/CakePHP-sqlsrv-driver/issues/2). --- models/datasources/dbo/dbo_sqlsrv.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/models/datasources/dbo/dbo_sqlsrv.php b/models/datasources/dbo/dbo_sqlsrv.php index 80099fc..f4cbeac 100755 --- a/models/datasources/dbo/dbo_sqlsrv.php +++ b/models/datasources/dbo/dbo_sqlsrv.php @@ -683,15 +683,20 @@ function fetchResult() { if ($row = sqlsrv_fetch_array($this->results, SQLSRV_FETCH_NUMERIC)) { $resultRow = array(); $i = 0; + foreach ($row as $index => $field) { - $a = $this->map[$index]; - list($table, $column) = $a; + list($table, $column) = $this->map[$index]; + if (is_a($row[$index], 'DateTime')) { + $dateTimeObj = $row[$index]; + $row[$index] = $dateTimeObj->format('Y-m-d H:i:s'); + } $resultRow[$table][$column] = $row[$index]; $i++; } return $resultRow; + } else { + return false; } - return false; } /**