示例#1
0
        internal ExecuteSqlParamsContext UpdateInternal(object bindedObj = null, IDictionary <string, object> _PrimaryKeyValues = null, params string[] ignoreProperties)
        {
            var pd = pocoData;

            if (bindedObj != null)//自动绑定对象
            {
                BindPropertiesExts.BindPropertiesFromForDb(trackedObject, bindedObj, pd, ignoreProperties);
            }
            var    columns = UpdatedColumns();
            object poco    = trackedObject;

            //int defaultValue = 0;
            if (columns != null && !columns.Any())
            {
                return(null);
            }

            var    sb          = new StringBuilder();
            var    index       = 0;
            string paramPrefix = "";
            var    pkCols      = pd.Properties.Where(p => p.IsPrimaryKey).ToList();
            IDictionary <string, object> PrimaryKeyValues = new Dictionary <string, object>();

            if (_PrimaryKeyValues != null && _PrimaryKeyValues.Count > 0)
            {
                PrimaryKeyValues = _PrimaryKeyValues;
            }
            else
            {
                if (pkCols == null || pkCols.Count == 0)
                {
                    throw new ArgumentException("当前对象没有主键情况下,需要指定更新的主键值");
                }
                else
                {
                    var Properties = pd.Properties.Select(p => p.PropertyInfo);// poco.GetType().GetProperties();
                    foreach (var pk in pkCols)
                    {
                        string pName       = pk.Name;
                        string pColumnName = pk.Name;//  pk.ColumnName;
                        var    o           = Properties.FirstOrDefault(y => string.Equals(pName, y.Name, StringComparison.OrdinalIgnoreCase));
                        if (o != null)
                        {
                            var pValue = o.GetValue(poco, null);
                            PrimaryKeyValues.Add(pColumnName, pValue);
                        }
                    }
                }
            }


            IDictionary <string, object> parameters = new Dictionary <string, object>();


            var waitToUpdated = pd.Properties.Where(p => columns.Contains(p.ColumnName) || p.IsVersionColumn == true);

            foreach (var pocoColumn in waitToUpdated)
            {
                string properyName = pocoColumn.Name;//pocoColumn.ColumnName;

                // Don't update the primary key, but grab the value if we don't have it
                if (_PrimaryKeyValues == null && PrimaryKeyValues.ContainsKey(properyName))
                {
                    continue;
                }

                // Dont update result only columns
                if (pocoColumn.Ignored || pocoColumn.IsReadOnly || pocoColumn.IsPrimaryKey)
                {
                    continue;
                }

                var colType = pocoColumn.PropertyInfo.PropertyType;

                object value = pocoColumn.PropertyInfo.GetValue(poco, null);// GetColumnValue(pd, poco, ProcessMapper);
                if (pocoColumn.IsVersionColumn)
                {
                    if (colType == typeof(int) || colType == typeof(Int32))
                    {
                        value = Convert.ToInt32(value) + 1;
                    }
                    else if (colType == typeof(long) || colType == typeof(Int64))
                    {
                        value = Convert.ToInt64(value) + 1;
                    }
                    else if (colType == typeof(short) || colType == typeof(Int16))
                    {
                        value = Convert.ToInt16(value) + 1;
                    }
                }
                else
                {
                    if (Snapshotter.GetAutoFilterEmptyValueColumnsWhenTrack())
                    {
                        var defaultValueOfCol = ReflectionHelper.GetDefaultValueForType(colType);
                        if (object.Equals(defaultValueOfCol, value))//过滤空值列
                        {
                            continue;
                        }
                    }
                }



                // Build the sql
                if (index > 0)
                {
                    sb.Append(", ");
                }

                //string paramName = paramPrefix + index.ToString();// pocoColumn.Name;
                string paramName = paramPrefix + properyName;
                sb.AppendFormat("{0} = {1}{2}", db.SqlGenerator.GetColumnName(pd, pocoColumn, false), db.SqlGenerator.Configuration.Dialect.ParameterPrefix, paramName);

                parameters.Add(paramName, value);
                index++;
            }


            if (columns != null && columns.Any() && sb.Length == 0)
            {
                throw new ArgumentException("There were no columns in the columns list that matched your table", "columns");
            }

            var sql      = string.Format("UPDATE {0} SET {1} WHERE {2}", db.SqlGenerator.GetTableName(pd), sb, BuildWhereSql(db, pd, PrimaryKeyValues, paramPrefix, ref index));
            int temIndex = parameters.Count;

            foreach (var item in PrimaryKeyValues)
            {
                parameters.Add(paramPrefix + item.Key, item.Value);
                //parameters.Add(paramPrefix + temIndex, item.Value);
                temIndex++;
            }


            if (db.DatabaseType == DatabaseType.Oracle && LobConverter.Enable == true)
            {
                IDictionary <string, object> dictParameters = parameters;
                int convertCount = LobConverter.UpdateDynamicParameterForLobColumn(pd, dictParameters);
                if (convertCount > 0)
                {
                    //return db.Execute(sql, dictParameters);
                    return(new ExecuteSqlParamsContext(sql, dictParameters));
                }
                else
                {
                    //执行原始SQL
                    //return db.Execute(sql, parameters);

                    return(new ExecuteSqlParamsContext(sql, parameters));
                }
            }
            else
            {
                //执行原始SQL
                //return db.Execute(sql, parameters);
                return(new ExecuteSqlParamsContext(sql, parameters));
            }
        }