1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.ning.metrics.goodwill.access;
18
19 import com.google.common.collect.ImmutableMap;
20 import com.ning.metrics.serialization.schema.SchemaField;
21 import com.ning.metrics.serialization.schema.SchemaFieldType;
22 import org.codehaus.jackson.JsonGenerationException;
23 import org.codehaus.jackson.annotate.JsonCreator;
24 import org.codehaus.jackson.annotate.JsonProperty;
25 import org.codehaus.jackson.annotate.JsonValue;
26 import org.codehaus.jackson.map.ObjectMapper;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30
31
32
33
34
35
36
37 public class GoodwillSchemaField
38 {
39 private static final ObjectMapper mapper = new ObjectMapper();
40
41 public static final String JSON_THRIFT_FIELD_NAME = "name";
42 public static final String JSON_THRIFT_FIELD_TYPE = "type";
43 public static final String JSON_THRIFT_FIELD_ID = "position";
44 public static final String JSON_THRIFT_FIELD_DESCRIPTION = "description";
45
46
47
48
49
50
51
52 public static final String JSON_THRIFT_FIELD_SQL_KEY = "sql";
53 public static final String JSON_THRIFT_FIELD_SQL_TYPE = "type";
54 public static final String JSON_THRIFT_FIELD_SQL_LENGTH = "length";
55 public static final String JSON_THRIFT_FIELD_SQL_SCALE = "scale";
56 public static final String JSON_THRIFT_FIELD_SQL_PRECISION = "precision";
57
58 private final SchemaField schemaField;
59 private final String description;
60 private Sql sql;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 @JsonCreator
84 @SuppressWarnings("unused")
85 public GoodwillSchemaField(
86 @JsonProperty(JSON_THRIFT_FIELD_NAME) final String name,
87 @JsonProperty(JSON_THRIFT_FIELD_TYPE) final String type,
88 @JsonProperty(JSON_THRIFT_FIELD_ID) final short id,
89 @JsonProperty(JSON_THRIFT_FIELD_DESCRIPTION) final String description,
90 @JsonProperty(JSON_THRIFT_FIELD_SQL_KEY) final Sql sql
91 )
92 {
93 this(name, type, id, description, sql == null ? null : sql.getType(), sql == null ? null : sql.getLength(), sql == null ? null : sql.getScale(), sql == null ? null : sql.getPrecision());
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public GoodwillSchemaField(
109 final String name,
110 final String type,
111 final short id,
112 final String description,
113 final String sqlType,
114 final Integer sqlLength,
115 final Integer sqlScale,
116 final Integer sqlPrecision
117 )
118 {
119 if (name == null) {
120 throw new IllegalArgumentException("GoodwillSchemaField name can't be null");
121 }
122
123 if ((sqlType == null || sqlType.equals("string")) && (sqlScale != null || sqlPrecision != null)) {
124 throw new IllegalArgumentException("Strings cannot have a scale or precision");
125 }
126
127 this.schemaField = SchemaFieldType.createSchemaField(name, type, id);
128
129
130 sql = new Sql(sqlType, sqlLength, sqlScale, sqlPrecision);
131 this.description = description;
132 }
133
134 public GoodwillSchemaField(final SchemaField field)
135 {
136 this(field.getName(), field.getType().name(), field.getId(), null, null);
137 }
138
139 public static GoodwillSchemaField decode(final String thriftItemJson) throws IOException
140 {
141 return mapper.readValue(thriftItemJson, GoodwillSchemaField.class);
142 }
143
144 @JsonValue
145 @SuppressWarnings({"unchecked", "unused"})
146 public ImmutableMap toMap()
147 {
148 return new ImmutableMap.Builder()
149 .put(JSON_THRIFT_FIELD_NAME, getName())
150 .put(JSON_THRIFT_FIELD_TYPE, getType())
151 .put(JSON_THRIFT_FIELD_ID, getId())
152 .put(JSON_THRIFT_FIELD_DESCRIPTION, getDescription() == null ? "" : getDescription())
153 .put(JSON_THRIFT_FIELD_SQL_KEY, getSql() == null ? "" : getSql())
154 .build();
155 }
156
157
158
159
160 public static class Sql
161 {
162 private final String type;
163 private final Integer length;
164 private final Integer scale;
165 private final Integer precision;
166
167 @JsonCreator
168 public Sql(
169 @JsonProperty(JSON_THRIFT_FIELD_SQL_TYPE) final String type,
170 @JsonProperty(JSON_THRIFT_FIELD_SQL_LENGTH) final Integer length,
171 @JsonProperty(JSON_THRIFT_FIELD_SQL_SCALE) final Integer scale,
172 @JsonProperty(JSON_THRIFT_FIELD_SQL_PRECISION) final Integer precision)
173 {
174 this.type = type;
175 this.length = length;
176 this.scale = scale;
177 this.precision = precision;
178 }
179
180
181 @JsonValue
182 @SuppressWarnings({"unchecked", "unused"})
183 public ImmutableMap toMap()
184 {
185 return new ImmutableMap.Builder()
186 .put(JSON_THRIFT_FIELD_SQL_TYPE, getType() == null ? "" : getType())
187 .put(JSON_THRIFT_FIELD_SQL_LENGTH, getLength() == null ? "" : getLength())
188 .put(JSON_THRIFT_FIELD_SQL_SCALE, getScale() == null ? "" : getScale())
189 .put(JSON_THRIFT_FIELD_SQL_PRECISION, getPrecision() == null ? "" : getPrecision())
190 .build();
191 }
192
193 public String getType()
194 {
195 return type;
196 }
197
198 public Integer getLength()
199 {
200 return length;
201 }
202
203 public Integer getScale()
204 {
205 return scale;
206 }
207
208 public Integer getPrecision()
209 {
210 return precision;
211 }
212 }
213
214 public String getName()
215 {
216 return schemaField.getName();
217 }
218
219 public SchemaFieldType getType()
220 {
221 return schemaField.getType();
222 }
223
224 public short getId()
225 {
226 return schemaField.getId();
227 }
228
229 public Sql getSql()
230 {
231 return sql;
232 }
233
234 public String getDescription()
235 {
236 return description;
237 }
238
239 @Override
240 public String toString()
241 {
242 try {
243 return toJSON().toString();
244 }
245 catch (JsonGenerationException e) {
246 return "GoodwillSchemaField{" +
247 JSON_THRIFT_FIELD_NAME + "='" + getName() + '\'' +
248 ", " + JSON_THRIFT_FIELD_TYPE + "='" + getType() + '\'' +
249 ", " + JSON_THRIFT_FIELD_ID + "=" + getId() +
250 ", " + JSON_THRIFT_FIELD_SQL_TYPE + "='" + sql.type + '\'' +
251 ", " + JSON_THRIFT_FIELD_SQL_LENGTH + "=" + sql.length +
252 ", " + JSON_THRIFT_FIELD_SQL_SCALE + "=" + sql.scale +
253 ", " + JSON_THRIFT_FIELD_SQL_PRECISION + "=" + sql.precision +
254 ", " + JSON_THRIFT_FIELD_DESCRIPTION + "=" + description +
255 '}';
256 }
257 catch (IOException e) {
258 return "GoodwillSchemaField{" +
259 JSON_THRIFT_FIELD_NAME + "='" + getName() + '\'' +
260 ", " + JSON_THRIFT_FIELD_TYPE + "='" + getType() + '\'' +
261 ", " + JSON_THRIFT_FIELD_ID + "=" + getId() +
262 ", " + JSON_THRIFT_FIELD_SQL_TYPE + "='" + sql.type + '\'' +
263 ", " + JSON_THRIFT_FIELD_SQL_LENGTH + "=" + sql.length +
264 ", " + JSON_THRIFT_FIELD_SQL_SCALE + "=" + sql.scale +
265 ", " + JSON_THRIFT_FIELD_SQL_PRECISION + "=" + sql.precision +
266 ", " + JSON_THRIFT_FIELD_DESCRIPTION + "=" + description +
267 '}';
268 }
269 }
270
271
272
273
274
275
276
277
278
279
280 public ByteArrayOutputStream toJSON() throws IOException
281 {
282 final ByteArrayOutputStream out = new ByteArrayOutputStream();
283 mapper.writeValue(out, this);
284 return out;
285 }
286
287
288
289
290
291
292
293 @SuppressWarnings("unused")
294 public String getFullSQLType()
295 {
296 String fullSQLType = null;
297
298 if (sql.type == null) {
299 return null;
300 }
301 else if (sql.type.equals("decimal") || sql.type.equals("numeric")) {
302 if (sql.precision != null) {
303 if (sql.scale != null) {
304 fullSQLType = sql.type + "(" + sql.precision + ", " + sql.scale + ")";
305 }
306 else {
307 fullSQLType = sql.type + "(" + sql.precision + ")";
308 }
309 }
310 }
311 else {
312 if (sql.type.equals("nvarchar") || sql.type.equals("varchar")) {
313 if (sql.length != null) {
314 fullSQLType = sql.type + "(" + sql.length + ")";
315 }
316 }
317 }
318
319 if (fullSQLType == null) {
320 fullSQLType = sql.type;
321 }
322
323 return fullSQLType;
324 }
325 }