|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package bigquery |
| 16 | + |
| 17 | +import ( |
| 18 | + "google.golang.org/api/option" |
| 19 | + "google.golang.org/api/option/internaloption" |
| 20 | +) |
| 21 | + |
| 22 | +// type for collecting custom ClientOption values. |
| 23 | +type customClientConfig struct { |
| 24 | + jobCreationMode JobCreationMode |
| 25 | +} |
| 26 | + |
| 27 | +type customClientOption interface { |
| 28 | + option.ClientOption |
| 29 | + ApplyCustomClientOpt(*customClientConfig) |
| 30 | +} |
| 31 | + |
| 32 | +func newCustomClientConfig(opts ...option.ClientOption) *customClientConfig { |
| 33 | + conf := &customClientConfig{} |
| 34 | + for _, opt := range opts { |
| 35 | + if cOpt, ok := opt.(customClientOption); ok { |
| 36 | + cOpt.ApplyCustomClientOpt(conf) |
| 37 | + } |
| 38 | + } |
| 39 | + return conf |
| 40 | +} |
| 41 | + |
| 42 | +// JobCreationMode controls how job creation is handled. Some queries may |
| 43 | +// be run without creating a job to expedite fetching results. |
| 44 | +type JobCreationMode string |
| 45 | + |
| 46 | +var ( |
| 47 | + // JobCreationModeUnspecified is the default (unspecified) option. |
| 48 | + JobCreationModeUnspecified JobCreationMode = "JOB_CREATION_MODE_UNSPECIFIED" |
| 49 | + // JobCreationModeRequired indicates job creation is required. |
| 50 | + JobCreationModeRequired JobCreationMode = "JOB_CREATION_REQUIRED" |
| 51 | + // JobCreationModeOptional indicates job creation is optional, and returning |
| 52 | + // results immediately is prioritized. The conditions under which BigQuery |
| 53 | + // can choose to avoid job creation are internal and subject to change. |
| 54 | + JobCreationModeOptional JobCreationMode = "JOB_CREATION_OPTIONAL" |
| 55 | +) |
| 56 | + |
| 57 | +// WithDefaultJobCreationMode is a ClientOption that governs the job creation |
| 58 | +// mode used when executing queries that can be accelerated via the jobs.Query |
| 59 | +// API. Users may experience performance improvements by leveraging the |
| 60 | +// JobCreationModeOptional mode. |
| 61 | +func WithDefaultJobCreationMode(mode JobCreationMode) option.ClientOption { |
| 62 | + return &applierJobCreationMode{mode: mode} |
| 63 | +} |
| 64 | + |
| 65 | +// applier for propagating the custom client option to the config object |
| 66 | +type applierJobCreationMode struct { |
| 67 | + internaloption.EmbeddableAdapter |
| 68 | + mode JobCreationMode |
| 69 | +} |
| 70 | + |
| 71 | +func (s *applierJobCreationMode) ApplyCustomClientOpt(c *customClientConfig) { |
| 72 | + c.jobCreationMode = s.mode |
| 73 | +} |
0 commit comments