Beispiel #1
0
        private IList <T> QueryItems <T>(string column, object param, DataTable table, string[] conditions, string orderBy, bool activeOnly) where T : class, IDbQuery, new()
        {
            var dbEntity  = DbEntityCache.GetDbEntity <T>();
            var fields    = param == null ? new ColumnField[] { } : ColumnField.GetFields(param);
            var tableName = WriteToServer(table);
            var sql       = CommandTextCache.GetSplitQueryCommandText(tableName, column, dbEntity, fields, conditions, orderBy, activeOnly);

            return(conn.Query <T>(sql, param, tran, true, null, CommandType.Text).ToList());
        }
Beispiel #2
0
        public void Insert <T>(T item, Guid actedBy) where T : class, IDbEntity, IDbModel, new()
        {
            var dbEntity = DbEntityCache.GetDbEntity <T>();

            SetInsertAudit <T>(item, dbEntity, actedBy);

            this.GetMapper().Insert <T>(item);
            this.GetLogger().Add <T>(item, ActionType.Insert);
        }
Beispiel #3
0
        private static IDictionary <string, object> GetPrimaryKeyParams <T>(T item) where T : class, IDbQuery, new()
        {
            var pairs       = new Dictionary <string, object>();
            var primaryKeys = DbEntityCache.GetDbEntity <T>().PrimaryKeys.Split(',');

            var helpers = PropertyHelper.GetProperties(item);

            helpers.Where(helper => primaryKeys.Contains(helper.Name)).Each(helper => pairs[helper.Name] = helper.GetValue(item));

            return(pairs);
        }
Beispiel #4
0
        private IList <T> QueryItems <T>(string key, object param, string[] conditions, string orderBy, bool activeOnly) where T : class, IDbQuery, new()
        {
            var sql = CommandTextCache.GetCachedCommandText(key);

            if (string.IsNullOrEmpty(sql))
            {
                var dbEntity = DbEntityCache.GetDbEntity <T>();
                var fields   = param == null ? new ColumnField[] { } : ColumnField.GetFields(param);
                sql = CommandTextCache.GetMultiQueryCommandText(key, dbEntity, fields, conditions, orderBy, activeOnly);
            }

            return(conn.Query <T>(sql, param, tran, true, null, CommandType.Text).ToList());
        }
Beispiel #5
0
        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));
        }
Beispiel #6
0
        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());
        }
Beispiel #7
0
        public void BulkInsert <T>(IEnumerable <T> items, Guid actedBy) where T : class, IDbEntity, IDbModel, new()
        {
            if (items.Count() > 0)
            {
                var list = items.ToList();

                var dbEntity = DbEntityCache.GetDbEntity <T>();
                list.Each(item => SetInsertAudit <T>(item, dbEntity, actedBy));

                var table = BulkHelper <T> .ConvertToInsertTable(list);

                this.GetMapper().BulkInsert <T>(table);
                this.GetLogger().Add <T>(table, ActionType.Insert);
            }
        }
Beispiel #8
0
        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));
        }
Beispiel #9
0
        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));
        }
Beispiel #10
0
        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);
        }
Beispiel #11
0
        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);
        }
Beispiel #12
0
        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());
        }
Beispiel #13
0
        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);
        }
Beispiel #14
0
 private string GetTableName <T>() where T : class, IDbEntity, IDbModel, new()
 {
     return(string.Format("{0}_Log", DbEntityCache.GetDbEntity <T>().Name));
 }