Exemplo n.º 1
0
        /// <summary>
        /// Returns a generated array of <see cref="DbParameter"/>s
        /// that should be passed in with the command.
        /// </summary>
        /// <returns></returns>
        protected virtual DbParameter[] CreateParameters()
        {
            // add insert/update values
            var ret = new List <DbParameter>();

            foreach (var colmap_kvp in ModelMap.FieldMappings)
            {
                string fld = colmap_kvp.Key;
                DataModelColumnAttribute colmap = colmap_kvp.Value;
                DbParameter param = null;
                switch (StatementCommand)
                {
                case SqlStatement.INSERT:
                    if (string.IsNullOrEmpty(colmap.InsertParam))
                    {
                        continue;
                    }
                    if (colmap.IncludeOnInsert ||
                        colmap.ReturnAsOutputOnInsert)
                    {
                        param = Provider.DbFactory.CreateParameter();
                        param.ParameterName = !colmap.InsertParam.StartsWith("@")
                                                      ? "@" + colmap.InsertParam
                                                      : colmap.InsertParam;
                        if (colmap.ReturnAsOutputOnInsert)
                        {
                            param.Direction = ParameterDirection.Output;
                        }
                    }
                    break;

                case SqlStatement.UPDATE:
                    if (string.IsNullOrEmpty(colmap.UpdateParam))
                    {
                        continue;
                    }
                    bool add = true;
                    if (!CommandBuilder.SetAllValues)
                    {
                        add = DataModel.ModifiedProperties.Contains(fld);
                    }
                    if (add)
                    {
                        param = Provider.DbFactory.CreateParameter();
                        param.ParameterName = !colmap.UpdateParam.StartsWith("@")
                                                      ? "@" + colmap.UpdateParam
                                                      : colmap.UpdateParam;
                    }
                    break;
                }
                if (param != null && ret.Find(p => p.ParameterName == param.ParameterName) == null)
                {
                    if (param is SqlParameter)
                    {
                        ((SqlParameter)param).SqlDbType = colmap.SqlDbType;
                    }
                    else
                    {
                        param.DbType = colmap.DbType;
                    }
                    if (colmap.ColumnSize.HasValue)
                    {
                        param.Value = colmap.ColumnSize.Value;
                    }
                    else if (colmap.DataType == typeof(string))
                    {
                        param.Size = (DataModel[fld] ?? string.Empty).ToString().Length;
                    }
                    if (param.Direction == ParameterDirection.Input ||
                        param.Direction == ParameterDirection.InputOutput)
                    {
                        param.IsNullable = colmap.IsNullable;
                        param.Value      = DataModel[fld] ?? DBNull.Value;
                    }
                    ret.Add(param);
                }
            }

            foreach (var foreignMapping_kvp in ModelMap.ForeignModelMappings)
            {
                ForeignDataModelAttribute foreignMapping = foreignMapping_kvp.Value;
                if (foreignMapping.TargetMemberType.IsOrInherits(typeof(IEnumerable)))
                {
                    continue;
                }
                switch (StatementCommand)
                {
                case SqlStatement.INSERT:
                    var fieldValue = DataModel.ColumnMappedValue[foreignMapping.LocalColumn];
                    var paramName  = "@" + foreignMapping.LocalColumn;
                    if (ret.Find(p => p.ParameterName == paramName) == null)
                    {
                        if (ret.Find(p => p.ParameterName == paramName) != null)
                        {
                            continue;
                        }
                        var param = Provider.DbFactory.CreateParameter();
                        param.ParameterName = paramName;
                        if (param is SqlParameter)
                        {
                            ((SqlParameter)param).SqlDbType = foreignMapping.LocalColumnSqlDbType;
                        }
                        else
                        {
                            param.DbType = foreignMapping.LocalColumnDbType;
                        }
                        if (foreignMapping.LocalColumnSize.HasValue)
                        {
                            param.Value = foreignMapping.LocalColumnSize.Value;
                        }
                        else if (foreignMapping.TargetMemberType == typeof(string))
                        {
                            param.Size = (fieldValue ?? string.Empty).ToString().Length;
                        }
                        if (param.Direction == ParameterDirection.Input ||
                            param.Direction == ParameterDirection.InputOutput)
                        {
                            param.IsNullable = foreignMapping.LocalColumnIsNullable;
                            param.Value      = fieldValue ?? DBNull.Value;
                        }
                        ret.Add(param);
                    }
                    break;
                }
            }

            // add query conditions
            if (Query != null)
            {
                foreach (DataModelQueryCondition <TModel> cond in Query.Conditions)
                {
                    DataModelColumnAttribute fm = cond.FieldMap;
                    if (fm == null)
                    {
                        switch (cond.FindFieldMappingBy)
                        {
                        case FieldMappingKeyType.ClrMember:
                            fm = ModelMap[cond.EvalSubject];
                            break;

                        case FieldMappingKeyType.DbColumn:
                            fm = ModelMap.GetFieldMappingByDbColumnName(cond.EvalSubject);
                            break;
                        }
                    }
                    string paramName = string.Empty;
                    if (fm != null)
                    {
                        switch (StatementCommand)
                        {
                        case SqlStatement.SELECT:
                            if (string.IsNullOrEmpty(fm.SelectParam))
                            {
                                continue;
                            }
                            paramName = fm.SelectParam;
                            break;

                        case SqlStatement.INSERT:
                            if (string.IsNullOrEmpty(fm.InsertParam))
                            {
                                continue;
                            }
                            paramName = fm.InsertParam;
                            break;

                        case SqlStatement.UPDATE:
                            if (string.IsNullOrEmpty(fm.UpdateParam))
                            {
                                continue;
                            }
                            paramName = fm.UpdateParam;
                            break;

                        case SqlStatement.DELETE:
                            if (string.IsNullOrEmpty(fm.DeleteParam))
                            {
                                continue;
                            }
                            paramName = fm.DeleteParam;
                            break;
                        }
                        if (ret.Find(p => p.ParameterName == paramName) == null)
                        {
                            if (UsesSproc && cond.CompareOp != Compare.Equal)
                            {
                                throw new InvalidOperationException(
                                          "Cannot produce an ad hoc WHERE clause with a SQL Stored Procedure.");
                            }
                            DbParameter param = Provider.DbFactory.CreateParameter();
                            param.ParameterName = !paramName.StartsWith("@")
                                                      ? "@" + paramName
                                                      : paramName;
                            if (param is SqlParameter)
                            {
                                ((SqlParameter)param).SqlDbType = fm.SqlDbType;
                            }
                            else
                            {
                                param.DbType = fm.DbType;
                            }
                            if (fm.ColumnSize.HasValue)
                            {
                                param.Value = fm.ColumnSize.Value;
                            }
                            else if (fm.DataType == typeof(string))
                            {
                                fm.ColumnSize =
                                    (cond.CompareValue ?? string.Empty).ToString().Length;
                            }
                            param.IsNullable = fm.IsNullable;
                            param.Value      = cond.CompareValue ?? DBNull.Value;
                            ret.Add(param);
                        }
                    }
                    else
                    {
                        if (cond.CompareValue != null)
                        {
                            paramName = "@" + cond.EvalSubject;
                            if (ret.Find(p => p.ParameterName == paramName) == null)
                            {
                                DbParameter param = Provider.DbFactory.CreateParameter();
                                param.ParameterName = paramName;
                                if (param is SqlParameter)
                                {
                                    ((SqlParameter)param).SqlDbType
                                        = DbTypeConverter.ToSqlDbType(cond.CompareValue);
                                }
                                else
                                {
                                    param.DbType = DbTypeConverter.ToDbType(cond.CompareValue);
                                }
                                if (cond.CompareValue is string)
                                {
                                    param.Size = ((string)cond.CompareValue).Length;
                                }
                                param.IsNullable = true;
                                param.Value      = cond.CompareValue;
                                ret.Add(param);
                            }
                        }
                    }
                }
            }
            return(ret.ToArray());
        }