source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Ado/BMXNetCommand.cs@ 1146

Last change on this file since 1146 was 1146, checked in by Sam Habiel, 13 years ago

Initial Import of BMX4

File size: 10.3 KB
Line 
1using System;
2using System.Data;
3using System.Data.Common;
4
5namespace IndianHealthService.BMXNet.Ado
6{
7 /// <summary>
8 /// BMXNET implementaiton of DbCommand. This class can be used like
9 /// any ADO.NET DbCommand implementation or the suggested RemoteSession API's can
10 /// be used.
11 ///
12 /// See ADO.NET documentation for details of this class.
13 /// </summary>
14 public class BMXNetCommand : System.Data.Common.DbCommand, IDbCommand
15 {
16 BMXNetConnection m_connection;
17 BMXNetTransaction m_txn;
18 string m_sCmdText;
19 UpdateRowSource m_updatedRowSource = UpdateRowSource.None;
20 BMXNetParameterCollection m_parameters = new BMXNetParameterCollection();
21
22 // the default constructor
23 public BMXNetCommand()
24 {
25 }
26
27 // other constructors
28 public BMXNetCommand(string cmdText)
29 {
30 m_sCmdText = cmdText;
31 }
32
33 internal BMXNetCommand(string cmdText, BMXNetConnection connection)
34 {
35 m_sCmdText = cmdText;
36 m_connection = connection;
37 }
38
39 internal BMXNetCommand(string cmdText, BMXNetConnection connection, BMXNetTransaction txn)
40 {
41 m_sCmdText = cmdText;
42 m_connection = connection;
43 m_txn = txn;
44 }
45
46 /****
47 * IMPLEMENT THE REQUIRED PROPERTIES.
48 ****/
49 override public string CommandText
50 {
51 get { return m_sCmdText; }
52 set { m_sCmdText = value; }
53 }
54
55 override public int CommandTimeout
56 {
57 /*
58 * BMXNet does not support a command time-out. As a result,
59 * for the get, zero is returned because zero indicates an indefinite
60 * time-out period. For the set, throw an exception.
61 */
62 get { return 0; }
63 set { if (value != 0) throw new NotSupportedException(); }
64 }
65
66 override public CommandType CommandType
67 {
68 /*
69 * BMXNet only supports CommandType.Text.
70 */
71 get { return CommandType.Text; }
72 set { if (value != CommandType.Text) throw new NotSupportedException(); }
73 }
74
75 protected override DbConnection DbConnection
76 {
77 get
78 {
79 return m_connection;
80 }
81 set
82 {
83 if (m_connection != value)
84 this.Transaction = null;
85
86 m_connection = (BMXNetConnection)value;
87 }
88 }
89
90 protected override DbParameterCollection DbParameterCollection
91 {
92 get { throw new NotImplementedException(); }
93 }
94
95 new public IDbConnection Connection
96 {
97 /*
98 * The user should be able to set or change the connection at
99 * any time.
100 */
101 get { return m_connection; }
102 set
103 {
104 /*
105 * The connection is associated with the transaction
106 * so set the transaction object to return a null reference if the connection
107 * is reset.
108 */
109 if (m_connection != value)
110 this.Transaction = null;
111
112 m_connection = (BMXNetConnection)value;
113 }
114 }
115
116 internal BMXNetParameterCollection BmxNetParameters
117 {
118 get { return m_parameters; }
119 }
120
121 new IDataParameterCollection Parameters
122 {
123 get { return m_parameters; }
124 }
125
126
127
128 new public IDbTransaction Transaction
129 {
130 /*
131 * Set the transaction. Consider additional steps to ensure that the transaction
132 * is compatible with the connection, because the two are usually linked.
133 */
134 get { return m_txn; }
135 set { m_txn = (BMXNetTransaction)value; }
136 }
137
138 override protected DbTransaction DbTransaction
139 {
140 /*
141 * Set the transaction. Consider additional steps to ensure that the transaction
142 * is compatible with the connection, because the two are usually linked.
143 */
144 get
145 {
146 throw new Exception("The method or operation is not implemented.");
147 }
148 set
149 {
150 throw new Exception("The method or operation is not implemented.");
151 }
152 }
153
154 override public bool DesignTimeVisible
155 {
156 get
157 {return false ; }
158 set { ;}
159 }
160
161 override public UpdateRowSource UpdatedRowSource
162 {
163 get { return m_updatedRowSource; }
164 set { m_updatedRowSource = value; }
165 }
166
167
168 /****
169 * IMPLEMENT THE REQUIRED METHODS.
170 ****/
171 override public void Cancel()
172 {
173 // BMXNet does not support canceling a command
174 // once it has been initiated.
175 throw new NotSupportedException();
176 }
177
178 new public IDbDataParameter CreateParameter()
179 {
180 return (IDbDataParameter)(new BMXNetParameter());
181 }
182
183 override protected DbParameter CreateDbParameter()
184 {
185 throw new Exception("The method or operation is not implemented.");
186 }
187
188 override public int ExecuteNonQuery()
189 {
190 /*
191 * ExecuteNonQuery is intended for commands that do
192 * not return results, instead returning only the number
193 * of records affected.
194 */
195
196 // There must be a valid and open connection.
197 if (m_connection == null || m_connection.State != ConnectionState.Open)
198 throw new InvalidOperationException("Connection must valid and open");
199
200 // Execute the command.
201 RPMSDb.RPMSDbResultSet resultset;
202 m_connection.RPMSDb.Execute(m_sCmdText, out resultset);
203
204 // Return the number of records affected.
205 return resultset.recordsAffected;
206 }
207
208 new public IDataReader ExecuteReader()
209 {
210 /*
211 * ExecuteReader should retrieve results from the data source
212 * and return a DataReader that allows the user to process
213 * the results.
214 */
215 // There must be a valid and open connection.
216 if (m_connection == null || m_connection.State != ConnectionState.Open)
217 throw new InvalidOperationException("Connection must valid and open");
218
219 // Execute the command.
220 RPMSDb.RPMSDbResultSet resultset;
221 m_connection.RPMSDb.Execute(m_sCmdText, out resultset);
222
223 return new BMXNetDataReader(resultset);
224 }
225
226 new public IDataReader ExecuteReader(CommandBehavior behavior)
227 {
228 /*
229 * ExecuteReader should retrieve results from the data source
230 * and return a DataReader that allows the user to process
231 * the results.
232 */
233
234 // There must be a valid and open connection.
235 if (m_connection == null || m_connection.State != ConnectionState.Open)
236 throw new InvalidOperationException("Connection must valid and open");
237
238 /*If SchemaOnly or KeyInfo behavior, Set BMXSchema flag
239 *execute the command, then unset the BMXSchema flag
240 *Otherwise, just Execute the command.
241 */
242 RPMSDb.RPMSDbResultSet resultset;
243 if (((behavior & (CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo)) > 0))
244 {
245
246 //TODO: This BMX SCHEMA ONLY TRUE is a setting a key flag
247 //to respond with only the schema. If should be good with just
248 //one call, the set to FALSE should be unneeded.
249 m_connection.SessionConnection.TransmitRPC("BMX SCHEMA ONLY", "TRUE");
250 m_connection.RPMSDb.Execute(m_sCmdText, out resultset);
251 m_connection.SessionConnection.TransmitRPC("BMX SCHEMA ONLY", "FALSE");
252 }
253 else
254 {
255 m_connection.RPMSDb.Execute(m_sCmdText, out resultset);
256 }
257
258 /*
259 * The only CommandBehavior option supported by BMXNet
260 * is the automatic closing of the connection
261 * when the user is done with the reader.
262 */
263 if (behavior == CommandBehavior.CloseConnection)
264 return new BMXNetDataReader(resultset, m_connection);
265 else
266 return new BMXNetDataReader(resultset);
267 }
268
269 override protected DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
270 {
271 throw new Exception("The method or operation is not implemented.");
272 }
273
274 override public object ExecuteScalar()
275 {
276 /*
277 * ExecuteScalar assumes that the command will return a single
278 * row with a single column, or if more rows/columns are returned
279 * it will return the first column of the first row.
280 */
281
282 // There must be a valid and open connection.
283 if (m_connection == null || m_connection.State != ConnectionState.Open)
284 throw new InvalidOperationException("Connection must valid and open");
285
286 // Execute the command.
287 RPMSDb.RPMSDbResultSet resultset;
288 m_connection.RPMSDb.Execute(m_sCmdText, out resultset);
289
290 // Return the first column of the first row.
291 // Return a null reference if there is no data.
292 if (resultset.data.Length == 0)
293 return null;
294
295 return resultset.data[0, 0];
296 }
297
298 override public void Prepare()
299 {
300 // BMXNet Prepare is a no-op.
301 }
302
303 void IDisposable.Dispose()
304 {
305 this.Dispose(true);
306 System.GC.SuppressFinalize(this);
307 }
308
309 //private void Dispose(bool disposing)
310 //{
311 // /*
312 // * Dispose of the object and perform any cleanup.
313 // */
314 //}
315
316 /****
317 * IMPLEMENT BMX-Specific METHODS.
318 ****/
319 public void BMXBuildUpdateCommand(DataTable dtSchema)
320 {
321 string sText = "UPDATE ";
322 sText += "@File, ";
323 sText += "@Record, ";
324
325 //Build Parameters array
326 BMXNetParameter[] parms = new BMXNetParameter[dtSchema.Rows.Count+2];
327
328 parms[0] = new BMXNetParameter("@File", DbType.String);
329 parms[0].SourceVersion = DataRowVersion.Original;
330 parms[0].SourceColumn = dtSchema.ExtendedProperties["BMXTable"].ToString();
331 Parameters.Add(parms[0]);
332
333 parms[1] = new BMXNetParameter("@Record", DbType.String);
334 parms[1].SourceVersion = DataRowVersion.Original;
335 parms[1].SourceColumn = dtSchema.ExtendedProperties["BMXKey"].ToString();;
336 Parameters.Add(parms[1]);
337
338 int i = 1;
339 foreach (DataRow r in dtSchema.Rows)
340 {
341 //Make a parameter for the Key Field and all non-ReadOnly fields
342 if ( ((bool) r["IsReadOnly"] == false) || ( (bool) r["IsKey"] == true ) )
343 {
344 i++;
345 parms[i] = new BMXNetParameter(r["ColumnName"].ToString(), DbType.String);
346 parms[i].SourceVersion = DataRowVersion.Current;
347 parms[i].SourceColumn = r["BaseColumnName"].ToString(); //FM FieldNumber
348 parms[i].IsKey = Convert.ToBoolean(r["IsKey"]);
349 Parameters.Add(parms[i]);
350 }
351 }
352 }
353 }
354}
Note: See TracBrowser for help on using the repository browser.