示例#1
0
        /// <summary>
        /// Returns a command for performing an update on an object
        /// </summary>
        /// <param name="item">The object to update</param>
        /// <returns></returns>
        public virtual IDbCommand GetUpdateCommand(object item)
        {
            DatabaseTypeInfo data      = TypeParser.GetTypeInfo(item.GetType());
            IDbCommand       toReturn  = _connection.GetCommand();
            StringBuilder    fieldList = new StringBuilder("UPDATE ");

            fieldList.Append(ResolveTableName(data));
            fieldList.Append(" SET ");
            bool addComma = false;

            foreach (DataFieldInfo dfi in data.DataFields)
            {
                if (!dfi.PrimaryKey && dfi.SetOnInsert)
                {
                    if (addComma)
                    {
                        fieldList.Append(",");
                    }
                    object value = dfi.Getter(item);

                    if (value != null)
                    {
                        string fName = GetParameterName(toReturn);
                        fieldList.Append(string.Concat(dfi.EscapedFieldName, "=", fName));
                        toReturn.Parameters.Add(_connection.GetParameter(fName, value));
                    }
                    else
                    {
                        fieldList.Append(string.Format(@"{0}=null", dfi.EscapedFieldName));
                    }
                    addComma = true;
                }
            }

            fieldList.Append(" WHERE ");
            for (int i = 0; i < data.PrimaryKeys.Count(); i++)
            {
                DataFieldInfo dField = data.PrimaryKeys.ElementAt(i);
                if (i > 0)
                {
                    fieldList.Append(" AND ");
                }

                string fName = GetParameterName(toReturn);
                fieldList.Append(string.Concat(dField.EscapedFieldName, "=", fName));
                toReturn.Parameters.Add(_connection.GetParameter(fName, dField.Getter(item)));
            }

            toReturn.CommandText = fieldList.ToString();
            return(toReturn);
        }