1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.hbql.statement;
22
23 import org.apache.hadoop.hbase.hbql.client.ExecutionResults;
24 import org.apache.hadoop.hbase.hbql.client.HBqlException;
25 import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
26 import org.apache.hadoop.hbase.hbql.mapping.AttribMapping;
27 import org.apache.hadoop.hbase.hbql.mapping.FamilyMapping;
28 import org.apache.hadoop.hbase.hbql.mapping.TableMapping;
29 import org.apache.hadoop.hbase.hbql.statement.args.KeyInfo;
30 import org.apache.hadoop.hbase.hbql.util.Sets;
31
32 import java.util.List;
33 import java.util.Set;
34
35 public class CreateMappingStatement extends StatementWithMapping implements ConnectionStatement {
36
37 private final boolean tempMapping;
38 private final boolean systemMapping;
39 private final String tableName;
40 private final KeyInfo keyInfo;
41 private final List<FamilyMapping> familyMappingList;
42
43 public CreateMappingStatement(final StatementPredicate predicate,
44 final boolean tempMapping,
45 final boolean systemMapping,
46 final String mappingName,
47 final String tableName,
48 final AttribMapping attribMapping) {
49 super(predicate, mappingName);
50 this.tempMapping = tempMapping;
51 this.systemMapping = systemMapping;
52 this.tableName = (tableName == null || tableName.length() == 0) ? mappingName : tableName;
53 this.keyInfo = attribMapping != null ? attribMapping.getKeyInfo() : null;
54 this.familyMappingList = attribMapping != null ? attribMapping.getFamilyMappingList() : null;
55 }
56
57 private boolean isTempMapping() {
58 return this.tempMapping;
59 }
60
61 private boolean isSystemMapping() {
62 return this.systemMapping;
63 }
64
65 private String getTableName() {
66 return this.tableName;
67 }
68
69 public KeyInfo getKeyInfo() {
70 return this.keyInfo;
71 }
72
73 private List<FamilyMapping> getFamilyMappingList() {
74 return familyMappingList;
75 }
76
77 public void validate() throws HBqlException {
78
79
80 if (this.getKeyInfo() != null)
81 this.getKeyInfo().validate();
82
83
84 if (this.getFamilyMappingList() != null) {
85 final Set<String> nameSet = Sets.newHashSet();
86 for (final FamilyMapping familyMapping : this.getFamilyMappingList()) {
87
88 familyMapping.validate();
89
90 final String familyName = familyMapping.getFamilyName();
91 if (nameSet.contains(familyName))
92 throw new HBqlException("Family name already mapped: " + familyName);
93
94 nameSet.add(familyName);
95 }
96 }
97 }
98
99 protected ExecutionResults execute(final HConnectionImpl conn) throws HBqlException {
100
101 final TableMapping tableMapping = conn.createMapping(this.isTempMapping(),
102 this.isSystemMapping(),
103 this.getMappingContext().getMappingName(),
104 this.getTableName(),
105 this.getKeyInfo(),
106 this.getFamilyMappingList());
107 this.getMappingContext().setMapping(tableMapping);
108 tableMapping.validate(tableMapping.getMappingName());
109 return new ExecutionResults("Mapping " + tableMapping.getMappingName() + " defined.");
110 }
111
112 public static String usage() {
113 return "CREATE [TEMP] MAPPING mapping_name [FOR TABLE table_name] [(key_name KEY [WIDTH int_expr], family_mapping_list)] [IF bool_expr]";
114 }
115 }