/// <summary> /// Delete registers based on ID informed. Other values are skipped. /// </summary> /// <param name="parTerritoriesInfo">Item to delete</param> /// <param name="errorMessage">Error message</param> public virtual void DeleteByID(TerritoriesInfo parTerritoriesInfo, out string errorMessage) { TerritoriesInfo newParam = new TerritoriesInfo(); newParam.TerritoryID = parTerritoriesInfo.TerritoryID; this.Delete(newParam, out errorMessage); }
/// <summary> /// Performs one "select * from MyTable where [InformedProperties]". MinValues and nulls are skipped from filter. /// </summary> /// <param name="filter">TerritoriesInfo</param> /// <returns>List of found records.</returns> public virtual List <TerritoriesInfo> GetSome(TerritoriesInfo filter) { List <TerritoriesInfo> AllInfoList = new List <TerritoriesInfo>(); string filterWhere = string.Empty; List <DbParameter> paramList = null; GenerateWhere(filter, out filterWhere, out paramList); motor.ClearCommandParameters(); motor.AddCommandParameters(paramList); motor.CommandText = GetSelectCommand() + " " + filterWhere; DbDataReader dbReader = motor.ExecuteReader(); ClassFiller classFiller = new ClassFiller(typeof(TerritoriesInfo), dbReader); using (dbReader) { while (dbReader.Read()) { TerritoriesInfo TerritoriesInfo = new TerritoriesInfo(); ///Warning: performance issues with this automation. See method description for details. classFiller.Fill(TerritoriesInfo); AllInfoList.Add(TerritoriesInfo); } } return(AllInfoList); }
/// <summary> /// Delete registers based on class values informed. MinValues and nulls are skipped. /// </summary> /// <param name="parTerritoriesInfo">Item to delete</param> /// <param name="transaction">Transaction context</param> /// <param name="errorMessage">Error message</param> public virtual void Delete(TerritoriesInfo parTerritoriesInfo, DbTransaction transaction, out string errorMessage) { errorMessage = null; try { string whereClausule = string.Empty; var pks = GetPrimaryKey(); List <string> primaryKeys = new List <string>(); foreach (var item in pks) { primaryKeys.Add(item.Key); } List <DbParameter> paramList = ParameterBuilder.GetParametersForDelete(primaryKeys, typeof(TerritoriesInfo), parTerritoriesInfo, motor.Command, out whereClausule); motor.CommandText = GetDeleteCommand() + " " + whereClausule; motor.ClearCommandParameters(); motor.AddCommandParameters(paramList); motor.AddTransaction(transaction); motor.ExecuteNonQuery(); } catch (Exception ex) { errorMessage = ex.Message; } }
/// <summary> /// Get one register using only ID as key. /// </summary> /// <returns></returns> public virtual TerritoriesInfo GetValueByID(string TerritoryID) { //ToDo: set multiple PK filter motor.ClearCommandParameters(); motor.CommandText = GetSelectCommand() + GetWherePrimaryKey(); List <DbParameter> paramList = new List <DbParameter>(); DbParameter paramTerritoryID = motor.Command.CreateParameter(); paramTerritoryID.ParameterName = "@param_TerritoryID"; paramTerritoryID.Value = TerritoryID; paramList.Add(paramTerritoryID); motor.AddCommandParameters(paramList); TerritoriesInfo InfoValue = new TerritoriesInfo(); DbDataReader dbReader = motor.ExecuteReader(); ClassFiller classFiller = new ClassFiller(typeof(TerritoriesInfo), dbReader); using (dbReader) { if (dbReader.Read()) { InfoValue = new TerritoriesInfo(); classFiller.Fill(InfoValue); } else { return(null); } } return(InfoValue); }
/// <summary> /// Delete registers based on class values informed. MinValues and nulls are skipped. /// Must have "MultipleActiveResultSets=True" on connection string. /// </summary> /// <param name="parTerritoriesInfo">Item to delete</param> /// <param name="transaction">Transaction context</param> /// <param name="errorMessage">Error message</param> public virtual void Delete(TerritoriesInfo parTerritoriesInfo, DbTransaction transaction, out string errorMessage) { errorMessage = string.Empty; TerritoriesDAO.Delete(parTerritoriesInfo, transaction, out errorMessage); //By default, the caller of this method will do the commit. //motor.Commit(); //motor.CloseConnection(); }
public void DeleteData(ModelNotifiedForTerritories modelNotifiedForTerritories, out string error) { TerritoriesBsn bsn = new TerritoriesBsn(wpfConfig); TerritoriesInfo dbItem = new TerritoriesInfo(); Cloner.CopyAllTo(typeof(ModelNotifiedForTerritories), modelNotifiedForTerritories, typeof(TerritoriesInfo), dbItem); bsn.DeleteByID(dbItem, out error); }
/// <summary> /// Performs one "update" database command. /// Will commit the transaction and close the connection. Use for independent delete. /// </summary> /// <param name="TerritoriesInfo">Object to update.</param> /// <param name="errorMessage">Error message if exception is throwed</param> public virtual void UpdateOne(TerritoriesInfo parTerritoriesInfo, out string errorMessage) { errorMessage = string.Empty; DbTransaction transaction = motor.BeginTransaction(); this.UpdateOne(parTerritoriesInfo, transaction, out errorMessage); motor.Commit(); motor.CloseConnection(); }
/// <summary> /// Generate the "where" clausule, used for select data using "GetSome" method. /// </summary> /// <param name="filter">Class used to apply the filter</param> /// <param name="whereClausule">Result whith a string that add filter to the select comand</param> /// <param name="paramList">Result whith the parameters list</param> protected void GenerateWhere(TerritoriesInfo filter, out string whereClausule, out List <DbParameter> paramList) { StringBuilder where = new StringBuilder(); paramList = new List <DbParameter>(); where.Append("where 1=1"); // 1) Adding filter for field TerritoryID if (filter.TerritoryID != null) { DbParameter param = motor.Command.CreateParameter(); param.ParameterName = "@param_TerritoryID"; param.Value = filter.TerritoryID; paramList.Add(param); where.Append(" and Territories.TerritoryID=@param_TerritoryID"); //Hint: use the code below to add a "like" search. Warning: may cause data performance issues. //param.ParameterName = "@param_TerritoryID"; //param.Value = "%" + filter.TerritoryID "%"; //paramList.Add(param); //where.Append(" and Territories.TerritoryID like @param_TerritoryID"); } // 2) Adding filter for field TerritoryDescription if (filter.TerritoryDescription != null) { DbParameter param = motor.Command.CreateParameter(); param.ParameterName = "@param_TerritoryDescription"; param.Value = filter.TerritoryDescription; paramList.Add(param); where.Append(" and Territories.TerritoryDescription=@param_TerritoryDescription"); //Hint: use the code below to add a "like" search. Warning: may cause data performance issues. //param.ParameterName = "@param_TerritoryDescription"; //param.Value = "%" + filter.TerritoryDescription "%"; //paramList.Add(param); //where.Append(" and Territories.TerritoryDescription like @param_TerritoryDescription"); } // 3) Adding filter for field RegionID if (filter.RegionID != Int32.MinValue) { DbParameter param = motor.Command.CreateParameter(); param.ParameterName = "@param_RegionID"; param.Value = filter.RegionID; paramList.Add(param); where.Append(" and Territories.RegionID=@param_RegionID"); } // 4) Adding filter for field RegionDescription if (filter.FK0_RegionDescription != null) { DbParameter param = motor.Command.CreateParameter(); param.ParameterName = "@param_FK0_RegionDescription"; param.Value = filter.FK0_RegionDescription; paramList.Add(param); where.Append(" and FK0_Region.RegionDescription=@param_FK0_RegionDescription"); } whereClausule = where.ToString(); }
public void AddData(ModelNotifiedForTerritories modelNotifiedForTerritories, out string error) { TerritoriesBsn bsn = new TerritoriesBsn(wpfConfig); TerritoriesInfo dbItem = new TerritoriesInfo(); Cloner.CopyAllTo(typeof(ModelNotifiedForTerritories), modelNotifiedForTerritories, typeof(TerritoriesInfo), dbItem); bsn.InsertOne(dbItem, out error); modelNotifiedForTerritories.NewItem = false; Cloner.CopyAllTo(typeof(TerritoriesInfo), dbItem, typeof(ModelNotifiedForTerritories), modelNotifiedForTerritories); }
/// <summary> /// Delete registers based on class ID informed in transactional context. Other values are skipped. /// Must have "MultipleActiveResultSets=True" on connection string. /// </summary> /// <param name="parTerritoriesInfo">Item to delete</param> /// <param name="transaction">Transaction context</param> /// <param name="errorMessage">Error message</param> public virtual void DeleteByID(TerritoriesInfo parTerritoriesInfo, DbTransaction transaction, out string errorMessage) { TerritoriesInfo newParam = new TerritoriesInfo(); newParam.TerritoryID = parTerritoriesInfo.TerritoryID; this.Delete(newParam, transaction, out errorMessage); //By default, the caller of this method will do the commit. //motor.Commit(); //motor.CloseConnection(); }
public ModelNotifiedForTerritories GetTerritoriesByID(string TerritoryID, out string error) { error = null; TerritoriesBsn bsn = new TerritoriesBsn(wpfConfig); TerritoriesInfo dbItem = bsn.GetValueByID(TerritoryID); ModelNotifiedForTerritories item = new ModelNotifiedForTerritories(); Cloner.CopyAllTo(typeof(TerritoriesInfo), dbItem, typeof(ModelNotifiedForTerritories), item); return(item); }
/// <summary> /// Retrieves the data using only the primary key ID. Ex.: "Select * from MyTable where id=1" /// </summary> /// <returns>The class filled if found.</returns> public virtual TerritoriesInfo GetValueByID(string TerritoryID) { motor.OpenConnection(); TerritoriesInfo value = TerritoriesDAO.GetValueByID(TerritoryID); if (this.closeConnectionWhenFinish) { motor.CloseConnection(); } return(value); }
/// <summary> /// Perform one "select" command to database. Filter the data using "filter" class. /// </summary> /// <param name="filter">Class to use as filter</param> /// <returns>List with filtered data</returns> public virtual List <TerritoriesInfo> GetSome(TerritoriesInfo filter) { motor.OpenConnection(); List <TerritoriesInfo> list = TerritoriesDAO.GetSome(filter); if (this.closeConnectionWhenFinish) { motor.CloseConnection(); } return(list); }
/// <summary> /// Performs one "update" database command in a transactional context. /// * The method uses a transaction object already created and does not close the connection. /// * Must have "MultipleActiveResultSets=True" on connection string. /// </summary> /// <param name="TerritoriesInfo">Object to update.</param> /// <param name="transaction">Inform "DBTransaction".</param> /// <param name="errorMessage">Error message if exception is throwed.</param> public virtual void UpdateOne(TerritoriesInfo parTerritoriesInfo, DbTransaction transaction, out string errorMessage) { errorMessage = string.Empty; //If is trying to insert FKValue without the ID but has the unique description, //the system will try to get the class with the ID and populate it. if ((parTerritoriesInfo.RegionID == Int32.MinValue) && (parTerritoriesInfo.FK0_RegionDescription != null)) { RegionInfo fkClass = Get_RegionIDID_FKRegion(parTerritoriesInfo, transaction); parTerritoriesInfo.RegionID = fkClass.RegionID; } TerritoriesDAO.UpdateOne(parTerritoriesInfo, transaction, out errorMessage); //By default, the caller of this method will do the commit. //motor.Commit(); //motor.CloseConnection(); }
/// <summary> /// Performs a "update [FildList] set [FieldList] where id = @id". Must have the ID informed. /// </summary> /// <param name="parTerritoriesInfo">Item to update</param> /// <param name="transaction">Transaction context</param> /// <param name="errorMessage">Error message</param> public virtual void UpdateOne(TerritoriesInfo parTerritoriesInfo, DbTransaction transaction, out string errorMessage) { errorMessage = null; try { motor.CommandText = GetUpdateCommand(); ///Warning: performance issues with this automation. See method description for details. List <DbParameter> paramList = ParameterBuilder.GetParametersForUpdate(typeof(TerritoriesInfo), parTerritoriesInfo, motor.Command); motor.ClearCommandParameters(); motor.AddCommandParameters(paramList); motor.AddTransaction(transaction); motor.ExecuteNonQuery(); } catch (Exception ex) { errorMessage = ex.Message; } }
/// <summary> /// Use parameters to apply simple filters /// </summary> /// <param name="filterExpression"></param> /// <returns></returns> public virtual List <TerritoriesInfo> GetAll(List <DataFilterExpressionDB> filterExpression) { List <TerritoriesInfo> AllInfoList = new List <TerritoriesInfo>(); motor.ClearCommandParameters(); motor.CommandText = GetSelectCommand() + " where 1=1 "; List <DbParameter> paramList = new List <DbParameter>(); string where = ""; foreach (DataFilterExpressionDB filter in filterExpression) { DbParameter param = motor.Command.CreateParameter(); param.ParameterName = "@param_" + filter.FieldName; param.Value = filter.Filter; param.DbType = HelperDBType.GetDBType(typeof(TerritoriesInfo), filter.FieldName); if (filter.FilterType == DataFilterExpressionDB._FilterType.Equal) { param.Value = filter.Filter; where += string.Format(" and Territories.{0} = {1}", filter.FieldName, param.ParameterName); } else { param.Value = "%" + filter.Filter + "%"; where += string.Format(" and Territories.{0} like {1}", filter.FieldName, param.ParameterName); } paramList.Add(param); } motor.CommandText += where; motor.AddCommandParameters(paramList); DbDataReader dbReader = motor.ExecuteReader(); ClassFiller classFiller = new ClassFiller(typeof(TerritoriesInfo), dbReader); using (dbReader) { while (dbReader.Read()) { TerritoriesInfo classInfo = new TerritoriesInfo(); classFiller.Fill(classInfo); AllInfoList.Add(classInfo); } } return(AllInfoList); }
/// <summary> /// Performs one "Select * from MyTable". Use wisely. /// </summary> /// <returns>List of found records.</returns> public virtual List <TerritoriesInfo> GetAll() { List <TerritoriesInfo> AllInfoList = new List <TerritoriesInfo>(); motor.CommandText = GetSelectCommand(); DbDataReader dbReader = motor.ExecuteReader(); ClassFiller classFiller = new ClassFiller(typeof(TerritoriesInfo), dbReader); using (dbReader) { while (dbReader.Read()) { TerritoriesInfo classInfo = new TerritoriesInfo(); classFiller.Fill(classInfo); AllInfoList.Add(classInfo); } } return(AllInfoList); }
/// <summary> /// Same as get all but filter the result set /// </summary> /// <param name="numberOfRowsToSkip">Skip first X rows</param> /// <param name="numberOfRows">Like "TOP" in sql server</param> /// <returns></returns> public List <TerritoriesInfo> GetAll(int numberOfRowsToSkip, int numberOfRows) { List <TerritoriesInfo> AllInfoList = new List <TerritoriesInfo>(); motor.CommandText = base.GetFilteredRowNumAndSkipQuery("AttributeLists", "id", numberOfRowsToSkip, numberOfRows); DbDataReader dbReader = motor.ExecuteReader(); ClassFiller classFiller = new ClassFiller(typeof(TerritoriesInfo), dbReader); using (dbReader) { while (dbReader.Read()) { TerritoriesInfo classInfo = new TerritoriesInfo(); classFiller.Fill(classInfo); AllInfoList.Add(classInfo); } } return(AllInfoList); }
/// <summary> /// Delete registers based on class values informed. MinValues and nulls are skipped. /// </summary> /// <param name="parTerritoriesInfo">Item to delete</param> /// <param name="errorMessage">Error message</param> public virtual void Delete(TerritoriesInfo parTerritoriesInfo, out string errorMessage) { errorMessage = string.Empty; //1) Start the transaction context. DbTransaction transaction = motor.BeginTransaction(); //2) Call the overload of this method, which call the DAO but does not commit. this.Delete(parTerritoriesInfo, transaction, out errorMessage); //3) Commit the transaction. motor.Commit(); //4) Close the conection (if configured to do so). if (this.closeConnectionWhenFinish) { motor.CloseConnection(); } }
/// <summary> /// Perform a search to find the class "RegionInfo" using as key the field "Region". /// </summary> /// <param name="parTerritoriesInfo">Main class that contains the aggregation.</param> /// <returns>Foreing key attched class.</returns> public virtual RegionInfo Get_RegionIDID_FKRegion(TerritoriesInfo parTerritoriesInfo, DbTransaction transaction) { RegionInfo filter = new RegionInfo(); filter.RegionDescription = parTerritoriesInfo.FK0_RegionDescription; RegionBsn myClass = new RegionBsn(false, this.motor); List <RegionInfo> list = myClass.GetSome(filter); if (list.Count == 0) { //This error occurs when try to search for the ID in one table, but it does not find the value. //Ex.: Select id,SomeField from myTable where SomeField='myValue') If no data return, this error will trigger. throw new Exception(string.Format("Can not define ID for parTerritoriesInfo.", parTerritoriesInfo.FK0_RegionDescription)); /* [Hint] The code below do one insert in the table "Region" informing only the "RegionID" field. * [Warning] The code may crash if other fields are necessary. * [Instructions] Comment the exception above. Uncomment the code below. */ //string errorMsg = string.Empty; //myClass.InsertOne(filter, transaction, out errorMsg); //if (errorMsg != string.Empty) //{ //throw new Exception(errorMsg); //} //else //{ //return filter; //} } if (list.Count > 1) { //This error occurs when try to search for the ID in one table, but it return more then one value. //Ex.: Select id,SomeField from myTable where SomeField='myValue') If more then one line return, this error will trigger. throw new Exception(string.Format("Can not define ID for parTerritoriesInfo. Theres more then one ID value for this field. ", parTerritoriesInfo.FK0_RegionDescription)); } else { //Return the only one class found. return(list[0]); } }
public void TryUpdate(UpdateTerritoriesView viewToUpdate, out RestExceptionError error) { error = null; TerritoriesInfo dbViewToInclude = new TerritoriesInfo(); try { Cloner.CopyAllTo(typeof(UpdateTerritoriesView), viewToUpdate, typeof(TerritoriesInfo), dbViewToInclude); } catch (Exception ex) { error = new RestExceptionError(); error.InternalMessage = "Internal Error parsing data for (Territories.TryUpdate/Parsing)"; error.ExceptionMessage = ex.Message; error.SourceError = RestExceptionError._SourceError.ServerSide; error.StackTrace = ex.StackTrace; } try { TerritoriesBsn bsn = new TerritoriesBsn(restConfig); string dbError = null; bsn.UpdateOne(dbViewToInclude, out dbError); if (dbError != null) { error = new RestExceptionError(); error.InternalMessage = "Internal Error Save data for [Territories.TryUpdate]"; error.ExceptionMessage = dbError; error.SourceError = RestExceptionError._SourceError.ServerSide; error.StackTrace = ""; } } catch (Exception ex) { error = new RestExceptionError(); error.InternalMessage = "Internal Error Update data for [Territories.TryUpdate]"; error.ExceptionMessage = ex.Message; error.SourceError = RestExceptionError._SourceError.ServerSide; error.StackTrace = ex.StackTrace; } }
/// <summary> /// Performs one "insert into" database command in a transactional context. /// * The method uses a transaction object already created and does not close the connection. /// * Must have "MultipleActiveResultSets=True" on connection string. /// </summary> /// <param name="EmployeeTerritoriesInfo">Object to insert.</param> /// <param name="transaction">Inform "DBTransaction".</param> /// <param name="errorMessage">Error message if exception is throwed.</param> public virtual void InsertOne(EmployeeTerritoriesInfo parEmployeeTerritoriesInfo, DbTransaction transaction, out string errorMessage) { errorMessage = string.Empty; //If is trying to insert FKValue without the ID but has the unique description, //the system will try to get the class with the ID and populate it. if ((parEmployeeTerritoriesInfo.EmployeeID == Int32.MinValue) && (parEmployeeTerritoriesInfo.FK0_LastName != null)) { EmployeesInfo fkClass = Get_EmployeeIDID_FKEmployees(parEmployeeTerritoriesInfo, transaction); parEmployeeTerritoriesInfo.EmployeeID = fkClass.EmployeeID; } if ((parEmployeeTerritoriesInfo.TerritoryID == string.Empty) && (parEmployeeTerritoriesInfo.FK1_TerritoryDescription != null)) { TerritoriesInfo fkClass = Get_TerritoryIDID_FKTerritories(parEmployeeTerritoriesInfo, transaction); parEmployeeTerritoriesInfo.TerritoryID = fkClass.TerritoryID; } EmployeeTerritoriesDAO.InsertOne(parEmployeeTerritoriesInfo, transaction, out errorMessage); //By default, the caller of this method will do the commit. //motor.Commit(); //motor.CloseConnection(); }