Merge lp:~tjoneslo/akiban-server/rest-parse-dates-timestamp into lp:~akiban-technologies/akiban-server/trunk

Proposed by Thomas Jones-Low
Status: Merged
Approved by: Nathan Williams
Approved revision: 2612
Merged at revision: 2607
Proposed branch: lp:~tjoneslo/akiban-server/rest-parse-dates-timestamp
Merge into: lp:~akiban-technologies/akiban-server/trunk
Diff against target: 178 lines (+61/-12)
4 files modified
src/main/java/com/akiban/ais/model/aisb2/AISBBasedBuilder.java (+14/-4)
src/main/java/com/akiban/ais/model/aisb2/NewUserTableBuilder.java (+3/-0)
src/main/java/com/akiban/server/entity/changes/EntityParser.java (+20/-3)
src/test/java/com/akiban/server/entity/changes/EntityParserIT.java (+24/-5)
To merge this branch: bzr merge lp:~tjoneslo/akiban-server/rest-parse-dates-timestamp
Reviewer Review Type Date Requested Status
Nathan Williams Approve
Thomas Jones-Low Needs Resubmitting
Review via email: mp+156384@code.launchpad.net

Description of the change

Add datetime discovery for the REST EntityParser.

Now performs two checks on strings to see if they might be, and exactly match, a ISO formatted date time. If so, create the column as a timestamp rather than a string.

To post a comment you must log in.
Revision history for this message
Nathan Williams (nwilliams) wrote :

TIMESTAMP is a little funny, so we may want to add a colDatetime to get a DATETIME instead. Otherwise looks as I would expect.

review: Needs Fixing
2612. By Thomas Jones-Low

Add colDateTime to AISBuilder, use in EntityParser

Revision history for this message
Thomas Jones-Low (tjoneslo) wrote :

Update per comments, now creates a DateTime column for the entity.

review: Needs Resubmitting
Revision history for this message
Nathan Williams (nwilliams) wrote :

