Ejemplo n.º 1
0
        public DataParameterBuilder(Type queryType)
        {
            _queryType = Verify.ArgumentNotNull(queryType, "queryType");

            PropertyInfo[] properties = queryType.GetProperties();
            Dictionary<string, DataParameterInfo> namedParameters = null;
            Dictionary<int, DataParameterInfo> orderedParameters = null;

            foreach (PropertyInfo property in properties)
            {
                ParameterAttribute attribute = GetParameterAttribute(property);
                if (attribute.Ignore)
                {
                    continue;
                }

                if (DataParameterTypeMapping.ForType(property.PropertyType) == null)
                {
                    _errors.Add("Cannot handle property types of " + property.PropertyType.Name);
                }

                if (attribute.ParameterOrder > 0)
                {
                    if (orderedParameters == null)
                    {
                        orderedParameters = new Dictionary<int, DataParameterInfo>();
                    }

                    if (orderedParameters.ContainsKey(attribute.ParameterOrder))
                    {
                        string message = string.Format("Properties {0} and {1} both have a parameter order of {2}",
                                                       orderedParameters[attribute.ParameterOrder].Property.Name,
                                                       property.Name,
                                                       attribute.ParameterOrder);
                        _errors.Add(message);
                    }
                    orderedParameters[attribute.ParameterOrder] = new DataParameterInfo(attribute, property);
                }
                else
                {
                    if (namedParameters == null)
                    {
                        namedParameters = new Dictionary<string, DataParameterInfo>();
                    }
                    string parameterName = SanitizeParameterName(attribute.ParameterName);
                    if (namedParameters.ContainsKey(parameterName))
                    {
                        _errors.Add("Parameter name is defined more than once: " + parameterName);
                    }
                    namedParameters[parameterName] = new DataParameterInfo(attribute, property);
                }
            }

            if (namedParameters != null && orderedParameters != null)
            {
                _errors.Add("Cannot have a mix of named properties and ordered properties");
            }

            if (namedParameters != null)
            {
                _namedParameters = namedParameters;
            }
            else if (orderedParameters != null)
            {
                var array = new DataParameterInfo[orderedParameters.Count];
                for (int index = 0; index < array.Length; ++index)
                {
                    int propertyOrder = index + 1;
                    DataParameterInfo property;
                    if (orderedParameters.TryGetValue(propertyOrder, out property))
                    {
                        array[index] = property;
                    }
                    else
                    {
                        _errors.Add("Missing property with order of " + propertyOrder);
                    }
                }

                _orderedParameters = array;
            }
        }
Ejemplo n.º 2
0
        private static string GetParameterNamePrefix(IDbCommand cmd, DataParameterInfo parameterInfo)
        {
            string typeName = cmd.GetType().FullName;
            if (typeName.StartsWith("System.Data.SqlClient") ||
                typeName.StartsWith("System.Data.SqlServerCe"))
            {
                return "@";
            }
            if (typeName.StartsWith("MySql"))
            {
                return "?";
            }

            if (!Char.IsLetterOrDigit(parameterInfo.Attribute.ParameterName[0]))
            {
                return parameterInfo.Attribute.ParameterName[0].ToString();
            }

            // TODO: probably need to look into postgres, firebird, etc
            return ":";
        }