3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 using System.ComponentModel;
26 namespace IBM.Data.DB2
29 public sealed class DB2CommandBuilder : Component
32 bool disposed = false;
34 private DB2DataAdapter dataAdapter;
35 private DB2Command insertCommand;
36 private DB2Command updateCommand;
37 private DB2Command deleteCommand;
39 private string tableName = String.Empty;
41 public DB2CommandBuilder ()
44 public DB2CommandBuilder (DB2DataAdapter adapter)
46 DataAdapter = adapter;
49 public DB2DataAdapter DataAdapter
57 if (dataAdapter != null)
59 throw new Exception ("DataAdapter is already set");
62 string select_text = dataAdapter.SelectCommand.CommandText;
63 string[] words = select_text.Split(new char [] {' '});
64 bool from_found = false;
65 for (int i = 0; i < words.Length; i++)
67 if (from_found && (words[i] != String.Empty))
72 if (words[i].ToLower() == "from")
80 public string QuotePrefix
90 public string QuoteSuffix
100 public static void DeriveParameters (DB2Command command)
103 public DB2Command GetInsertCommand ()
105 DataTable dt = GetSchema();
106 if (insertCommand == null)
110 for (int i = 0; i < dt.Rows.Count; i++)
112 //DataColumn column = dt.Columns[i];
113 DataRow dr = dt.Rows[i];
114 DataColumn column = new DataColumn((string)dr["ColumnName"], DB2TypeConverter.GetManagedType((int)dr["ProviderType"]));
116 if (fields.Length != 0 && !((bool)dr["IsAutoIncrement"]))
122 if(!((bool)dr["IsAutoIncrement"]))
124 fields += column.ColumnName;
125 //values += ":v_" + column.ColumnName;
129 if (tableName == String.Empty)
131 tableName = dt.TableName;
133 DB2Command cmdaux = new DB2Command("insert into " + tableName + " (" + fields + ") values (" + values + ")", dataAdapter.SelectCommand.Connection);
134 for (int i = 0;i < dt.Rows.Count;i++)
136 //DataColumn column = dt.Columns[i];
137 DataRow dr = dt.Rows[i];
138 DataColumn column = new DataColumn((string)dr["ColumnName"], DB2TypeConverter.GetManagedType((int)dr["ProviderType"]));
139 if (!((bool)dr["IsAutoIncrement"]))
141 DB2Parameter aux = new DB2Parameter("v_" + column.ColumnName, column.DataType);
142 aux.Direction = ParameterDirection.Input;
143 aux.SourceColumn = column.ColumnName;
144 cmdaux.Parameters.Add(aux);
147 insertCommand = cmdaux;
149 return insertCommand;
152 public DB2Command GetUpdateCommand ()
154 DataTable dt = GetSchema();
155 if (updateCommand == null)
159 for (int i = 0; i < dt.Rows.Count; i++)
161 if (sets.Length != 0 && !((bool)dt.Rows[i]["IsAutoIncrement"]))
169 DataRow dr = dt.Rows[i];
170 DataColumn column = new DataColumn((string)dr["ColumnName"], DB2TypeConverter.GetManagedType((int)dr["ProviderType"]));
171 if(!((bool)dr["IsAutoIncrement"])){sets += String.Format("{0} = ? ", column.ColumnName);}
172 wheres += String.Format("(({0} is null) or ({0} = ?))", column.ColumnName);
174 if (tableName == String.Empty)
176 tableName = (string)dt.Rows[0]["BaseTableName"];
178 DB2Command cmdaux = new DB2Command("update " + tableName + " set " + sets + " where ( " + wheres + " )", dataAdapter.SelectCommand.Connection);
179 for (int i = 0; i < dt.Rows.Count; i++)
181 DataRow dr = dt.Rows[i];
182 DataColumn column = new DataColumn((string)dr["ColumnName"], DB2TypeConverter.GetManagedType((int)dr["ProviderType"]));
183 if (!((bool)dr["IsAutoIncrement"]))
185 DB2Parameter aux = new DB2Parameter("s_" + column.ColumnName, column.DataType);
186 aux.Direction = ParameterDirection.Input;
187 aux.SourceColumn = column.ColumnName;
188 aux.SourceVersion = DataRowVersion.Current;
189 cmdaux.Parameters.Add(aux);
192 for (int i = 0; i < dt.Rows.Count; i++)
194 DataRow dr = dt.Rows[i];
195 DataColumn column = new DataColumn((string)dr["ColumnName"], DB2TypeConverter.GetManagedType((int)dr["ProviderType"]));
196 DB2Parameter aux = new DB2Parameter("w_" + column.ColumnName, column.DataType);
197 aux.Direction = ParameterDirection.Input;
198 aux.SourceColumn = column.ColumnName;
199 aux.SourceVersion = DataRowVersion.Original;
200 cmdaux.Parameters.Add(aux);
202 updateCommand = cmdaux;
205 return updateCommand;
208 public DB2Command GetDeleteCommand ()
210 DataTable dt = GetSchema();
211 if (deleteCommand == null)
214 for (int i = 0; i < dt.Rows.Count; i++)
216 //DataColumn column = row.Table.Columns[i];
217 DataRow dr = dt.Rows[i];
218 DataColumn column = new DataColumn((string)dr["ColumnName"], DB2TypeConverter.GetManagedType((int)dr["ProviderType"]));
223 //wheres += String.Format("(({0} is null) or ({0} = v_{0}))", column.ColumnName);
224 wheres += String.Format("(({0} is null) or ({0} = ?))", column.ColumnName);
226 if (tableName == String.Empty)
228 tableName = (string)dt.Rows[0]["BaseTableName"];
230 DB2Command cmdaux = new DB2Command("delete from " + tableName + " where ( " + wheres + " )", dataAdapter.SelectCommand.Connection);
231 for (int i = 0; i < dt.Rows.Count; i++)
233 DataRow dr = dt.Rows[i];
234 DataColumn column = new DataColumn((string)dr["ColumnName"], DB2TypeConverter.GetManagedType((int)dr["ProviderType"]));
236 DB2Parameter aux = new DB2Parameter("v_" + column.ColumnName, column.DataType);
237 aux.Direction = ParameterDirection.Input;
238 aux.SourceColumn = column.ColumnName;
239 aux.SourceVersion = DataRowVersion.Original;
240 cmdaux.Parameters.Add(aux);
242 deleteCommand = cmdaux;
244 return deleteCommand;
247 public void RefreshSchema ()
249 insertCommand = null;
250 updateCommand = null;
251 deleteCommand = null;
254 private DataTable GetSchema()
256 dataAdapter.SelectCommand.Connection.Open();
257 DB2Command cmd = new DB2Command(dataAdapter.SelectCommand.CommandText, dataAdapter.SelectCommand.Connection);
258 DB2DataReader fake = cmd.ExecuteReader(CommandBehavior.KeyInfo);
260 DataTable dt = fake.GetSchemaTable();
262 dataAdapter.SelectCommand.Connection.Close();
267 protected override void Dispose (bool disposing)
273 if (insertCommand != null)
275 insertCommand.Dispose();
277 if (updateCommand != null)
279 updateCommand.Dispose();
281 if (deleteCommand != null)
283 deleteCommand.Dispose();