Return-Path: X-Original-To: apmail-camel-users-archive@www.apache.org Delivered-To: apmail-camel-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 193AD1906E for ; Fri, 1 Apr 2016 11:46:24 +0000 (UTC) Received: (qmail 78720 invoked by uid 500); 1 Apr 2016 11:46:18 -0000 Delivered-To: apmail-camel-users-archive@camel.apache.org Received: (qmail 78667 invoked by uid 500); 1 Apr 2016 11:46:18 -0000 Mailing-List: contact users-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@camel.apache.org Delivered-To: mailing list users@camel.apache.org Received: (qmail 78655 invoked by uid 99); 1 Apr 2016 11:46:18 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 01 Apr 2016 11:46:18 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id C4DA1C2AAB for ; Fri, 1 Apr 2016 11:46:17 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -0.802 X-Spam-Level: X-Spam-Status: No, score=-0.802 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd1-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id 5pdwuLWAIf9Q for ; Fri, 1 Apr 2016 11:46:15 +0000 (UTC) Received: from mail-io0-f173.google.com (mail-io0-f173.google.com [209.85.223.173]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id 028E95F1D5 for ; Fri, 1 Apr 2016 11:46:15 +0000 (UTC) Received: by mail-io0-f173.google.com with SMTP id g185so149467851ioa.2 for ; Fri, 01 Apr 2016 04:46:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=PM7sIjZOAtG4V6qBC/ZBwDBdub/RMPsmRgrnxsocSSk=; b=eRCRl4GYqnE1dpHLa8seZ/o9p1hvAjAXwwjeLny4CF5CEnjPSNKDa5AzPyVvmIwhnd h825kSdii4oxPIdEFA6GcNhvqxVXcBVQrMXfyEeJmxifC0ARe+Y+R8ppvLqW2oCMDUrZ SvdKwZtDTXWoVkW50gNeQJPoaXMHX+Ac0jYRKFurBin3pLbLBBAV8Ueg92jssJJis3vT bj+WTDud1R0B/53KWMU7MlxORB+OIv1VTCduzanh0Spr6wwOCpHcx2+qVY5GkJUuVe1r P3KVSeHIUrHTWrZtcMEzH64GoYW6JenAt0tjlPMcmWyPW0EQPaiYu9IKMDGKbyfZ6mDV MOHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=PM7sIjZOAtG4V6qBC/ZBwDBdub/RMPsmRgrnxsocSSk=; b=m5fH76J30K8HabyohZtCJG7b++PURNOyFYzqaR6HaZARY0CW20G35026ux/pUH4l28 /LL1QDHJB+qaWTvlR3Ibdywz+pT+zlycDwlCsEd7JjrzCh8JrQA+fEflAiBO2jnzWpke HGfqa+4gN88I04ItWR3o8jQZ1WlrCjQa24VhwEtyGhIwWe+4y07UwUlPyvpBLInQPFjl QqhOCsc/0A0mTA/d6ZC9YzAcb3DlOxl9N/X+QQWyLcpzHxu4qeTpHHLY7+A/SFa9bbDV oNJGxauKiDkBq/Us5wzCDFu76JfVKejQli7GV0QZm+OLY0gctbHUTINGB3EZ2oHIls5j RSZw== X-Gm-Message-State: AD7BkJLxaUIRy8URrJOINs5fM9bR/rhEWRuewuDjeXxQEv8g8IUOQLAPYd01ynPCvCO60enl3QByosjZHiXvvA== X-Received: by 10.107.7.20 with SMTP id 20mr7983473ioh.181.1459511173718; Fri, 01 Apr 2016 04:46:13 -0700 (PDT) MIME-Version: 1.0 Received: by 10.79.16.2 with HTTP; Fri, 1 Apr 2016 04:45:54 -0700 (PDT) In-Reply-To: <563177DB60AFC34783EBCACEF9CBC9A45C9B7A4B@cacl-it-mail10.flatironssolutions.com> References: <563177DB60AFC34783EBCACEF9CBC9A45C9B7A17@cacl-it-mail10.flatironssolutions.com> <563177DB60AFC34783EBCACEF9CBC9A45C9B7A4B@cacl-it-mail10.flatironssolutions.com> From: Claus Ibsen Date: Fri, 1 Apr 2016 13:45:54 +0200 Message-ID: Subject: Re: [CAMEL] Sql-component (producer) removes body when &outputHeader=myHeader is used an result is empty To: "users@camel.apache.org" Content-Type: text/plain; charset=UTF-8 What version of Camel do you use? On Fri, Apr 1, 2016 at 1:12 PM, Thomas Bender wrote: > Hi, > > I have an issue with the camel sql-component. > > I use the options outputHeader=myHeader and outputType=SelectOne. When the query returns no result, the body of the Exchange is set to null instead of preserved what I would expect when using outputHeader. > > In my opinion the problem is in lines 175-185 of SqlProducer.java that does not set the body in case the data is null (missing else block). As I am new to Camel, I don't know if this was intended for a special case. > > // Code section > > if (data != null) { // <--- Missing the else > // for noop=true we still want to enrich with the row count header > if (getEndpoint().isNoop()) { > exchange.getOut().setBody(exchange.getIn().getBody()); > } else if (getEndpoint().getOutputHeader() != null) { > exchange.getOut().setBody(exchange.getIn().getBody()); > exchange.getOut().setHeader(getEndpoint().getOutputHeader(), data); > } else { > exchange.getOut().setBody(data); > } > exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 1); > } > > > -------- > following could be added (untested) > > else { // if data == null > if (getEndpoint().isNoop()) { > exchange.getOut().setBody(exchange.getIn().getBody()); > } else if (getEndpoint().getOutputHeader() != null) { > exchange.getOut().setBody(exchange.getIn().getBody()); > } > exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 0); > } > > > > -------------------------- > Following testcase: > > import org.apache.camel.EndpointInject; > import org.apache.camel.Produce; > import org.apache.camel.ProducerTemplate; > import org.apache.camel.builder.RouteBuilder; > import org.apache.camel.component.mock.MockEndpoint; > import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; > import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; > import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner; > import org.junit.Test; > import org.junit.runner.RunWith; > import org.springframework.context.annotation.Bean; > import org.springframework.context.annotation.Configuration; > import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; > import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; > import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; > import org.springframework.test.annotation.DirtiesContext; > import org.springframework.test.context.ContextConfiguration; > > @RunWith(CamelSpringJUnit4ClassRunner.class) > @ContextConfiguration(classes = { CamelSqlComponentTest.TestConfig.class }, loader = CamelSpringDelegatingTestContextLoader.class) > public class CamelSqlComponentTest { > > @EndpointInject(uri = "mock:result") > protected MockEndpoint resultEndpoint; > > @Produce(uri = "direct:start") > protected ProducerTemplate template; > > @Test > @DirtiesContext > public void testSqlEndpoint() throws Exception { > > String expectedBody = "body"; > resultEndpoint.expectedBodiesReceived(expectedBody); > template.sendBody(expectedBody); > resultEndpoint.assertIsSatisfied(); > > } > > @Configuration > public static class TestConfig extends SingleRouteCamelConfiguration { > > @Bean(destroyMethod = "shutdown", name = "dataSource") > public EmbeddedDatabase dataSource() { > return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2) > .addScript("db-camel-schema.sql").addScript("db-camel-data.sql").build(); > } > > @Bean > @Override > public RouteBuilder route() { > return new RouteBuilder() { > @Override > public void configure() throws Exception { > from("direct:start") > .to("sql:select id from mytable where 1 = 2?dataSource=dataSource&outputHeader=myHeader&outputType=SelectOne") > .log("${body}").to("mock:result"); > } > }; > } > } > } > > -------------------------- > db-camel-data.sql > > insert into MYTABLE (ID, SUBJECT) VALUES (1, 'subject'); > > -------------------------- > db-camel-schema.sql > > CREATE TABLE MYTABLE ( > ID INT PRIMARY KEY, > SUBJECT VARCHAR(255) > ); > > Have a nice weekend. > Thomas > -- Claus Ibsen ----------------- http://davsclaus.com @davsclaus Camel in Action 2: https://www.manning.com/ibsen2