Example #1
0
        private List <DiffLogTableInfo> GetDiffTable(string sql, List <SugarParameter> parameters)
        {
            List <DiffLogTableInfo> result = new List <DiffLogTableInfo>();
            var whereSql = Regex.Replace(sql, ".* WHERE ", "", RegexOptions.Singleline);
            var dt       = this.Context.Queryable <T>().Where(whereSql).AddParameters(parameters).ToDataTable();

            if (dt.Rows != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    DiffLogTableInfo item = new DiffLogTableInfo();
                    item.TableDescription = this.EntityInfo.TableDescription;
                    item.TableName        = this.EntityInfo.DbTableName;
                    item.Columns          = new List <DiffLogColumnInfo>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        DiffLogColumnInfo addItem = new DiffLogColumnInfo();
                        addItem.Value             = row[col.ColumnName];
                        addItem.ColumnName        = col.ColumnName;
                        addItem.ColumnDescription = this.EntityInfo.Columns.Where(it => it.DbColumnName != null).First(it => it.DbColumnName.Equals(col.ColumnName, StringComparison.CurrentCultureIgnoreCase)).ColumnDescription;
                        item.Columns.Add(addItem);
                    }
                    result.Add(item);
                }
            }
            return(result);
        }
        private List <DiffLogTableInfo> GetDiffTableByEntity()
        {
            List <SugarParameter>   parameters = new List <SugarParameter>();
            List <DiffLogTableInfo> result     = new List <DiffLogTableInfo>();
            var dt2 = this.Context.Utilities.ListToDataTable <T>(this.InsertObjs.ToList());

            foreach (DataRow row in dt2.Rows)
            {
                DiffLogTableInfo item = new DiffLogTableInfo();
                item.TableDescription = this.EntityInfo.TableDescription;
                item.TableName        = this.EntityInfo.DbTableName;
                item.Columns          = new List <DiffLogColumnInfo>();
                foreach (DataColumn col in dt2.Columns)
                {
                    var sugarColumn = this.EntityInfo.Columns.Where(it => it.DbColumnName != null).FirstOrDefault(it =>
                                                                                                                  it.DbColumnName.Equals(col.ColumnName, StringComparison.CurrentCultureIgnoreCase));
                    DiffLogColumnInfo addItem = new DiffLogColumnInfo();
                    addItem.Value             = row[col.ColumnName];
                    addItem.ColumnName        = col.ColumnName;
                    addItem.IsPrimaryKey      = sugarColumn?.IsPrimarykey ?? false;
                    addItem.ColumnDescription = sugarColumn?.ColumnDescription;
                    item.Columns.Add(addItem);
                }
                result.Add(item);
            }
            return(result);
        }
Example #3
0
        private List <DiffLogTableInfo> GetDiffTable(string sql, List <SugarParameter> parameters)
        {
            List <DiffLogTableInfo> result = new List <DiffLogTableInfo>();
            DataTable dt = null;

            if (this.UpdateParameterIsNull)
            {
                var whereSql = Regex.Replace(sql, ".* WHERE ", "", RegexOptions.Singleline);
                if (sql.Contains("WHERE (EXISTS"))
                {
                    whereSql = Regex.Match(sql, @"\(EXISTS.+").Value;
                }
                dt = this.Context.Queryable <T>().Where(whereSql).AddParameters(parameters).ToDataTable();
            }
            else
            {
                if (this.UpdateObjs.ToList().Count == 0)
                {
                    dt = new DataTable();
                }
                else
                {
                    dt = this.Context.Queryable <T>().WhereClassByPrimaryKey(this.UpdateObjs.ToList()).ToDataTable();
                }
            }
            if (dt.Rows != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    DiffLogTableInfo item = new DiffLogTableInfo();
                    item.TableDescription = this.EntityInfo.TableDescription;
                    item.TableName        = this.EntityInfo.DbTableName;
                    item.Columns          = new List <DiffLogColumnInfo>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        var sugarColumn = this.EntityInfo.Columns.Where(it => it.DbColumnName != null).First(it =>
                                                                                                             it.DbColumnName.Equals(col.ColumnName, StringComparison.CurrentCultureIgnoreCase));
                        DiffLogColumnInfo addItem = new DiffLogColumnInfo();
                        addItem.Value             = row[col.ColumnName];
                        addItem.ColumnName        = col.ColumnName;
                        addItem.IsPrimaryKey      = sugarColumn.IsPrimarykey;
                        addItem.ColumnDescription = sugarColumn.ColumnDescription;
                        item.Columns.Add(addItem);
                    }
                    result.Add(item);
                }
            }
            return(result);
        }
