1// Copyright 2016 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
15package bigquery
16
17import (
18	"context"
19
20	bq "google.golang.org/api/bigquery/v2"
21)
22
23// TableCopyOperationType is used to indicate the type of operation performed by a BigQuery
24// copy job.
25type TableCopyOperationType string
26
27var (
28	// CopyOperation indicates normal table to table copying.
29	CopyOperation TableCopyOperationType = "COPY"
30	// SnapshotOperation indicates creating a snapshot from a regular table.
31	SnapshotOperation TableCopyOperationType = "SNAPSHOT"
32	// RestoreOperation indicates creating/restoring a table from a snapshot.
33	RestoreOperation TableCopyOperationType = "RESTORE"
34)
35
36// CopyConfig holds the configuration for a copy job.
37type CopyConfig struct {
38	// Srcs are the tables from which data will be copied.
39	Srcs []*Table
40
41	// Dst is the table into which the data will be copied.
42	Dst *Table
43
44	// CreateDisposition specifies the circumstances under which the destination table will be created.
45	// The default is CreateIfNeeded.
46	CreateDisposition TableCreateDisposition
47
48	// WriteDisposition specifies how existing data in the destination table is treated.
49	// The default is WriteEmpty.
50	WriteDisposition TableWriteDisposition
51
52	// The labels associated with this job.
53	Labels map[string]string
54
55	// Custom encryption configuration (e.g., Cloud KMS keys).
56	DestinationEncryptionConfig *EncryptionConfig
57
58	// One of the supported operation types when executing a Table Copy jobs.  By default this
59	// copies tables, but can also be set to perform snapshot or restore operations.
60	OperationType TableCopyOperationType
61}
62
63func (c *CopyConfig) toBQ() *bq.JobConfiguration {
64	var ts []*bq.TableReference
65	for _, t := range c.Srcs {
66		ts = append(ts, t.toBQ())
67	}
68	return &bq.JobConfiguration{
69		Labels: c.Labels,
70		Copy: &bq.JobConfigurationTableCopy{
71			CreateDisposition:                  string(c.CreateDisposition),
72			WriteDisposition:                   string(c.WriteDisposition),
73			DestinationTable:                   c.Dst.toBQ(),
74			DestinationEncryptionConfiguration: c.DestinationEncryptionConfig.toBQ(),
75			SourceTables:                       ts,
76			OperationType:                      string(c.OperationType),
77		},
78	}
79}
80
81func bqToCopyConfig(q *bq.JobConfiguration, c *Client) *CopyConfig {
82	cc := &CopyConfig{
83		Labels:                      q.Labels,
84		CreateDisposition:           TableCreateDisposition(q.Copy.CreateDisposition),
85		WriteDisposition:            TableWriteDisposition(q.Copy.WriteDisposition),
86		Dst:                         bqToTable(q.Copy.DestinationTable, c),
87		DestinationEncryptionConfig: bqToEncryptionConfig(q.Copy.DestinationEncryptionConfiguration),
88		OperationType:               TableCopyOperationType(q.Copy.OperationType),
89	}
90	for _, t := range q.Copy.SourceTables {
91		cc.Srcs = append(cc.Srcs, bqToTable(t, c))
92	}
93	return cc
94}
95
96// A Copier copies data into a BigQuery table from one or more BigQuery tables.
97type Copier struct {
98	JobIDConfig
99	CopyConfig
100	c *Client
101}
102
103// CopierFrom returns a Copier which can be used to copy data into a
104// BigQuery table from one or more BigQuery tables.
105// The returned Copier may optionally be further configured before its Run method is called.
106func (t *Table) CopierFrom(srcs ...*Table) *Copier {
107	return &Copier{
108		c: t.c,
109		CopyConfig: CopyConfig{
110			Srcs: srcs,
111			Dst:  t,
112		},
113	}
114}
115
116// Run initiates a copy job.
117func (c *Copier) Run(ctx context.Context) (*Job, error) {
118	return c.c.insertJob(ctx, c.newJob(), nil)
119}
120
121func (c *Copier) newJob() *bq.Job {
122	return &bq.Job{
123		JobReference:  c.JobIDConfig.createJobRef(c.c),
124		Configuration: c.CopyConfig.toBQ(),
125	}
126}
127