kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | c0702f73685f15cbc9f4b3b3373a799e88f2b31fd3e576d00718d5b25b90d10e |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-09-06 18:47 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-09-16 10:45 -07:00 |
| Parents | c16b0d92503b |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +11 −6
| @@ -292,14 +292,19 @@ | ||
| 292 | 292 | @Override |
| 293 | 293 | public String toString() { |
| 294 | 294 | try { |
| 295 | - if (forceNoValues) { | |
| 295 | + if (forceNoValues) | |
| 296 | 296 | return getQueryString(); |
| 297 | + // 1) try first with all values inlined (will not work if some values require custom codecs, | |
| 298 | + // or if the required codecs are registered in a different CodecRegistry instance than the default one) | |
| 299 | + return maybeAddSemicolon(buildQueryString(null, CodecRegistry.DEFAULT_INSTANCE)).toString(); | |
| 300 | + } catch (RuntimeException e1) { | |
| 301 | + // 2) try next with bind markers for all values to avoid usage of custom codecs | |
| 302 | + try { | |
| 303 | + return maybeAddSemicolon(buildQueryString(new ArrayList<Object>(), CodecRegistry.DEFAULT_INSTANCE)).toString(); | |
| 304 | + } catch (RuntimeException e2) { | |
| 305 | + // Ugly but we have absolutely no context to get the registry from | |
| 306 | + return String.format("built query (could not generate with default codec registry: %s)", e2.getMessage()); | |
| 297 | 307 | } |
| 298 | - StringBuilder queryString = buildQueryString(null, CodecRegistry.DEFAULT_INSTANCE); | |
| 299 | - return maybeAddSemicolon(queryString).toString(); | |
| 300 | - } catch (CodecNotFoundException e) { | |
| 301 | - // Ugly but we have absolutely no context to get the registry from | |
| 302 | - return String.format("built query (could not generate with default codec registry: %s)", e.getMessage()); | |
| 303 | 308 | } |
| 304 | 309 | } |
| 305 | 310 |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +59 −0
| @@ -999,4 +999,63 @@ | ||
| 999 | 999 | } |
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | + static class Foo { | |
| 1003 | + int bar; | |
| 1004 | + | |
| 1005 | + public Foo(int bar) { | |
| 1006 | + this.bar = bar; | |
| 1007 | + } | |
| 1008 | + } | |
| 1009 | + | |
| 1010 | + static class FooCodec extends TypeCodec<Foo> { | |
| 1011 | + | |
| 1012 | + public FooCodec() { | |
| 1013 | + super(DataType.cint(), Foo.class); | |
| 1014 | + } | |
| 1015 | + | |
| 1016 | + @Override | |
| 1017 | + public ByteBuffer serialize(Foo value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 1018 | + // not relevant for this test | |
| 1019 | + return null; | |
| 1020 | + } | |
| 1021 | + | |
| 1022 | + @Override | |
| 1023 | + public Foo deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 1024 | + // not relevant for this test | |
| 1025 | + return null; | |
| 1026 | + } | |
| 1027 | + | |
| 1028 | + @Override | |
| 1029 | + public Foo parse(String value) throws InvalidTypeException { | |
| 1030 | + // not relevant for this test | |
| 1031 | + return null; | |
| 1032 | + } | |
| 1033 | + | |
| 1034 | + @Override | |
| 1035 | + public String format(Foo foo) throws InvalidTypeException { | |
| 1036 | + return Integer.toString(foo.bar); | |
| 1037 | + } | |
| 1038 | + } | |
| 1039 | + | |
| 1040 | + /** | |
| 1041 | + * Ensures that a statement can be printed with and without | |
| 1042 | + * a required custom codec. | |
| 1043 | + * The expectation is that if the codec is not registered, | |
| 1044 | + * then the query string should contain bind markers for all variables; | |
| 1045 | + * if however all codecs are properly registered, then | |
| 1046 | + * the query string should contain all variables inlined and formatted properly. | |
| 1047 | + * | |
| 1048 | + * @jira_ticket JAVA-1272 | |
| 1049 | + */ | |
| 1050 | + @Test(groups = "unit") | |
| 1051 | + public void should_inline_custom_codec() throws Exception { | |
| 1052 | + assertThat( | |
| 1053 | + insertInto("users").value("id", new Foo(42)).toString()) | |
| 1054 | + .isEqualTo("INSERT INTO users (id) VALUES (?);"); | |
| 1055 | + CodecRegistry.DEFAULT_INSTANCE.register(new FooCodec()); | |
| 1056 | + assertThat( | |
| 1057 | + insertInto("users").value("id", new Foo(42)).toString()) | |
| 1058 | + .isEqualTo("INSERT INTO users (id) VALUES (42);"); | |
| 1059 | + } | |
| 1060 | + | |
| 1002 | 1061 | } |