Esempio n. 1
0
 public static TypeSchema GetSchema(Type type)
 {
     if (!TypeSchemas.ContainsKey(type))
     {
         var fields = type.GetProperties()
                      .Where(p => p.GetCustomAttribute <SchemaIgnoreAttribute>() == null)
                      .Select(p =>
         {
             TypeFieldSchema field = new TypeFieldSchema()
             {
                 Name                 = p.Name,
                 FieldType            = p.MapFieldType(),
                 IsIndex              = p.GetCustomAttribute <IndexAttribute>() != null,
                 IsRequired           = p.GetCustomAttribute <RequiredAttribute>() != null,
                 IsPrimaryKey         = p.GetCustomAttribute <KeyAttribute>() != null,
                 OriginalPropertyType = p.PropertyType,
                 PropertyInfo         = p,
                 IsNullable           = p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == TypeMapping.type_nullable
             };
             field.IsGeoType = (int)field.FieldType >= (int)FieldTypeEnum.GeometryPoint &&
                               (int)field.FieldType <= (int)FieldTypeEnum.GeometryCollection;
             var stringLength = p.GetCustomAttribute <StringLengthAttribute>();
             if (stringLength != null)
             {
                 field.MaxLength = stringLength.MaximumLength;
                 field.MinLength = stringLength.MinimumLength;
             }
             var forceType = p.GetCustomAttribute <ForceTypeAttribute>();
             if (forceType != null)
             {
                 field.Converter = forceType.Converter;
             }
             else
             {
                 field.Converter = identity;
             }
             return(field);
         });
         TypeSchema schema = new TypeSchema()
         {
             Name = type.Name
         };
         foreach (var field in fields)
         {
             schema.typeFields.Add(field.Name, field);
         }
         TypeSchemas.Add(type, schema);
     }
     return(TypeSchemas[type]);
 }
        public static string TableDefinition(this TypeSchema schema, string suffix = null)
        {
            return($@"CREATE TABLE {schema.TableNameWithSuffix(suffix)}(
{string.Join(",\n", schema.Fields.Select(f => f.FieldDeclaration()))}
);");
        }