5 ///////////////////////////////////////////////////////////////////////////
7 // NOTICE OF COPYRIGHT //
9 // ADOdb - Database Abstraction Library for PHP //
10 // http://adodb.sourceforge.net/ //
12 // Copyright (c) 2000-2011 John Lim (jlim\@natsoft.com.my) //
13 // All rights reserved. //
14 // Released under both BSD license and LGPL library license. //
15 // Whenever there is any discrepancy between the two licenses, //
16 // the BSD license will take precedence //
18 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
19 // http://moodle.com //
21 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
22 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
24 // This program is free software; you can redistribute it and/or modify //
25 // it under the terms of the GNU General Public License as published by //
26 // the Free Software Foundation; either version 2 of the License, or //
27 // (at your option) any later version. //
29 // This program is distributed in the hope that it will be useful, //
30 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
31 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
32 // GNU General Public License for more details: //
34 // http://www.gnu.org/copyleft/gpl.html //
36 ///////////////////////////////////////////////////////////////////////////
39 * MSSQL Driver with auto-prepended "N" for correct unicode storage
40 * of SQL literal strings. Intended to be used with MSSQL drivers that
41 * are sending UCS-2 data to MSSQL (FreeTDS and ODBTP) in order to get
42 * true cross-db compatibility from the application point of view.
45 // security - hide paths
46 if (!defined('ADODB_DIR')) die();
48 // one useful constant
49 if (!defined('SINGLEQUOTE')) define('SINGLEQUOTE', "'");
51 include_once(ADODB_DIR
.'/drivers/adodb-mssql.inc.php');
53 class ADODB_mssql_n
extends ADODB_mssql
{
54 var $databaseType = "mssql_n";
56 function ADODB_mssqlpo()
58 ADODB_mssql
::ADODB_mssql();
61 function _query($sql,$inputarr=false)
63 $sql = $this->_appendN($sql);
64 return ADODB_mssql
::_query($sql,$inputarr);
68 * This function will intercept all the literals used in the SQL, prepending the "N" char to them
69 * in order to allow mssql to store properly data sent in the correct UCS-2 encoding (by freeTDS
70 * and ODBTP) keeping SQL compatibility at ADOdb level (instead of hacking every project to add
71 * the "N" notation when working against MSSQL.
73 * Note that this hack only must be used if ALL the char-based columns in your DB are of type nchar,
76 function _appendN($sql) {
80 /// Check we have some single quote in the query. Exit ok.
81 if (strpos($sql, SINGLEQUOTE
) === false) {
85 /// Check we haven't an odd number of single quotes (this can cause problems below
86 /// and should be considered one wrong SQL). Exit with debug info.
87 if ((substr_count($sql, SINGLEQUOTE
) & 1)) {
89 ADOConnection
::outp("{$this->databaseType} internal transformation: not converted. Wrong number of quotes (odd)");
94 /// Check we haven't any backslash + single quote combination. It should mean wrong
95 /// backslashes use (bad magic_quotes_sybase?). Exit with debug info.
96 $regexp = '/(\\\\' . SINGLEQUOTE
. '[^' . SINGLEQUOTE
. '])/';
97 if (preg_match($regexp, $sql)) {
99 ADOConnection
::outp("{$this->databaseType} internal transformation: not converted. Found bad use of backslash + single quote");
104 /// Remove pairs of single-quotes
106 $regexp = '/(' . SINGLEQUOTE
. SINGLEQUOTE
. ')/';
107 preg_match_all($regexp, $result, $list_of_pairs);
108 if ($list_of_pairs) {
109 foreach (array_unique($list_of_pairs[0]) as $key=>$value) {
110 $pairs['<@#@#@PAIR-'.$key.'@#@#@>'] = $value;
112 if (!empty($pairs)) {
113 $result = str_replace($pairs, array_keys($pairs), $result);
117 /// Remove the rest of literals present in the query
119 $regexp = '/(N?' . SINGLEQUOTE
. '.*?' . SINGLEQUOTE
. ')/is';
120 preg_match_all($regexp, $result, $list_of_literals);
121 if ($list_of_literals) {
122 foreach (array_unique($list_of_literals[0]) as $key=>$value) {
123 $literals['<#@#@#LITERAL-'.$key.'#@#@#>'] = $value;
125 if (!empty($literals)) {
126 $result = str_replace($literals, array_keys($literals), $result);
131 /// Analyse literals to prepend the N char to them if their contents aren't numeric
132 if (!empty($literals)) {
133 foreach ($literals as $key=>$value) {
134 if (!is_numeric(trim($value, SINGLEQUOTE
))) {
135 /// Non numeric string, prepend our dear N
136 $literals[$key] = 'N' . trim($value, 'N'); //Trimming potentially existing previous "N"
141 /// Re-apply literals to the text
142 if (!empty($literals)) {
143 $result = str_replace(array_keys($literals), $literals, $result);
146 /// Any pairs followed by N' must be switched to N' followed by those pairs
147 /// (or strings beginning with single quotes will fail)
148 $result = preg_replace("/((<@#@#@PAIR-(\d+)@#@#@>)+)N'/", "N'$1", $result);
150 /// Re-apply pairs of single-quotes to the text
151 if (!empty($pairs)) {
152 $result = str_replace(array_keys($pairs), $pairs, $result);
155 /// Print transformation if debug = on
156 if ($result != $sql && $this->debug
) {
157 ADOConnection
::outp("{$this->databaseType} internal transformation:<br>{$sql}<br>to<br>{$result}");
164 class ADORecordset_mssql_n
extends ADORecordset_mssql
{
165 var $databaseType = "mssql_n";
166 function ADORecordset_mssql_n($id,$mode=false)
168 $this->ADORecordset_mssql($id,$mode);