Beispiel #1
0
        /// <summary>
        /// Creates a nested field with the specified schema, and adds it to the schema being built.
        /// </summary>
        /// <param name="name">The name of the field. Must be a valid field name.</param>
        /// <param name="nestedSchema">The schema for the nested field. Must not be null.</param>
        /// <param name="mode">The mode of the field. Must be a defined member within <see cref="BigQueryFieldMode"/>.</param>
        /// <param name="description">The description of the field. May be null.</param>
        public void Add(string name, TableSchema nestedSchema, BigQueryFieldMode mode = BigQueryFieldMode.Nullable, string description = null)
        {
            ValidateFieldName(name, nameof(name));
            GaxPreconditions.CheckNotNull(nestedSchema, nameof(nestedSchema));

            Add(new TableFieldSchema
            {
                Name        = name,
                Fields      = nestedSchema.Fields,
                Type        = EnumMap.ToApiValue(BigQueryDbType.Struct),
                Mode        = EnumMap.ToApiValue(mode, nameof(mode)),
                Description = description,
            });
        }
Beispiel #2
0
        /// <summary>
        /// Creates a field with the specified details, and adds it to the schema being built.
        /// </summary>
        /// <param name="name">The name of the field. Must be a valid field name.</param>
        /// <param name="type">The type of the field. Must be a defined member within <see cref="BigQueryDbType"/>, other than <c>Struct</c> or <c>Array</c>.</param>
        /// <param name="mode">The mode of the field. Must be a defined member within <see cref="BigQueryFieldMode"/>.</param>
        /// <param name="description">The description of the field. May be null.</param>
        public void Add(string name, BigQueryDbType type, BigQueryFieldMode mode = BigQueryFieldMode.Nullable, string description = null)
        {
            ValidateFieldName(name, nameof(name));
            GaxPreconditions.CheckArgument(type != BigQueryDbType.Struct, nameof(type),
                                           $"{nameof(BigQueryDbType.Struct)} fields must be specified with their schema");
            GaxPreconditions.CheckArgument(type != BigQueryDbType.Array, nameof(type),
                                           $"{nameof(BigQueryDbType.Array)} fields must be specified with by element type with a {nameof(BigQueryFieldMode)} of {nameof(BigQueryFieldMode.Repeated)}");

            Add(new TableFieldSchema
            {
                Name        = name,
                Type        = EnumMap.ToApiValue(type, nameof(type)),
                Mode        = EnumMap.ToApiValue(mode, nameof(mode)),
                Description = description,
            });
        }
        private static (string, BigQueryFieldMode) GetBigQueryTypeAndMode(PropertyInfo propertyInfo)
        {
            //in the GCP website, when creating a new table, it shows this options
            // differs from -> Google.Cloud.BigQuery.V2.BigQueryDbType

            //STRING
            //BYTES

            //INTEGER
            //FLOAT
            //BOOLEAN

            //TIMESTAMP
            //DATE
            //TIME
            //DATETIME

            //RECORD

            //-----------------------------------

            string            type      = "STRING";
            BigQueryFieldMode fieldMode = GetBaseFiledMode(propertyInfo);

            Type propertyType = propertyInfo.PropertyType;

            if (propertyType.Name.Equals("Nullable`1"))
            {
                propertyType = propertyType.GenericTypeArguments[0];
                fieldMode    = BigQueryFieldMode.Nullable;
            }

            bool isIEnumerable = false;

            if (propertyType.Name.Equals("IEnumerable`1"))
            {
                propertyType  = propertyType.GenericTypeArguments[0];
                isIEnumerable = true;
            }

            var propertyTypeName = propertyType.Name.ToUpper();

            if (propertyType.IsArray)
            {
                propertyTypeName = propertyTypeName.TrimEnd('[', ']');
                isIEnumerable    = true;
            }

            if (isIEnumerable && propertyTypeName != "BYTE")
            {
                fieldMode = BigQueryFieldMode.Repeated;
            }

            switch (propertyTypeName)
            {
            case "BOOLEAN":
                type = "BOOLEAN";
                break;

            case "CHAR":
                fieldMode = GetBaseFiledMode(propertyInfo);
                break;

            case "DECIMAL":
            case "DOUBLE":
            case "SINGLE":
                type = "FLOAT";
                break;

            case "DATETIME":
            case "DATETIMEOFFSET":
                type = "DATETIME";
                break;

            case "INT":
            case "INT16":
            case "INT32":
            case "INT64":
            case "UINT16":
            case "UINT32":
            case "UINT64":
                type = "INTEGER";
                break;

            case "BYTE":
                if (isIEnumerable)
                {
                    type      = "BYTES";
                    fieldMode = GetBaseFiledMode(propertyInfo);
                }
                else
                {
                    type = "INTEGER";
                }
                break;
            }

            return(type, fieldMode);
        }
        private static TableFieldSchema GetTableFieldSchema(PropertyInfo propertyInfo, Dictionary <string, IEnumerable <System.Reflection.PropertyInfo> > dictionaryOfProperties)
        {
            //var fields = from property in properties
            //             let mode = BigQueryFieldMode.Nullable
            //             //let mode = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute)) ? BigQueryFieldMode.Required : BigQueryFieldMode.Nullable
            //             let type = GetBigQueryType(property.PropertyType)
            //             select new TableFieldSchema() { Name = property.Name, Type = type, Mode = mode.ToString() }
            //             ;

            var propertyType = propertyInfo.PropertyType;

            var descripton = propertyInfo
                             .CustomAttributes
                             .FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute))
                             ?.ConstructorArguments[0].Value.ToString();

            string                   type   = null;
            BigQueryFieldMode        mode   = BigQueryFieldMode.Nullable;
            IList <TableFieldSchema> fields = null;

            var innerPropertyType = propertyType;

            if (propertyType.Name.Equals("IEnumerable`1"))
            {
                innerPropertyType = propertyType.GenericTypeArguments[0];
            }
            else
            {
                if (propertyType.IsArray)
                {
                    innerPropertyType = propertyType.GetElementType();
                }
            }

            if (innerPropertyType.IsClass && innerPropertyType.Namespace != "System")  //crappy but works for now

            {
                mode = BigQueryFieldMode.Nullable;
                if (propertyType.IsArray || propertyType.Name.Equals("IEnumerable`1"))
                {
                    mode = BigQueryFieldMode.Repeated;
                }

                IEnumerable <PropertyInfo> properties = null;
                if (dictionaryOfProperties.ContainsKey(innerPropertyType.FullName))
                {
                    properties = dictionaryOfProperties[innerPropertyType.FullName];
                }
                else
                {
                    properties = GetPropertyInfo(innerPropertyType);
                    dictionaryOfProperties.Add(innerPropertyType.FullName, properties);
                }
                fields = properties.Select(p => GetTableFieldSchema(p, dictionaryOfProperties)).ToList();

                type = "RECORD";
            }
            else
            {
                (type, mode) = GetBigQueryTypeAndMode(propertyInfo);
            }

            return(new TableFieldSchema()
            {
                Name = propertyInfo.Name, Type = type, Mode = mode.ToString().ToUpper(), Description = descripton, Fields = fields
            });
        }
Beispiel #5
0
        /// <summary>
        /// Creates a nested field with the specified schema, and adds it to the schema being built.
        /// This method simply delegates to <see cref="Add(string, TableSchema, BigQueryFieldMode, string)"/> after calling <see cref="Build"/>
        /// on <paramref name="nestedSchema"/>.
        /// </summary>
        /// <param name="name">The name of the field. Must be a valid field name.</param>
        /// <param name="nestedSchema">The schema for the nested field, in the form of a <see cref="TableSchemaBuilder"/>. Must not be null.</param>
        /// <param name="mode">The mode of the field. Must be a defined member within <see cref="BigQueryFieldMode"/>.</param>
        /// <param name="description">The description of the field. May be null.</param>
        public void Add(string name, TableSchemaBuilder nestedSchema, BigQueryFieldMode mode = BigQueryFieldMode.Nullable, string description = null)
        {
            var builtSchema = GaxPreconditions.CheckNotNull(nestedSchema, nameof(nestedSchema)).Build();

            Add(name, builtSchema, mode, description);
        }