private bool TranslatesIntoDefaultConstraint(DefaultValue defaultValue)
 {
     return defaultValue != null && defaultValue.ValueType != DefaultValueType.RandomString;
 }
        private string SetDefaultValue(string tableName, string columnName, DefaultValue defaultValue)
        {
            if (!TranslatesIntoDefaultConstraint(defaultValue))
                return string.Empty;

            string constraintName = SqlSafeName("DF", tableName, columnName);
            return string.Format("ALTER TABLE [{0}] ADD CONSTRAINT [{1}] DEFAULT {2} FOR [{3}];", tableName, constraintName, GetDefaultValueText(defaultValue), columnName);
        }
        /// <summary>
        /// Deserialize a <see cref="DataFieldDescriptor"/>.
        /// </summary>
        /// <param name="element">Deserialized DataFieldDescriptor</param>
        /// <returns></returns>
        public static DataFieldDescriptor FromXml(XElement element)
        {
            if (element.Name != "DataFieldDescriptor")
            {
                throw new ArgumentException("The xml is not correctly formatted");
            }

            Guid       id         = (Guid)element.GetRequiredAttribute("id");
            string     name       = element.GetRequiredAttributeValue("name");
            bool       isNullable = (bool)element.GetRequiredAttribute("isNullable");
            int        position   = (int)element.GetRequiredAttribute("position");
            bool       inherited  = (bool)element.GetRequiredAttribute("inherited");
            XAttribute groupByPriorityAttribute = element.Attribute("groupByPriority");
            XAttribute instanceTypeAttribute    = element.GetRequiredAttribute("instanceType");
            XAttribute storeTypeAttribute       = element.GetRequiredAttribute("storeType");
            XAttribute isReadOnlyAttribute      = element.Attribute("isReadOnly");
            XAttribute newInstanceDefaultFieldValueAttribute = element.Attribute("newInstanceDefaultFieldValue");


            bool isReadOnly      = isReadOnlyAttribute != null && (bool)isReadOnlyAttribute;
            int  groupByPriority = groupByPriorityAttribute != null ? (int)groupByPriorityAttribute : 0;

            XAttribute defaultValueAttribute = element.Attribute("defaultValue");
            XAttribute foreignKeyReferenceTypeNameAttribute = element.Attribute("foreignKeyReferenceTypeName");
            XElement   formRenderingProfileElement          = element.Element("FormRenderingProfile");
            XElement   treeOrderingProfileElement           = element.Element("TreeOrderingProfile");
            XElement   validationFunctionMarkupsElement     = element.Element("ValidationFunctionMarkups");
            XElement   dataUrlProfileElement = element.Element("DataUrlProfile");
            XElement   searchProfileElement  = element.Element(nameof(SearchProfile));



            Type           instanceType = TypeManager.GetType(instanceTypeAttribute.Value);
            StoreFieldType storeType    = StoreFieldType.Deserialize(storeTypeAttribute.Value);

            var dataFieldDescriptor = new DataFieldDescriptor(id, name, storeType, instanceType, inherited)
            {
                IsNullable      = isNullable,
                Position        = position,
                GroupByPriority = groupByPriority,
                IsReadOnly      = isReadOnly
            };

            if (newInstanceDefaultFieldValueAttribute != null)
            {
                dataFieldDescriptor.NewInstanceDefaultFieldValue = newInstanceDefaultFieldValueAttribute.Value;
            }

            if (defaultValueAttribute != null)
            {
                DefaultValue defaultValue = DefaultValue.Deserialize(defaultValueAttribute.Value);
                dataFieldDescriptor.DefaultValue = defaultValue;
            }

            if (foreignKeyReferenceTypeNameAttribute != null)
            {
                string typeName = foreignKeyReferenceTypeNameAttribute.Value;

                typeName = TypeManager.FixLegasyTypeName(typeName);

                dataFieldDescriptor.ForeignKeyReferenceTypeName = typeName;
            }

            if (formRenderingProfileElement != null)
            {
                XAttribute labelAttribute                = formRenderingProfileElement.Attribute("label");
                XAttribute helpTextAttribute             = formRenderingProfileElement.Attribute("helpText");
                XAttribute widgetFunctionMarkupAttribute = formRenderingProfileElement.Attribute("widgetFunctionMarkup");

                var dataFieldFormRenderingProfile = new DataFieldFormRenderingProfile();

                if (labelAttribute != null)
                {
                    dataFieldFormRenderingProfile.Label = labelAttribute.Value;
                }

                if (helpTextAttribute != null)
                {
                    dataFieldFormRenderingProfile.HelpText = helpTextAttribute.Value;
                }

                if (widgetFunctionMarkupAttribute != null)
                {
                    dataFieldFormRenderingProfile.WidgetFunctionMarkup = widgetFunctionMarkupAttribute.Value;
                }

                dataFieldDescriptor.FormRenderingProfile = dataFieldFormRenderingProfile;
            }

            if (dataUrlProfileElement != null)
            {
                int order     = (int)dataUrlProfileElement.GetRequiredAttribute("Order");
                var formatStr = (string)dataUrlProfileElement.Attribute("Format");
                DataUrlSegmentFormat?format = null;

                if (formatStr != null)
                {
                    format = (DataUrlSegmentFormat)Enum.Parse(typeof(DataUrlSegmentFormat), formatStr);
                }

                dataFieldDescriptor.DataUrlProfile = new DataUrlProfile {
                    Order = order, Format = format
                };
            }

            if (searchProfileElement != null)
            {
                Func <string, bool> getAttr = fieldName => (bool)searchProfileElement.GetRequiredAttribute(CamelCase(fieldName));

                dataFieldDescriptor.SearchProfile = new SearchProfile
                {
                    IndexText     = getAttr(nameof(DynamicTypes.SearchProfile.IndexText)),
                    EnablePreview = getAttr(nameof(DynamicTypes.SearchProfile.EnablePreview)),
                    IsFacet       = getAttr(nameof(DynamicTypes.SearchProfile.IsFacet))
                };
            }

            if (treeOrderingProfileElement != null)
            {
                int? orderPriority   = (int?)treeOrderingProfileElement.Attribute("orderPriority");
                bool orderDescending = (bool)treeOrderingProfileElement.Attribute("orderDescending");

                dataFieldDescriptor.TreeOrderingProfile = new DataFieldTreeOrderingProfile
                {
                    OrderPriority   = orderPriority,
                    OrderDescending = orderDescending
                };
            }

            dataFieldDescriptor.ValidationFunctionMarkup = new List <string>();
            if (validationFunctionMarkupsElement != null)
            {
                foreach (XElement validationFunctionMarkupElement in validationFunctionMarkupsElement.Elements("ValidationFunctionMarkup"))
                {
                    string markup = validationFunctionMarkupElement.GetRequiredAttributeValue("markup");

                    dataFieldDescriptor.ValidationFunctionMarkup.Add(markup);
                }
            }

            return(dataFieldDescriptor);
        }
        private string GetDefaultValueText(DefaultValue defaultValue)
        {
            Verify.ArgumentNotNull(defaultValue, "defaultValue");

            switch (defaultValue.ValueType)
            {
                case DefaultValueType.DateTimeNow:
                    return "getdate()";
                case DefaultValueType.String:
                case DefaultValueType.Guid:
                    return "N" + SqlQuoted(defaultValue.Value);
                case DefaultValueType.NewGuid:
                    return "newid()";
                case DefaultValueType.Integer:
                    return defaultValue.Value.ToString();
                case DefaultValueType.Boolean:
                    return ((bool)defaultValue.Value ? "1" : "0");
                case DefaultValueType.DateTime:
                    return SqlQuoted(((DateTime)defaultValue.Value).ToString("yyyy-MM-dd HH:mm:ss"));
                case DefaultValueType.Decimal:
                    return ((decimal)defaultValue.Value).ToString("F", CultureInfo.InvariantCulture);
            }

            throw new NotImplementedException("Supplied DefaultValue contains an unsupported DefaultValueType '{0}'."
                                              .FormatWith(defaultValue.ValueType));
        }
