Elffuss BraidHub

kikocis/java-driver public

Merge pull request #618 from datastax/java1094

JAVA-1094: Backport TypeCodec parse and format fixes from 3.0.
Changeset064e9c2c785030b36cb73ae0e28a6afb2cbab18665a97eac5d7ddd09d51022d2
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-03-09 22:38 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-03-09 22:38 +01:00
Parents6ae8ffe222d7, 73a81f732ac5

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -37,6 +37,7 @@
3737 - [bug] JAVA-727: Allow monotonic timestamp generators to drift in the future + use microsecond precision when possible.
3838 - [improvement] JAVA-444: Add Java process information to UUIDs.makeNode() hash.
3939 - [improvement] JAVA-977: Preserve original cause when BuiltStatement value can't be serialized.
40+- [bug] JAVA-1094: Backport TypeCodec parse and format fixes from 3.0.
4041
4142 Merged from 2.0 branch:
4243
modified driver-core/src/main/java/com/datastax/driver/core/ParseUtils.java +68 −0
@@ -159,4 +159,72 @@
159159 public static boolean isBlank(int c) {
160160 return c == ' ' || c == '\t' || c == '\n';
161161 }
162+
163+ /**
164+ * Return {@code true} if the given string is surrounded
165+ * by single quotes, and {@code false} otherwise.
166+ *
167+ * @param value The string to inspect.
168+ * @return {@code true} if the given string is surrounded
169+ * by single quotes, and {@code false} otherwise.
170+ */
171+ public static boolean isQuoted(String value) {
172+ return value != null && value.length() > 1 && value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\'';
173+ }
174+
175+ /**
176+ * Quote the given string; single quotes are escaped.
177+ * If the given string is null, this method returns a quoted empty string ({@code ''}).
178+ *
179+ * @param value The value to quote.
180+ * @return The quoted string.
181+ */
182+ public static String quote(String value) {
183+ return '\'' + replaceChar(value, '\'', "''") + '\'';
184+ }
185+
186+ /**
187+ * Unquote the given string if it is quoted; single quotes are unescaped.
188+ * If the given string is not quoted, it is returned without any modification.
189+ *
190+ * @param value The string to unquote.
191+ * @return The unquoted string.
192+ */
193+ public static String unquote(String value) {
194+ if (!isQuoted(value))
195+ return value;
196+ return value.substring(1, value.length() - 1).replace("''", "'");
197+ }
198+
199+ // Simple method to replace a single character. String.replace is a bit too
200+ // inefficient (see JAVA-67)
201+ static String replaceChar(String text, char search, String replacement) {
202+ if (text == null || text.isEmpty())
203+ return text;
204+
205+ int nbMatch = 0;
206+ int start = -1;
207+ do {
208+ start = text.indexOf(search, start + 1);
209+ if (start != -1)
210+ ++nbMatch;
211+ } while (start != -1);
212+
213+ if (nbMatch == 0)
214+ return text;
215+
216+ int newLength = text.length() + nbMatch * (replacement.length() - 1);
217+ char[] result = new char[newLength];
218+ int newIdx = 0;
219+ for (int i = 0; i < text.length(); i++) {
220+ char c = text.charAt(i);
221+ if (c == search) {
222+ for (int r = 0; r < replacement.length(); r++)
223+ result[newIdx++] = replacement.charAt(r);
224+ } else {
225+ result[newIdx++] = c;
226+ }
227+ }
228+ return new String(result);
229+ }
162230 }
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +79 −46
@@ -370,47 +370,20 @@
370370
371371 @Override
372372 public String parse(String value) {
373- if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'')
373+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
374+ return null;
375+
376+ if (!ParseUtils.isQuoted(value))
374377 throw new InvalidTypeException("text values must enclosed by a single quotes");
375378
376- return value.substring(1, value.length() - 1).replace("''", "'");
379+ return ParseUtils.unquote(value);
377380 }
378381
379382 @Override
380383 public String format(String value) {
381- return '\'' + replace(value, '\'', "''") + '\'';
382- }
383-
384- // Simple method to replace a single character. String.replace is a bit too
385- // inefficient (see JAVA-67)
386- static String replace(String text, char search, String replacement) {
387- if (text == null || text.isEmpty())
388- return text;
389-
390- int nbMatch = 0;
391- int start = -1;
392- do {
393- start = text.indexOf(search, start + 1);
394- if (start != -1)
395- ++nbMatch;
396- } while (start != -1);
397-
398- if (nbMatch == 0)
399- return text;
400-
401- int newLength = text.length() + nbMatch * (replacement.length() - 1);
402- char[] result = new char[newLength];
403- int newIdx = 0;
404- for (int i = 0; i < text.length(); i++) {
405- char c = text.charAt(i);
406- if (c == search) {
407- for (int r = 0; r < replacement.length(); r++)
408- result[newIdx++] = replacement.charAt(r);
409- } else {
410- result[newIdx++] = c;
411- }
412- }
413- return new String(result);
384+ if (value == null)
385+ return "NULL";
386+ return ParseUtils.quote(value);
414387 }
415388
416389 @Override
@@ -432,7 +405,7 @@
432405 @Override
433406 public Long parse(String value) {
434407 try {
435- return Long.parseLong(value);
408+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Long.parseLong(value);
436409 } catch (NumberFormatException e) {
437410 throw new InvalidTypeException(String.format("Cannot parse 64-bits long value from \"%s\"", value));
438411 }
@@ -440,6 +413,8 @@
440413
441414 @Override
442415 public String format(Long value) {
416+ if (value == null)
417+ return "NULL";
443418 return Long.toString(value);
444419 }
445420
@@ -474,11 +449,13 @@
474449
475450 @Override
476451 public ByteBuffer parse(String value) {
477- return Bytes.fromHexString(value);
452+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Bytes.fromHexString(value);
478453 }
479454
480455 @Override
481456 public String format(ByteBuffer value) {
457+ if (value == null)
458+ return "NULL";
482459 return Bytes.toHexString(value);
483460 }
484461
@@ -502,6 +479,8 @@
502479
503480 @Override
504481 public Boolean parse(String value) {
482+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
483+ return null;
505484 if (value.equalsIgnoreCase(Boolean.FALSE.toString()))
506485 return false;
507486 if (value.equalsIgnoreCase(Boolean.TRUE.toString()))
@@ -512,6 +491,8 @@
512491
513492 @Override
514493 public String format(Boolean value) {
494+ if (value == null)
495+ return "NULL";
515496 return value ? "true" : "false";
516497 }
517498
@@ -545,7 +526,7 @@
545526 @Override
546527 public BigDecimal parse(String value) {
547528 try {
548- return new BigDecimal(value);
529+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : new BigDecimal(value);
549530 } catch (NumberFormatException e) {
550531 throw new InvalidTypeException(String.format("Cannot parse decimal value from \"%s\"", value));
551532 }
@@ -553,6 +534,8 @@
553534
554535 @Override
555536 public String format(BigDecimal value) {
537+ if (value == null)
538+ return "NULL";
556539 return value.toString();
557540 }
558541
@@ -592,7 +575,7 @@
592575 @Override
593576 public Double parse(String value) {
594577 try {
595- return Double.parseDouble(value);
578+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Double.parseDouble(value);
596579 } catch (NumberFormatException e) {
597580 throw new InvalidTypeException(String.format("Cannot parse 64-bits double value from \"%s\"", value));
598581 }
@@ -600,6 +583,8 @@
600583
601584 @Override
602585 public String format(Double value) {
586+ if (value == null)
587+ return "NULL";
603588 return Double.toString(value);
604589 }
605590
@@ -635,7 +620,7 @@
635620 @Override
636621 public Float parse(String value) {
637622 try {
638- return Float.parseFloat(value);
623+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Float.parseFloat(value);
639624 } catch (NumberFormatException e) {
640625 throw new InvalidTypeException(String.format("Cannot parse 32-bits float value from \"%s\"", value));
641626 }
@@ -643,6 +628,8 @@
643628
644629 @Override
645630 public String format(Float value) {
631+ if (value == null)
632+ return "NULL";
646633 return Float.toString(value);
647634 }
648635
@@ -677,11 +664,14 @@
677664
678665 @Override
679666 public InetAddress parse(String value) {
667+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
668+ return null;
669+
680670 value = value.trim();
681- if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'')
671+ if (!ParseUtils.isQuoted(value))
682672 throw new InvalidTypeException(String.format("inet values must be enclosed in single quotes (\"%s\")", value));
683673 try {
684- return InetAddress.getByName(value.substring(1, value.length() - 1));
674+ return InetAddress.getByName(ParseUtils.unquote(value));
685675 } catch (Exception e) {
686676 throw new InvalidTypeException(String.format("Cannot parse inet value from \"%s\"", value));
687677 }
@@ -689,6 +679,8 @@
689679
690680 @Override
691681 public String format(InetAddress value) {
682+ if (value == null)
683+ return "NULL";
692684 return "'" + value.getHostAddress() + "'";
693685 }
694686
@@ -715,7 +707,7 @@
715707 @Override
716708 public Integer parse(String value) {
717709 try {
718- return Integer.parseInt(value);
710+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Integer.parseInt(value);
719711 } catch (NumberFormatException e) {
720712 throw new InvalidTypeException(String.format("Cannot parse 32-bits int value from \"%s\"", value));
721713 }
@@ -723,6 +715,8 @@
723715
724716 @Override
725717 public String format(Integer value) {
718+ if (value == null)
719+ return "NULL";
726720 return Integer.toString(value);
727721 }
728722
@@ -801,6 +795,12 @@
801795
802796 @Override
803797 public Date parse(String value) {
798+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
799+ return null;
800+
801+ if (ParseUtils.isQuoted(value))
802+ value = ParseUtils.unquote(value);
803+
804804 if (IS_LONG_PATTERN.matcher(value).matches()) {
805805 try {
806806 return new Date(Long.parseLong(value));
@@ -818,6 +818,8 @@
818818
819819 @Override
820820 public String format(Date value) {
821+ if (value == null)
822+ return "NULL";
821823 return Long.toString(value.getTime());
822824 }
823825
@@ -840,7 +842,7 @@
840842 @Override
841843 public UUID parse(String value) {
842844 try {
843- return UUID.fromString(value);
845+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : UUID.fromString(value);
844846 } catch (IllegalArgumentException e) {
845847 throw new InvalidTypeException(String.format("Cannot parse UUID value from \"%s\"", value));
846848 }
@@ -848,6 +850,8 @@
848850
849851 @Override
850852 public String format(UUID value) {
853+ if (value == null)
854+ return "NULL";
851855 return value.toString();
852856 }
853857
@@ -873,7 +877,7 @@
873877 @Override
874878 public UUID parse(String value) {
875879 UUID id = super.parse(value);
876- if (id.version() != 1)
880+ if (id != null && id.version() != 1)
877881 throw new InvalidTypeException(String.format("Cannot parse type 1 UUID value from \"%s\": represents a type %d UUID", value, id.version()));
878882 return id;
879883 }
@@ -895,7 +899,7 @@
895899 @Override
896900 public BigInteger parse(String value) {
897901 try {
898- return new BigInteger(value);
902+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : new BigInteger(value);
899903 } catch (NumberFormatException e) {
900904 throw new InvalidTypeException(String.format("Cannot parse varint value from \"%s\"", value));
901905 }
@@ -903,6 +907,8 @@
903907
904908 @Override
905909 public String format(BigInteger value) {
910+ if (value == null)
911+ return "NULL";
906912 return value.toString();
907913 }
908914
@@ -929,6 +935,9 @@
929935
930936 @Override
931937 public List<T> parse(String value) {
938+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
939+ return null;
940+
932941 int idx = ParseUtils.skipSpaces(value, 0);
933942 if (value.charAt(idx++) != '[')
934943 throw new InvalidTypeException(String.format("cannot parse list value from \"%s\", at character %d expecting '[' but got '%c'", value, idx, value.charAt(idx)));
@@ -963,6 +972,9 @@
963972
964973 @Override
965974 public String format(List<T> value) {
975+ if (value == null)
976+ return "NULL";
977+
966978 StringBuilder sb = new StringBuilder();
967979 sb.append("[");
968980 for (int i = 0; i < value.size(); i++) {
@@ -1012,6 +1024,9 @@
10121024
10131025 @Override
10141026 public Set<T> parse(String value) {
1027+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
1028+ return null;
1029+
10151030 int idx = ParseUtils.skipSpaces(value, 0);
10161031 if (value.charAt(idx++) != '{')
10171032 throw new InvalidTypeException(String.format("cannot parse set value from \"%s\", at character %d expecting '{' but got '%c'", value, idx, value.charAt(idx)));
@@ -1046,6 +1061,9 @@
10461061
10471062 @Override
10481063 public String format(Set<T> value) {
1064+ if (value == null)
1065+ return "NULL";
1066+
10491067 StringBuilder sb = new StringBuilder();
10501068 sb.append("{");
10511069 int i = 0;
@@ -1098,6 +1116,9 @@
10981116
10991117 @Override
11001118 public Map<K, V> parse(String value) {
1119+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
1120+ return null;
1121+
11011122 int idx = ParseUtils.skipSpaces(value, 0);
11021123 if (value.charAt(idx++) != '{')
11031124 throw new InvalidTypeException(String.format("cannot parse map value from \"%s\", at character %d expecting '{' but got '%c'", value, idx, value.charAt(idx)));
@@ -1149,6 +1170,9 @@
11491170
11501171 @Override
11511172 public String format(Map<K, V> value) {
1173+ if (value == null)
1174+ return "NULL";
1175+
11521176 StringBuilder sb = new StringBuilder();
11531177 sb.append("{");
11541178 int i = 0;
@@ -1201,11 +1225,15 @@
12011225
12021226 @Override
12031227 public UDTValue parse(String value) {
1228+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
1229+ return null;
12041230 return definition.parseValue(value);
12051231 }
12061232
12071233 @Override
12081234 public String format(UDTValue value) {
1235+ if (value == null)
1236+ return "NULL";
12091237 return value.toString();
12101238 }
12111239
@@ -1251,6 +1279,9 @@
12511279
12521280 @Override
12531281 public TupleValue parse(String value) {
1282+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
1283+ return null;
1284+
12541285 TupleValue v = type.newValue();
12551286
12561287 int idx = ParseUtils.skipSpaces(value, 0);
@@ -1290,6 +1321,8 @@
12901321
12911322 @Override
12921323 public String format(TupleValue value) {
1324+ if (value == null)
1325+ return "NULL";
12931326 return value.toString();
12941327 }
12951328
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeTest.java +15 −0
@@ -24,6 +24,8 @@
2424 import java.math.BigInteger;
2525 import java.net.InetAddress;
2626 import java.nio.ByteBuffer;
27+import java.text.ParseException;
28+import java.text.SimpleDateFormat;
2729 import java.util.*;
2830
2931 import static com.datastax.driver.core.Assertions.assertThat;
@@ -284,4 +286,17 @@
284286 .cannotBeDeserializedAs(new TypeToken<Map<Integer, Collection<Integer>>>() {
285287 });
286288 }
289+
290+ @Test(groups = "unit")
291+ public void should_handle_quotes_in_map_elements() throws ParseException {
292+ DataType type = DataType.map(DataType.timestamp(), DataType.text());
293+ String text = "{ '2013-9-22 12:01' : 'birthday wishes to Bilbo', '2013-10-1 18:00' : 'Check into Inn of Prancing Pony' }";
294+ @SuppressWarnings("unchecked")
295+ Map<Date, String> map = (Map<Date, String>) type.parse(text);
296+
297+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
298+ assertThat(map)
299+ .containsEntry(format.parse("2013-9-22 12:01"), "birthday wishes to Bilbo")
300+ .containsEntry(format.parse("2013-10-1 18:00"), "Check into Inn of Prancing Pony");
301+ }
287302 }