private static QueryParameter PopulateArrayParameter(QueryParameter parameter, object value, BigqueryParameterType?arrayType)
        {
            if (value == null)
            {
                throw new InvalidOperationException("The value of an array parameter cannot be null");
            }
            if (!IsArrayValue(value))
            {
                throw new InvalidOperationException($"Invalid value for array parameter: {value.GetType()}");
            }
            List <object> values = ((IEnumerable)value).Cast <object>().ToList();

            if (values.Any(v => v == null))
            {
                throw new InvalidOperationException("Array parameter values cannot contain null elements");
            }
            BigqueryParameterType actualArrayType = arrayType ?? s_typeMapping[GetArrayElementType(value)];

            parameter.ParameterType = new QueryParameterType
            {
                Type      = EnumMap.ToApiValue(BigqueryParameterType.Array),
                ArrayType = new QueryParameterType {
                    Type = EnumMap.ToApiValue(actualArrayType)
                }
            };
            var parameterValues = values
                                  .Select(p => new BigqueryParameter(actualArrayType, p).ToQueryParameter(BigqueryParameterMode.Positional).ParameterValue)
                                  .ToList();

            parameter.ParameterValue = new QueryParameterValue {
                ArrayValues = parameterValues
            };
            return(parameter);
        }
        /// <summary>
        /// If the value is null, populate the parameter with an QueryParameterValue. Otherwise,
        /// throw an exception - this is expected to be the last call in a chain, so at this point we know
        /// we can't handle a value of this type.
        /// </summary>
        internal static QueryParameter UseNullScalarOrThrow(this QueryParameter parameter, object value)
        {
            if (value == null)
            {
                parameter.ParameterValue = new QueryParameterValue();
                return(parameter);
            }
            var clrEnum = EnumMap <BigqueryParameterType> .ToValue(parameter.ParameterType.Type);

            throw new InvalidOperationException($"Value of type {value.GetType()} cannot be used for a parameter of type {clrEnum}");
        }
        /// <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>Record</c>.</param>
        /// <param name="mode">The mode of the field. Must be a defined member within <see cref="FieldMode"/>.</param>
        /// <param name="description">The description of the field. May be null.</param>
        public void Add(string name, BigqueryDbType type, FieldMode mode = FieldMode.Nullable, string description = null)
        {
            ValidateFieldName(name, nameof(name));
            GaxRestPreconditions.CheckArgument(type != BigqueryDbType.Record, nameof(type), "Record fields must be specified with their schema");

            Add(new TableFieldSchema
            {
                Name        = name,
                Type        = EnumMap <BigqueryDbType> .ToApiValue(type, nameof(type)),
                Mode        = EnumMap <FieldMode> .ToApiValue(mode, nameof(mode)),
                Description = description,
            });
        }
        /// <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="FieldMode"/>.</param>
        /// <param name="description">The description of the field. May be null.</param>
        public void Add(string name, TableSchema nestedSchema, FieldMode mode = FieldMode.Nullable, string description = null)
        {
            ValidateFieldName(name, nameof(name));
            GaxRestPreconditions.CheckNotNull(nestedSchema, nameof(nestedSchema));

            Add(new TableFieldSchema
            {
                Name        = name,
                Fields      = nestedSchema.Fields,
                Type        = EnumMap <BigqueryDbType> .ToApiValue(BigqueryDbType.Record),
                Mode        = EnumMap <FieldMode> .ToApiValue(mode, nameof(mode)),
                Description = description,
            });
        }
 internal void ModifyRequest(JobConfigurationQuery query)
 {
     // Note: no validation of combinations (flatten results etc). Leave this to the server,
     // to avoid restrictions getting out of date.
     if (AllowLargeResults != null)
     {
         query.AllowLargeResults = AllowLargeResults;
     }
     if (CreateDisposition != null)
     {
         query.CreateDisposition = EnumMap.ToApiValue(CreateDisposition.Value);
     }
     if (DefaultDataset != null)
     {
         query.DefaultDataset = DefaultDataset;
     }
     if (DestinationTable != null)
     {
         query.DestinationTable = DestinationTable;
     }
     if (FlattenResults != null)
     {
         query.FlattenResults = FlattenResults;
     }
     if (MaximumBillingTier != null)
     {
         query.MaximumBillingTier = MaximumBillingTier;
     }
     if (MaximumBytesBilled != null)
     {
         query.MaximumBytesBilled = MaximumBytesBilled;
     }
     if (Priority != null)
     {
         query.Priority = EnumMap.ToApiValue(Priority.Value);
     }
     if (UseQueryCache != null)
     {
         query.UseQueryCache = UseQueryCache;
     }
     if (WriteDisposition != null)
     {
         query.WriteDisposition = EnumMap.ToApiValue(WriteDisposition.Value);
     }
     if (UseLegacySql != null)
     {
         query.UseLegacySql = UseLegacySql;
     }
 }
 internal void ModifyConfiguration(JobConfigurationLoad loadRequest)
 {
     if (AllowUnknownFields != null)
     {
         loadRequest.IgnoreUnknownValues = AllowUnknownFields;
     }
     if (MaxBadRecords != null)
     {
         loadRequest.MaxBadRecords = MaxBadRecords;
     }
     if (CreateDisposition != null)
     {
         loadRequest.CreateDisposition = EnumMap <CreateDisposition> .ToApiValue(CreateDisposition.Value);
     }
     if (WriteDisposition != null)
     {
         loadRequest.WriteDisposition = EnumMap <WriteDisposition> .ToApiValue(WriteDisposition.Value);
     }
 }