Example #4
0
        private List <DiffLogTableInfo> GetDiffTable(string sql, long?identity)
        {
            List <SugarParameter>   parameters = new List <SugarParameter>();
            List <DiffLogTableInfo> result     = new List <DiffLogTableInfo>();
            var whereSql = string.Empty;
            List <IConditionalModel> cons = new List <IConditionalModel>();

            if (identity != null && identity > 0 && GetIdentityKeys().HasValue())
            {
                var fieldName = GetIdentityKeys().Last();
                cons.Add(new ConditionalModel()
                {
                    ConditionalType = ConditionalType.Equal, FieldName = fieldName, FieldValue = identity.ToString()
                });
            }
            else
            {
                foreach (var item in this.EntityInfo.Columns.Where(it => it.IsIgnore == false && GetPrimaryKeys().Any(pk => pk.Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase))))
                {
                    var fielddName = item.DbColumnName;
                    var fieldValue = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName == item.PropertyName).PropertyInfo.GetValue(this.InsertObjs.Last(), null).ObjToString();
                    cons.Add(new ConditionalModel()
                    {
                        ConditionalType = ConditionalType.Equal, FieldName = fielddName, FieldValue = fieldValue
                    });
                }
            }
            Check.Exception(cons.IsNullOrEmpty(), "Insertable.EnableDiffLogEvent need primary key");
            var sqlable = this.SqlBuilder.ConditionalModelToSql(cons);

            whereSql = sqlable.Key;
            parameters.AddRange(sqlable.Value);
            var dt = this.Context.Queryable <T>().Where(whereSql).AddParameters(parameters).ToDataTable();

            if (dt.Rows != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    DiffLogTableInfo item = new DiffLogTableInfo();
                    item.TableDescription = this.EntityInfo.TableDescription;
                    item.TableName        = this.EntityInfo.DbTableName;
                    item.Columns          = new List <DiffLogColumnInfo>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        DiffLogColumnInfo addItem = new DiffLogColumnInfo();
                        addItem.Value             = row[col.ColumnName];
                        addItem.ColumnName        = col.ColumnName;
                        addItem.ColumnDescription = this.EntityInfo.Columns.Where(it => it.DbColumnName != null).First(it => it.DbColumnName.Equals(col.ColumnName, StringComparison.CurrentCultureIgnoreCase)).ColumnDescription;
                        item.Columns.Add(addItem);
                    }
                    result.Add(item);
                }
                return(result);
            }
            else
            {
                DiffLogTableInfo diffTable = new DiffLogTableInfo();
                diffTable.TableName        = this.EntityInfo.DbTableName;
                diffTable.TableDescription = this.EntityInfo.TableDescription;
                diffTable.Columns          = this.EntityInfo.Columns.Where(it => it.IsIgnore == false).Select(it => new DiffLogColumnInfo()
                {
                    ColumnDescription = it.ColumnDescription,
                    ColumnName        = it.DbColumnName,
                    Value             = it.PropertyInfo.GetValue(this.InsertObjs.Last(), null)
                }).ToList();
                return(new List <DiffLogTableInfo>()
                {
                    diffTable
                });
            }
        }