Пример #1
0
        private static string GetSqlLiteralForValue(object value, MappedColumn column)
        {
            object sqlValue = ConvertObjectToSql(column.FieldType, value);

            if (sqlValue == null)
            {
                return("NULL");
            }

            switch (column.Affinity)
            {
            case TypeAffinity.Text:
            case TypeAffinity.DateTime:
                string stringValue = (string)sqlValue;
                return("'" + stringValue.Replace("'", "''") + "'");

            case TypeAffinity.Int64:
                long longValue = Convert.ToInt64(sqlValue);
                return(longValue.ToString());

            case TypeAffinity.Double:
                double doubleValue = Convert.ToDouble(sqlValue);
                return(doubleValue.ToString());

            default:
                throw new NotImplementedException();
            }
        }
Пример #2
0
        public static SQLiteMappedColumnSet CreateColumnSetForType(Type classType)
        {
            SQLiteMappedColumnSet columnSet = new SQLiteMappedColumnSet();

            foreach (MemberInfo memberInfo in classType.GetMembers(
                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                Type         memberType;
                PropertyInfo propertyInfo = memberInfo as PropertyInfo;
                FieldInfo    fieldInfo    = memberInfo as FieldInfo;
                if (propertyInfo != null)
                {
                    memberType = propertyInfo.PropertyType;
                }
                else if (fieldInfo != null)
                {
                    memberType = fieldInfo.FieldType;
                }
                else
                {
                    continue;
                }

                SQLiteMapperPropertyAttribute attribute = null;
                foreach (Attribute baseAttribute in memberInfo.GetCustomAttributes(true))
                {
                    attribute = baseAttribute as SQLiteMapperPropertyAttribute;
                    if (attribute != null)
                    {
                        break;
                    }
                }
                if (attribute == null)
                {
                    continue;
                }

                TypeAffinity affinity = GetSQLiteAffinityForType(memberType);

                MappedColumn column = new MappedColumn(memberInfo.Name, memberType, affinity);
                column.PrimaryKey = attribute.PrimaryKey;

                bool nullable;
                if (attribute.Nullable == OptionalBool.Unspecified && memberType != typeof(SQLiteIdList))
                {
                    nullable = Nullable.GetUnderlyingType(memberType) != null;
                }
                else
                {
                    nullable = attribute.Nullable != OptionalBool.False;
                }

                column.Nullable        = nullable;
                column.SqlDefaultValue = attribute.SqlDefaultValue;

                columnSet.Columns.Add(column);
            }
            return(columnSet);
        }
Пример #3
0
        protected override void OnFreezing()
        {
            if (IsFrozen)
            {
                return;
            }

            base.OnFreezing();
            foreach (var column in columns)
            {
                column.Freeze();
            }

            // Clear the cache
            primaryKeyColumn = null;
        }
Пример #4
0
        internal static List <PropertyDescriptor> GetPropertyDescriptors(Type classType, SQLiteMappedColumnSet columnSet)
        {
            var descriptorsByColumn = new Dictionary <MappedColumn, PropertyDescriptor>();

            var emptyArray = new object[0];

            foreach (MemberInfo memberInfo in classType.GetMembers(BindingFlags.Public | BindingFlags.Instance))
            {
                Type         memberType;
                PropertyInfo propertyInfo = memberInfo as PropertyInfo;
                FieldInfo    fieldInfo    = memberInfo as FieldInfo;
                if (propertyInfo != null)
                {
                    memberType = propertyInfo.PropertyType;
                }
                else if (fieldInfo != null)
                {
                    memberType = fieldInfo.FieldType;
                }
                else
                {
                    continue;
                }

                MappedColumn column = columnSet.Columns.FirstOrDefault(x => x.Name == memberInfo.Name);
                if (column == null)
                {
                    continue;
                }

                PropertyDescriptor descriptor;

                if (propertyInfo != null)
                {
                    descriptor = new LambdaPropertyDescriptor(
                        column.Name,
                        memberType,
                        (object record) => propertyInfo.GetValue(record, emptyArray),
                        delegate(object record, object value) {
                        propertyInfo.SetValue(record, value, emptyArray);
                    }
                        );
                }
                else
                {
                    descriptor = new LambdaPropertyDescriptor(
                        column.Name,
                        memberType,
                        (object record) => fieldInfo.GetValue(record),
                        delegate(object record, object value) {
                        fieldInfo.SetValue(record, value);
                    }
                        );
                }

                descriptorsByColumn.Add(column, descriptor);
            }

            List <PropertyDescriptor> descriptors = new List <PropertyDescriptor>(columnSet.Columns.Count);

            foreach (var column in columnSet.Columns)
            {
                PropertyDescriptor descriptor = null;
                if (!descriptorsByColumn.TryGetValue(column, out descriptor))
                {
                    throw new Exception("No match for column " + column.Name);
                }
                descriptors.Add(descriptor);
            }
            return(descriptors);
        }