| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Xml;
- using IwbZero.ToolCommon.FileHelpers;
- using IwbZero.ToolCommon.StringModel;
- namespace IwbZero.IwbDataQuery
- {
- public partial class IwbQuery
- {
- protected delegate void EvDictionaryChanged(Dictionary<string, object> dictionary);
- public IwbQuery()
- {
- ClearEnvironment();
- //DictionaryChanged += SaveToXml;
- }
- public IwbQuery(string id) : this()
- {
- Id = id;
- }
- public IwbQuery(string id, string filePath) : this(id)
- {
- FilePath = filePath;
- }
- public string Id { get; }
- private string _filePath;
- public string FilePath
- {
- get => GetFilePath(Id);
- set => _filePath = value;
- }
- private string GetFilePath(string id)
- {
- return _filePath.Empty() ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Resource/Data/IwbQuery/{id}.iwbx") : _filePath;
- }
- protected string _backendTableName = "";
- protected string TableName { get; set; }
- private DataTable _myDataTable;
- protected DataTable MyDataTable
- {
- get => _myDataTable ?? GetDataTable(FilePath);
- set => _myDataTable = value;
- }
- private DataTable GetDataTable(string filePath)
- {
- return filePath.CXmlFileToDataTable();
- }
- private Hashtable Columns { get; set; }
- protected int CurrentIndex { get; set; }
- private string CustomDefaultColumns { get; set; }
- private string CustomDefaultValues { get; set; }
- protected int LastRecordId { get; set; }
- public string SortString { get; set; }
- private readonly ArrayList LinkQueries = new ArrayList();
- public DataRowCollection Rows => MyDataTable.Rows;
- public DataRow CurrentRow
- {
- get
- {
- if (((CurrentIndex != -1) && (MyDataTable != null)) && (CurrentIndex < MyDataTable.DefaultView.Count))
- {
- return MyDataTable.DefaultView[CurrentIndex].Row;
- }
- return null;
- }
- }
- public DataTable CurrentTable => MyDataTable;
- public bool IsNewRow
- {
- get
- {
- bool flag = false;
- if (CurrentRow != null)
- {
- flag = CurrentRow.RowState == DataRowState.Added;
- }
- return flag;
- }
- }
- public bool IsOpened => (MyDataTable != null);
- public DataColumnCollection CurColumns => MyDataTable.Columns;
- public ArrayList IwbColumns
- {
- get
- {
- ArrayList list = new ArrayList();
- for (int i = 0; i < MyDataTable.Columns.Count; i++)
- {
- string key = MyDataTable.Columns[i].ColumnName.UAndT();
- if (IwbDataColumns.ContainsKey(key))
- {
- list.Add(IwbDataColumns[key]);
- }
- }
- return list;
- }
- }
- public Hashtable IwbDataColumns
- {
- get
- {
- if ((MyDataTable != null) && (Columns == null))
- {
- Columns = new Hashtable();
- for (int i = 0; i < MyDataTable.Columns.Count; i++)
- {
- string columnName = MyDataTable.Columns[i].ColumnName;
- //IwbDataColumn dataColumn = AppEnv.GetDataColumn(IwbDataColumn.GetBaseColumn(columnName), null);
- var dataColumn = new IwbDataColumn();
- if (dataColumn != null)
- {
- Columns.Add(columnName.ToUpper(), dataColumn);
- }
- }
- }
- return Columns;
- }
- }
- public int PageCount
- {
- get
- {
- int num = RecCount / RecordsPerPage;
- if ((RecCount % RecordsPerPage) != 0)
- {
- num++;
- }
- return num;
- }
- }
- public int PageNo
- {
- get
- {
- return ((RecNo / RecordsPerPage) + 1);
- }
- set
- {
- if (value > 0)
- {
- var recCount = ((value - 1) * RecordsPerPage) + 1;
- if (recCount > RecCount)
- {
- recCount = RecCount;
- }
- int recNo = RecNo;
- Go(recCount - 1);
- if (RowIndexInPage(recNo) != -1)
- {
- Go(recNo);
- }
- }
- }
- }
- public int RecCount
- {
- get
- {
- if (MyDataTable != null)
- {
- return MyDataTable.DefaultView.Count;
- }
- return -1;
- }
- }
- public int RecNo
- {
- get
- {
- if ((CurrentIndex < 0) | (CurrentIndex >= RecCount))
- {
- return -1;
- }
- return CurrentIndex;
- }
- set => Go(value);
- }
- public int RecordId
- {
- get
- {
- int num = -1;
- DataRow currentRow = CurrentRow;
- if ((currentRow != null) && MyDataTable.Columns.Contains(IwbZeroConsts.RecordIdentityCol))
- {
- object obj2 = currentRow[IwbZeroConsts.RecordIdentityCol];
- if (!(obj2 is DBNull))
- {
- num = (int)obj2;
- }
- }
- return num;
- }
- }
- public int RecordsPerPage { get; set; }
- public virtual DataRow AddCurrentRowFrom(IwbQuery poSource)
- {
- DataRow row = MyDataTable.NewRow();
- MyDataTable.Rows.Add(row);
- GoTo(row);
- ReplaceCurrentRow(poSource.CurrentRow);
- return row;
- }
- public DataRow MoveFirst()
- {
- if ((CurrentIndex != 0))
- //if ((_CurrentIndex != 0) && FireBeforeRecordMoved())
- {
- CurrentIndex = 0;
- //FireRecordMovedEvent();
- }
- return CurrentRow;
- }
- public DataRow MoveLast()
- {
- if ((CurrentIndex != (MyDataTable.DefaultView.Count - 1)))
- //if ((_CurrentIndex != (_myDataTable.DefaultView.Count - 1)) && FireBeforeRecordMoved())
- {
- CurrentIndex = MyDataTable.DefaultView.Count - 1;
- //FireRecordMovedEvent();
- }
- return CurrentRow;
- }
- public DataRow MoveNext()
- {
- if ((CurrentIndex < (MyDataTable.DefaultView.Count - 1)))
- // if ((_CurrentIndex < (_myDataTable.DefaultView.Count - 1)) && FireBeforeRecordMoved())
- {
- CurrentIndex++;
- //FireRecordMovedEvent();
- }
- return CurrentRow;
- }
- public DataRow MovePrev()
- {
- if ((CurrentIndex > 0))
- //if ((_CurrentIndex > 0) && FireBeforeRecordMoved())
- {
- CurrentIndex--;
- //FireRecordMovedEvent();
- }
- return CurrentRow;
- }
- public virtual DataRow NewRow()
- {
- DataRow row;
- //if (FireBeforeAddNewRecord())
- {
- row = MyDataTable.NewRow();
- MyDataTable.Rows.Add(row);
- //MarkNewRowRecordId(row);
- CurrentIndex = -1;
- GoTo(row);
- //ApplyCustomDefaults();
- }
- return row;
- }
- public DataRow NewRowFrom(DataRow poDr)
- {
- DataRow row = MyDataTable.NewRow();
- row.BeginEdit();
- foreach (DataColumn column in CurColumns)
- {
- string name = column.ColumnName.Trim().ToUpper();
- if (poDr.Table.Columns.Contains(name))
- {
- row[name] = poDr[name];
- }
- }
- row.EndEdit();
- MyDataTable.Rows.Add(row);
- GoTo(row);
- return row;
- }
- public byte[] GetBytes(string pcFieldName)
- {
- byte[] buffer = null;
- if (CurColumns.Contains(pcFieldName))
- {
- object obj2 = CurrentRow[pcFieldName];
- if ((obj2 != null) && !(obj2 is DBNull))
- {
- string s = GetString(pcFieldName);
- if (s != "")
- {
- buffer = Convert.FromBase64String(s);
- }
- }
- }
- return buffer;
- }
- public DateTime GetDateTime(string pcFieldName)
- {
- return GetString(pcFieldName, CurrentRow).StrToDt();
- }
- public decimal GetDecimal(string pcFieldName)
- {
- return GetString(pcFieldName).ValD();
- }
- public string GetDisplayString(string pcFieldName)
- {
- string pcStandardValue = GetString(pcFieldName);
- string str2 = StringHelper.UAndT(pcFieldName);
- if (IwbDataColumns[str2] != null)
- {
- pcStandardValue = ((IwbDataColumn)IwbDataColumns[str2]).GetDisplayString(pcStandardValue);
- }
- return pcStandardValue;
- }
- public string GetFieldValues(string pcFieldNames)
- {
- return GetFieldValues(pcFieldNames, CurrentRow);
- }
- public string GetFieldValues(string pcFieldNames, DataRow poDataRow)
- {
- string pcSource = "";
- foreach (string str2 in StringHelper.StrToArray(pcFieldNames))
- {
- if (CurColumns.Contains(str2))
- {
- pcSource = StringHelper.AddStr(pcSource, GetString(str2, poDataRow));
- }
- else
- {
- pcSource = StringHelper.AddStr(pcSource, str2);
- }
- }
- return pcSource;
- }
- public int GetInt(string pcFieldName) => StringHelper.ValI(GetString(pcFieldName));
- public IwbDataColumn GetDataColumn(string pcColumnName)
- {
- IwbDataColumn column = null;
- pcColumnName = pcColumnName.UAndT();
- if (IwbDataColumns.ContainsKey(pcColumnName))
- {
- column = (IwbDataColumn)IwbDataColumns[pcColumnName];
- }
- return column;
- }
- public string GetString(string pcFieldName)
- {
- return GetString(pcFieldName, CurrentRow);
- }
- public string GetString(string pcFieldName, DataRow poDr)
- {
- string str = "";
- if (((MyDataTable == null) || !MyDataTable.Columns.Contains(pcFieldName)) || (poDr == null))
- {
- return str;
- }
- if (poDr[pcFieldName] is DateTime)
- {
- try
- {
- DateTime time = (DateTime)poDr[pcFieldName];
- if (((time.Year != 0x76c) || (time.Month != 1)) || (time.Day != 1))
- {
- return time.ToString("G");
- }
- return "";
- }
- catch (Exception)
- {
- return "";
- }
- }
- return poDr[pcFieldName].ToString().Trim();
- }
- public string GetString(string pcFieldName, int piDecimalDigit)
- {
- string str = GetString(pcFieldName);
- decimal poObject = str.ValD();
- if (poObject.UAndT() != str)
- {
- return str;
- }
- string format = "0.";
- for (int i = 0; i < piDecimalDigit; i++)
- {
- format = format + "0";
- }
- return poObject.ToString(format);
- }
- public Hashtable GetValuesByCols(string pcCols, DataRow poDr)
- {
- Hashtable hashtable = new Hashtable();
- Array array = pcCols.StrToArray();
- for (int i = 0; i < array.Length; i++)
- {
- string key = array.GetValue(i).ToString().ToUpper().Trim();
- hashtable.Add(key, poDr[key]);
- }
- return hashtable;
- }
- public DataRow Go(int recordNumber)
- {
- //if ((recordNumber != _CurrentIndex) && FireBeforeRecordMoved())
- if ((recordNumber != CurrentIndex))
- {
- CurrentIndex = recordNumber;
- //FireRecordMovedEvent();
- }
- return CurrentRow;
- }
- public bool GoByRecordId(int piRecordId, bool plFireRecordMoveEvent)
- {
- bool flag = false;
- if (MyDataTable.Columns.Contains(IwbZeroConsts.RecordIdentityCol))
- {
- int num = CurrentIndex;
- CurrentIndex = 0;
- while (CurrentIndex < RecCount)
- {
- if (RecordId == piRecordId)
- {
- //if (plFireRecordMoveEvent)
- //{
- // bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
- // IsToFireRecordMovedEvent = true;
- // FireRecordMovedEvent();
- // IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
- //}
- flag = true;
- break;
- }
- CurrentIndex++;
- }
- if (!flag)
- {
- CurrentIndex = num;
- }
- }
- return flag;
- }
- public void GoTo(DataRow dr)
- {
- if ((dr != null) && ((RecNo <= -1) || (MyDataTable.DefaultView[RecNo].Row != dr)))
- {
- for (int i = 0; i < RecCount; i++)
- {
- if (MyDataTable.DefaultView[i].Row == dr)
- {
- Go(i);
- return;
- }
- }
- }
- }
- //public void GoTo(DataRow podr, bool plFireEvent)
- //{
- // bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
- // IsToFireRecordMovedEvent = plFireEvent;
- // GoTo(podr);
- // IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
- //}
- public bool GoToRecordByFieldsAndValues(string pcFields, string pcValues)
- {
- int num = CurrentIndex;
- StringHelper.StrToArray(pcFields);
- StringHelper.StrToArray(pcValues);
- bool flag = false;
- CurrentIndex = 0;
- while (CurrentIndex < RecCount)
- {
- if (IsValuesMatch(CurrentRow, pcFields, pcValues))
- {
- flag = true;
- break;
- }
- CurrentIndex++;
- }
- if (flag && (CurrentIndex != num))
- {
- //FireRecordMovedEvent();
- return true;
- }
- CurrentIndex = num;
- return flag;
- }
- public DataRow GoToRecordByString(string pcStr, string pcColName)
- {
- int num = CurrentIndex;
- pcStr = StringHelper.UAndT(pcStr);
- int length = pcStr.Length;
- bool flag = false;
- if (CurColumns.Contains(pcColName))
- {
- CurrentIndex = 0;
- while (CurrentIndex < RecCount)
- {
- string str = StringHelper.UAndT(CurrentRow[pcColName]);
- if ((str.Length >= length) && (str.Substring(0, length) == pcStr))
- {
- flag = true;
- break;
- }
- CurrentIndex++;
- }
- }
- if (flag)
- {
- //FireRecordMovedEvent();
- }
- else
- {
- CurrentIndex = num;
- }
- return CurrentRow;
- }
- public void GoToRecordInPage(int piRecordIndex)
- {
- if ((piRecordIndex >= 0) && (piRecordIndex < RecordsPerPage))
- {
- int recordNumber = ((PageNo - 1) * RecordsPerPage) + piRecordIndex;
- if (recordNumber < 0)
- {
- recordNumber = 0;
- }
- if (recordNumber >= RecCount)
- {
- recordNumber = RecCount - 1;
- }
- Go(recordNumber);
- }
- }
- public bool IsDataValid(string pcColName, string pcValue)
- {
- bool flag = true;
- string key = pcColName.UAndT();
- if (IwbDataColumns.ContainsKey(key))
- {
- IwbDataColumn column = (IwbDataColumn)IwbDataColumns[key];
- if (column == null)
- {
- return true;
- }
- IwbDataType dataType = column.IwbDataType;
- try
- {
- dataType.TranslateValue(pcValue);
- }
- catch (Exception e)
- {
- flag = false;
- //string pcString = AppEnv.T(0x18733, pcValue, column.ColumnName, exception.Message);
- //AppEnv.InfoBox(pcString);
- //if (AppEnv.AppMode == npAppModes.Web)
- //{
- // throw new Exception(pcString);
- //}
- }
- }
- return flag;
- }
- public bool IsValuesMatch(DataRow poDr, Hashtable poCriteria)
- {
- if (poCriteria != null)
- {
- Array array = new String[poCriteria.Count];
- poCriteria.Keys.CopyTo(array, 0);
- for (int i = 0; i < array.Length; i++)
- {
- string str = StringHelper.UAndT(array.GetValue(i));
- IwbDataColumn column = (IwbDataColumn)IwbDataColumns[str];
- if ((column == null) || (column.IwbDataType.Compare(poDr[str], poCriteria[str]) != 0))
- {
- return false;
- }
- }
- }
- return true;
- }
- public bool IsValuesMatch(DataRow poDr, string pcFields, string pcValues)
- {
- bool flag = true;
- Array array = pcFields.StrToArray();
- Array array2 = pcValues.StrToArray();
- Hashtable columns = IwbDataColumns;
- for (int i = 0; i < array.Length; i++)
- {
- string key = StringHelper.UAndT(array.GetValue(i));
- string poDest = "";
- if (i < array2.Length)
- {
- poDest = array2.GetValue(i).ToString();
- }
- if (columns.ContainsKey(key))
- {
- IwbDataColumn column = (IwbDataColumn)columns[key];
- if (column.IwbDataType.Compare(poDr[key], poDest) != 0)
- {
- flag = false;
- }
- }
- else
- {
- flag = IwbDataType.CompareStatic(poDest, poDr[key]) == 0;
- }
- if (!flag)
- {
- return false;
- }
- }
- return true;
- }
- public void ReplaceCurrentRow(DataRow dr)
- {
- DataRow currentRow = CurrentRow;
- if (currentRow != null)
- {
- currentRow.BeginEdit();
- foreach (DataColumn column in MyDataTable.Columns)
- {
- string columnName = column.ColumnName;
- if (dr.Table.Columns.Contains(columnName))
- {
- try
- {
- currentRow[columnName] = dr[columnName];
- }
- catch (Exception)
- {
- SetField(columnName, dr[columnName].ToString());
- }
- }
- }
- currentRow.EndEdit();
- //bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
- //IsToFireRecordMovedEvent = false;
- GoTo(currentRow);
- //IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
- }
- //FireRefreshRequested();
- }
- public void ReplaceFieldValues(string pcFields, string pcValues)
- {
- Array array = pcFields.StrToArray();
- Array array2 = pcValues.StrToArray();
- DataRow currentRow = CurrentRow;
- if ((array.Length == array2.Length) && (currentRow != null))
- {
- currentRow.BeginEdit();
- for (int i = 0; i < array.Length; i++)
- {
- string pcColName = array.GetValue(i).ToString().Trim();
- SetField(pcColName, array2.GetValue(i).ToString());
- }
- currentRow.EndEdit();
- //FireRefreshRequested();
- }
- }
- public void RestoreAllRows()
- {
- MyDataTable.RejectChanges();
- MoveFirst();
- }
- public bool RestoreCurrentRow()
- {
- DataRow currentRow = CurrentRow;
- if (currentRow != null)
- {
- if (currentRow.RowState == DataRowState.Added)
- {
- DeleteCurrentRow();
- }
- else
- {
- currentRow.RejectChanges();
- }
- //FireRecordMovedEvent();
- }
- return true;
- }
- public int RowIndexInPage(int piRecNo)
- {
- int num = -1;
- int num2 = (PageNo - 1) * RecordsPerPage;
- int num3 = (PageNo * RecordsPerPage) - 1;
- if (num3 >= RecCount)
- {
- num3 = RecCount - 1;
- }
- if ((piRecNo >= num2) && (piRecNo <= num3))
- {
- num = piRecNo - num2;
- }
- return num;
- }
- public virtual void SetBytes(string pcColName, byte[] poBytes)
- {
- if (CurColumns.Contains(pcColName) && (CurrentRow != null))
- {
- CurrentRow.BeginEdit();
- if ((poBytes == null))
- {
- CurrentRow[pcColName] = DBNull.Value;
- }
- else
- {
- string str = Convert.ToBase64String(poBytes, 0, poBytes.Length);
- CurrentRow[pcColName] = str;
- }
- CurrentRow.EndEdit();
- }
- }
- public void SetCustomDefault(string pcColumns, string pcValues)
- {
- CustomDefaultColumns = pcColumns;
- CustomDefaultValues = pcValues;
- }
- public virtual void SetField(string pcColName, string pcValue)
- {
- SetField(pcColName, pcValue, true);
- }
- public virtual void SetField(string pcColName, string pcValue, bool plFireFieldChangedEvent)
- {
- pcColName = pcColName.UAndT();
- DataRow currentRow = CurrentRow;
- if (currentRow != null)
- {
- string poDest = currentRow[pcColName].ToString().Trim();
- bool flag;
- if (IwbDataColumns.ContainsKey(pcColName))
- {
- flag = ((IwbDataColumn)IwbDataColumns[pcColName]).IwbDataType.Compare(pcValue, poDest) == 0;
- }
- else
- {
- flag = pcValue.Trim() == poDest;
- }
- if ((!flag || (currentRow[pcColName] == DBNull.Value)) && IsDataValid(pcColName, pcValue))
- {
- //bool isToFireColumnChanged = IsToFireColumnChanged;
- //IsToFireColumnChanged = plFireFieldChangedEvent;
- SetField(pcColName, pcValue, currentRow);
- if (CurrentRow != currentRow)
- {
- //bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
- //IsToFireRecordMovedEvent = false;
- GoTo(currentRow);
- //IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
- }
- //FireFieldValueChanged(pcColName);
- //IsToFireColumnChanged = isToFireColumnChanged;
- }
- }
- }
- public virtual void SetField(string pcColName, string pcValue, DataRow poDataRow)
- {
- if (poDataRow.Table.Columns.Contains(pcColName))
- {
- DataColumn column = poDataRow.Table.Columns[pcColName];
- if ((column.MaxLength >= 0) && (column.MaxLength < pcValue.Length))
- {
- pcValue = pcValue.Substring(0, column.MaxLength);
- }
- IwbDataType npDataTypeByType = IwbDataType.GetIwbDataTypeByType(CurColumns[pcColName].DataType);
- string pcOldValue = GetString(pcColName, poDataRow);
- CascadeValuesToChild(poDataRow, pcColName, pcValue, pcOldValue);
- poDataRow.BeginEdit();
- poDataRow[pcColName] = npDataTypeByType.TranslateValue(pcValue);
- poDataRow.EndEdit();
- }
- }
- private void SetNextLocalId()
- {
- object obj2 = MyDataTable.Compute("Max(" + IwbZeroConsts.IdenField + ")", "");
- if (obj2 is DBNull)
- {
- obj2 = 0;
- }
- SetField(IwbZeroConsts.IdenField, (((int)obj2) + 1).ToString(), false);
- }
- public void SortBy(string pcSort)
- {
- try
- {
- if (MyDataTable != null)
- {
- int recNo = -1;
- SortString = pcSort;
- DataRow currentRow = CurrentRow;
- MyDataTable.DefaultView.Sort = pcSort;
- if (currentRow != null)
- {
- GoTo(currentRow);
- recNo = RecNo;
- }
- DataTable table = MyDataTable.Copy();
- MyDataTable.Rows.Clear();
- MyDataTable.DefaultView.Sort = "";
- DataView view = new DataView(table, "", pcSort, DataViewRowState.CurrentRows);
- foreach (DataRowView view2 in view)
- {
- MyDataTable.ImportRow(view2.Row);
- }
- view = new DataView(table, "", pcSort, DataViewRowState.Deleted);
- foreach (DataRowView view3 in view)
- {
- MyDataTable.ImportRow(view3.Row);
- }
- CurrentIndex = recNo;
- //FireRefreshRequested();
- }
- }
- catch (Exception e)
- {
- //
- }
- }
- public void SortBy(string pcColumnId, bool plAscending)
- {
- string pcSort = pcColumnId + (plAscending ? " asc" : " desc");
- SortBy(pcSort);
- }
- public void PageFirst()
- {
- PageNo = 1;
- }
- public void PageLast()
- {
- PageNo = PageCount;
- }
- public void PageNext()
- {
- PageNo++;
- }
- public void PagePrev()
- {
- PageNo--;
- }
- public void AddLocalBal(string pcSourceQtyField)
- {
- AddLocalBal(pcSourceQtyField, IwbZeroConsts.IdenField);
- }
- public void AddLocalBal(string pcSourceQtyField, string pcTargetField)
- {
- if (IsOpened)
- {
- MyDataTable.Columns.Add(pcTargetField, typeof(decimal)).Caption = "Local Bal";
- Columns = null;
- int num = CurrentIndex;
- CurrentIndex = 0;
- while (CurrentIndex < RecCount)
- {
- SetField(pcTargetField, GetString(pcSourceQtyField), false);
- CurrentIndex++;
- }
- CurrentIndex = num;
- }
- }
- public void AddLocalIdentity()
- {
- if (IsOpened)
- {
- MyDataTable.Columns.Add(IwbZeroConsts.IdenField, typeof(int));
- Columns = null;
- int num = CurrentIndex;
- CurrentIndex = 0;
- while (CurrentIndex < RecCount)
- {
- SetNextLocalId();
- CurrentIndex++;
- }
- CurrentIndex = num;
- }
- }
- public void AddPk(string pcFields)
- {
- if ((MyDataTable != null) && !MyDataTable.Constraints.Contains("PrimaryKey"))
- {
- Array array = pcFields.StrToArray();
- Array array2 = Array.CreateInstance(typeof(DataColumn), array.Length);
- int num = 0;
- foreach (string str in array)
- {
- array2.SetValue(MyDataTable.Columns[str], num++);
- }
- MyDataTable.Constraints.Add("PrimaryKey", (DataColumn[])array2, true);
- }
- }
- protected virtual void ApplyCustomDefaults()
- {
- //FireAfterAddNewRecord();
- ReplaceFieldValues(CustomDefaultColumns, CustomDefaultValues);
- }
- private void CascadeValuesToChild(DataRow poParentRow, string pcColumn, string pcNewValue, string pcOldValue)
- {
- foreach (IwbLinkQuery query in LinkQueries)
- {
- query.CascadeFieldValues(this, poParentRow, pcColumn, pcNewValue, pcOldValue);
- }
- }
- protected virtual void ClearEnvironment()
- {
- CurrentIndex = -1;
- CustomDefaultColumns = "";
- CustomDefaultValues = "";
- }
- public virtual void CloneFrom(IwbQuery poSource)
- {
- //ConnectionId = poSource.ConnectionId;
- MyDataTable = poSource.CurrentTable.Clone();
- poSource.CurrentTable.Constraints.Clear();
- MyDataTable.TableName = "T" + StringHelper.GetRandom();
- //QueryCriteria = poSource.QueryCriteria;
- }
- public void Close()
- {
- ClearEnvironment();
- if (CurrentTable != null)
- {
- MyDataTable = null;
- }
- }
- protected void ColumnChanged(object sender, DataColumnChangeEventArgs e)
- {
- //FireFieldValueChanged(e.Column.ColumnName);
- }
- public object Compute(string pcAggrFunction, string pcExpr, string pcCriteria)
- {
- object obj2 = null;
- try
- {
- CurColumns.Add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", typeof(decimal), pcExpr);
- obj2 = CurrentTable.Compute(pcAggrFunction + "(ABCDEFGHIJKLMNOPQRSTUVWXYZ)", pcCriteria);
- CurColumns.Remove("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
- }
- catch (Exception exception)
- {
- obj2 = exception;
- }
- return obj2;
- }
- public virtual DataRow CopyRow()
- {
- DataRow currentRow = CurrentRow;
- DataRow row = null;
- if (currentRow != null)
- {
- row = MyDataTable.NewRow();
- row.BeginEdit();
- foreach (DataColumn column in MyDataTable.Columns)
- {
- string columnName = column.ColumnName;
- row[columnName] = currentRow[columnName];
- }
- row.EndEdit();
- MyDataTable.Rows.Add(row);
- MarkNewRowRecordId(row);
- }
- return row;
- }
- protected void MarkNewRowRecordId(DataRow poRow)
- {
- if ((poRow != null) && MyDataTable.Columns.Contains(IwbZeroConsts.RecordIdentityCol))
- {
- poRow[IwbZeroConsts.RecordIdentityCol] = ++LastRecordId;
- }
- }
- public void CreateFrom(ArrayList poDataColumns)
- {
- MyDataTable = new DataTable(TableName);
- foreach (DataColumn column in poDataColumns)
- {
- MyDataTable.Columns.Add(column);
- }
- }
- public void DeleteAllRecord()
- {
- //bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
- //IsToFireRecordMovedEvent = false;
- while (RecCount > 0)
- {
- MoveFirst();
- DeleteCurrentRow();
- }
- //IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
- //FireRecordMovedEvent();
- }
- public virtual DataRow DeleteCurrentRow()
- {
- DataRow currentRow = CurrentRow;
- if (currentRow.RowState == DataRowState.Added)
- {
- MyDataTable.Rows.Remove(currentRow);
- }
- else
- {
- currentRow.Delete();
- }
- currentRow = CurrentRow;
- //bool isToFireRecordMovedEvent = IsToFireRecordMovedEvent;
- //IsToFireRecordMovedEvent = false;
- if (currentRow != null)
- {
- if (currentRow.RowState == DataRowState.Deleted)
- {
- currentRow = MoveNext();
- }
- }
- else
- {
- currentRow = MoveLast();
- }
- if (CurrentIndex >= RecCount)
- {
- CurrentIndex = -1;
- }
- //IsToFireRecordMovedEvent = isToFireRecordMovedEvent;
- //FireRecordMovedEvent();
- return currentRow;
- }
- public void FilterBy(string pcExpression)
- {
- MyDataTable.DefaultView.RowFilter = pcExpression;
- }
- public virtual void SaveToXml(DataTable dt)
- {
- dt.CDataToXmlFile(FilePath);
- }
- public virtual void SaveToXml()
- {
- MyDataTable.CDataToXmlFile(FilePath);
- }
- ~IwbQuery()
- {
- }
- //public static Dictionary<string,object> Dictionary { get; set; }
- //protected event EvDictionaryChanged DictionaryChanged;
- //public void SetField(string name, object value)
- //{
- // Dictionary = Dictionary ?? GetDictionary();
- // if (Dictionary.ContainsKey(name))
- // {
- // var oldValue = Dictionary[name];
- // if (oldValue != value)
- // {
- // Dictionary[name] = value;
- // DictionaryChanged?.Invoke(Dictionary);
- // }
- // }
- // else
- // {
- // Dictionary.Add(name,value);
- // DictionaryChanged?.Invoke(Dictionary);
- // }
- //}
- //public string GetString(string name)
- //{
- // //Dictionary = Dictionary ?? GetDictionary();
- // //if (Dictionary.ContainsKey(name))
- // //{
- // // return Dictionary[name].ToString();
- // //}
- // return "";
- //}
- //public int GetInt(string name)
- //{
- // return GetString(name).ValI();
- //}
- //public decimal GetDecimal(string name)
- //{
- // return GetString(name).ValD();
- //}
- //public bool GetBool(string name)
- //{
- // return GetString(name).ValB();
- //}
- //public DateTime GetDate(string name)
- //{
- // return GetString(name).StrToDt();
- //}
- //public virtual void SaveToXml(Dictionary<string, object> dictionary)
- //{
- // if (dictionary.Count > 0)
- // {
- // var str = "";
- // foreach (var o in dictionary)
- // {
- // str += $"<{o.Key}>{o.Value.ToString().FormatCode2Xml()}</{o.Key}>\r\n";
- // }
- // XmlDocument xDoc = new XmlDocument {InnerText = str};
- // xDoc.Save(FilePath);
- // }
- //}
- //public Dictionary<string, object> GetDictionary()
- //{
- // if (Dictionary != null)
- // {
- // return Dictionary;
- // }
- // var dic= new Dictionary<string,object>();
- // if (FilePath.IsNotEmpty())
- // {
- // XmlDocument xDoc= new XmlDocument();
- // try
- // {
- // xDoc.Load(FilePath);
- // var xmlNode = xDoc.LastChild;
- // if (xmlNode != null && xmlNode.HasChildNodes)
- // {
- // foreach (XmlNode node in xmlNode.ChildNodes)
- // {
- // dic.Add(node.Name, (node.FirstChild?.Value).FormatXml2Code() ?? "");
- // }
- // }
- // }
- // catch(Exception )
- // {
- // //
- // }
- // }
- // return dic;
- //}
- }
- }
|