source: BMXNET_RPMS_dotNET_UTILITIES-BMX/trunk/cs/bmx_0200scr/BMX2/BMXNet/BMXNetAdapter.cs@ 1759

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

Support for other encodings besides ASCII. Constructors available in BMXNetLib and and BMXNetConnectInfo.
Comments in several files.

File size: 10.6 KB
Line 
1using System;
2using System.Data;
3using System.Data.Common;
4
5namespace IndianHealthService.BMXNet
6{
7 public class BMXNetDataAdapter : DbDataAdapter, IDbDataAdapter
8 {
9 private BMXNetCommand m_selectCommand;
10 private BMXNetCommand m_insertCommand;
11 private BMXNetCommand m_updateCommand;
12 private BMXNetCommand m_deleteCommand;
13
14 /*
15 * Inherit from Component through DbDataAdapter. The event
16 * mechanism is designed to work with the Component.Events
17 * property. These variables are the keys used to find the
18 * events in the components list of events.
19 */
20 static private readonly object EventRowUpdated = new object();
21 static private readonly object EventRowUpdating = new object();
22
23 public BMXNetDataAdapter()
24 {
25 }
26
27 new public BMXNetCommand SelectCommand
28 {
29 get { return m_selectCommand; }
30 set { m_selectCommand = value; }
31 }
32
33 IDbCommand IDbDataAdapter.SelectCommand
34 {
35 get { return m_selectCommand; }
36 set { m_selectCommand = (BMXNetCommand)value; }
37 }
38
39 new public BMXNetCommand InsertCommand
40 {
41 get { return m_insertCommand; }
42 set { m_insertCommand = value; }
43 }
44
45 IDbCommand IDbDataAdapter.InsertCommand
46 {
47 get { return m_insertCommand; }
48 set { m_insertCommand = (BMXNetCommand)value; }
49 }
50
51 new public BMXNetCommand UpdateCommand
52 {
53 get { return m_updateCommand; }
54 set { m_updateCommand = value; }
55 }
56
57 IDbCommand IDbDataAdapter.UpdateCommand
58 {
59 get { return m_updateCommand; }
60 set { m_updateCommand = (BMXNetCommand)value; }
61 }
62
63 new public BMXNetCommand DeleteCommand
64 {
65 get { return m_deleteCommand; }
66 set { m_deleteCommand = value; }
67 }
68
69 IDbCommand IDbDataAdapter.DeleteCommand
70 {
71 get { return m_deleteCommand; }
72 set { m_deleteCommand = (BMXNetCommand)value; }
73 }
74
75 /*
76 * Implement abstract methods inherited from DbDataAdapter.
77 */
78
79 public override int Fill(DataSet ds)
80 {
81 //The inital call to base.fill calls the RPC which loads up the array
82 //After base.fill returns, create a datareader
83 BMXNetConnection bmxConn = (BMXNetConnection) this.SelectCommand.Connection;
84 RPMSDb bmxDB = bmxConn.RPMSDb;
85
86 DataTable dt = new DataTable();
87
88 //Execute the RPC call
89 base.Fill(dt);
90 //Get the table name
91 dt.TableName = bmxDB.ResultSets[0].fmFileID;
92 dt.ExtendedProperties.Add("fmSeed", bmxDB.ResultSets[0].fmSeed);
93
94
95 string sParentTable = dt.TableName;
96
97 //Add the first table to the DataSet
98 ds.Tables.Add(dt);
99
100 //If bmxDB resultset array count is greater than 1
101 int nSets = bmxDB.ResultSets.GetUpperBound(0) + 1;
102
103 if (nSets > 1)
104 {
105 //Set primary key for first table
106 string sKeyField = bmxDB.ResultSets[0].fmKeyField;
107 DataColumn dcKey = dt.Columns[sKeyField];
108 DataColumn[] dcKeys = new DataColumn[1];
109 dcKeys[0] = dcKey;
110 dt.PrimaryKey = dcKeys;
111
112 string[] sRelations = new string[nSets];
113
114 //loop and get the rest of the tables
115 for (int k = 1; k < nSets; k++)
116 {
117 //Increment the current recordset counter in bmxDB
118 bmxDB.CurrentRecordSet++;
119 //Create the next table
120 dt = new DataTable();
121 //Fill it
122 base.Fill(dt);
123 //Get the table name
124 string sChildTable = bmxDB.ResultSets[k].fmFileID;
125 dt.TableName = sChildTable;
126 //Add it to the dataset
127 ds.Tables.Add(dt);
128
129 //Get the foreign key field
130 string sForeignKey = bmxDB.ResultSets[k].fmForeignKey;
131
132 //Set the data relationship
133 string sParentKey;
134 sParentKey = "BMXIEN";
135 sParentKey = "PATIENT_IEN";
136
137 DataRelation dr = new DataRelation("Relation" + k.ToString() , //Relation Name
138 ds.Tables[sParentTable].Columns[sParentKey], //; //parent
139 ds.Tables[sChildTable].Columns[sForeignKey]) ;//child
140 ds.Relations.Add(dr);
141 }
142 bmxDB.CurrentRecordSet = 0;
143 }
144 return dt.Rows.Count;
145 }
146
147 override protected DataTable FillSchema(
148 DataTable dataTable,
149 SchemaType schemaType,
150 IDbCommand command,
151 CommandBehavior behavior
152 )
153 {
154 behavior = CommandBehavior.SchemaOnly;
155 BMXNetDataReader dReader =(BMXNetDataReader) command.ExecuteReader(behavior);
156 DataTable dtSchema = dReader.GetSchemaTable();
157 return dtSchema;
158 }
159
160 override protected int Update(
161 DataRow[] dataRows,
162 DataTableMapping tableMapping
163 )
164 {
165 //Build UpdateCommand's m_sCmdText using UpdateCommand's parameters
166 // and data in dataRows;
167 // execute non query and increment nRet;
168
169 string sCmd = "";
170 int nRecordsAffected = 0;
171
172 //Get recordset-level info from parameters
173 BMXNetParameter parm = (BMXNetParameter) UpdateCommand.Parameters[0];
174 string sFileID = parm.SourceColumn;
175 parm = (BMXNetParameter) UpdateCommand.Parameters[1];
176 string sKeyField = parm.SourceColumn;
177 string sKeyID = "";
178 char[] cRecDelim = new char[1];
179 cRecDelim[0] = (char) 30;
180
181 string sValue = "";
182 string sFMFieldID;
183 string sColumnName;
184 int nColIndex;
185
186 //Process deletions
187 foreach (DataRow r in dataRows)
188 {
189 if (r.RowState == DataRowState.Deleted)
190 {
191 r.RejectChanges(); //so that I can get to the row id
192 //Build DAS
193 char cSep = Convert.ToChar(",");
194 string[] saKeyFields = sKeyField.Split(cSep);
195 string sTmp = "";
196 for (int j = 0; j < saKeyFields.GetLength(0); j++)
197 {
198 if (saKeyFields[j] != "")
199 {
200 if (j > 0)
201 sTmp = sTmp + ",";
202 if (j == saKeyFields.GetLength(0) - 1)
203 sTmp += "-";
204 sTmp += r[saKeyFields[j]];
205 }
206 }
207 sCmd = sTmp;
208 sCmd = sFileID + "^" + sCmd + "^";
209 UpdateCommand.CommandText = "UPDATE " + sCmd;
210 int nRet = this.UpdateCommand.ExecuteNonQuery();
211 r.Delete();
212 nRecordsAffected += nRet;
213 }
214 }
215
216 //Process Edits and Adds
217 foreach (DataRow r in dataRows)
218 {
219 if (r.RowState != DataRowState.Deleted)
220 {
221 string sMsg = "";
222 sKeyID = "";
223 for (int j=2; j < UpdateCommand.Parameters.Count; j++)
224 {
225 parm = (BMXNetParameter) UpdateCommand.Parameters[j];
226 sColumnName = parm.ParameterName;
227 sFMFieldID = parm.SourceColumn;
228 //Find a column id in r whose column name is sColumnName
229 nColIndex = -1;
230 for (int k = 0; k < r.Table.Columns.Count; k ++)
231 {
232 if (r.Table.Columns[k].ColumnName == sColumnName)
233 {
234 nColIndex = k;
235 break;
236 }
237 }
238 if (nColIndex > -1)
239 {
240 if (r.ItemArray[nColIndex].GetType() == typeof(System.DateTime))
241 {
242 DateTime dValue = (DateTime) r.ItemArray[nColIndex];
243 if ((dValue.Minute == 0) && (dValue.Hour == 0))
244 {
245 sValue = dValue.ToString("M-d-yyyy");
246 }
247 else
248 {
249 sValue = dValue.ToString("M-d-yyyy@HH:mm");
250 }
251 }
252 else
253 {
254 sValue = r.ItemArray[nColIndex].ToString();
255 }
256 if (parm.IsKey == false)
257 {
258 if (sMsg != "")
259 sMsg += (char) 30;
260 sMsg += sFMFieldID + "|" + sValue;
261 }
262 }
263 switch (sFMFieldID)
264 {
265 case ".0001":
266 if (sKeyID == "")
267 {
268 sKeyID = sValue + ",";
269 }
270 else
271 {
272 sKeyID = sValue + "," + sKeyID;
273 }
274 break;
275 case ".001":
276 if (sKeyID == "")
277 {
278 sKeyID = sValue;
279 }
280 else
281 {
282 sKeyID = sKeyID + sValue;
283 }
284 break;
285 default:
286 break;
287
288 }
289 }
290 sCmd = sFileID + "^" + sKeyID + "^" + sMsg;
291 UpdateCommand.CommandText = "UPDATE " + sCmd;
292 int nRet = this.UpdateCommand.ExecuteNonQuery();
293 nRecordsAffected += nRet;
294 }//end if RowState != deleted
295 }//end for
296
297 return nRecordsAffected;
298 }
299
300 override protected RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
301 {
302 return new BMXNetRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
303 }
304
305 override protected RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
306 {
307 return new BMXNetRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
308 }
309
310 override protected void OnRowUpdating(RowUpdatingEventArgs value)
311 {
312 BMXNetRowUpdatingEventHandler handler = (BMXNetRowUpdatingEventHandler) Events[EventRowUpdating];
313 if ((null != handler) && (value is BMXNetRowUpdatingEventArgs))
314 {
315 handler(this, (BMXNetRowUpdatingEventArgs) value);
316 }
317 }
318
319 override protected void OnRowUpdated(RowUpdatedEventArgs value)
320 {
321 BMXNetRowUpdatedEventHandler handler = (BMXNetRowUpdatedEventHandler) Events[EventRowUpdated];
322 if ((null != handler) && (value is BMXNetRowUpdatedEventArgs))
323 {
324 handler(this, (BMXNetRowUpdatedEventArgs) value);
325 }
326 }
327
328 public event BMXNetRowUpdatingEventHandler RowUpdating
329 {
330 add { Events.AddHandler(EventRowUpdating, value); }
331 remove { Events.RemoveHandler(EventRowUpdating, value); }
332 }
333
334 public event BMXNetRowUpdatedEventHandler RowUpdated
335 {
336 add { Events.AddHandler(EventRowUpdated, value); }
337 remove { Events.RemoveHandler(EventRowUpdated, value); }
338 }
339 }
340
341 public delegate void BMXNetRowUpdatingEventHandler(object sender, BMXNetRowUpdatingEventArgs e);
342 public delegate void BMXNetRowUpdatedEventHandler(object sender, BMXNetRowUpdatedEventArgs e);
343
344 public class BMXNetRowUpdatingEventArgs : RowUpdatingEventArgs
345 {
346 public BMXNetRowUpdatingEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
347 : base(row, command, statementType, tableMapping)
348 {
349 }
350
351 // Hide the inherited implementation of the command property.
352 new public BMXNetCommand Command
353 {
354 get { return (BMXNetCommand)base.Command; }
355 set { base.Command = value; }
356 }
357 }
358
359 public class BMXNetRowUpdatedEventArgs : RowUpdatedEventArgs
360 {
361 public BMXNetRowUpdatedEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
362 : base(row, command, statementType, tableMapping)
363 {
364 }
365
366 // Hide the inherited implementation of the command property.
367 new public BMXNetCommand Command
368 {
369 get { return (BMXNetCommand)base.Command; }
370 }
371 }
372}
Note: See TracBrowser for help on using the repository browser.