|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery; |
| 18 | + |
| 19 | +import com.google.auto.value.AutoValue; |
| 20 | +import com.google.common.annotations.VisibleForTesting; |
| 21 | +import java.io.Serializable; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
| 24 | +import javax.annotation.Nullable; |
| 25 | + |
| 26 | +@AutoValue |
| 27 | +public abstract class ForeignKey implements Serializable { |
| 28 | + public static ForeignKey.Builder newBuilder() { |
| 29 | + return new AutoValue_ForeignKey.Builder(); |
| 30 | + } |
| 31 | + |
| 32 | + static ForeignKey fromPb( |
| 33 | + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys foreignKey) { |
| 34 | + ForeignKey.Builder builder = newBuilder(); |
| 35 | + |
| 36 | + if (foreignKey.getName() != null) { |
| 37 | + builder.setName(foreignKey.getName()); |
| 38 | + } |
| 39 | + |
| 40 | + if (foreignKey.getReferencedTable() != null) { |
| 41 | + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ReferencedTable |
| 42 | + referencedTable = foreignKey.getReferencedTable(); |
| 43 | + builder.setReferencedTable( |
| 44 | + TableId.of( |
| 45 | + referencedTable.getProjectId(), |
| 46 | + referencedTable.getDatasetId(), |
| 47 | + referencedTable.getTableId())); |
| 48 | + } |
| 49 | + |
| 50 | + if (foreignKey.getColumnReferences() != null) { |
| 51 | + builder.setColumnReferences( |
| 52 | + foreignKey.getColumnReferences().stream() |
| 53 | + .map(ColumnReference::fromPb) |
| 54 | + .collect(Collectors.toList())); |
| 55 | + } |
| 56 | + |
| 57 | + return builder.build(); |
| 58 | + } |
| 59 | + |
| 60 | + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys toPb() { |
| 61 | + |
| 62 | + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys foreignKey = |
| 63 | + new com.google.api.services.bigquery.model.TableConstraints.ForeignKeys(); |
| 64 | + if (getName() != null) { |
| 65 | + foreignKey.setName(getName()); |
| 66 | + } |
| 67 | + if (getReferencedTable() != null) { |
| 68 | + TableId referencedTableId = getReferencedTable(); |
| 69 | + foreignKey.setReferencedTable( |
| 70 | + new com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ReferencedTable() |
| 71 | + .setTableId(referencedTableId.getTable()) |
| 72 | + .setDatasetId(referencedTableId.getDataset()) |
| 73 | + .setProjectId(referencedTableId.getProject())); |
| 74 | + } |
| 75 | + if (getColumnReferences() != null) { |
| 76 | + foreignKey.setColumnReferences( |
| 77 | + getColumnReferences().stream().map(ColumnReference::toPb).collect(Collectors.toList())); |
| 78 | + } |
| 79 | + return foreignKey; |
| 80 | + } |
| 81 | + |
| 82 | + @Nullable |
| 83 | + public abstract String getName(); |
| 84 | + |
| 85 | + @Nullable |
| 86 | + public abstract TableId getReferencedTable(); |
| 87 | + |
| 88 | + @Nullable |
| 89 | + public abstract List<ColumnReference> getColumnReferences(); |
| 90 | + |
| 91 | + /** Returns a builder for foreign key. */ |
| 92 | + @VisibleForTesting |
| 93 | + public abstract ForeignKey.Builder toBuilder(); |
| 94 | + |
| 95 | + @AutoValue.Builder |
| 96 | + public abstract static class Builder { |
| 97 | + |
| 98 | + /** The name of the foreign key. * */ |
| 99 | + public abstract ForeignKey.Builder setName(String name); |
| 100 | + |
| 101 | + /** The table referenced by this foreign key. * */ |
| 102 | + public abstract ForeignKey.Builder setReferencedTable(TableId referencedTable); |
| 103 | + |
| 104 | + /** The set of column references for this foreign key. * */ |
| 105 | + public abstract ForeignKey.Builder setColumnReferences(List<ColumnReference> columnReferences); |
| 106 | + |
| 107 | + /** Creates a {@code ForignKey} object. */ |
| 108 | + public abstract ForeignKey build(); |
| 109 | + } |
| 110 | +} |
0 commit comments