/// <summary>
 /// Append the property definition
 /// </summary>
 /// <param name="propertyDefinitions"></param>
 private void AppendPropertyDefinition(IList <PropertyConfigurationType> propertyDefinitions)
 {
     for (int i = 0; i < propertyDefinitions.Count; i++)
     {
         PropertyConfigurationType propertyDefinition = propertyDefinitions[i];
         PropertyInfo pi = objectType.GetProperty(propertyDefinition.Name);
         if (pi == null)
         {
             throw new TypeLoadException(
                       string.Format(ErrorNoPropertyExists, propertyDefinition.Name, objectType.Name));
         }
         if (!string.IsNullOrEmpty(propertyDefinition.Ref))
         {
             var refType = GetRefType(propertyDefinition.Ref, pi.PropertyType);
             if (refType == null)
             {
                 throw new TypeLoadException(
                           string.Format(ErrorSettingRefProperty, propertyDefinition.Name, objectType.Name));
             }
             propertyRefTypes.Add(refType);
             properties.Add(pi);
             propertyValueTypes.Add(refType.GetType());
         }
         else if (TypeSerializer.CanCreateFromString(pi.PropertyType))
         {
             properties.Add(pi);
             propertyValueTypes.Add(pi.PropertyType);
         }
         else
         {
             throw new TypeLoadException(
                       string.Format(ErrorPropertyTypeNotSupported, propertyDefinition.Name, objectType.Name));
         }
     }
 }
 protected override string GetUndefinedColumnDefinition(Type fieldType, int?fieldLength)
 {
     if (TypeSerializer.CanCreateFromString(fieldType))
     {
         return(string.Format(StringLengthColumnDefinitionFormat, fieldLength.HasValue ? fieldLength.Value.ToString() : "MAX"));
     }
     return(base.GetUndefinedColumnDefinition(fieldType, fieldLength));
 }
        public void Can_convert_to_nullable_datetime()
        {
            Assert.That(TypeSerializer.CanCreateFromString(typeof(DateTime?)), Is.True);

            DateTime?dateValue   = new DateTime(1979, 5, 9);
            var      actualValue = TypeSerializer.DeserializeFromString <DateTime?>("1979-05-09");

            Assert.That(actualValue, Is.EqualTo(dateValue));
        }
        public void Can_convert_to_nullable_enum()
        {
            Assert.That(TypeSerializer.CanCreateFromString(typeof(TestEnum?)), Is.True);

            TestEnum?enumValue   = TestEnum.EnumValue1;
            var      actualValue = TypeSerializer.DeserializeFromString <TestEnum?>(enumValue.ToString());

            Assert.That(actualValue, Is.EqualTo(enumValue));
        }
        public void Can_convert_string_collection()
        {
            Assert.That(TypeSerializer.CanCreateFromString(typeof(string[])), Is.True);

            var stringValue    = TypeSerializer.SerializeToString(stringValues);
            var expectedString = "[" + string.Join(",", stringValues.ToArray()) + "]";

            Assert.That(stringValue, Is.EqualTo(expectedString));
        }
        protected virtual string GetUndefinedColumnDefinition(Type fieldType, int?fieldLength)
        {
            if (TypeSerializer.CanCreateFromString(fieldType))
            {
                return(string.Format(StringLengthColumnDefinitionFormat, fieldLength.GetValueOrDefault(DefaultStringLength)));
            }

            throw new NotSupportedException(
                      string.Format("Property of type: {0} is not supported", fieldType.FullName));
        }
        public void Can_convert_to_Byte_array()
        {
            Assert.That(TypeSerializer.CanCreateFromString(typeof(byte[])), Is.True);

            var byteArrayValue  = new byte[] { 0, 65, 97, 255, };
            var byteArrayString = TypeSerializer.SerializeToString(byteArrayValue);
            var actualValue     = TypeSerializer.DeserializeFromString <byte[]>(byteArrayString);

            Assert.That(actualValue, Is.EqualTo(byteArrayValue));
        }
        public void Can_convert_Guid()
        {
            Assert.That(TypeSerializer.CanCreateFromString(typeof(Guid)), Is.True);

            var guidValue      = Guid.NewGuid();
            var stringValue    = TypeSerializer.SerializeToString(guidValue);
            var expectedString = guidValue.ToString("N");

            Assert.That(stringValue, Is.EqualTo(expectedString));
        }
        protected virtual string GetUndefinedColumnDefintion(Type fieldType)
        {
            if (TypeSerializer.CanCreateFromString(fieldType))
            {
                return(this.StringColumnDefinition);
            }

            throw new NotSupportedException(
                      string.Format("Property of type: {0} is not supported", fieldType.FullName));
        }
        public void Can_convert_comma_delimited_string_to_List_String()
        {
            Assert.That(TypeSerializer.CanCreateFromString(typeof(List <string>)), Is.True);

            var stringValueList = "[" + string.Join(",", stringValues) + "]";

            var convertedJsvValues = TypeSerializer.DeserializeFromString <List <string> >(stringValueList);

            Assert.That(convertedJsvValues, Is.EquivalentTo(stringValues));

            var convertedJsonValues = JsonSerializer.DeserializeFromString <List <string> >(stringValueList);

            Assert.That(convertedJsonValues, Is.EquivalentTo(stringValues));
        }
        /// <summary>
        /// Find the right constructor for the matching types
        /// </summary>
        /// <param name="constructorArgDefinitions"></param>
        private void AppendConstructorDefinition(IList <PropertyConfigurationType> constructorArgDefinitions)
        {
            foreach (ConstructorInfo ci in objectType.GetConstructors())
            {
                ParameterInfo[] constructorParams = ci.GetParameters();

                //We can only call a constructor that has the right number of arguments.
                bool possibleMatch = constructorParams.Length == constructorArgDefinitions.Count;
                if (possibleMatch)
                {
                    for (int i = 0; i < constructorParams.Length; i++)
                    {
                        ParameterInfo             constructorParam         = constructorParams[i];
                        PropertyConfigurationType constructorArgDefinition = constructorArgDefinitions[i];

                        if (!string.IsNullOrEmpty(constructorArgDefinition.Ref))
                        {
                            RefType refType = GetRefType(constructorArgDefinition.Ref, constructorParam.ParameterType);
                            constructorRefTypes.Add(refType);
                            constructorValueTypes.Add(refType.GetType());
                        }
                        else if (TypeSerializer.CanCreateFromString(constructorParam.ParameterType))
                        {
                            constructorValueTypes.Add(constructorParam.ParameterType);
                        }
                        else
                        {
                            break;
                        }
                    }

                    bool matchingConstructorFound = constructorValueTypes.Count == constructorParams.Length;
                    if (matchingConstructorFound)
                    {
                        constructorInfo = ci;
                        return;
                    }
                }
            }
            //if it got this far no matching constructor *that we can use* has been found
            throw new TypeLoadException(string.Format(ErrorNoMatchingConstructor, objectType.Name));
        }
        public virtual string GetQuotedValue(object value, Type fieldType)
        {
            if (value == null)
            {
                return("NULL");
            }

            if (!fieldType.UnderlyingSystemType.IsValueType && fieldType != typeof(string))
            {
                if (TypeSerializer.CanCreateFromString(fieldType))
                {
                    return("'" + EscapeParam(TypeSerializer.SerializeToString(value)) + "'");
                }

                throw new NotSupportedException(
                          string.Format("Property of type: {0} is not supported", fieldType.FullName));
            }

            return(ShouldQuoteValue(fieldType)
                                        ? "'" + EscapeParam(value) + "'"
                                        : value.ToString());
        }
        public virtual string GetQuotedValue(object value, Type fieldType)
        {
            if (value == null)
            {
                return("NULL");
            }

            if (!fieldType.UnderlyingSystemType.IsValueType && fieldType != typeof(string))
            {
                if (TypeSerializer.CanCreateFromString(fieldType))
                {
                    return(OrmLiteConfig.DialectProvider.GetQuotedValue(TypeSerializer.SerializeToString(value)));
                }

                throw new NotSupportedException(
                          string.Format("Property of type: {0} is not supported", fieldType.FullName));
            }

            if (fieldType == typeof(float))
            {
                return(((float)value).ToString(CultureInfo.InvariantCulture));
            }

            if (fieldType == typeof(double))
            {
                return(((double)value).ToString(CultureInfo.InvariantCulture));
            }

            if (fieldType == typeof(decimal))
            {
                return(((decimal)value).ToString(CultureInfo.InvariantCulture));
            }

            return(ShouldQuoteValue(fieldType)
                    ? OrmLiteConfig.DialectProvider.GetQuotedValue(value.ToString())
                    : value.ToString());
        }