IwbQuery.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Xml;
  7. using IwbZero.ToolCommon.FileHelpers;
  8. using IwbZero.ToolCommon.StringModel;
  9. namespace IwbZero.IwbDataQuery
  10. {
  11. public partial class IwbQuery
  12. {
  13. protected delegate void EvDictionaryChanged(Dictionary<string, object> dictionary);
  14. public IwbQuery()
  15. {
  16. ClearEnvironment();
  17. //DictionaryChanged += SaveToXml;
  18. }
  19. public IwbQuery(string id) : this()
  20. {
  21. Id = id;
  22. }
  23. public IwbQuery(string id,string filePath):this(id)
  24. {
  25. FilePath = filePath;
  26. }
  27. public string Id { get; }
  28. private string _filePath;
  29. public string FilePath
  30. {
  31. get => GetFilePath(Id);
  32. set => _filePath = value;
  33. }
  34. private string GetFilePath(string id)
  35. {
  36. return _filePath.IsEmpty() ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Resource/Data/IwbQuery/{id}.iwbx") : _filePath;
  37. }
  38. protected string _backendTableName = "";
  39. protected string TableName { get; set; }
  40. private DataTable _myDataTable;
  41. protected DataTable MyDataTable
  42. {
  43. get => _myDataTable ?? GetDataTable(FilePath);
  44. set=>_myDataTable=value;
  45. }
  46. private DataTable GetDataTable(string filePath)
  47. {
  48. return filePath.CXmlFileToDataTable();
  49. }
  50. private Hashtable Columns { get; set; }
  51. protected int CurrentIndex { get; set; }
  52. private string CustomDefaultColumns { get; set; }
  53. private string CustomDefaultValues { get; set; }
  54. protected int LastRecordId { get; set; }
  55. public string SortString { get; set; }
  56. private readonly ArrayList LinkQueries = new ArrayList();
  57. public DataRowCollection Rows => MyDataTable.Rows;
  58. public DataRow CurrentRow
  59. {
  60. get
  61. {
  62. if (((CurrentIndex != -1) && (MyDataTable != null)) && (CurrentIndex < MyDataTable.DefaultView.Count))
  63. {
  64. return MyDataTable.DefaultView[CurrentIndex].Row;
  65. }
  66. return null;
  67. }
  68. }
  69. public DataTable CurrentTable => MyDataTable;
  70. public bool IsNewRow
  71. {
  72. get
  73. {
  74. bool flag = false;
  75. if (CurrentRow != null)
  76. {
  77. flag = CurrentRow.RowState == DataRowState.Added;
  78. }
  79. return flag;
  80. }
  81. }
  82. public bool IsOpened => (MyDataTable != null);
  83. public DataColumnCollection CurColumns => MyDataTable.Columns;
  84. public ArrayList IwbColumns
  85. {
  86. get
  87. {
  88. ArrayList list = new ArrayList();
  89. for (int i = 0; i < MyDataTable.Columns.Count; i++)
  90. {
  91. string key = MyDataTable.Columns[i].ColumnName.UAndT();
  92. if (IwbDataColumns.ContainsKey(key))
  93. {
  94. list.Add(IwbDataColumns[key]);
  95. }
  96. }
  97. return list;
  98. }
  99. }
  100. public Hashtable IwbDataColumns
  101. {
  102. get
  103. {
  104. if ((MyDataTable != null) && (Columns == null))
  105. {
  106. Columns = new Hashtable();
  107. for (int i = 0; i < MyDataTable.Columns.Count; i++)
  108. {
  109. string columnName = MyDataTable.Columns[i].ColumnName;
  110. //IwbDataColumn dataColumn = AppEnv.GetDataColumn(IwbDataColumn.GetBaseColumn(columnName), null);
  111. var dataColumn= new IwbDataColumn();
  112. if (dataColumn != null)
  113. {
  114. Columns.Add(columnName.ToUpper(), dataColumn);
  115. }
  116. }
  117. }
  118. return Columns;
  119. }
  120. }
  121. public int PageCount
  122. {
  123. get
  124. {
  125. int num = RecCount / RecordsPerPage;
  126. if ((RecCount % RecordsPerPage) != 0)
  127. {
  128. num++;
  129. }
  130. return num;
  131. }
  132. }
  133. public int PageNo
  134. {
  135. get
  136. {
  137. return ((RecNo / RecordsPerPage) + 1);
  138. }
  139. set
  140. {
  141. if (value > 0)
  142. {
  143. var recCount = ((value - 1) * RecordsPerPage) + 1;
  144. if (recCount > RecCount)
  145. {
  146. recCount = RecCount;
  147. }
  148. int recNo = RecNo;
  149. Go(recCount - 1);
  150. if (RowIndexInPage(recNo) != -1)
  151. {
  152. Go(recNo);
  153. }
  154. }
  155. }
  156. }
  157. public int RecCount
  158. {
  159. get
  160. {
  161. if (MyDataTable != null)
  162. {
  163. return MyDataTable.DefaultView.Count;
  164. }
  165. return -1;
  166. }
  167. }
  168. public int RecNo
  169. {
  170. get
  171. {
  172. if ((CurrentIndex < 0) | (CurrentIndex >= RecCount))
  173. {
  174. return -1;
  175. }
  176. return CurrentIndex;
  177. }
  178. set => Go(value);
  179. }
  180. public int RecordId
  181. {
  182. get
  183. {
  184. int num = -1;
  185. DataRow currentRow = CurrentRow;
  186. if ((currentRow != null) && MyDataTable.Columns.Contains(IwbZeroConsts.RecordIdentityCol))
  187. {
  188. object obj2 = currentRow[IwbZeroConsts.RecordIdentityCol];
  189. if (!(obj2 is DBNull))
  190. {
  191. num = (int)obj2;
  192. }
  193. }
  194. return num;
  195. }
  196. }
  197. public int RecordsPerPage { get; set; }
  198. public virtual DataRow AddCurrentRowFrom(IwbQuery poSource)
  199. {
  200. DataRow row = MyDataTable.NewRow();
  201. MyDataTable.Rows.Add(row);
  202. GoTo(row);
  203. ReplaceCurrentRow(poSource.CurrentRow);
  204. return row;
  205. }
  206. public DataRow MoveFirst()
  207. {
  208. if ((CurrentIndex != 0) )
  209. //if ((_CurrentIndex != 0) && FireBeforeRecordMoved())
  210. {
  211. CurrentIndex = 0;
  212. //FireRecordMovedEvent();
  213. }
  214. return CurrentRow;
  215. }
  216. public DataRow MoveLast()
  217. {
  218. if ((CurrentIndex != (MyDataTable.DefaultView.Count - 1)) )
  219. //if ((_CurrentIndex != (_myDataTable.DefaultView.Count - 1)) && FireBeforeRecordMoved())
  220. {
  221. CurrentIndex = MyDataTable.DefaultView.Count - 1;
  222. //FireRecordMovedEvent();
  223. }
  224. return CurrentRow;
  225. }
  226. public DataRow MoveNext()
  227. {
  228. if ((CurrentIndex < (MyDataTable.DefaultView.Count - 1)) )
  229. // if ((_CurrentIndex < (_myDataTable.DefaultView.Count - 1)) && FireBeforeRecordMoved())
  230. {
  231. CurrentIndex++;
  232. //FireRecordMovedEvent();
  233. }
  234. return CurrentRow;
  235. }
  236. public DataRow MovePrev()
  237. {
  238. if ((CurrentIndex > 0) )
  239. //if ((_CurrentIndex > 0) && FireBeforeRecordMoved())
  240. {
  241. CurrentIndex--;
  242. //FireRecordMovedEvent();
  243. }
  244. return CurrentRow;
  245. }
  246. public virtual DataRow NewRow()
  247. {
  248. DataRow row;
  249. //if (FireBeforeAddNewRecord())
  250. {
  251. row = MyDataTable.NewRow();
  252. MyDataTable.Rows.Add(row);
  253. //MarkNewRowRecordId(row);
  254. CurrentIndex = -1;
  255. GoTo(row);
  256. //ApplyCustomDefaults();
  257. }
  258. return row;
  259. }
  260. public DataRow NewRowFrom(DataRow poDr)
  261. {
  262. DataRow row = MyDataTable.NewRow();
  263. row.BeginEdit();
  264. foreach (DataColumn column in CurColumns)
  265. {
  266. string name = column.ColumnName.Trim().ToUpper();
  267. if (poDr.Table.Columns.Contains(name))
  268. {
  269. row[name] = poDr[name];
  270. }
  271. }
  272. row.EndEdit();
  273. MyDataTable.Rows.Add(row);
  274. GoTo(row);
  275. return row;
  276. }
  277. public byte[] GetBytes(string pcFieldName)
  278. {
  279. byte[] buffer = null;
  280. if (CurColumns.Contains(pcFieldName))
  281. {
  282. object obj2 = CurrentRow[pcFieldName];
  283. if ((obj2 != null) && !(obj2 is DBNull))
  284. {
  285. string s = GetString(pcFieldName);
  286. if (s != "")
  287. {
  288. buffer = Convert.FromBase64String(s);
  289. }
  290. }
  291. }
  292. return buffer;
  293. }
  294. public DateTime GetDateTime(string pcFieldName)
  295. {
  296. return GetString(pcFieldName, CurrentRow).StrToDt();
  297. }
  298. public decimal GetDecimal(string pcFieldName)
  299. {
  300. return GetString(pcFieldName).ValD();
  301. }
  302. public string GetDisplayString(string pcFieldName)
  303. {
  304. string pcStandardValue = GetString(pcFieldName);
  305. string str2 = StringHelper.UAndT(pcFieldName);
  306. if (IwbDataColumns[str2] != null)
  307. {
  308. pcStandardValue = ((IwbDataColumn)IwbDataColumns[str2]).GetDisplayString(pcStandardValue);
  309. }
  310. return pcStandardValue;
  311. }
  312. public string GetFieldValues(string pcFieldNames)
  313. {
  314. return GetFieldValues(pcFieldNames, CurrentRow);
  315. }
  316. public string GetFieldValues(string pcFieldNames, DataRow poDataRow)
  317. {
  318. string pcSource = "";
  319. foreach (string str2 in StringHelper.StrToArray(pcFieldNames))
  320. {
  321. if (CurColumns.Contains(str2))
  322. {
  323. pcSource = StringHelper.AddStr(pcSource, GetString(str2, poDataRow));
  324. }
  325. else
  326. {
  327. pcSource = StringHelper.AddStr(pcSource, str2);
  328. }
  329. }
  330. return pcSource;
  331. }
  332. public int GetInt(string pcFieldName) => StringHelper.ValI(GetString(pcFieldName));
  333. public IwbDataColumn GetDataColumn(string pcColumnName)
  334. {
  335. IwbDataColumn column = null;
  336. pcColumnName = pcColumnName.UAndT();
  337. if (IwbDataColumns.ContainsKey(pcColumnName))
  338. {
  339. column = (IwbDataColumn)IwbDataColumns[pcColumnName];
  340. }
  341. return column;
  342. }
  343. public string GetString(string pcFieldName)
  344. {
  345. return GetString(pcFieldName, CurrentRow);
  346. }
  347. public string GetString(string pcFieldName, DataRow poDr)
  348. {
  349. string str = "";
  350. if (((MyDataTable == null) || !MyDataTable.Columns.Contains(pcFieldName)) || (poDr == null))
  351. {
  352. return str;
  353. }
  354. if (poDr[pcFieldName] is DateTime)
  355. {
  356. try
  357. {
  358. DateTime time = (DateTime)poDr[pcFieldName];
  359. if (((time.Year != 0x76c) || (time.Month != 1)) || (time.Day != 1))
  360. {
  361. return time.ToString("G");
  362. }
  363. return "";
  364. }
  365. catch (Exception)
  366. {
  367. return "";
  368. }
  369. }
  370. return poDr[pcFieldName].ToString().Trim();
  371. }
  372. public string GetString(string pcFieldName, int piDecimalDigit)
  373. {
  374. string str = GetString(pcFieldName);
  375. decimal poObject = str.ValD();
  376. if (poObject.UAndT() != str)
  377. {
  378. return str;
  379. }
  380. string format = "0.";
  381. for (int i = 0; i < piDecimalDigit; i++)
  382. {
  383. format = format + "0";
  384. }
  385. return poObject.ToString(format);
  386. }
  387. public Hashtable GetValuesByCols(string pcCols, DataRow poDr)
  388. {
  389. Hashtable hashtable = new Hashtable();
  390. Array array = pcCols.StrToArray();
  391. for (int i = 0; i < array.Length; i++)
  392. {
  393. string key = array.GetValue(i).ToString().ToUpper().Trim();
  394. hashtable.Add(key, poDr[key]);
  395. }
  396. return hashtable;
  397. }
  398. public DataRow Go(int recordNumber)
  399. {
  400. //if ((recordNumber != _CurrentIndex) && FireBeforeRecordMoved())
  401. if ((recordNumber != CurrentIndex))
  402. {
  403. CurrentIndex = recordNumber;
  404. //FireRecordMovedEvent();
  405. }
  406. return CurrentRow;
  407. }
  408. public bool GoByRecordId(int piRecordId, bool plFireRecordMoveEvent)
  409. {
  410. bool flag = false;
  411. if (MyDataTable.Columns.Contains(IwbZeroConsts.RecordIdentityCol))
  412. {
  413. int num = CurrentIndex;
  414. CurrentIndex = 0;
  415. while (CurrentIndex < RecCount)
  416. {
  417. if (RecordId == piRecordId)
  418. {
  419. //if (plFireRecordMoveEvent)
  420. //{
  421. // bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
  422. // IsToFireRecordMovedEvent = true;
  423. // FireRecordMovedEvent();
  424. // IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
  425. //}
  426. flag = true;
  427. break;
  428. }
  429. CurrentIndex++;
  430. }
  431. if (!flag)
  432. {
  433. CurrentIndex = num;
  434. }
  435. }
  436. return flag;
  437. }
  438. public void GoTo(DataRow dr)
  439. {
  440. if ((dr != null) && ((RecNo <= -1) || (MyDataTable.DefaultView[RecNo].Row != dr)))
  441. {
  442. for (int i = 0; i < RecCount; i++)
  443. {
  444. if (MyDataTable.DefaultView[i].Row == dr)
  445. {
  446. Go(i);
  447. return;
  448. }
  449. }
  450. }
  451. }
  452. //public void GoTo(DataRow podr, bool plFireEvent)
  453. //{
  454. // bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
  455. // IsToFireRecordMovedEvent = plFireEvent;
  456. // GoTo(podr);
  457. // IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
  458. //}
  459. public bool GoToRecordByFieldsAndValues(string pcFields, string pcValues)
  460. {
  461. int num = CurrentIndex;
  462. StringHelper.StrToArray(pcFields);
  463. StringHelper.StrToArray(pcValues);
  464. bool flag = false;
  465. CurrentIndex = 0;
  466. while (CurrentIndex < RecCount)
  467. {
  468. if (IsValuesMatch(CurrentRow, pcFields, pcValues))
  469. {
  470. flag = true;
  471. break;
  472. }
  473. CurrentIndex++;
  474. }
  475. if (flag && (CurrentIndex != num))
  476. {
  477. //FireRecordMovedEvent();
  478. return true;
  479. }
  480. CurrentIndex = num;
  481. return flag;
  482. }
  483. public DataRow GoToRecordByString(string pcStr, string pcColName)
  484. {
  485. int num = CurrentIndex;
  486. pcStr = StringHelper.UAndT(pcStr);
  487. int length = pcStr.Length;
  488. bool flag = false;
  489. if (CurColumns.Contains(pcColName))
  490. {
  491. CurrentIndex = 0;
  492. while (CurrentIndex < RecCount)
  493. {
  494. string str = StringHelper.UAndT(CurrentRow[pcColName]);
  495. if ((str.Length >= length) && (str.Substring(0, length) == pcStr))
  496. {
  497. flag = true;
  498. break;
  499. }
  500. CurrentIndex++;
  501. }
  502. }
  503. if (flag)
  504. {
  505. //FireRecordMovedEvent();
  506. }
  507. else
  508. {
  509. CurrentIndex = num;
  510. }
  511. return CurrentRow;
  512. }
  513. public void GoToRecordInPage(int piRecordIndex)
  514. {
  515. if ((piRecordIndex >= 0) && (piRecordIndex < RecordsPerPage))
  516. {
  517. int recordNumber = ((PageNo - 1) * RecordsPerPage) + piRecordIndex;
  518. if (recordNumber < 0)
  519. {
  520. recordNumber = 0;
  521. }
  522. if (recordNumber >= RecCount)
  523. {
  524. recordNumber = RecCount - 1;
  525. }
  526. Go(recordNumber);
  527. }
  528. }
  529. public bool IsDataValid(string pcColName, string pcValue)
  530. {
  531. bool flag = true;
  532. string key = pcColName.UAndT();
  533. if (IwbDataColumns.ContainsKey(key))
  534. {
  535. IwbDataColumn column = (IwbDataColumn)IwbDataColumns[key];
  536. if (column == null)
  537. {
  538. return true;
  539. }
  540. IwbDataType dataType = column.IwbDataType;
  541. try
  542. {
  543. dataType.TranslateValue(pcValue);
  544. }
  545. catch
  546. {
  547. flag = false;
  548. //string pcString = AppEnv.T(0x18733, pcValue, column.ColumnName, exception.Message);
  549. //AppEnv.InfoBox(pcString);
  550. //if (AppEnv.AppMode == npAppModes.Web)
  551. //{
  552. // throw new Exception(pcString);
  553. //}
  554. }
  555. }
  556. return flag;
  557. }
  558. public bool IsValuesMatch(DataRow poDr, Hashtable poCriteria)
  559. {
  560. if (poCriteria != null)
  561. {
  562. Array array = new String[poCriteria.Count];
  563. poCriteria.Keys.CopyTo(array, 0);
  564. for (int i = 0; i < array.Length; i++)
  565. {
  566. string str = StringHelper.UAndT(array.GetValue(i));
  567. IwbDataColumn column = (IwbDataColumn)IwbDataColumns[str];
  568. if ((column == null) || (column.IwbDataType.Compare(poDr[str], poCriteria[str]) != 0))
  569. {
  570. return false;
  571. }
  572. }
  573. }
  574. return true;
  575. }
  576. public bool IsValuesMatch(DataRow poDr, string pcFields, string pcValues)
  577. {
  578. bool flag = true;
  579. Array array = pcFields.StrToArray();
  580. Array array2 = pcValues.StrToArray();
  581. Hashtable columns = IwbDataColumns;
  582. for (int i = 0; i < array.Length; i++)
  583. {
  584. string key = StringHelper.UAndT(array.GetValue(i));
  585. string poDest = "";
  586. if (i < array2.Length)
  587. {
  588. poDest = array2.GetValue(i).ToString();
  589. }
  590. if (columns.ContainsKey(key))
  591. {
  592. IwbDataColumn column = (IwbDataColumn)columns[key];
  593. if (column.IwbDataType.Compare(poDr[key], poDest) != 0)
  594. {
  595. flag = false;
  596. }
  597. }
  598. else
  599. {
  600. flag = IwbDataType.CompareStatic(poDest, poDr[key]) == 0;
  601. }
  602. if (!flag)
  603. {
  604. return false;
  605. }
  606. }
  607. return true;
  608. }
  609. public void ReplaceCurrentRow(DataRow dr)
  610. {
  611. DataRow currentRow = CurrentRow;
  612. if (currentRow != null)
  613. {
  614. currentRow.BeginEdit();
  615. foreach (DataColumn column in MyDataTable.Columns)
  616. {
  617. string columnName = column.ColumnName;
  618. if (dr.Table.Columns.Contains(columnName))
  619. {
  620. try
  621. {
  622. currentRow[columnName] = dr[columnName];
  623. }
  624. catch (Exception)
  625. {
  626. SetField(columnName, dr[columnName].ToString());
  627. }
  628. }
  629. }
  630. currentRow.EndEdit();
  631. //bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
  632. //IsToFireRecordMovedEvent = false;
  633. GoTo(currentRow);
  634. //IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
  635. }
  636. //FireRefreshRequested();
  637. }
  638. public void ReplaceFieldValues(string pcFields, string pcValues)
  639. {
  640. Array array = pcFields.StrToArray();
  641. Array array2 = pcValues.StrToArray();
  642. DataRow currentRow = CurrentRow;
  643. if ((array.Length == array2.Length) && (currentRow != null))
  644. {
  645. currentRow.BeginEdit();
  646. for (int i = 0; i < array.Length; i++)
  647. {
  648. string pcColName = array.GetValue(i).ToString().Trim();
  649. SetField(pcColName, array2.GetValue(i).ToString());
  650. }
  651. currentRow.EndEdit();
  652. //FireRefreshRequested();
  653. }
  654. }
  655. public void RestoreAllRows()
  656. {
  657. MyDataTable.RejectChanges();
  658. MoveFirst();
  659. }
  660. public bool RestoreCurrentRow()
  661. {
  662. DataRow currentRow = CurrentRow;
  663. if (currentRow != null)
  664. {
  665. if (currentRow.RowState == DataRowState.Added)
  666. {
  667. DeleteCurrentRow();
  668. }
  669. else
  670. {
  671. currentRow.RejectChanges();
  672. }
  673. //FireRecordMovedEvent();
  674. }
  675. return true;
  676. }
  677. public int RowIndexInPage(int piRecNo)
  678. {
  679. int num = -1;
  680. int num2 = (PageNo - 1) * RecordsPerPage;
  681. int num3 = (PageNo * RecordsPerPage) - 1;
  682. if (num3 >= RecCount)
  683. {
  684. num3 = RecCount - 1;
  685. }
  686. if ((piRecNo >= num2) && (piRecNo <= num3))
  687. {
  688. num = piRecNo - num2;
  689. }
  690. return num;
  691. }
  692. public virtual void SetBytes(string pcColName, byte[] poBytes)
  693. {
  694. if (CurColumns.Contains(pcColName) && (CurrentRow != null))
  695. {
  696. CurrentRow.BeginEdit();
  697. if ((poBytes == null))
  698. {
  699. CurrentRow[pcColName] = DBNull.Value;
  700. }
  701. else
  702. {
  703. string str = Convert.ToBase64String(poBytes, 0, poBytes.Length);
  704. CurrentRow[pcColName] = str;
  705. }
  706. CurrentRow.EndEdit();
  707. }
  708. }
  709. public void SetCustomDefault(string pcColumns, string pcValues)
  710. {
  711. CustomDefaultColumns = pcColumns;
  712. CustomDefaultValues = pcValues;
  713. }
  714. public virtual void SetField(string pcColName, string pcValue)
  715. {
  716. SetField(pcColName, pcValue, true);
  717. }
  718. public virtual void SetField(string pcColName, string pcValue, bool plFireFieldChangedEvent)
  719. {
  720. pcColName = pcColName.UAndT();
  721. DataRow currentRow = CurrentRow;
  722. if (currentRow != null)
  723. {
  724. string poDest = currentRow[pcColName].ToString().Trim();
  725. bool flag;
  726. if (IwbDataColumns.ContainsKey(pcColName))
  727. {
  728. flag = ((IwbDataColumn)IwbDataColumns[pcColName]).IwbDataType.Compare(pcValue, poDest) == 0;
  729. }
  730. else
  731. {
  732. flag = pcValue.Trim() == poDest;
  733. }
  734. if ((!flag || (currentRow[pcColName] == DBNull.Value)) && IsDataValid(pcColName, pcValue))
  735. {
  736. //bool isToFireColumnChanged = IsToFireColumnChanged;
  737. //IsToFireColumnChanged = plFireFieldChangedEvent;
  738. SetField(pcColName, pcValue, currentRow);
  739. if (CurrentRow != currentRow)
  740. {
  741. //bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
  742. //IsToFireRecordMovedEvent = false;
  743. GoTo(currentRow);
  744. //IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
  745. }
  746. //FireFieldValueChanged(pcColName);
  747. //IsToFireColumnChanged = isToFireColumnChanged;
  748. }
  749. }
  750. }
  751. public virtual void SetField(string pcColName, string pcValue, DataRow poDataRow)
  752. {
  753. if (poDataRow.Table.Columns.Contains(pcColName))
  754. {
  755. DataColumn column = poDataRow.Table.Columns[pcColName];
  756. if ((column.MaxLength >= 0) && (column.MaxLength < pcValue.Length))
  757. {
  758. pcValue = pcValue.Substring(0, column.MaxLength);
  759. }
  760. IwbDataType npDataTypeByType = IwbDataType.GetIwbDataTypeByType(CurColumns[pcColName].DataType);
  761. string pcOldValue = GetString(pcColName, poDataRow);
  762. CascadeValuesToChild(poDataRow, pcColName, pcValue, pcOldValue);
  763. poDataRow.BeginEdit();
  764. poDataRow[pcColName] = npDataTypeByType.TranslateValue(pcValue);
  765. poDataRow.EndEdit();
  766. }
  767. }
  768. private void SetNextLocalId()
  769. {
  770. object obj2 = MyDataTable.Compute("Max(" + IwbZeroConsts.IdenField + ")", "");
  771. if (obj2 is DBNull)
  772. {
  773. obj2 = 0;
  774. }
  775. SetField(IwbZeroConsts.IdenField, (((int)obj2) + 1).ToString(), false);
  776. }
  777. public void SortBy(string pcSort)
  778. {
  779. try
  780. {
  781. if (MyDataTable != null)
  782. {
  783. int recNo = -1;
  784. SortString = pcSort;
  785. DataRow currentRow = CurrentRow;
  786. MyDataTable.DefaultView.Sort = pcSort;
  787. if (currentRow != null)
  788. {
  789. GoTo(currentRow);
  790. recNo = RecNo;
  791. }
  792. DataTable table = MyDataTable.Copy();
  793. MyDataTable.Rows.Clear();
  794. MyDataTable.DefaultView.Sort = "";
  795. DataView view = new DataView(table, "", pcSort, DataViewRowState.CurrentRows);
  796. foreach (DataRowView view2 in view)
  797. {
  798. MyDataTable.ImportRow(view2.Row);
  799. }
  800. view = new DataView(table, "", pcSort, DataViewRowState.Deleted);
  801. foreach (DataRowView view3 in view)
  802. {
  803. MyDataTable.ImportRow(view3.Row);
  804. }
  805. CurrentIndex = recNo;
  806. //FireRefreshRequested();
  807. }
  808. }
  809. catch
  810. {
  811. //
  812. }
  813. }
  814. public void SortBy(string pcColumnId, bool plAscending)
  815. {
  816. string pcSort = pcColumnId + (plAscending ? " asc" : " desc");
  817. SortBy(pcSort);
  818. }
  819. public void PageFirst()
  820. {
  821. PageNo = 1;
  822. }
  823. public void PageLast()
  824. {
  825. PageNo = PageCount;
  826. }
  827. public void PageNext()
  828. {
  829. PageNo++;
  830. }
  831. public void PagePrev()
  832. {
  833. PageNo--;
  834. }
  835. public void AddLocalBal(string pcSourceQtyField)
  836. {
  837. AddLocalBal(pcSourceQtyField, IwbZeroConsts.IdenField);
  838. }
  839. public void AddLocalBal(string pcSourceQtyField, string pcTargetField)
  840. {
  841. if (IsOpened)
  842. {
  843. MyDataTable.Columns.Add(pcTargetField, typeof(decimal)).Caption = "Local Bal";
  844. Columns = null;
  845. int num = CurrentIndex;
  846. CurrentIndex = 0;
  847. while (CurrentIndex < RecCount)
  848. {
  849. SetField(pcTargetField, GetString(pcSourceQtyField), false);
  850. CurrentIndex++;
  851. }
  852. CurrentIndex = num;
  853. }
  854. }
  855. public void AddLocalIdentity()
  856. {
  857. if (IsOpened)
  858. {
  859. MyDataTable.Columns.Add(IwbZeroConsts.IdenField, typeof(int));
  860. Columns = null;
  861. int num = CurrentIndex;
  862. CurrentIndex = 0;
  863. while (CurrentIndex < RecCount)
  864. {
  865. SetNextLocalId();
  866. CurrentIndex++;
  867. }
  868. CurrentIndex = num;
  869. }
  870. }
  871. public void AddPk(string pcFields)
  872. {
  873. if ((MyDataTable != null) && !MyDataTable.Constraints.Contains("PrimaryKey"))
  874. {
  875. Array array = pcFields.StrToArray();
  876. Array array2 = Array.CreateInstance(typeof(DataColumn), array.Length);
  877. int num = 0;
  878. foreach (string str in array)
  879. {
  880. array2.SetValue(MyDataTable.Columns[str], num++);
  881. }
  882. MyDataTable.Constraints.Add("PrimaryKey", (DataColumn[])array2, true);
  883. }
  884. }
  885. protected virtual void ApplyCustomDefaults()
  886. {
  887. //FireAfterAddNewRecord();
  888. ReplaceFieldValues(CustomDefaultColumns, CustomDefaultValues);
  889. }
  890. private void CascadeValuesToChild(DataRow poParentRow, string pcColumn, string pcNewValue, string pcOldValue)
  891. {
  892. foreach (IwbLinkQuery query in LinkQueries)
  893. {
  894. query.CascadeFieldValues(this, poParentRow, pcColumn, pcNewValue, pcOldValue);
  895. }
  896. }
  897. protected virtual void ClearEnvironment()
  898. {
  899. CurrentIndex = -1;
  900. CustomDefaultColumns = "";
  901. CustomDefaultValues = "";
  902. }
  903. public virtual void CloneFrom(IwbQuery poSource)
  904. {
  905. //ConnectionId = poSource.ConnectionId;
  906. MyDataTable = poSource.CurrentTable.Clone();
  907. poSource.CurrentTable.Constraints.Clear();
  908. MyDataTable.TableName = "T" + StringHelper.GetRandom();
  909. //QueryCriteria = poSource.QueryCriteria;
  910. }
  911. public void Close()
  912. {
  913. ClearEnvironment();
  914. if (CurrentTable != null)
  915. {
  916. MyDataTable = null;
  917. }
  918. }
  919. protected void ColumnChanged(object sender, DataColumnChangeEventArgs e)
  920. {
  921. //FireFieldValueChanged(e.Column.ColumnName);
  922. }
  923. public object Compute(string pcAggrFunction, string pcExpr, string pcCriteria)
  924. {
  925. object obj2 = null;
  926. try
  927. {
  928. CurColumns.Add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", typeof(decimal), pcExpr);
  929. obj2 = CurrentTable.Compute(pcAggrFunction + "(ABCDEFGHIJKLMNOPQRSTUVWXYZ)", pcCriteria);
  930. CurColumns.Remove("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  931. }
  932. catch (Exception exception)
  933. {
  934. obj2 = exception;
  935. }
  936. return obj2;
  937. }
  938. public virtual DataRow CopyRow()
  939. {
  940. DataRow currentRow = CurrentRow;
  941. DataRow row = null;
  942. if (currentRow != null)
  943. {
  944. row = MyDataTable.NewRow();
  945. row.BeginEdit();
  946. foreach (DataColumn column in MyDataTable.Columns)
  947. {
  948. string columnName = column.ColumnName;
  949. row[columnName] = currentRow[columnName];
  950. }
  951. row.EndEdit();
  952. MyDataTable.Rows.Add(row);
  953. MarkNewRowRecordId(row);
  954. }
  955. return row;
  956. }
  957. protected void MarkNewRowRecordId(DataRow poRow)
  958. {
  959. if ((poRow != null) && MyDataTable.Columns.Contains(IwbZeroConsts.RecordIdentityCol))
  960. {
  961. poRow[IwbZeroConsts.RecordIdentityCol] = ++LastRecordId;
  962. }
  963. }
  964. public void CreateFrom(ArrayList poDataColumns)
  965. {
  966. MyDataTable = new DataTable(TableName);
  967. foreach (DataColumn column in poDataColumns)
  968. {
  969. MyDataTable.Columns.Add(column);
  970. }
  971. }
  972. public void DeleteAllRecord()
  973. {
  974. //bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
  975. //IsToFireRecordMovedEvent = false;
  976. while (RecCount > 0)
  977. {
  978. MoveFirst();
  979. DeleteCurrentRow();
  980. }
  981. //IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
  982. //FireRecordMovedEvent();
  983. }
  984. public virtual DataRow DeleteCurrentRow()
  985. {
  986. DataRow currentRow = CurrentRow;
  987. if (currentRow.RowState == DataRowState.Added)
  988. {
  989. MyDataTable.Rows.Remove(currentRow);
  990. }
  991. else
  992. {
  993. currentRow.Delete();
  994. }
  995. currentRow = CurrentRow;
  996. //bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
  997. //IsToFireRecordMovedEvent = false;
  998. if (currentRow != null)
  999. {
  1000. if (currentRow.RowState == DataRowState.Deleted)
  1001. {
  1002. currentRow = MoveNext();
  1003. }
  1004. }
  1005. else
  1006. {
  1007. currentRow = MoveLast();
  1008. }
  1009. if (CurrentIndex >= RecCount)
  1010. {
  1011. CurrentIndex = -1;
  1012. }
  1013. //IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
  1014. //FireRecordMovedEvent();
  1015. return currentRow;
  1016. }
  1017. public void FilterBy(string pcExpression)
  1018. {
  1019. MyDataTable.DefaultView.RowFilter = pcExpression;
  1020. }
  1021. public virtual void SaveToXml(DataTable dt)
  1022. {
  1023. dt.CDataToXmlFile(FilePath);
  1024. }
  1025. public virtual void SaveToXml()
  1026. {
  1027. MyDataTable.CDataToXmlFile(FilePath);
  1028. }
  1029. ~IwbQuery()
  1030. {
  1031. }
  1032. //public static Dictionary<string,object> Dictionary { get; set; }
  1033. //protected event EvDictionaryChanged DictionaryChanged;
  1034. //public void SetField(string name, object value)
  1035. //{
  1036. // Dictionary = Dictionary ?? GetDictionary();
  1037. // if (Dictionary.ContainsKey(name))
  1038. // {
  1039. // var oldValue = Dictionary[name];
  1040. // if (oldValue != value)
  1041. // {
  1042. // Dictionary[name] = value;
  1043. // DictionaryChanged?.Invoke(Dictionary);
  1044. // }
  1045. // }
  1046. // else
  1047. // {
  1048. // Dictionary.Add(name,value);
  1049. // DictionaryChanged?.Invoke(Dictionary);
  1050. // }
  1051. //}
  1052. //public string GetString(string name)
  1053. //{
  1054. // //Dictionary = Dictionary ?? GetDictionary();
  1055. // //if (Dictionary.ContainsKey(name))
  1056. // //{
  1057. // // return Dictionary[name].ToString();
  1058. // //}
  1059. // return "";
  1060. //}
  1061. //public int GetInt(string name)
  1062. //{
  1063. // return GetString(name).ValI();
  1064. //}
  1065. //public decimal GetDecimal(string name)
  1066. //{
  1067. // return GetString(name).ValD();
  1068. //}
  1069. //public bool GetBool(string name)
  1070. //{
  1071. // return GetString(name).ValB();
  1072. //}
  1073. //public DateTime GetDate(string name)
  1074. //{
  1075. // return GetString(name).StrToDt();
  1076. //}
  1077. //public virtual void SaveToXml(Dictionary<string, object> dictionary)
  1078. //{
  1079. // if (dictionary.Count > 0)
  1080. // {
  1081. // var str = "";
  1082. // foreach (var o in dictionary)
  1083. // {
  1084. // str += $"<{o.Key}>{o.Value.ToString().FormatCode2Xml()}</{o.Key}>\r\n";
  1085. // }
  1086. // XmlDocument xDoc = new XmlDocument {InnerText = str};
  1087. // xDoc.Save(FilePath);
  1088. // }
  1089. //}
  1090. //public Dictionary<string, object> GetDictionary()
  1091. //{
  1092. // if (Dictionary != null)
  1093. // {
  1094. // return Dictionary;
  1095. // }
  1096. // var dic= new Dictionary<string,object>();
  1097. // if (FilePath.IsNotEmpty())
  1098. // {
  1099. // XmlDocument xDoc= new XmlDocument();
  1100. // try
  1101. // {
  1102. // xDoc.Load(FilePath);
  1103. // var xmlNode = xDoc.LastChild;
  1104. // if (xmlNode != null && xmlNode.HasChildNodes)
  1105. // {
  1106. // foreach (XmlNode node in xmlNode.ChildNodes)
  1107. // {
  1108. // dic.Add(node.Name, (node.FirstChild?.Value).FormatXml2Code() ?? "");
  1109. // }
  1110. // }
  1111. // }
  1112. // catch(Exception )
  1113. // {
  1114. // //
  1115. // }
  1116. // }
  1117. // return dic;
  1118. //}
  1119. }
  1120. }