2 // ParmUtil.cs - utility to bind variables in a SQL statement to parameters in C# code
\r
3 // This is in the PostgreSQL .NET Data provider in Mono
\r
6 // Daniel Morgan <danmorg@sc.rr.com>
\r
8 // (c)copyright 2002 Daniel Morgan
\r
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 // comment DEBUG_ParmUtil for production, for debug messages, uncomment
\r
33 //#define DEBUG_ParmUtil
\r
39 namespace Mono.Data.PostgreSqlClient {
\r
41 enum PostgresBindVariableCharacter {
\r
47 public class ParmUtil {
\r
49 private string sql = "";
\r
50 private string resultSql = "";
\r
51 private PgSqlParameterCollection parmsCollection = null;
\r
53 static private PostgresBindVariableCharacter PgbindChar = PostgresBindVariableCharacter.Semicolon;
\r
54 static char bindChar;
\r
56 // static constructor
\r
58 switch(PgbindChar) {
\r
59 case PostgresBindVariableCharacter.Semicolon:
\r
62 case PostgresBindVariableCharacter.At:
\r
65 case PostgresBindVariableCharacter.QuestionMark:
\r
66 // this doesn't have named parameters,
\r
67 // they must be in order
\r
73 public ParmUtil(string query, PgSqlParameterCollection parms) {
\r
75 parmsCollection = parms;
\r
78 public string ResultSql {
\r
84 // TODO: currently only works for input variables,
\r
85 // need to do input/output, output, and return
\r
86 public string ReplaceWithParms() {
\r
88 StringBuilder result = new StringBuilder();
\r
89 char[] chars = sql.ToCharArray();
\r
90 bool bStringConstFound = false;
\r
92 for(int i = 0; i < chars.Length; i++) {
\r
93 if(chars[i] == '\'') {
\r
94 if(bStringConstFound == true)
\r
95 bStringConstFound = false;
\r
97 bStringConstFound = true;
\r
99 result.Append(chars[i]);
\r
101 else if(chars[i] == bindChar &&
\r
102 bStringConstFound == false) {
\r
104 Console.WriteLine("Bind Variable character found...");
\r
106 StringBuilder parm = new StringBuilder();
\r
108 while(i <= chars.Length) {
\r
110 if(i == chars.Length)
\r
111 ch = ' '; // a space
\r
115 #if DEBUG_ParmUtil
\r
116 Console.WriteLine("Is char Letter or digit?");
\r
118 if(Char.IsLetterOrDigit(ch)) {
\r
120 Console.WriteLine("Char IS letter or digit. " +
\r
121 "Now, append char to parm StringBuilder");
\r
127 Console.WriteLine("Char is NOT letter or char. " +
\r
128 "thus we got rest of bind variable name. ");
\r
130 // replace bind variable placeholder
\r
131 // with data value constant
\r
132 Console.WriteLine("parm StringBuilder to string p...");
\r
134 string p = parm.ToString();
\r
136 Console.WriteLine("calling BindReplace...");
\r
138 bool found = BindReplace(result, p);
\r
140 Console.WriteLine(" Found = " + found);
\r
145 // *** Error Handling
\r
146 Console.WriteLine("Error: parameter not found: " + p);
\r
155 result.Append(chars[i]);
\r
158 resultSql = result.ToString();
\r
162 public bool BindReplace (StringBuilder result, string p) {
\r
164 bool found = false;
\r
167 Console.WriteLine("Does the parmsCollection contain the parameter???: " + p);
\r
169 if(parmsCollection.Contains(p) == true) {
\r
172 Console.WriteLine("Parameter Found: " + p);
\r
174 PgSqlParameter prm = parmsCollection[p];
\r
176 #if DEBUG_ParmUtil
\r
178 Console.WriteLine(" Value: " + prm.Value);
\r
179 Console.WriteLine(" Direction: " + prm.Direction);
\r
181 // convert object to string and place
\r
183 if(prm.Direction == ParameterDirection.Input) {
\r
184 string strObj = PostgresHelper.
\r
185 ObjectToString(prm.DbType,
\r
187 result.Append(strObj);
\r
190 result.Append(bindChar + p);
\r