예제 #1
0
        private void AddParameterToQuery(string name, string parameter, DBQuery query)
        {
            const string ParameterTypeString   = "string";
            const string ParameterTypeNumber   = "number";
            const string ParameterTypeDateTime = "date";
            const string ParameterTypeObjectId = "objectid";
            const string ParameterTypeNull     = "null";
            const string ParameterTypeBool     = "bool";

            int split = parameter.IndexOf(":");

            if (split == -1)
            {
                return;
            }

            string type  = parameter.Substring(0, split);
            string value = parameter.Substring(split + 1);

            if (type == ParameterTypeString)
            {
                query.AddParameter(name, value);
            }
            else if (type == ParameterTypeNumber)
            {
                double number = 0;
                try
                {
                    number = Convert.ToDouble(value, CultureInfo.InvariantCulture);
                }
                catch (Exception)
                {
                    throw new RuntimeException("Invalid number value", new string[] { parameter });
                }

                query.AddParameter(name, number);
            }
            else if (type == ParameterTypeDateTime)
            {
                MC2DateTimeValue dateTime = null;

                try
                {
                    dateTime = (MC2DateTimeValue)MC2DateTimeValue.TryConvertValueFromString(value);
                }
                catch (Exception)
                {
                    throw new RuntimeException("Invalid date time value", new string[] { parameter });
                }

                query.AddParameter(name, dateTime);
            }
            else if (type == ParameterTypeObjectId)
            {
                if (string.IsNullOrEmpty(value))
                {
                    // Add empty object id if value is empty
                    query.AddParameter(name, new ObjectId());
                }
                else
                {
                    ObjectId objectId = new ObjectId();

                    try
                    {
                        objectId = new ObjectId(value);
                    }
                    catch (Exception)
                    {
                        throw new RuntimeException("Invalid object id value", new string[] { parameter });
                    }

                    query.AddParameter(name, objectId);
                }
            }
            else if (type == ParameterTypeNull)
            {
                query.AddNullParameter(name);
            }
            else if (type == ParameterTypeBool)
            {
                if (value == "true")
                {
                    query.AddParameter(name, true);
                }
                else if (value == "false")
                {
                    query.AddParameter(name, false);
                }
                else
                {
                    throw new RuntimeException("Invalid value for boolean parameter", new string[] { name, value });
                }
            }
        }