예제 #1
0
        static void parseParameters(JProperty jpParameters, Method method, IDictionary<string, ParameterType> parameterTypes, Func<string, string> tokenLookup)
        {
            if (jpParameters.Value.Type != JTokenType.Object)
            {
                method.Errors.Add("The `parameters` property is expected to be of type object");
                return;
            }
            var joParameters = (JObject)jpParameters.Value;

            // Keep track of unique SQL parameter names:
            var sqlNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

            // Parse parameter properties:
            method.Parameters = new Dictionary<string, Parameter>(joParameters.Count, StringComparer.OrdinalIgnoreCase);
            foreach (var jpParam in joParameters.Properties())
            {
                if (jpParam.Value.Type != JTokenType.Object)
                {
                    method.Errors.Add("Parameter property '{0}' is expected to be of type object".F(jpParam.Name));
                    continue;
                }

                var joParam = (JObject)jpParam.Value;
                var sqlName = getString(joParam.Property("sqlName")).Interpolate(tokenLookup);
                var sqlType = getString(joParam.Property("sqlType")).Interpolate(tokenLookup);
                var typeName = getString(joParam.Property("type")).Interpolate(tokenLookup);
                var isOptional = getBool(joParam.Property("optional")) ?? false;
                var desc = getString(joParam.Property("description"));
                object defaultValue = DBNull.Value;

                // Assign a default `sqlName` if null:
                if (sqlName == null) sqlName = "@" + jpParam.Name;
                // TODO: validate sqlName is valid SQL parameter identifier!

                if (sqlNames.Contains(sqlName))
                {
                    method.Errors.Add("Duplicate SQL parameter name (`sqlName`): '{0}'".F(sqlName));
                    continue;
                }

                var param = new Parameter()
                {
                    Name = jpParam.Name,
                    SqlName = sqlName,
                    Description = desc,
                    IsOptional = isOptional
                };

                if (sqlType != null)
                {
                    int? length;
                    int? scale;
                    var typeBase = parseSqlType(sqlType, out length, out scale);
                    var sqlDbType = getSqlType(typeBase);
                    if (!sqlDbType.HasValue)
                    {
                        method.Errors.Add("Unknown SQL type name '{0}' for parameter '{1}'".F(typeBase, jpParam.Name));
                        continue;
                    }
                    else
                    {
                        param.SqlType = new ParameterType()
                        {
                            TypeBase = typeBase,
                            SqlDbType = sqlDbType.Value,
                            Length = length,
                            Scale = scale
                        };
                    }
                }
                else
                {
                    ParameterType paramType;
                    if (!parameterTypes.TryGetValue(typeName, out paramType))
                    {
                        method.Errors.Add("Could not find parameter type '{0}' for parameter '{1}'".F(typeName, jpParam.Name));
                        continue;
                    }
                    param.Type = paramType;
                }

                if (isOptional)
                {
                    var jpDefault = joParam.Property("default");
                    if (jpDefault != null)
                    {
                        // Parse the default value into a SqlValue:
                        param.DefaultSQLValue = jsonToSqlValue(jpDefault.Value, param.SqlType ?? param.Type);
                        param.DefaultCLRValue = jsonToCLRValue(jpDefault.Value, param.SqlType ?? param.Type);
                    }
                    else
                    {
                        // Use null:
                        param.DefaultSQLValue = DBNull.Value;
                        param.DefaultCLRValue = null;
                    }
                }

                method.Parameters.Add(jpParam.Name, param);
            }
        }
예제 #2
0
 internal ParameterSerialized(Parameter desc)
 {
     this.desc = desc;
 }