Excellent. Thanks.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/main/java/com/akiban/ais/model/aisb2/AISBBasedBuilder.java'
2--- src/main/java/com/akiban/ais/model/aisb2/AISBBasedBuilder.java 2013-03-25 22:50:36 +0000
3+++ src/main/java/com/akiban/ais/model/aisb2/AISBBasedBuilder.java 2013-04-01 18:38:20 +0000
4@@ -19,10 +19,8 @@
5
6 import com.akiban.ais.model.AISBuilder;
7 import com.akiban.ais.model.AkibanInformationSchema;
8-import com.akiban.ais.model.DefaultNameGenerator;
9 import com.akiban.ais.model.Group;
10 import com.akiban.ais.model.Index;
11-import com.akiban.ais.model.NameGenerator;
12 import com.akiban.ais.model.Parameter;
13 import com.akiban.ais.model.Routine;
14 import com.akiban.ais.model.TableName;
15@@ -55,7 +53,7 @@
16 return new ActualBuilder().defaultSchema(defaultSchema);
17 }
18
19- private static class ActualBuilder implements NewAISBuilder, NewViewBuilder, NewAkibanJoinBuilder, NewRoutineBuilder, NewSQLJJarBuilder {
20+ private static class ActualBuilder implements NewViewBuilder, NewAkibanJoinBuilder, NewRoutineBuilder, NewSQLJJarBuilder {
21
22 // NewAISProvider interface
23
24@@ -326,6 +324,18 @@
25 aisb.column(schema, userTable, name, uTableColumnPos++, "TEXT", null, null, nullable, false, null, null);
26 return this;
27 }
28+
29+ @Override
30+ public NewUserTableBuilder colDateTime (String name) {
31+ return colDateTime(name, NULLABLE_DEFAULT);
32+ }
33+
34+ @Override
35+ public NewUserTableBuilder colDateTime (String name, boolean nullable) {
36+ aisb.column(schema, userTable, name, uTableColumnPos++, "DATETIME", null, null, nullable, false, null, null);
37+ return this;
38+ }
39+
40
41 @Override
42 public NewUserTableBuilder pk(String... columns) {
43@@ -552,7 +562,7 @@
44 catch (MalformedURLException ex1) {
45 File file = new File(value);
46 try {
47- url = file.toURL();
48+ url = file.toURI().toURL();
49 }
50 catch (MalformedURLException ex2) {
51 // Report original failure.
52
53=== modified file 'src/main/java/com/akiban/ais/model/aisb2/NewUserTableBuilder.java'
54--- src/main/java/com/akiban/ais/model/aisb2/NewUserTableBuilder.java 2013-03-25 22:50:36 +0000
55+++ src/main/java/com/akiban/ais/model/aisb2/NewUserTableBuilder.java 2013-04-01 18:38:20 +0000
56@@ -122,6 +122,9 @@
57
58 NewUserTableBuilder colText(String name);
59 NewUserTableBuilder colText(String name, boolean nullable);
60+
61+ NewUserTableBuilder colDateTime (String name);
62+ NewUserTableBuilder colDateTime (String name, boolean nullable);
63
64 /**
65 * Adds a PK
66
67=== modified file 'src/main/java/com/akiban/server/entity/changes/EntityParser.java'
68--- src/main/java/com/akiban/server/entity/changes/EntityParser.java 2013-03-26 16:44:27 +0000
69+++ src/main/java/com/akiban/server/entity/changes/EntityParser.java 2013-04-01 18:38:20 +0000
70@@ -19,8 +19,11 @@
71 import java.io.IOException;
72 import java.util.Iterator;
73 import java.util.Map.Entry;
74+import java.util.regex.Matcher;
75+import java.util.regex.Pattern;
76
77 import org.codehaus.jackson.JsonNode;
78+import org.joda.time.format.ISODateTimeFormat;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
81
82@@ -152,11 +155,25 @@
83 }
84 }
85 }
86-
87+ private static final Pattern DATE_PATTERN = Pattern.compile("^((\\d+)-(\\d+)-(\\d+)).*");
88 private void addColumnToTable (JsonNode node, String name, NewUserTableBuilder table) {
89 if (node.isTextual()) {
90- int len = Math.max(node.asText().length(), stringWidth);
91- table.colString(name, len, true);
92+ boolean dateColumn = false;
93+ // Do a simple "could be a date" check first, if so, do an exact verify.
94+ Matcher m = DATE_PATTERN.matcher(node.asText().trim());
95+ if (m.matches()) {
96+ try {
97+ ISODateTimeFormat.dateTimeParser().parseDateTime(node.asText());
98+ table.colDateTime(name, true);
99+ dateColumn = true;
100+ } catch (IllegalArgumentException ex) {
101+ dateColumn = false;
102+ }
103+ }
104+ if (!dateColumn) {
105+ int len = Math.max(node.asText().length(), stringWidth);
106+ table.colString(name, len, true);
107+ }
108 } else if (node.isIntegralNumber()) {
109 table.colBigInt(name, true);
110 } else if (node.isDouble()) {
111
112=== modified file 'src/test/java/com/akiban/server/entity/changes/EntityParserIT.java'
113--- src/test/java/com/akiban/server/entity/changes/EntityParserIT.java 2013-03-26 16:02:35 +0000
114+++ src/test/java/com/akiban/server/entity/changes/EntityParserIT.java 2013-04-01 18:38:20 +0000
115@@ -28,8 +28,6 @@
116 import org.codehaus.jackson.JsonProcessingException;
117 import org.junit.Before;
118 import org.junit.Test;
119-import org.slf4j.Logger;
120-import org.slf4j.LoggerFactory;
121
122 import com.akiban.ais.model.AkibanInformationSchema;
123 import com.akiban.ais.model.Join;
124@@ -39,7 +37,6 @@
125 import com.akiban.server.test.it.ITBase;
126
127 public class EntityParserIT extends ITBase {
128- private static final Logger LOG = LoggerFactory.getLogger(EntityParserIT.class);
129
130 private EntityParser parser;
131
132@@ -68,7 +65,7 @@
133 @Test
134 public void testOrders() throws JsonProcessingException, IOException {
135 TableName tableName = new TableName ("test", "orders");
136- String postInput = "{\"oid\" : 103, \"cid\" : 2, \"odate\": \"2012-12-31 12:00:00\"}";
137+ String postInput = "{\"oid\" : 103, \"cid\" : 2, \"odate\": \"2012-12-31T12:00:00\"}";
138 AkibanInformationSchema ais = processParse(tableName, postInput);
139
140 assertTrue(ais.getTable(tableName).getColumns().size() == 3);
141@@ -128,7 +125,7 @@
142 TableName tableName = new TableName ("test", "customers");
143 String postInput ="{\"cid\": 6, \"first_name\": \"John\",\"last_name\": \"Smith\"," +
144 "\"addresses\": {\"aid\": 104, \"cid\": 6, \"state\": \"MA\", \"city\": \"Boston\"}," +
145- "\"orders\": {\"oid\" : 103, \"cid\" : 2, \"odate\": \"2012-12-31 12:00:00\"}}";
146+ "\"orders\": {\"oid\" : 103, \"cid\" : 2, \"odate\": \"2012-12-31T12:00:00\"}}";
147 AkibanInformationSchema ais = processParse(tableName, postInput);
148
149 UserTable c = ais.getUserTable(tableName);
150@@ -288,6 +285,28 @@
151 assertTrue(ais.getTable(tableName).getColumn(1).getType().equals(Types.BOOLEAN));
152 }
153
154+ @Test
155+ public void testDateGood() throws IOException {
156+ TableName tableName = new TableName ("test", "customers");
157+ String postInput = "{\"cid\":6, \"order_date\":\"1970-01-01T00:00:01Z\"}";
158+
159+ AkibanInformationSchema ais = processParse (tableName, postInput);
160+ assertTrue(ais.getTable(tableName).getColumns().size() == 2);
161+ assertTrue(ais.getTable(tableName).getColumn(0).getType().equals(Types.BIGINT));
162+ assertTrue(ais.getTable(tableName).getColumn(1).getType().equals(Types.DATETIME));
163+ }
164+
165+ @Test
166+ public void testDateBad() throws IOException {
167+ TableName tableName = new TableName ("test", "customers");
168+ String postInput = "{\"cid\":6, \"phone-number\":\"802-555-1212\", \"ssn\": \"000-41-9999\"}";
169+ AkibanInformationSchema ais = processParse (tableName, postInput);
170+ assertTrue(ais.getTable(tableName).getColumns().size() == 3);
171+ assertTrue(ais.getTable(tableName).getColumn(0).getType().equals(Types.BIGINT));
172+ assertTrue(ais.getTable(tableName).getColumn(1).getType().equals(Types.VARCHAR));
173+ assertTrue(ais.getTable(tableName).getColumn(2).getType().equals(Types.VARCHAR));
174+ }
175+
176 private AkibanInformationSchema processParse (TableName tableName, String postInput) throws JsonProcessingException, IOException{
177 JsonNode node = readTree(postInput);
178 parser.parseAndCreate(ddl(), session(), tableName, node);

Subscribers

People subscribed via source and target branches