Exemple #1
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);
        }
Exemple #2
0
        public static string GetInsertStatement(SQLiteMappedColumnSet columnSet, string tableName,
                                                SQLiteConflictResolution conflictResolution)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append("INSERT");

            if (conflictResolution != SQLiteConflictResolution.Abort)
            {
                builder.Append(" OR ");
                builder.Append(conflictResolution.ToString().ToUpperInvariant());
            }

            builder.Append(" INTO " + GetEscaped(tableName) + " (");

            string paramList = "";

            bool needsComma = false;

            foreach (MappedColumn column in columnSet.Columns)
            {
                if (needsComma)
                {
                    builder.Append(",");
                    paramList += ",";
                }
                needsComma = true;

                builder.AppendLine();
                builder.Append("  " + GetEscaped(column.Name));
                paramList += "?";
            }
            builder.AppendLine();
            builder.AppendLine(") VALUES (");
            builder.AppendLine("  " + paramList);
            builder.AppendLine(");");

            return(builder.ToString());
        }
Exemple #3
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);
        }