Exemplo n.º 1
0
        private static TableDefinition<T> FromType<T>(Type type)
        {
            if (type == null)
                type = typeof(T);

            var def = new TableDefinition<T>();
            var props = type
                .GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public |
                               BindingFlags.NonPublic)
                .Where(p => !p.IsDefined(typeof(PgSqlIgnoreAttribute)));

            foreach (var propertyInfo in props)
            {
                if (def.GetColumnBuilderByClrName(propertyInfo.Name) != null)
                    continue;

                if (propertyInfo.GetGetMethod(true)?.IsPublic == false &&
                    !propertyInfo.IsDefined(typeof(PgSqlIncludeAttribute)))
                    continue;

                ColumnBuilder col = ColumnBuilder.Build(propertyInfo.Name, propertyInfo.PropertyType);


                var attributes = propertyInfo.GetCustomAttributes<PgSqlAttribute>(true);
                foreach (var attr in attributes) {

                    if (attr is PgSqlPrimaryKeyAttribute primaryKeyAttribute) {
                        col.AsPrimaryKey();
                        if (!String.IsNullOrWhiteSpace(primaryKeyAttribute.Value?.ToString())) {
                            col.DefaultValue(primaryKeyAttribute.Value?.ToString());
                        }
                    }

                    if (attr is PgSqlColumnAttribute colAttr)
                        col.SetDbName(colAttr.Name);
                    
                    if (attr is PgSqlDefaultValueAttribute defaultvalue)
                        col.DefaultValue(defaultvalue.Value?.ToString());

                    if (attr is PgSqlUniqueAttribute unique)
                        col.MustBeUnique((bool)unique.Value);

                    if (attr is PgSqlCustomTypeAttribute custom)
                        col.SetCustomDbType((string)custom.Value);
                }



                def.AddColumn(col);
            }

            return def;
        }
Exemplo n.º 2
0
 public static ColumnBuilder Build <T>(string name)
 {
     return(ColumnBuilder.Build <T>(name));
 }
Exemplo n.º 3
0
 public static ColumnBuilder Build(string name, string typeName)
 {
     return(ColumnBuilder.Build(name, typeName));
 }
Exemplo n.º 4
0
 public static ColumnBuilder Build(string name, Type dotnetType)
 {
     return(ColumnBuilder.Build(name, dotnetType));
 }
Exemplo n.º 5
0
 public ColumnBuilder AddColumn(string name, string typeName)
 {
     Column col = ColumnBuilder.Build(name, typeName);
     AddColumn(col);
     return GetColumnBuilderByClrName(col.ClrName);
 }