using System;using System.Collections.Generic;using System.Data;using com.tg.framework.basic.dataaccess;using com.tg.framework.basic.logging;using com.tg.framework.common;namespace com.tg.framework.core{ /// <summary> /// This is the base DAO of business. /// </summary> public class BaseDAO : IDAO { #region Constants and Fields /// <summary> /// Variable dataaccess. /// </summary> protected IDataAccessManager dataaccess = null; /// <summary> /// Variable LoggerManager. /// </summary> private ILogManager LoggerManager = LogFactory.GetLoggerManager(); /// <summary> /// Varibale datamodel. /// </summary> private IDataModel datamodel = null; #endregion #region Constructors and Destructors /// <summary> /// Initializes a new instance of the BaseDAO class. /// </summary> public BaseDAO() { ////set current connection and transaction to current thread this.dataaccess = DataAccessFactory.GetDataAccess(); } /// <summary> /// Initializes a new instance of the BaseDAO class, with parameter rerionName. /// </summary> /// <param name="regionName">parameter region name</param> public BaseDAO(string regionName) { this.dataaccess = DataAccessFactory.GetDataAccess(regionName); } #endregion #region Public Methods /// <summary> /// Set data model /// </summary> /// <param name="model">the data model</param> public void SetDataModel(IDataModel model) { this.datamodel = model; } /// <summary> /// Get data model /// </summary> /// <returns>return the data model</returns> public IDataModel GetDataModel() { return this.datamodel; } /// <summary> /// Persist a data model /// </summary> /// <param name="model">parameter -- data model</param> /// <returns>return the result of persist</returns> public string Add(IDataModel model) { string[] avaiFields = null; object[] avaiObjects = null; ////use create time and update time if (model.GetModelUseRecordTime()) { model.SetCreateTime(System.DateTime.Now); } ////use creator and updator and remoteaddress if (model.GetModelUseRecordClient()) { model.SetByLoginId(GlobalObject.GetLoginID()); model.SetByLoginName(GlobalObject.GetLoginName()); model.SetByRemoteAddress(GlobalObject.GetRemoteAddress()); } //// get the fields of available model.GetModelAvaiableFields(out avaiFields, out avaiObjects); //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = model.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); string sql = this.dataaccess.GetInsertSQL(tableName, avaiFields, avaiObjects) + ";SELECT @@Identity"; ////get the db parameters var dbparams = ParameterHelper.GetInDbParameters(avaiFields, avaiObjects); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model string id = this.dataaccess.ExecuteScalar(CommandType.Text, sql, dbparams).ToString(); return id; } /// <summary> /// Update a data model /// </summary> /// <param name="model">parameter -- data model</param> /// <returns>return the number of updated object</returns> public int Update(IDataModel model) { string[] avaiFields = null; object[] avaiObjects = null; ////use create time and update time if (model.GetModelUseRecordTime()) { model.SetUpdateTime(System.DateTime.Now); } ////use creator and updator and remoteaddress if (model.GetModelUseRecordClient()) { model.SetByLoginId(LayerConstant.DataModelFieldLoginID); model.SetByLoginName(GlobalObject.GetLoginName()); model.SetByRemoteAddress(GlobalObject.GetRemoteAddress()); } //// get the fields when they been updated model.GetModelUpdatedFields(out avaiFields, out avaiObjects); //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = model.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); ////get condition of key field string condition = LayerHelper.GeneralLayer.IdentifyName + "=" + LangUtil.FormatSqlInput(model.GetModelValue(LayerHelper.GeneralLayer.IdentifyName)); string sql = this.dataaccess.GetUpdateSQL(tableName, avaiFields, avaiObjects, condition); ////get the db parameters var dbparams = ParameterHelper.GetInDbParameters(avaiFields, avaiObjects); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model return this.dataaccess.ExecuteNonQuery(CommandType.Text, sql, dbparams); } /// <summary> /// Remove data model by id /// </summary> /// <param name="objectId">parameter -- remove condition</param> /// <returns>return the number of removed object</returns> public int Remove(string objectId) { //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = this.datamodel.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); ////get condition of key field string condition = LayerConstant.DefaultIdentifyName + "=" + LangUtil.FormatSqlInput(objectId); string sql = this.dataaccess.GetDeleteSQL(tableName, condition); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model int intCount = this.dataaccess.ExecuteNonQuery(CommandType.Text, sql, null); return intCount; } /// <summary> /// Remove data model by condition /// </summary> /// <param name="dc">parameter -- remove condition</param> /// <returns>return the number of removed object</returns> public int Remove(DataCondition dc) { //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = this.datamodel.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); string condition = dc.Condition; string sql = this.dataaccess.GetDeleteSQL(tableName, condition); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model int intCount = this.dataaccess.ExecuteNonQuery(CommandType.Text, sql, null); return intCount; } /// <summary> /// Get data model by object id /// </summary> /// <param name="objectId">parameter -- object id</param> /// <returns>return data model result</returns> public IDataModel Get(string objectId) { //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = this.datamodel.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); ////get select sql with key value string sql = this.dataaccess.GetSelectSQLById(tableName, LayerHelper.GeneralLayer.IdentifyName); ////get the parameters var dbparams = ParameterHelper.GetInDbParameters(LayerHelper.GeneralLayer.IdentifyName, objectId); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model DataSet dataSet = this.dataaccess.ExecuteQuery(CommandType.Text, sql, dbparams); ////get a new object with the datamodel's type from database var model = (IDataModel)DataObject.ParseFirstDataRowToObject(dataSet.Tables[0], this.datamodel.GetType()); ////return this result return model; } /// <summary> /// Get data model by condition /// </summary> /// <param name="dc">parameter -- data condition</param> /// <returns>return data model result</returns> public IDataModel Get(DataCondition dc) { //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = this.datamodel.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); ////get select sql with key value string sql = this.dataaccess.GetSelectSQL(tableName, dc.Condition); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model DataSet dataSet = this.dataaccess.ExecuteQuery(CommandType.Text, sql, null); ////get a new object with the datamodel's type from database var model = (IDataModel)DataObject.ParseFirstDataRowToObject(dataSet.Tables[0], this.datamodel.GetType()); ////return this result return model; } /// <summary> /// Judging whether object exist by id /// </summary> /// <param name="objectId">parameter -- object id</param> /// <returns>return judging result</returns> public bool Exist(string objectId) { //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = this.datamodel.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); ////get select sql with key value string sql = this.dataaccess.GetExistSQLById(tableName, LayerHelper.GeneralLayer.IdentifyName); ////get the parameters var dbparams = ParameterHelper.GetInDbParameters(LayerHelper.GeneralLayer.IdentifyName, objectId); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model var count = (int)this.dataaccess.ExecuteScalar(CommandType.Text, sql, dbparams); ////return false or true return Convert.ToBoolean(count); } /// <summary> /// Judging whether object exist by condition /// </summary> /// <param name="dc">parameter -- data condition value</param> /// <returns>return judging result</returns> public bool Exist(DataCondition dc) { //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = this.datamodel.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); ////get select sql with key value string sql = this.dataaccess.GetCountSQLById(tableName, dc.Condition); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model var count = (int)this.dataaccess.ExecuteScalar(CommandType.Text, sql, null); ////return false or true return Convert.ToBoolean(count); } /// <summary> /// Get the number of record /// </summary> /// <returns>return the number of record by condition</returns> public int GetCount() { //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = this.datamodel.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); ////get select sql with key value string sql = this.dataaccess.GetCountSQLById(tableName, ""); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model var count = (int)this.dataaccess.ExecuteScalar(CommandType.Text, sql, null); ////return the number of data return count; } /// <summary> /// Get the number of record by condition /// </summary> /// <param name="dc">parameter -- data condition value</param> /// <returns>return the number of record by condition</returns> public int GetCount(DataCondition dc) { //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = this.datamodel.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); ////get select sql with key value string sql = this.dataaccess.GetCountSQLById(tableName, dc.Condition); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model var count = (int)this.dataaccess.ExecuteScalar(CommandType.Text, sql, null); ////return the number of data return count; } /// <summary> /// Get list of data model by condition /// </summary> /// <param name="dc">parameter -- data condition value</param> /// <returns>return the data model list</returns> public List<IDataModel> GetList(DataCondition dc) { ////define the return object var listData = new List<IDataModel>(); //// database name string databaseName = this.dataaccess.GetDatabaseName(); //// model name string modelName = this.datamodel.GetModelName(); ////get insert sql string tableName = this.dataaccess.GetTableName(databaseName, modelName); ////fill the datacondition object if (string.IsNullOrEmpty(dc.TableList)) { dc.TableList = tableName; } if (string.IsNullOrEmpty(dc.FieldList)) { dc.FieldList = "*"; } if (string.IsNullOrEmpty(dc.KeyField)) { dc.KeyField = LayerHelper.GeneralLayer.IdentifyName; } if (string.IsNullOrEmpty(dc.FieldSort)) { dc.FieldSort = LayerHelper.GeneralLayer.IdentifyName; } ////get record count first dc.RecordCount = this.GetCount(dc); if (!dc.PageSize.Equals(0)) { dc.Compute(); } ////get select sql with key value string sql = this.dataaccess.GetSelectSQL(dc); ////log sql for debug this.LoggerManager.Debug(sql); ////run sql of add model DataSet dataSet = this.dataaccess.ExecuteQuery(CommandType.Text, sql, null); DataTable dt = dataSet.Tables[0]; ////get data from datatable to DataModel foreach (DataRow datarow in dt.Rows) { ////get a new object with the datamodel's type from database var model = (IDataModel)DataObject.ParseDataRowToObject(datarow, this.datamodel.GetType()); listData.Add(model); } ////return this result return listData; } /// <summary> /// Get list of data model by sql /// </summary> /// <param name="sql">parameter -- sql string</param> /// <returns>return the data model list</returns> public List<IDataModel> GetBySql(string sql) { List<IDataModel> listData = new List<IDataModel>(); ////log sql for debug this.LoggerManager.Debug(sql); DataSet dataSet = this.dataaccess.ExecuteQuery(CommandType.Text, sql, null); DataTable dt = dataSet.Tables[0]; foreach (DataRow row in dt.Rows) { listData.Add((IDataModel)DataObject.ParseDataRowToObject(row, this.datamodel.GetType())); } return listData; } /// <summary> /// Dispose data access /// </summary> public void Dispose() { DataAccessFactory.ReleaseDataAccess(this.dataaccess); } #endregion }}