EchoTex_Payroll/HRM.BO/Common/SearchObject.cs

368 lines
13 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
using Ease.Core.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
namespace HRM.BO
{
#region SearchObject
#region SearchObject Data Type
#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.Core.DataAccess.SQLParser.MakeSQL(sql, oItem.FirstValue);
break;
case EnumSQLOperator.Between:
case EnumSQLOperator.NotBetween:
sql = sql + " " + sX + " AND " + sX;
sQuery = sQuery + Ease.Core.DataAccess.SQLParser.MakeSQL(sql, oItem.FirstValue, oItem.SecondValue);
break;
case EnumSQLOperator.In:
case EnumSQLOperator.NotIn:
//if (oItem.FieldName.ToLower().Contains("employeeno"))
//{
// sql = sql + "(" + Ease.Core.DataAccess.SQLParser.MakeSQL(sX, oItem.FirstValue) + ")";
//}
//else
//{
// sql = sql + "(" + oItem.FirstValue + ")";
//}
string inVal = "";
if (oItem.DataType == EnumSearchObjDataType.String)
{
string[] val = oItem.FirstValue.Split(",");
for (int i = 0; i < val.Length; i++)
{
inVal = inVal + "'" + val[i] + "'" ;
if (i != val.Length-1)
inVal = inVal + ", ";
}
}
else
{
inVal = oItem.FirstValue;
}
sql = sql + " (" + inVal + ")";
sQuery = sQuery + sql;
break;
case EnumSQLOperator.Like:
string sLIKEClause = "'%" + oItem.FirstValue + "%'";
sql = sql + " " + "%q";
sQuery = sQuery + Ease.Core.DataAccess.SQLParser.MakeSQL(sql, sLIKEClause);
break;
case EnumSQLOperator.Null:
case EnumSQLOperator.NotNull:
sQuery = sQuery + sql;
break;
default:
break;
}
}
return sQuery;
}
#endregion
}
}