PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/adodb/drivers/adodb-mssql_n.inc.php

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