Exemplo n.º 1
0
        /// <summary>
        /// Update all items matching WHERE clause using fields from the item sent in.
        /// If `keys` has been specified on the current Mighty instance then any primary key fields in the item are ignored.
        /// The item is not filtered to remove fields not in the table, if you need that you can call <see cref="New"/> with first parameter `partialItem` and second parameter `false` first.
        /// </summary>
        /// <param name="partialItem">Item containing values to update with</param>
        /// <param name="where">WHERE clause specifying which rows to update</param>
        /// <param name="connection">Optional connection to use</param>
        /// <param name="inParams">Named input parameters</param>
        /// <param name="args">Auto-numbered input parameters</param>
        protected int UpdateUsingWithParams(object partialItem, string where,
                                            DbConnection connection,
                                            object inParams,
                                            params object[] args)
        {
            var updateValues          = new StringBuilder();
            var partialItemParameters = new NameValueTypeEnumerator(DataContract, partialItem);
            // TO DO: Test that this combinedInputParams approach works
            var combinedInputParams = inParams?.ToExpando() ?? new ExpandoObject();
            var toDict = combinedInputParams.ToDictionary();
            int i      = 0;

            foreach (var paramInfo in partialItemParameters)
            {
                if (!PrimaryKeyInfo.IsKey(paramInfo.Name))
                {
                    if (i > 0)
                    {
                        updateValues.Append(", ");
                    }
                    updateValues.Append(paramInfo.Name).Append(" = ").Append(Plugin.PrefixParameterName(paramInfo.Name));
                    i++;

                    toDict.Add(paramInfo.Name, paramInfo.Value);
                }
            }
            var sql    = Plugin.BuildUpdate(CheckGetTableName(), updateValues.ToString(), where);
            var retval = ExecuteWithParams(sql, args: args, inParams: combinedInputParams, outParams: new { __rowcount = new RowCount() }, connection: connection);

            return(retval.__rowcount);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Make a new item, with optional passed-in name-value collection as initialiser.
        /// </summary>
        /// <param name="connection">The connection to use</param>
        /// <param name="nameValues">The name-value collection</param>
        /// <param name="addNonPresentAsDefaults">
        /// If true also include default values for fields not present in the collection
        /// but which exist in columns for the current table in Mighty, which correctly
        /// reflect the defaults of the current database table.
        /// </param>
        /// <returns></returns>
        override public T New(DbConnection connection, object nameValues = null, bool addNonPresentAsDefaults = true)
        {
            var nvtEnumerator = new NameValueTypeEnumerator(DataContract, nameValues);
            Dictionary <string, object> columnNameToValue = new Dictionary <string, object>();

            foreach (var nvtInfo in nvtEnumerator)
            {
                if (DataContract.TryGetColumnName(nvtInfo.Name, out string columnName))
                {
                    columnNameToValue.Add(columnName, nvtInfo.Value);
                }
            }
            object item;
            IDictionary <string, object> newItemDictionary = null;

            if (!IsGeneric)
            {
                item = new ExpandoObject();
                newItemDictionary = ((ExpandoObject)item).ToDictionary();
            }
            else
            {
                item = new T();
            }
            // drive the loop by the actual column names
            foreach (var columnInfo in GetTableMetaData(connection))
            {
                if (!columnInfo.IS_MIGHTY_COLUMN)
                {
                    continue;
                }
                string columnName = columnInfo.COLUMN_NAME;
                object value;
                if (!columnNameToValue.TryGetValue(columnName, out value))
                {
                    if (!addNonPresentAsDefaults)
                    {
                        continue;
                    }
                    value = Plugin.GetColumnDefault(columnInfo);
                }
                if (value != null)
                {
                    if (IsGeneric)
                    {
                        DataContract.GetDataMemberInfo(columnName).SetValue(item, value);
                    }
                    else
                    {
                        newItemDictionary.Add(columnName, value);
                    }
                }
            }
            return((T)item);
        }