Updated Branches: refs/heads/camel-2.11.x 9639e77e1 -> 554e8e189 refs/heads/master 4d521ae86 -> 7b45b8466
CAMEL-6561: Fixed sql producer to support json like structure for # parameters when only 1 parameter in use. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7b45b846 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7b45b846 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7b45b846 Branch: refs/heads/master Commit: 7b45b8466ddfbac72358c28a5e0c1be44eef8c2c Parents: 4d521ae Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Aug 7 12:04:36 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Aug 7 12:04:36 2013 +0200 ---------------------------------------------------------------------- .../sql/DefaultSqlPrepareStatementStrategy.java | 22 ++++-- .../component/sql/SqlProducerJSONTest.java | 83 ++++++++++++++++++++ .../camel-sql/src/test/resources/servlet.json | 28 +++++++ .../sql/createAndPopulateDatabase2.sql | 23 ++++++ 4 files changed, 148 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/7b45b846/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java index 2bd8e6c..0ffac39 100644 --- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java +++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java @@ -19,6 +19,7 @@ package org.apache.camel.component.sql; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -125,15 +126,20 @@ public class DefaultSqlPrepareStatementStrategy implements SqlPrepareStatementSt } else { - // is the body a String - if (value instanceof String) { - // if the body is a String then honor quotes etc. - String[] tokens = StringQuoteHelper.splitSafeQuote((String)value, separator, true); - List<String> list = Arrays.asList(tokens); - return list.iterator(); + // if only 1 parameter and the body is a String then use body as is + if (expectedParams == 1 && value instanceof String) { + return Collections.singletonList(value).iterator(); } else { - // just use a regular iterator - return exchange.getContext().getTypeConverter().convertTo(Iterator.class, value); + // is the body a String + if (value instanceof String) { + // if the body is a String then honor quotes etc. + String[] tokens = StringQuoteHelper.splitSafeQuote((String)value, separator, true); + List<String> list = Arrays.asList(tokens); + return list.iterator(); + } else { + // just use a regular iterator + return exchange.getContext().getTypeConverter().convertTo(Iterator.class, value); + } } } } http://git-wip-us.apache.org/repos/asf/camel/blob/7b45b846/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerJSONTest.java ---------------------------------------------------------------------- diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerJSONTest.java b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerJSONTest.java new file mode 100755 index 0000000..8c8ef3a --- /dev/null +++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerJSONTest.java @@ -0,0 +1,83 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.sql; + +import java.io.File; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +/** + * @version + */ +public class SqlProducerJSONTest extends CamelTestSupport { + + private EmbeddedDatabase db; + private JdbcTemplate jdbcTemplate; + + @Before + public void setUp() throws Exception { + db = new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.DERBY).addScript("sql/createAndPopulateDatabase2.sql").build(); + + jdbcTemplate = new JdbcTemplate(db); + + super.setUp(); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + + db.shutdown(); + } + + @Test + public void testJSON() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + String json = context.getTypeConverter().convertTo(String.class, new File("src/test/resources/servlet.json")); + + template.sendBody("direct:start", json); + + mock.assertIsSatisfied(); + + assertEquals(4, jdbcTemplate.queryForInt("select count(*) from projects")); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + getContext().getComponent("sql", SqlComponent.class).setDataSource(db); + + from("direct:start") + .to("sql:insert into projects (id, project, license, description) values (4, 'Food, Inc', 'ASF', #)") + .to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/7b45b846/components/camel-sql/src/test/resources/servlet.json ---------------------------------------------------------------------- diff --git a/components/camel-sql/src/test/resources/servlet.json b/components/camel-sql/src/test/resources/servlet.json new file mode 100644 index 0000000..b07fba0 --- /dev/null +++ b/components/camel-sql/src/test/resources/servlet.json @@ -0,0 +1,28 @@ +{"web-app": { + "servlet": [ + { + "servlet-name": "fooServlet", + "servlet-class": "com.foo.FooServlet", + "init-param": { + "configGlossary:staticPath": "/content/static", + "useJSP": false, + "cachePackageTagsTrack": 200, + "cachePackageTagsStore": 200, + "cachePackageTagsRefresh": 60, + "cacheTemplatesTrack": 100, + "cacheTemplatesStore": 50, + "cacheTemplatesRefresh": 15, + "cachePagesTrack": 200, + "cachePagesStore": 100, + "cachePagesRefresh": 10, + "cachePagesDirtyRead": 10, + "dataStoreLogLevel": "debug", + "maxUrlLength": 500 + } + } + ], + "servlet-mapping": { + "fooSerlvet": "/" + } +} +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/7b45b846/components/camel-sql/src/test/resources/sql/createAndPopulateDatabase2.sql ---------------------------------------------------------------------- diff --git a/components/camel-sql/src/test/resources/sql/createAndPopulateDatabase2.sql b/components/camel-sql/src/test/resources/sql/createAndPopulateDatabase2.sql new file mode 100644 index 0000000..db01686 --- /dev/null +++ b/components/camel-sql/src/test/resources/sql/createAndPopulateDatabase2.sql @@ -0,0 +1,23 @@ +-- ------------------------------------------------------------------------ +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- ------------------------------------------------------------------------ + +-- START SNIPPET: e1 +create table projects (id integer primary key, project varchar(10), license varchar(5), description varchar(1000) default null); +insert into projects values (1, 'Camel', 'ASF', ''); +insert into projects values (2, 'AMQ', 'ASF', ''); +insert into projects values (3, 'Linux', 'XXX', ''); +-- END SNIPPET: e1 \ No newline at end of file