Beispiel #5
0
        /// <exclude />
        public static DefaultValue Deserialize(string serializedData)
        {
            Verify.ArgumentNotNullOrEmpty(serializedData, "serializedData");

            using (TimerProfilerFacade.CreateTimerProfiler())
            {
                Dictionary <string, string> dic = StringConversionServices.ParseKeyValueCollection(serializedData);

                Verify.That(dic.ContainsKey("ValueType"), "Wrong serialized format");

                string valueTypeString = StringConversionServices.DeserializeValue <string>(dic["ValueType"]);
                var    valueType       = (DefaultValueType)Enum.Parse(typeof(DefaultValueType), valueTypeString);

                bool hasValue = dic.ContainsKey("Value");

                switch (valueType)
                {
                case DefaultValueType.Boolean:
                    Verify.That(hasValue, "Wrong serialized format");
                    bool boolValue = StringConversionServices.DeserializeValueBool(dic["Value"]);
                    return(DefaultValue.Boolean(boolValue));

                case DefaultValueType.DateTime:
                    Verify.That(hasValue, "Wrong serialized format");
                    DateTime dateTimeValue = StringConversionServices.DeserializeValueDateTime(dic["Value"]);
                    return(DefaultValue.DateTime(dateTimeValue));

                case DefaultValueType.DateTimeNow:
                    return(DefaultValue.Now);

                case DefaultValueType.Decimal:
                    Verify.That(hasValue, "Wrong serialized format");
                    decimal decimalValue = StringConversionServices.DeserializeValueDecimal(dic["Value"]);
                    return(DefaultValue.Decimal(decimalValue));

                case DefaultValueType.Guid:
                    Verify.That(hasValue, "Wrong serialized format");
                    Guid guidValue = StringConversionServices.DeserializeValueGuid(dic["Value"]);
                    return(DefaultValue.Guid(guidValue));

                case DefaultValueType.Integer:
                    Verify.That(hasValue, "Wrong serialized format");
                    int intValue = StringConversionServices.DeserializeValueInt(dic["Value"]);
                    return(DefaultValue.Integer(intValue));

                case DefaultValueType.NewGuid:
                    return(DefaultValue.NewGuid);

                case DefaultValueType.String:
                    string stringValue = null;
                    if (hasValue)
                    {
                        stringValue = StringConversionServices.DeserializeValueString(dic["Value"]);
                    }
                    return(DefaultValue.String(stringValue));

                case DefaultValueType.RandomString:
                    var settings = RandomStringSettings.Deserialize(dic);

                    return(new DefaultValue(settings));

                default:
                    throw new NotImplementedException("DefaultValueType = " + valueType);
                }
            }
        }