source: BMXNET_RPMS_dotNET_UTILITIES-BMX/trunk/cs/bmx_0200scr/BMX2/BMXNet/BMXNetCommand.cs@ 815

Last change on this file since 815 was 815, checked in by Sam Habiel, 14 years ago

Initial commit of C# Source Code. Now to try to get it to compile.

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