ParmUtil.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // ParmUtil.cs - utility to bind variables in a SQL statement to parameters in C# code
  3. // This is in the PostgreSQL .NET Data provider in Mono
  4. //
  5. // Author:
  6. // Daniel Morgan <[email protected]>
  7. //
  8. // (c)copyright 2002 Daniel Morgan
  9. //
  10. // comment DEBUG_ParmUtil for production, for debug messages, uncomment
  11. //#define DEBUG_ParmUtil
  12. using System;
  13. using System.Data;
  14. using System.Text;
  15. namespace System.Data.SqlClient {
  16. enum PostgresBindVariableCharacter {
  17. Semicolon,
  18. At,
  19. QuestionMark
  20. }
  21. public class ParmUtil {
  22. private string sql = "";
  23. private string resultSql = "";
  24. private SqlParameterCollection parmsCollection = null;
  25. static private PostgresBindVariableCharacter PgbindChar = PostgresBindVariableCharacter.Semicolon;
  26. static char bindChar;
  27. // static constructor
  28. static ParmUtil() {
  29. switch(PgbindChar) {
  30. case PostgresBindVariableCharacter.Semicolon:
  31. bindChar = ':';
  32. break;
  33. case PostgresBindVariableCharacter.At:
  34. bindChar = '@';
  35. break;
  36. case PostgresBindVariableCharacter.QuestionMark:
  37. // this doesn't have named parameters,
  38. // they must be in order
  39. bindChar = '?';
  40. break;
  41. }
  42. }
  43. public ParmUtil(string query, SqlParameterCollection parms) {
  44. sql = query;
  45. parmsCollection = parms;
  46. }
  47. public string ResultSql {
  48. get {
  49. return resultSql;
  50. }
  51. }
  52. // TODO: currently only works for input variables,
  53. // need to do input/output, output, and return
  54. public string ReplaceWithParms() {
  55. StringBuilder result = new StringBuilder();
  56. char[] chars = sql.ToCharArray();
  57. bool bStringConstFound = false;
  58. for(int i = 0; i < chars.Length; i++) {
  59. if(chars[i] == '\'') {
  60. if(bStringConstFound == true)
  61. bStringConstFound = false;
  62. else
  63. bStringConstFound = true;
  64. result.Append(chars[i]);
  65. }
  66. else if(chars[i] == bindChar &&
  67. bStringConstFound == false) {
  68. #if DEBUG_ParmUtil
  69. Console.WriteLine("Bind Variable character found...");
  70. #endif
  71. StringBuilder parm = new StringBuilder();
  72. i++;
  73. while(i <= chars.Length) {
  74. char ch;
  75. if(i == chars.Length)
  76. ch = ' '; // a space
  77. else
  78. ch = chars[i];
  79. #if DEBUG_ParmUtil
  80. Console.WriteLine("Is char Letter or digit?");
  81. #endif
  82. if(Char.IsLetterOrDigit(ch)) {
  83. #if DEBUG_ParmUtil
  84. Console.WriteLine("Char IS letter or digit. " +
  85. "Now, append char to parm StringBuilder");
  86. #endif
  87. parm.Append(ch);
  88. }
  89. else {
  90. #if DEBUG_ParmUtil
  91. Console.WriteLine("Char is NOT letter or char. " +
  92. "thus we got rest of bind variable name. ");
  93. // replace bind variable placeholder
  94. // with data value constant
  95. Console.WriteLine("parm StringBuilder to string p...");
  96. #endif
  97. string p = parm.ToString();
  98. #if DEBUG_ParmUtil
  99. Console.WriteLine("calling BindReplace...");
  100. #endif
  101. bool found = BindReplace(result, p);
  102. #if DEBUG_ParmUtil
  103. Console.WriteLine(" Found = " + found);
  104. #endif
  105. if(found == true)
  106. break;
  107. else {
  108. // *** Error Handling
  109. Console.WriteLine("Error: parameter not found: " + p);
  110. return "";
  111. }
  112. }
  113. i++;
  114. }
  115. i--;
  116. }
  117. else
  118. result.Append(chars[i]);
  119. }
  120. resultSql = result.ToString();
  121. return resultSql;
  122. }
  123. public bool BindReplace (StringBuilder result, string p) {
  124. // bind variable
  125. bool found = false;
  126. #if DEBUG_ParmUtil
  127. Console.WriteLine("Does the parmsCollection contain the parameter???: " + p);
  128. #endif
  129. if(parmsCollection.Contains(p) == true) {
  130. // parameter found
  131. #if DEBUG_ParmUtil
  132. Console.WriteLine("Parameter Found: " + p);
  133. #endif
  134. SqlParameter prm = parmsCollection[p];
  135. #if DEBUG_ParmUtil
  136. // DEBUG
  137. Console.WriteLine(" Value: " + prm.Value);
  138. Console.WriteLine(" Direction: " + prm.Direction);
  139. #endif
  140. // convert object to string and place
  141. // into SQL
  142. if(prm.Direction == ParameterDirection.Input) {
  143. string strObj = PostgresHelper.
  144. ObjectToString(prm.DbType,
  145. prm.Value);
  146. result.Append(strObj);
  147. }
  148. else
  149. result.Append(bindChar + p);
  150. found = true;
  151. }
  152. return found;
  153. }
  154. }
  155. }