Example #1
0
        public static StringBuilder AppendKey <T>(this StringBuilder builder, SqliteProperty <T> property)
        {
            var key   = property.Key ?? throw new NullReferenceException("Key must not be null.");
            var order = key.IsDescending ? " DESC" : " ASC";

            return(builder.AppendField(property.ColumnName).Append(order));
        }
Example #2
0
        public static int CompareKeys(SqliteProperty <T> a, SqliteProperty <T> b)
        {
            var ka = a.Key ?? throw new NullReferenceException();
            var kb = b.Key ?? throw new NullReferenceException();

            return(ka.Ordinal.CompareTo(kb.Ordinal));
        }
Example #3
0
        public static StringBuilder AppendProperty <T>(
            this StringBuilder builder,
            SqliteProperty <T> property)
        {
            builder
            .AppendField(property.ColumnName)
            .Append(' ')
            .Append(property.PropertyHandler.DataType);

            if (!property.CanBeNull)
            {
                builder.Append(" NOT NULL");
            }

            return(builder);
        }
Example #4
0
        public SqliteTable(string tableName, Func <string, string> namingPolicy)
        {
            TableName = tableName;

            var keyArrayBuilder  = ImmutableArray.CreateBuilder <SqliteProperty <T> >();
            var propArrayBuilder = ImmutableArray.CreateBuilder <SqliteProperty <T> >();

            foreach (var propertyInfo in typeof(T).GetProperties())
            {
                // if (System.Diagnostics.Debugger.IsAttached)
                //     Console.WriteLine($"{propertyInfo.Name} : {propertyInfo.PropertyType}");

                var propertyHandlerType = typeof(PropertyHandler <,>).MakeGenericType(typeof(T), propertyInfo.PropertyType);
                var typeHandlerType     = typeof(ITypeHandler <>).MakeGenericType(propertyInfo.PropertyType);
                var typeHandler         = TypeHandler.Get(propertyInfo.PropertyType);
                var constructor         = propertyHandlerType.GetConstructor(new Type[] { typeof(PropertyInfo), typeHandlerType });

                if (constructor is null)
                {
                    throw new NullReferenceException("Failed to locate appropriate constructor");
                }

                var propertyHandler = (IPropertyHandler <T>)constructor.Invoke(new[] { propertyInfo, typeHandler });

                if (propertyInfo.GetCustomAttribute <SqliteIgnore>() is null)
                {
                    var property = new SqliteProperty <T>(propertyInfo, propertyHandler, namingPolicy);
                    propArrayBuilder.Add(property);

                    if (property.Key != null)
                    {
                        keyArrayBuilder.Add(property);
                    }
                }
            }

            _properties = propArrayBuilder.ToImmutable();

            keyArrayBuilder.Sort(SqliteProperty <T> .CompareKeys);
            _keyProperties = keyArrayBuilder.ToImmutable();
        }