/// <summary> /// Run a stored procedure or sql query /// </summary> /// <param name="commandType"></param> /// <param name="parameters"></param> public void ExecuteSQL(CommandType commandType, string statement, Dictionary <string, string> parameters = null) { try { switch (commandType) { case CommandType.StoredProcedure: SqlUtil.ExecuteStoredProc(statement, parameters); break; case CommandType.Text: SqlUtil.ExecuteDynamicQuery(statement); break; case CommandType.TableDirect: throw new Exception("Command type 'TableDirect' not implemented yet."); default: throw new Exception("Command type not recognized."); } } catch (Exception ex) { throw new Exception(CustomErrorResponse(ex)); } }
/// <summary> /// Deletes the specified entity based off Primary Key attribute. /// </summary> /// <param name="entity"></param> public void Delete <T>(T entity) where T : new() { try { if (entity == null) { throw new ArgumentNullException("entity"); } string query = BaseQuery.DELETE_WHERE <T>(entity.ToDeleteQuery()); SqlUtil.ExecuteDynamicQuery(query); } catch (Exception ex) { throw new Exception(CustomErrorResponse(ex)); } }
/// <summary> /// Toggles the Flag Attribute active or inactive. /// </summary> /// <param name="entity"></param> public void MarkActive <T>(int id, bool isActive) where T : new() { try { if (id == 0) { throw new ArgumentNullException("id"); } string query = BaseQuery.MARK_ACTIVE <T>(isActive, id); SqlUtil.ExecuteDynamicQuery(query); } catch (Exception ex) { throw new Exception(CustomErrorResponse(ex)); } }
/// <summary> /// Inserts the specified entity. /// </summary> /// <param name="entity"></param> public void Insert <T>(T entity) where T : new() { try { if (entity == null) { throw new ArgumentNullException("entity"); } string query = BaseQuery.INSERT <T>(entity.ToInsertQuery()); SqlUtil.ExecuteDynamicQuery(query); } catch (Exception ex) { throw new Exception(CustomErrorResponse(ex)); } }
/// <summary> /// Updates the specified entity. /// </summary> /// <param name="entity"></param> public void Update <T>(T entity, Expression <Func <T, bool> > expression = null) where T : new() { try { if (entity == null) { throw new ArgumentNullException("entity"); } string condition = expression == null ? "" : QueryUtil.Translate(expression); string query = BaseQuery.UPDATE_WHERE <T>(entity.ToUpdateQuery(condition)); SqlUtil.ExecuteDynamicQuery(query); } catch (Exception ex) { throw new Exception(CustomErrorResponse(ex)); } }
/// <summary> /// Deletes the specified entity or entities based off provided expression. /// </summary> /// <param name="entity"></param> public void Delete <T>(Expression <Func <T, bool> > expression, bool deleteAll = false) { try { string table = typeof(T).Name; string condition = expression == null ? "" : QueryUtil.Translate(expression); string query = deleteAll ? (string.IsNullOrWhiteSpace(condition) ? BaseQuery.DELETE_ALL <T>(table) : BaseQuery.DELETE_WHERE <T>(new string[] { table, condition })) : BaseQuery.DELETE_FIRST_WHERE <T>(new string[] { table, condition }); SqlUtil.ExecuteDynamicQuery(query); } catch (Exception ex) { throw new Exception(CustomErrorResponse(ex)); } }