using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Drawing; using System.Xml; using System.IO; namespace SysBaseLibs { public class rsQuery { public rsQuery() { this._backendTableName = ""; this._QueryCriteria = ""; //this._LastRecordId = 0; this._RecordsPerPage = 1; this._SortString = ""; this._lFireRecordMoved = true; this._lColumnChanged = true; this._tableName = "TAble" + AppEnv.GetRandom(); this.ClearEnvironment(); } #region Events public event evDataOperationRequested AfterAddingNewRecord; public event evDataOperationRequested AfterSavingOfData; public event evDataOperationRequested BeforeAddingNewRecord; public event evDataOperationRequested BeforeRecordMoved; public event evDataOperationRequested BeforeSavingOfData; public event evFieldValueChanged FieldValueChanged; public event evRefreshRequested QueryOpened; public event evRefreshRequested RefreshRequested; #endregion #region Fields protected int _CurrentIndex; private string _CustomDefaultColumns; private string _CustomDefaultValues; //protected int _LastRecordId; private bool _lColumnChanged; private bool _lFireRecordMoved; protected DataTable _myDataTable; protected string _QueryCriteria; private int _RecordsPerPage; private string _SortString; protected string _sql; protected string _tableName; public evRecordMoved RecordMoved; protected string _backendTableName; protected DBConnSql _DBConn = null; private DataSet _DataSet = null; #endregion #region Properties public string AllFields { get { string text1 = ""; foreach (DataColumn column1 in this.Columns) { text1 = UtilStr.AppendToStr(text1, column1.ColumnName); } return text1; } } public string DisplayColumns { get { string text1 = ""; foreach (DataColumn column1 in this.Columns) { string lcStr = column1.ColumnName + ":" + column1.MaxLength; text1 = UtilStr.AppendToStr(text1, lcStr); } return text1; } } public string BackendTableName { get { return _backendTableName; } } public bool Changed { get { bool lbRetVal = false; if (this._myDataTable != null) { DataTable loTable = this._myDataTable.GetChanges(); lbRetVal = loTable != null; } return lbRetVal; } } public DataColumnCollection Columns { get { return this._myDataTable.Columns; } } public DataRow CurrentRow { get { DataRow loRetVal = null; if (((this._CurrentIndex != -1) && (this._myDataTable != null)) && (this._CurrentIndex < this._myDataTable.DefaultView.Count)) { loRetVal = this._myDataTable.DefaultView[this._CurrentIndex].Row; } return loRetVal; } } public DataTable CurrentTable { get { return this._myDataTable; } } public bool IsNewRow { get { bool lbRetVal = false; if (this.CurrentRow != null) { lbRetVal = this.CurrentRow.RowState == DataRowState.Added; } return lbRetVal; } } public bool IsOpened { get { return (this._myDataTable != null); } } public bool IsToFireColumnChanged { get { return this._lColumnChanged; } set { this._lColumnChanged = value; } } public bool IsToFireRecordMovedEvent { get { return this._lFireRecordMoved; } set { this._lFireRecordMoved = value; } } public object this[string pcColumnName] { get { object loRetVal = ""; if (this._myDataTable.Columns.Contains(pcColumnName) && (this.CurrentRow != null)) { loRetVal = this.CurrentRow[pcColumnName]; } return loRetVal; } } public int PageCount { get { int liRetVal = this.RecCount / this.RecordsPerPage; if ((this.RecCount % this.RecordsPerPage) != 0) { liRetVal++; } return liRetVal; } } public int PageNo { get { int liRetVal = 1; if (this._RecordsPerPage > 0) liRetVal = (this.RecNo / this.RecordsPerPage) + 1; return liRetVal; } set { int num1 = 1; if (value > 0) { num1 = ((value - 1) * this.RecordsPerPage) + 1; if (num1 > this.RecCount) { num1 = this.RecCount; } int num2 = this.RecNo; this.Go(num1 - 1); if (this.RowIndexInPage(num2) != -1) { this.Go(num2); } } } } public string QueryCriteria { get { return this._QueryCriteria; } set { this._QueryCriteria = value; } } public int RecCount { get { if (this._myDataTable != null) { return this._myDataTable.DefaultView.Count; } return -1; } } public int RecNo { get { if ((this._CurrentIndex < 0) | (this._CurrentIndex >= this.RecCount)) { return -1; } return this._CurrentIndex; } set { this.Go(value); } } public int RecordsPerPage { get { return this._RecordsPerPage; } set { this._RecordsPerPage = value; } } public DataRowCollection Rows { get { return this._myDataTable.Rows; } } public string SortString { get { return this._SortString; } } public DataSet MyDataSet { get { if (this._myDataTable != null) this._DataSet = this._myDataTable.DataSet; return this._DataSet; } set { this._DataSet = value; } } public DBConnSql DBConn { get { return _DBConn; } } #endregion #region methods // Methods public void AddPk(string pcFields) { if ((this._myDataTable != null) && !this._myDataTable.Constraints.Contains("PrimaryKey")) { Array loArr1 = UtilStr.StrToArray(pcFields); Array loArr2 = Array.CreateInstance(typeof(DataColumn), loArr1.Length); int num1 = 0; foreach (string text1 in loArr1) { loArr2.SetValue(this._myDataTable.Columns[text1], num1++); } this._myDataTable.Constraints.Add("PrimaryKey", (DataColumn[])loArr2, true); } } protected virtual void ClearEnvironment() { this._sql = ""; this._CurrentIndex = -1; this._CustomDefaultColumns = ""; this._CustomDefaultValues = ""; } public virtual void CloneFrom(rsQuery poSource) { this._myDataTable = poSource.CurrentTable.Clone(); poSource.CurrentTable.Constraints.Clear(); this._myDataTable.TableName = AppEnv.GetNextDataTableName();//DBConn.GetNextDataTableName(); this.QueryCriteria = poSource.QueryCriteria; } public void Close() { this.ClearEnvironment(); if (this.CurrentTable != null) { //DBConn.CloseDataTable(this.CurrentTable.TableName);//songyf if (this.CurrentTable.DataSet != null) { _myDataTable.DataSet.Tables.Remove(_myDataTable); _myDataTable.Clear(); _myDataTable.Constraints.Clear(); _myDataTable.Columns.Clear(); _myDataTable.Dispose(); this._myDataTable = null; } } } protected void ColumnChanged(object sender, DataColumnChangeEventArgs e) { this.FireFieldValueChanged(e.Column.ColumnName); } public object Compute(string pcAggrFunction, string pcExpr, string pcCriteria) { object obj1 = null; try { this.Columns.Add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", typeof(decimal), pcExpr); obj1 = this.CurrentTable.Compute(pcAggrFunction + "(ABCDEFGHIJKLMNOPQRSTUVWXYZ)", pcCriteria); this.Columns.Remove("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); } catch (Exception exception1) { obj1 = exception1; } return obj1; } public virtual DataRow CopyRow() { DataRow row1 = this.CurrentRow; DataRow row2 = null; if (row1 != null) { row2 = this._myDataTable.NewRow(); if (row2 == null) { return row2; } row2.BeginEdit(); foreach (DataColumn column1 in this._myDataTable.Columns) { string text1 = column1.ColumnName; row2[text1] = row1[text1]; } row2.EndEdit(); this._myDataTable.Rows.Add(row2); //this.MarkNewRowRecordId(row2); } return row2; } public void CreateFrom(List poDataColumns) { this._myDataTable = new DataTable(this._tableName); foreach (DataColumn column1 in poDataColumns) { this._myDataTable.Columns.Add(column1); } } public void DeleteAllRecord() { bool flag1 = this.IsToFireRecordMovedEvent; this.IsToFireRecordMovedEvent = false; while (this.RecCount > 0) { this.MoveFirst(); this.DeleteCurrentRow(); } this.IsToFireRecordMovedEvent = flag1; this.FireRecordMovedEvent(); } public virtual DataRow DeleteCurrentRow() { DataRow row1 = this.CurrentRow; if (row1.RowState == DataRowState.Added) { this._myDataTable.Rows.Remove(row1); } else { row1.Delete(); } row1 = this.CurrentRow; bool flag1 = this.IsToFireRecordMovedEvent; this.IsToFireRecordMovedEvent = false; if (row1 != null) { if (row1.RowState == DataRowState.Deleted) { row1 = this.MoveNext(); } } else { row1 = this.MoveLast(); } if (this._CurrentIndex >= this.RecCount) { this._CurrentIndex = -1; } this.IsToFireRecordMovedEvent = flag1; this.FireRecordMovedEvent(); return row1; } public void FilterBy(string pcExpression) { this._myDataTable.DefaultView.RowFilter = pcExpression; } public bool FireAfterAddNewRecord() { bool flag1 = true; if (this.AfterAddingNewRecord != null) { flag1 = this.AfterAddingNewRecord(this); } return flag1; } public bool FireAfterSavingOfData() { bool flag1 = true; if (this.AfterSavingOfData != null) { flag1 = this.AfterSavingOfData(this); } return flag1; } public bool FireBeforeAddNewRecord() { bool flag1 = true; if (this.BeforeAddingNewRecord != null) { flag1 = this.BeforeAddingNewRecord(this); } return flag1; } public bool FireBeforeRecordMoved() { bool flag1 = true; if ((this.BeforeRecordMoved != null) && this._lFireRecordMoved) { flag1 = this.BeforeRecordMoved(this); } return flag1; } public bool FireBeforeSavingOfData() { bool flag1 = true; if (this.BeforeSavingOfData != null) { flag1 = this.BeforeSavingOfData(this); } return flag1; } public void FireFieldValueChanged(string pcColumnName) { if ((this.FieldValueChanged != null) && this.IsToFireColumnChanged) { this.FieldValueChanged(this, pcColumnName); } } protected void FireFieldValueChangedEx(rsQuery poQuery, string pcColumnName) { if ((this.FieldValueChanged != null) && this.IsToFireColumnChanged) { this.FieldValueChanged(poQuery, pcColumnName); } } public void FireQueryOpened() { if (this.QueryOpened != null) { this.QueryOpened(this); } } public void FireRecordMovedEvent() { if (this._lFireRecordMoved) { this.FireRefreshRequested(); if (this.RecordMoved != null) { this.RecordMoved(); } } } public void FireRefreshRequested() { if (this.RefreshRequested != null) { this.RefreshRequested(this); } } public byte[] GetBytes(string pcFieldName) { byte[] buffer1 = null; if (this.Columns.Contains(pcFieldName)) { object obj1 = this.CurrentRow[pcFieldName]; if ((obj1 != null) && !(obj1 is DBNull)) { string text1 = this.GetString(pcFieldName); if (text1 != "") { buffer1 = Convert.FromBase64String(text1); } } } return buffer1; } public DateTime GetDateTime(string pcFieldName) { return UtilStr.StrToDt(this.GetString(pcFieldName, this.CurrentRow)); } public decimal GetDecimal(string pcFieldName) { return Utils .ValD(this.GetString(pcFieldName)); } public string GetFieldValues(string pcFieldNames) { return this.GetFieldValues(pcFieldNames, this.CurrentRow); } public string GetFieldValues(string pcFieldNames, DataRow poDataRow) { string text1 = ""; Array array1 = UtilStr.StrToArray(pcFieldNames); foreach (string text2 in array1) { if (this.Columns.Contains(text2)) { text1 = UtilStr.AddStr(text1, this.GetString(text2, poDataRow)); } else { text1 = UtilStr.AddStr(text1, text2); } } return text1; } public object GetImage(string pcFieldName) { object obj1 = null; if (this.CurrentRow != null) { obj1 = Utils.BytesToImage(this.CurrentRow[pcFieldName]); } return obj1; } //public byte[] GetBytes(string pcFieldName) //{ // byte[] obj1 = null; // if (this.CurrentRow != null) // { // obj1 = this.CurrentRow[pcFieldName]; // } // return obj1; //} public int GetInt(string pcFieldName) { return Utils.ValI(this.GetString(pcFieldName)); } public string GetString(string pcFieldName) { return this.GetString(pcFieldName, this.CurrentRow); } public bool GetBool(string pcFieldName) { return UtilStr.StrToBool(GetString(pcFieldName)); } public string GetString(string pcFieldName, DataRow poDr) { string text1 = ""; if (((this._myDataTable == null) || !this._myDataTable.Columns.Contains(pcFieldName)) || (poDr == null)) { return text1; } if (poDr[pcFieldName] is DateTime) { try { DateTime time1 = (DateTime)poDr[pcFieldName]; if (((time1.Year != 0x76c) || (time1.Month != 1)) || (time1.Day != 1)) { return time1.ToString("G"); } return ""; } catch (Exception) { return ""; } } return poDr[pcFieldName].ToString().Trim(); } public string GetString(string pcFieldName, int piDecimalDigit) { string text1 = this.GetString(pcFieldName); decimal num1 = Utils.ValD(text1); if (UtilStr.UAndT(num1) != text1) { return text1; } string text2 = "0."; for (int num2 = 0; num2 < piDecimalDigit; num2++) { text2 = text2 + "0"; } return num1.ToString(text2); } public DataRow Go(int recordNumber) { if ((recordNumber != this._CurrentIndex) && this.FireBeforeRecordMoved()) { this._CurrentIndex = recordNumber; this.FireRecordMovedEvent(); } return this.CurrentRow; } public void GoTo(DataRow dr) { if ((dr != null) && ((this.RecNo <= -1) || (this._myDataTable.DefaultView[this.RecNo].Row != dr))) { for (int num1 = 0; num1 < this.RecCount; num1++) { if (this._myDataTable.DefaultView[num1].Row == dr) { this.Go(num1); return; } } } } public void GoTo(DataRow podr, bool plFireEvent) { bool flag1 = this.IsToFireRecordMovedEvent; this.IsToFireRecordMovedEvent = plFireEvent; this.GoTo(podr); this.IsToFireRecordMovedEvent = flag1; } public bool GoToRecordByFieldsAndValues(string pcFields, string pcValues) { int num1 = this._CurrentIndex; //UtilStr.StrToArray(pcFields); //UtilStr.StrToArray(pcValues); bool flag1 = false; this._CurrentIndex = 0; while (this._CurrentIndex < this.RecCount) { if (DALibs.IsValuesMatch(this.CurrentRow, pcFields, pcValues)) { flag1 = true; break; } this._CurrentIndex++; } if (flag1 && (this._CurrentIndex != num1)) { this.FireRecordMovedEvent(); return flag1; } this._CurrentIndex = num1; return flag1; } public DataRow GoToRecordByString(string pcStr, string pcColName) { int num1 = this._CurrentIndex; pcStr = UtilStr.UAndT(pcStr); int num2 = pcStr.Length; bool flag1 = false; if (this.Columns.Contains(pcColName)) { this._CurrentIndex = 0; while (this._CurrentIndex < this.RecCount) { string text1 = UtilStr.UAndT(this.CurrentRow[pcColName]); if (text1.Length >= num2) { text1 = text1.Substring(0, num2); if (text1 == pcStr) { flag1 = true; break; } } this._CurrentIndex++; } } if (flag1) { this.FireRecordMovedEvent(); } else { this._CurrentIndex = num1; } return this.CurrentRow; } public void GoToRecordInPage(int piRecordIndex) { if ((piRecordIndex >= 0) && (piRecordIndex < this.RecordsPerPage)) { int num1 = ((this.PageNo - 1) * this.RecordsPerPage) + piRecordIndex; if (num1 < 0) { num1 = 0; } if (num1 >= this.RecCount) { num1 = this.RecCount - 1; } this.Go(num1); } } public DataRow Locate(object[] paValues) { DataRow row1 = this._myDataTable.Rows.Find(paValues); if (row1 != null) { this.GoTo(row1); } return row1; } public DataRow MoveFirst() { if ((this._CurrentIndex != 0) && this.FireBeforeRecordMoved()) { this._CurrentIndex = 0; this.FireRecordMovedEvent(); } return this.CurrentRow; } public DataRow MoveLast() { if ((this._CurrentIndex != (this._myDataTable.DefaultView.Count - 1)) && this.FireBeforeRecordMoved()) { this._CurrentIndex = this._myDataTable.DefaultView.Count - 1; this.FireRecordMovedEvent(); } return this.CurrentRow; } public DataRow MoveNext() { if ((this._CurrentIndex < (this._myDataTable.DefaultView.Count - 1)) && this.FireBeforeRecordMoved()) { this._CurrentIndex++; this.FireRecordMovedEvent(); } return this.CurrentRow; } public DataRow MovePrev() { if ((this._CurrentIndex > 0) && this.FireBeforeRecordMoved()) { this._CurrentIndex--; this.FireRecordMovedEvent(); } return this.CurrentRow; } /// /// /// /// public bool Next() { if (this._CurrentIndex < this.RecCount - 1 && this._CurrentIndex > -1) { this._CurrentIndex++; this.FireRecordMovedEvent(); return true; } return false; } public DataRow NewRowFrom(DataRow poDr) { DataRow row1 = this._myDataTable.NewRow(); if (row1 != null) { row1.BeginEdit(); foreach (DataColumn column1 in this.Columns) { string text1 = column1.ColumnName.Trim().ToUpper(); if (poDr.Table.Columns.Contains(text1)) { row1[text1] = poDr[text1]; } } row1.EndEdit(); } this._myDataTable.Rows.Add(row1); this.GoTo(row1); return row1; } public virtual bool Open(string pcSQLCommand, DBConnSql poDBConn) { this._backendTableName = AppEnv.GetRandom(); ; this._CurrentIndex = -1; this._sql = pcSQLCommand; this._DBConn = poDBConn; this._myDataTable = poDBConn.OpenDataTable(pcSQLCommand); if (this._myDataTable != null) { if (this._myDataTable.Rows.Count > 0) { this._CurrentIndex = 0; } this.FireQueryOpened(); this.FireRecordMovedEvent(); } return (this._myDataTable != null); } public virtual bool Open(DataSet poDataSet, string pcTableName, string pcSQLCommand, DBConnSql poDBConn) { this._backendTableName = pcTableName; this._CurrentIndex = -1; this._sql = pcSQLCommand; this._DBConn = poDBConn; this._myDataTable = poDBConn.OpenDataTable(poDataSet, pcTableName, pcSQLCommand); if (this._myDataTable != null) { if (this._myDataTable.Rows.Count > 0) { this._CurrentIndex = 0; } this.FireQueryOpened(); this.FireRecordMovedEvent(); } return (this._myDataTable != null); } public void OpenFromDataTable(DataTable poDataTable) { if (this._myDataTable != null) { this.Close(); } this._myDataTable = poDataTable; if (this._myDataTable != null) { if (this._myDataTable.Rows.Count > 0) { this._CurrentIndex = 0; } this.FireQueryOpened(); } } public void PageFirst() { this.PageNo = 1; } public void PageLast() { this.PageNo = this.PageCount; } public void PageNext() { this.PageNo++; } public void PagePrev() { this.PageNo--; } public bool ReQuery() { bool lbRetVal = false; if (this._sql != "") { lbRetVal = this.Open(this.MyDataSet, this.BackendTableName, this._sql, this._DBConn); } return lbRetVal; } public void RestoreAllRows() { this._myDataTable.RejectChanges(); this.MoveFirst(); } public bool RestoreCurrentRow() { DataRow row1 = this.CurrentRow; if (row1 != null) { if (row1.RowState == DataRowState.Added) { this.DeleteCurrentRow(); } else { row1.RejectChanges(); } this.FireRecordMovedEvent(); } return true; } public int RowIndexInPage() { return RowIndexInPage(this.RecNo); } public int RowIndexInPage(int piRecNo) { int num1 = -1; int num2 = (this.PageNo - 1) * this.RecordsPerPage; int num3 = (this.PageNo * this.RecordsPerPage) - 1; if (num3 >= this.RecCount) { num3 = this.RecCount - 1; } if ((piRecNo >= num2) && (piRecNo <= num3)) { num1 = piRecNo - num2; } return num1; } public virtual void SetBytes(string pcColName, byte[] poBytes) { if (this.Columns.Contains(pcColName) && (this.CurrentRow != null)) { this.CurrentRow.BeginEdit(); if ((poBytes == null) || (0 != 0)) { this.CurrentRow[pcColName] = DBNull.Value; } else { string text1 = Convert.ToBase64String(poBytes, 0, poBytes.Length); this.CurrentRow[pcColName] = text1; } this.CurrentRow.EndEdit(); } } public virtual void SetField(string pcColName, string pcValue) { this.SetField(pcColName, pcValue, true); } public virtual void SetField(string pcColName, string pcValue, bool plFireFieldChangedEvent) { pcColName = UtilStr.UAndT(pcColName); DataRow row1 = this.CurrentRow; if (row1 != null) { string text1 = row1[pcColName].ToString().Trim(); bool flag1 = false; if (!flag1 || (row1[pcColName] == DBNull.Value)) { bool flag2 = this.IsToFireColumnChanged; this.IsToFireColumnChanged = plFireFieldChangedEvent; this.SetField(pcColName, pcValue, row1); if (this.CurrentRow != row1) { bool flag3 = this.IsToFireRecordMovedEvent; this.IsToFireRecordMovedEvent = false; this.GoTo(row1); this.IsToFireRecordMovedEvent = flag3; } this.FireFieldValueChanged(pcColName); this.IsToFireColumnChanged = flag2; } } } public virtual void SetField(string pcColName, string pcValue, DataRow poDataRow) { if (poDataRow.Table.Columns.Contains(pcColName)) { DataColumn column1 = poDataRow.Table.Columns[pcColName]; if ((column1.MaxLength >= 0) && (column1.MaxLength < pcValue.Length)) { pcValue = pcValue.Substring(0, column1.MaxLength); } string text1 = this.GetString(pcColName, poDataRow); poDataRow.BeginEdit(); poDataRow[pcColName] = pcValue; poDataRow.EndEdit(); } } public void SetCustomDefault(string pcColumns, string pcValues) { this._CustomDefaultColumns = pcColumns; this._CustomDefaultValues = pcValues; } public void SortBy(string pcSort) { try { if (this._myDataTable != null) { int num1 = -1; this._SortString = pcSort; DataRow row1 = this.CurrentRow; this._myDataTable.DefaultView.Sort = pcSort; if (row1 != null) { this.GoTo(row1, false); num1 = this.RecNo; } DataTable table1 = this._myDataTable.Copy(); this._myDataTable.Rows.Clear(); this._myDataTable.DefaultView.Sort = ""; DataView view1 = new DataView(table1, "", pcSort, DataViewRowState.CurrentRows); foreach (DataRowView view2 in view1) { this._myDataTable.ImportRow(view2.Row); } view1 = new DataView(table1, "", pcSort, DataViewRowState.Deleted); foreach (DataRowView view3 in view1) { this._myDataTable.ImportRow(view3.Row); } this._CurrentIndex = num1; this.FireRefreshRequested(); } } catch (Exception) { } } public void SortBy(string pcColumnId, bool plAscending) { string text1 = pcColumnId + (plAscending ? " asc" : " desc"); this.SortBy(text1); } public void ToXML() { ToXML(this.AllFields); } public void ToXML(string pcCols) { if (this.IsOpened) { Array array1 = UtilStr.StrToArray(pcCols); XmlTextWriter writer1 = new XmlTextWriter(@"c:\result.xml", Encoding.UTF8); writer1.WriteStartDocument(true); writer1.WriteStartElement("Data"); this._CurrentIndex = 0; while (this._CurrentIndex < this.RecCount) { string text1 = "Record"; writer1.WriteStartElement(text1); for (int num1 = 0; num1 < array1.Length; num1++) { string text2 = array1.GetValue(num1).ToString(); writer1.WriteElementString(text2, this.GetString(text2)); } writer1.WriteEndElement(); this._CurrentIndex++; } writer1.WriteEndElement(); writer1.WriteEndDocument(); writer1.Flush(); writer1.Close(); } } #endregion } }