private static string GetHashValue <T>(SqlConnection conn, SqlTransaction tran, T item) where T : class, IDbQuery, new() { var key = $"{typeof(T)}.GetItem.QueryHashValue"; var sql = CommandTextCache.GetCachedCommandText(key); if (string.IsNullOrEmpty(sql)) { sql = CommandTextCache.GetHashValueQueryCommandText(key, DbEntityCache.GetDbEntity <T>()); } var param = GetPrimaryKeyParams <T>(item); return(param.Keys.Count == 0 ? string.Empty : conn.ExecuteScalar <string>(sql, param, tran, null, CommandType.Text)); }
public IList <R> GetValues <T, R>(string by, string field, object param, string[] conditions, string orderBy) where T : class, IDbQuery, new() { var key = string.Format("{0}.GetValues.Param.{1}", typeof(T), by); var sql = CommandTextCache.GetCachedCommandText(key); if (string.IsNullOrEmpty(sql)) { var dbEntity = DbEntityCache.GetDbEntity <T>(); var fields = param == null ? new string[] { } : ConvertHelper.ToKeys(param); sql = CommandTextCache.GetValuesCommandText(key, dbEntity, field, fields, conditions, orderBy); } return(conn.Query <R>(sql, param, tran, true, null, CommandType.Text).ToList()); }
private T QueryItem <T>(string key, object param, string[] conditions, bool activeOnly) where T : class, IDbQuery, new() { var sql = CommandTextCache.GetCachedCommandText(key); if (string.IsNullOrEmpty(sql)) { var dbEntity = DbEntityCache.GetDbEntity <T>(); var pairs = param as IDictionary <string, object>; var fields = pairs == null?ConvertHelper.ToKeys(param) : pairs.Keys; sql = CommandTextCache.GetSingleQueryCommandText(key, dbEntity, fields, conditions, activeOnly); } return(conn.QueryFirstOrDefault <T>(sql, param, tran, null, CommandType.Text)); }
public R GetValue <T, R>(string by, string column, object param, string[] conditions) where T : class, IDbQuery, new() { ThrowIfNull(param); var key = string.Format("{0}.GetValue.Param.{1}", typeof(T), by); var sql = CommandTextCache.GetCachedCommandText(key); if (string.IsNullOrEmpty(sql)) { var dbEntity = DbEntityCache.GetDbEntity <T>(); var fields = ConvertHelper.ToKeys(param); sql = CommandTextCache.GetValueCommandText(key, dbEntity, column, fields, conditions); } return(conn.ExecuteScalar <R>(sql, param, tran, null, CommandType.Text)); }
public void Update <T>(T item) where T : class, IDbEntity, IDbModel, new() { ThrowIfNull(item); var key = string.Format("{0}.Update", typeof(T)); var sql = CommandTextCache.GetCachedCommandText(key); if (string.IsNullOrEmpty(sql)) { var dbEntity = DbEntityCache.GetDbEntity <T>(item); var fields = ColumnField.GetFields(item); sql = CommandTextCache.GetUpdateCommandText(key, dbEntity, fields); } conn.Execute(sql, item, tran, null, CommandType.Text); }
private BulkCommandText GetBulkDeleteCommandText(Type type, string tableKey, string executionKey) { var tableCommandText = CommandTextCache.GetCachedCommandText(tableKey); var executionCommandText = CommandTextCache.GetCachedCommandText(executionKey); var commandText = new BulkCommandText(tableCommandText, executionCommandText); if (string.IsNullOrEmpty(tableCommandText) || string.IsNullOrEmpty(executionCommandText)) { var dbEntity = DbEntityCache.GetDbEntity(type); var dbColumns = GetDbColumns(dbEntity.Name).Where(dbColumn => !DbConst.BulkUpdateExcludedColumns.Contains(dbColumn.Name)); commandText.TableCommandText = CommandTextCache.GetBulkTableCommandText(tableKey, dbColumns); commandText.ExecutionCommandText = CommandTextCache.GetBulkDeleteCommandText(executionKey, dbEntity); } return(commandText); }
public IList <T> GetTop <T>(string by, int count, object param, string[] conditions, string orderBy) where T : class, IDbQuery, new() { ThrowIfNull(param); var format = count == 1 ? "{0}.GetTop.Param.{1}" : "{0}.GetTops.Param.{1}"; var key = string.Format(format, typeof(T), by); var sql = CommandTextCache.GetCachedCommandText(key); if (string.IsNullOrEmpty(sql)) { var dbEntity = DbEntityCache.GetDbEntity <T>(); var fields = ConvertHelper.ToKeys(param); sql = CommandTextCache.GetTopQueryCommandText(key, dbEntity, count, fields, conditions, orderBy); } return(conn.Query <T>(sql, param, tran, true, null, CommandType.Text).ToList()); }
public void Delete <T>(object param, IDbModel dbModel) where T : class, IDbEntity, IDbModel, new() { ThrowIfNull(param); var key = string.Format("{0}.Delete", typeof(T)); var sql = CommandTextCache.GetCachedCommandText(key); if (string.IsNullOrEmpty(sql)) { var dbEntity = DbEntityCache.GetDbEntity <T>(); sql = CommandTextCache.GetDeleteCommandText(key, dbEntity); } sql = sql.Replace("@UpdatedBy", dbModel.UpdatedBy.Value.ToString()); sql = sql.Replace("@UpdatedDate", dbModel.UpdatedDate.Value.ToString("yyyy-MM-dd HH:mm:ss.fff")); conn.Execute(sql, param, tran, null, CommandType.Text); }