Exemplo n.º 1
0
        protected virtual string ToUpdateStatement(T item, bool excludeDefaults)
        {
            var setFields = new StringBuilder();

            foreach (var fieldDef in modelDef.Columns)
            {
                if (Context.UpdateFields.Count > 0 && !Context.UpdateFields.Contains(fieldDef.Value.MemberInfo.Name))
                {
                    continue;                                                                                                   // added
                }
                object value = fieldDef.Value.GetValue(item);
                if (_database.Mapper != null)
                {
                    var converter = _database.Mapper.GetToDbConverter(fieldDef.Value.ColumnType, fieldDef.Value.MemberInfo.GetMemberInfoType());
                    if (converter != null)
                    {
                        value = converter(value);
                    }
                }

                if (excludeDefaults && (value == null || value.Equals(PocoData.GetDefault(value.GetType()))))
                {
                    continue;                                                                                           //GetDefaultValue?
                }
                if (setFields.Length > 0)
                {
                    setFields.Append(", ");
                }

                setFields.AppendFormat("{0} = {1}", _databaseType.EscapeSqlIdentifier(fieldDef.Value.ColumnName), CreateParam(value));
            }

            return(string.Format("UPDATE {0} SET {1} {2}", _databaseType.EscapeTableName(modelDef.TableInfo.TableName), setFields, WhereExpression));
        }