Пример #7
0
 internal void ModifyConfiguration(JobConfigurationLoad loadRequest)
 {
     if (SkipLeadingRows != null)
     {
         loadRequest.SkipLeadingRows = SkipLeadingRows;
     }
     if (Quote != null)
     {
         loadRequest.Quote = Quote;
     }
     if (AllowQuotedNewlines != null)
     {
         loadRequest.AllowQuotedNewlines = AllowQuotedNewlines;
     }
     if (AllowJaggedRows != null)
     {
         loadRequest.AllowJaggedRows = AllowJaggedRows;
     }
     if (FieldDelimiter != null)
     {
         loadRequest.FieldDelimiter = FieldDelimiter;
     }
     if (AllowTrailingColumns != null)
     {
         loadRequest.IgnoreUnknownValues = AllowTrailingColumns;
     }
     if (MaxBadRecords != null)
     {
         loadRequest.MaxBadRecords = MaxBadRecords;
     }
     if (CreateDisposition != null)
     {
         loadRequest.CreateDisposition = EnumMap.ToApiValue(CreateDisposition.Value);
     }
     if (WriteDisposition != null)
     {
         loadRequest.WriteDisposition = EnumMap.ToApiValue(WriteDisposition.Value);
     }
     // TODO: Encoding? Only UTF-8 and ISO-8859-1 are supported... unsure what to do with this.
 }
        internal QueryParameter ToQueryParameter(BigqueryParameterMode parameterMode)
        {
            if (parameterMode == BigqueryParameterMode.Named && string.IsNullOrEmpty(Name))
            {
                throw new InvalidOperationException("Unnamed parameters cannot be used in command using BigqueryParameterMode.Named");
            }
            var value = Value;

            if (Type == null && value == null)
            {
                throw new InvalidOperationException("A null-valued parameter must have an explicitly specified type");
            }
            var type      = Type ?? InferParameterType(value);
            var parameter = new QueryParameter
            {
                Name          = Name,
                ParameterType = new QueryParameterType {
                    Type = EnumMap.ToApiValue(type)
                },
            };

            switch (type)
            {
            case BigqueryParameterType.Array:
                return(PopulateArrayParameter(parameter, value, ArrayType));

            case BigqueryParameterType.Bool:
                return(parameter.PopulateScalar <bool>(value, x => x ? "TRUE" : "FALSE")
                       ?? parameter.PopulateScalar <string>(value, x => x)
                       ?? parameter.UseNullScalarOrThrow(value));

            case BigqueryParameterType.Bytes:
                return(parameter.PopulateScalar <byte[]>(value, x => Convert.ToBase64String(x))
                       ?? parameter.PopulateScalar <string>(value, x => x)
                       ?? parameter.UseNullScalarOrThrow(value));

            case BigqueryParameterType.Date:
                return(parameter.PopulateScalar <DateTime>(value, x => x.ToString("yyyy-MM-dd", InvariantCulture))
                       ?? parameter.PopulateScalar <DateTimeOffset>(value, x => x.ToString("yyyy-MM-dd", InvariantCulture))
                       ?? parameter.PopulateScalar <string>(value, x => x)
                       ?? parameter.UseNullScalarOrThrow(value));

            case BigqueryParameterType.DateTime:
                return(parameter.PopulateScalar <DateTime>(value, x => x.ToString("yyyy-MM-dd HH:mm:ss.FFFFFF", InvariantCulture))
                       ?? parameter.PopulateScalar <DateTimeOffset>(value, x => x.ToString("yyyy-MM-dd HH:mm:ss.FFFFFF", InvariantCulture))
                       ?? parameter.PopulateScalar <string>(value, x => x)
                       ?? parameter.UseNullScalarOrThrow(value));

            case BigqueryParameterType.Float64:
                return(parameter.PopulateInteger(value)
                       ?? parameter.PopulateFloatingPoint(value)
                       ?? parameter.PopulateScalar <string>(value, x => x)
                       ?? parameter.UseNullScalarOrThrow(value));

            case BigqueryParameterType.Int64:
                return(parameter.PopulateInteger(value)
                       ?? parameter.PopulateScalar <string>(value, x => x)
                       ?? parameter.UseNullScalarOrThrow(value));

            case BigqueryParameterType.String:
                return(parameter.PopulateScalar <string>(value, x => x)
                       ?? parameter.UseNullScalarOrThrow(value));

            case BigqueryParameterType.Struct: throw new NotImplementedException("Struct parameters are not yet implemented");

            case BigqueryParameterType.Time:
                return(parameter.PopulateScalar <TimeSpan>(value, FormatTimeSpan)
                       ?? parameter.PopulateScalar <DateTimeOffset>(value, x => x.ToString("HH:mm:ss.FFFFFF", InvariantCulture))
                       ?? parameter.PopulateScalar <DateTime>(value, x => x.ToString("HH:mm:ss.FFFFFF", InvariantCulture))
                       ?? parameter.PopulateScalar <string>(value, x => x)
                       ?? parameter.UseNullScalarOrThrow(value));

            case BigqueryParameterType.Timestamp:
                return(parameter.PopulateScalar <DateTime>(value, x =>
                {
                    if (x.Kind != DateTimeKind.Utc)
                    {
                        throw new InvalidOperationException($"A DateTime with a Kind of {x.Kind} cannot be used for a Timestamp parameter");
                    }
                    return x.ToString("yyyy-MM-dd HH:mm:ss.FFFFFF");
                })
                       ?? parameter.PopulateScalar <DateTimeOffset>(value, x => x.ToString("yyyy-MM-dd HH:mm:ss.FFFFFFzzz", InvariantCulture))
                       ?? parameter.PopulateScalar <string>(value, x => x)
                       ?? parameter.UseNullScalarOrThrow(value));

            default: throw new InvalidOperationException($"No conversion available for parameter type {type}");
            }
        }
Пример #9
0
 internal static string ToApiValue <T>(T value, string paramName = "value") where T : struct =>
 EnumMap <T> .ToApiValue(value, paramName);
 /// <summary>
 /// Returns the mode of a field as a <see cref="FieldMode"/>.
 /// </summary>
 internal static FieldMode GetFieldMode(this TableFieldSchema field) =>
 EnumMap <FieldMode> .ToValue(field.Mode);
 /// <summary>
 /// Returns the type of a field as a <see cref="BigqueryDbType"/>.
 /// </summary>
 internal static BigqueryDbType GetFieldType(this TableFieldSchema field) =>
 EnumMap <BigqueryDbType> .ToValue(field.Type);