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
11 // comment DEBUG_ParmUtil for production, for debug messages, uncomment
\r
12 //#define DEBUG_ParmUtil
\r
18 namespace Mono.Data.PostgreSqlClient {
\r
20 enum PostgresBindVariableCharacter {
\r
26 public class ParmUtil {
\r
28 private string sql = "";
\r
29 private string resultSql = "";
\r
30 private PgSqlParameterCollection parmsCollection = null;
\r
32 static private PostgresBindVariableCharacter PgbindChar = PostgresBindVariableCharacter.Semicolon;
\r
33 static char bindChar;
\r
35 // static constructor
\r
37 switch(PgbindChar) {
\r
38 case PostgresBindVariableCharacter.Semicolon:
\r
41 case PostgresBindVariableCharacter.At:
\r
44 case PostgresBindVariableCharacter.QuestionMark:
\r
45 // this doesn't have named parameters,
\r
46 // they must be in order
\r
52 public ParmUtil(string query, PgSqlParameterCollection parms) {
\r
54 parmsCollection = parms;
\r
57 public string ResultSql {
\r
63 // TODO: currently only works for input variables,
\r
64 // need to do input/output, output, and return
\r
65 public string ReplaceWithParms() {
\r
67 StringBuilder result = new StringBuilder();
\r
68 char[] chars = sql.ToCharArray();
\r
69 bool bStringConstFound = false;
\r
71 for(int i = 0; i < chars.Length; i++) {
\r
72 if(chars[i] == '\'') {
\r
73 if(bStringConstFound == true)
\r
74 bStringConstFound = false;
\r
76 bStringConstFound = true;
\r
78 result.Append(chars[i]);
\r
80 else if(chars[i] == bindChar &&
\r
81 bStringConstFound == false) {
\r
83 Console.WriteLine("Bind Variable character found...");
\r
85 StringBuilder parm = new StringBuilder();
\r
87 while(i <= chars.Length) {
\r
89 if(i == chars.Length)
\r
90 ch = ' '; // a space
\r
95 Console.WriteLine("Is char Letter or digit?");
\r
97 if(Char.IsLetterOrDigit(ch)) {
\r
99 Console.WriteLine("Char IS letter or digit. " +
\r
100 "Now, append char to parm StringBuilder");
\r
106 Console.WriteLine("Char is NOT letter or char. " +
\r
107 "thus we got rest of bind variable name. ");
\r
109 // replace bind variable placeholder
\r
110 // with data value constant
\r
111 Console.WriteLine("parm StringBuilder to string p...");
\r
113 string p = parm.ToString();
\r
115 Console.WriteLine("calling BindReplace...");
\r
117 bool found = BindReplace(result, p);
\r
119 Console.WriteLine(" Found = " + found);
\r
124 // *** Error Handling
\r
125 Console.WriteLine("Error: parameter not found: " + p);
\r
134 result.Append(chars[i]);
\r
137 resultSql = result.ToString();
\r
141 public bool BindReplace (StringBuilder result, string p) {
\r
143 bool found = false;
\r
146 Console.WriteLine("Does the parmsCollection contain the parameter???: " + p);
\r
148 if(parmsCollection.Contains(p) == true) {
\r
151 Console.WriteLine("Parameter Found: " + p);
\r
153 PgSqlParameter prm = parmsCollection[p];
\r
155 #if DEBUG_ParmUtil
\r
157 Console.WriteLine(" Value: " + prm.Value);
\r
158 Console.WriteLine(" Direction: " + prm.Direction);
\r
160 // convert object to string and place
\r
162 if(prm.Direction == ParameterDirection.Input) {
\r
163 string strObj = PostgresHelper.
\r
164 ObjectToString(prm.DbType,
\r
166 result.Append(strObj);
\r
169 result.Append(bindChar + p);
\r