CEL_Payroll/Payroll.BO/Global/SearchObject.cs

351 lines
12 KiB
C#
Raw Normal View History

2024-09-17 14:30:13 +06:00
using System;
using System.Collections;
using Ease.CoreV35.Model;
using Ease.CoreV35;
using System.Collections.Generic;
using System.IO;
namespace Payroll.BO
{
#region SearchObject
[Serializable]
#region SearchObject Data Type
public enum EnumSearchObjDataType
{
String = 1,
Number = 2,
Date = 3,
DateWithTime = 4,
Object = 5,
boolean=6
}
#endregion
public class SearchObject
{
#region Constructor
public SearchObject()
{
_secondValue = "none";
_collection = new SearchCollection();
_fieldName = "";
_operator = EnumSQLOperator.None;
_firstValue = "";
_secondValue = "";
//_sqlOperator = EnumSQLOperator.None;
}
#endregion
#region Properties
#region Field
private string _fieldName;
protected string FieldName
{
get { return _fieldName; }
set { _fieldName = value; }
}
#endregion
#region Field
private string _tableName;
protected string TableName
{
get { return _tableName; }
set { _tableName = value; }
}
#endregion
//#region SQL operator
//private EnumSQLOperator _sqlOperator;
//protected EnumSQLOperator SQLOperator
//{
// get { return _sqlOperator; }
// set { _sqlOperator = value; }
//}
//#endregion
#region Operator
private EnumSQLOperator _operator;
protected EnumSQLOperator Operator
{
get { return _operator; }
set { _operator = value; }
}
#endregion
#region FirstValue
private string _firstValue;
protected string FirstValue
{
get { return _firstValue; }
set { _firstValue = value; }
}
#endregion
#region SecondValue
private string _secondValue;
protected string SecondValue
{
get { return _secondValue; }
set { _secondValue = value; }
}
#endregion
#region Data Type
private EnumSearchObjDataType _dataType = EnumSearchObjDataType.String;
protected EnumSearchObjDataType DataType
{
get { return _dataType; }
set { _dataType = value; }
}
#endregion
#region Search Objects
private SearchCollection _collection;
#endregion
#endregion
private class SearchCollection : List<SearchObject>
{
private List<SearchObject> _SearchCollection;
public List<SearchObject> Collection
{
get
{
return _SearchCollection;
}
set
{
_SearchCollection = value;
}
}
}
//public void AddSingleOperatorValue(string fieldName, string Operator,
// string firstValue, string secondValue, EnumSearchObjDataType nDataType)
//{
// SearchObject oSearchObject = new SearchObject();
// oSearchObject.FieldName = fieldName;
// oSearchObject.Operator = Operator;
// oSearchObject.FirstValue = firstValue;
// oSearchObject.SecondValue = secondValue;
// oSearchObject.DataType = nDataType;
// _collection.Add(oSearchObject);
//}
//public void AddSingleOperatorValue(string fieldName, string Operator, string value, EnumSearchObjDataType nDataType)
//{
// SearchObject oSearchObject = new SearchObject();
// oSearchObject.FieldName = fieldName;
// oSearchObject.Operator = Operator;
// oSearchObject.FirstValue = value;
// oSearchObject.DataType = nDataType;
// _collection.Add(oSearchObject);
//}
//public void AddINOperatorValue(string fieldName, EnumSearchObjDataType nDataType,string value)
//{
// SearchObject oSearchObject = new SearchObject();
// oSearchObject.FieldName = fieldName;
// oSearchObject.Operator = "IN";
// oSearchObject.DataType = nDataType;
// oSearchObject.FirstValue = value;
// _collection.Add(oSearchObject);
//}
//public void AddLIKEOperatorValue(string fieldName, EnumSearchObjDataType nDataType, string value)
//{
// SearchObject oSearchObject = new SearchObject();
// oSearchObject.FieldName = fieldName;
// oSearchObject.Operator = "LIKE";
// oSearchObject.DataType = nDataType;
// oSearchObject.FirstValue = value;
// _collection.Add(oSearchObject);
//}
//public void AddNullOperator(string fieldName, bool CheckIsNull)
//{
// SearchObject oSearchObject = new SearchObject();
// oSearchObject.FieldName = fieldName;
// oSearchObject.Operator = (CheckIsNull==true)? "IS NULL" : "NOT IS NULL";
// oSearchObject.DataType = EnumSearchObjDataType.String ;
// oSearchObject.FirstValue = "";
// _collection.Add(oSearchObject);
//}
public void Add(EnumSQLOperator sqlOperator, string fieldName, string firstValue, string secondValue, EnumSearchObjDataType datatype)
{
SearchObject oSearchObject = new SearchObject();
oSearchObject.FieldName = fieldName;
oSearchObject.Operator = sqlOperator;
oSearchObject.FirstValue = firstValue;
oSearchObject.SecondValue = secondValue;
oSearchObject.DataType = datatype;
if (fieldName == "" ) throw new ServiceException("Search Object: field name not found");
switch (sqlOperator)
{
case EnumSQLOperator.None:
break;
case EnumSQLOperator.EqualTo:
if (firstValue == "") throw new ServiceException("Search Object: first value is empty");
break;
case EnumSQLOperator.NotEqualTo:
if (firstValue == "") throw new ServiceException("Search Object: first value is empty");
break;
case EnumSQLOperator.GraterThan:
if (firstValue == "") throw new ServiceException("Search Object: first value is empty");
break;
case EnumSQLOperator.SmallerThan:
if (firstValue == "") throw new ServiceException("Search Object: first value is empty");
break;
case EnumSQLOperator.Between:
if (firstValue == "" || secondValue =="") throw new ServiceException("Search Object: first & second value is empty");
break;
case EnumSQLOperator.NotBetween:
if (firstValue == "" || secondValue == "") throw new ServiceException("Search Object: first & second value is empty");
break;
case EnumSQLOperator.In:
if (firstValue == "") throw new ServiceException("Search Object: first value is empty");
break;
case EnumSQLOperator.NotIn:
if (firstValue == "") throw new ServiceException("Search Object: first value is empty");
break;
case EnumSQLOperator.Like:
if (firstValue == "") throw new ServiceException("Search Object: first value is empty");
break;
case EnumSQLOperator.Null:
break;
case EnumSQLOperator.NotNull:
break;
default:
break;
}
_collection.Add(oSearchObject);
}
#region Functions
private string GetOperator()
{
string sopt = "";
switch (_operator )
{
case EnumSQLOperator.EqualTo:
sopt = "=";
break;
case EnumSQLOperator.NotEqualTo:
sopt = "<>";
break;
case EnumSQLOperator.GraterThan:
sopt = ">";
break;
case EnumSQLOperator.SmallerThan:
sopt = "<";
break;
case EnumSQLOperator.Between:
sopt = "BETWEEN";
break;
case EnumSQLOperator.NotBetween:
sopt = "NOT BETWEEN";
break;
case EnumSQLOperator.In:
sopt = "IN";
break;
case EnumSQLOperator.NotIn:
sopt = "NOT IN";
break;
case EnumSQLOperator.Like:
sopt = "Like";
break;
case EnumSQLOperator.Null:
sopt = "IS NULL";
break;
case EnumSQLOperator.NotNull:
sopt = "IS NOT NULL";
break;
default:
break;
}
return sopt;
}
public string BuildQuery(bool buildFromWhere)
{
string sQuery = (buildFromWhere == true) ? "" : " ";
string sX = "";
string sql = "";
foreach (SearchObject oItem in this._collection)
{
if (sQuery != "")
{
sQuery = sQuery + " AND ";
}
else
{
sQuery = " WHERE ";
}
sql = oItem.FieldName + " " + oItem.GetOperator();
switch (oItem.DataType)
{
case EnumSearchObjDataType.Number:
sX = "%n";
break;
case EnumSearchObjDataType.String:
sX = "%s";
break;
case EnumSearchObjDataType.Date:
sX = "%d";
break;
case EnumSearchObjDataType.DateWithTime:
sX = "%D";
break;
case EnumSearchObjDataType.Object:
sX = "%q";
break;
case EnumSearchObjDataType.boolean:
sX = "%b";
break;
default:
break;
}
switch (oItem.Operator)
{
case EnumSQLOperator.None:
//int nindex = sql.LastIndexOf(" and ");
//sql = sql.Remove(nindex);
break;
case EnumSQLOperator.EqualTo:
case EnumSQLOperator.NotEqualTo:
case EnumSQLOperator.GraterThan:
case EnumSQLOperator.SmallerThan:
sql = sql + " " + sX;
sQuery = sQuery + Ease.CoreV35.DataAccess.SQLParser.MakeSQL(sql, oItem.FirstValue);
break;
case EnumSQLOperator.Between:
case EnumSQLOperator.NotBetween:
sql = sql + " " + sX + " AND " + sX;
sQuery = sQuery + Ease.CoreV35.DataAccess.SQLParser.MakeSQL(sql, oItem.FirstValue, oItem.SecondValue);
break;
case EnumSQLOperator.In:
case EnumSQLOperator.NotIn:
sql = sql + "(" + oItem.FirstValue + ")";
sQuery = sQuery + sql;
break;
case EnumSQLOperator.Like:
string sLIKEClause = "'%" + oItem.FirstValue + "%'";
sql = sql + " " + "%q";
sQuery = sQuery + Ease.CoreV35.DataAccess.SQLParser.MakeSQL(sql, sLIKEClause);
break;
case EnumSQLOperator.Null:
case EnumSQLOperator.NotNull:
sQuery = sQuery + sql;
break;
default:
break;
}
}
return sQuery;
}
#endregion
}
#endregion
}