Coverage Report - org.apache.camel.builder.sql.SqlBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
SqlBuilder
80% 
100% 
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.builder.sql;
 18  
 
 19  
 import org.apache.camel.Exchange;
 20  
 import org.apache.camel.Expression;
 21  
 import org.apache.camel.Message;
 22  
 import org.apache.camel.Predicate;
 23  
 import org.apache.camel.RuntimeExpressionException;
 24  
 import org.apache.camel.util.ObjectHelper;
 25  
 import org.josql.Query;
 26  
 import org.josql.QueryExecutionException;
 27  
 import org.josql.QueryParseException;
 28  
 
 29  
 import java.util.Collections;
 30  
 import java.util.List;
 31  
 import java.util.Map;
 32  
 import java.util.Set;
 33  
 import java.util.HashMap;
 34  
 
 35  
 /**
 36  
  * A builder of SQL {@link org.apache.camel.Expression} and {@link org.apache.camel.Predicate} implementations
 37  
  *
 38  
  * @version $Revision: $
 39  
  */
 40  5
 public class SqlBuilder<E extends Exchange> implements Expression<E>, Predicate<E> {
 41  
 
 42  
     private Query query;
 43  5
     private Map<String,Object> variables = new HashMap<String, Object>();
 44  
 
 45  5
     public SqlBuilder(Query query) {
 46  5
         this.query = query;
 47  5
     }
 48  
 
 49  
     public Object evaluate(E exchange) {
 50  2
         return evaluateQuery(exchange);
 51  
     }
 52  
 
 53  
     public boolean matches(E exchange) {
 54  3
         List list = evaluateQuery(exchange);
 55  3
         return matches(exchange, list);
 56  
     }
 57  
 
 58  
     public void assertMatches(String text, E exchange) throws AssertionError {
 59  2
         List list = evaluateQuery(exchange);
 60  2
         if (!matches(exchange, list)) {
 61  0
             throw new AssertionError(this + " failed on " + exchange + " as found " + list);
 62  
         }
 63  2
     }
 64  
 
 65  
     // Builder API
 66  
     //-----------------------------------------------------------------------
 67  
 
 68  
     /**
 69  
      * Creates a new builder for the given SQL query string
 70  
      *
 71  
      * @param sql the SQL query to perform
 72  
      * @return a new builder
 73  
      * @throws QueryParseException if there is an issue with the SQL
 74  
      */
 75  
     public static <E extends Exchange> SqlBuilder<E> sql(String sql) throws QueryParseException {
 76  5
         Query q = new Query();
 77  5
         q.parse(sql);
 78  5
         return new SqlBuilder(q);
 79  
     }
 80  
 
 81  
     /**
 82  
      * Adds the variable value to be used by the SQL query
 83  
      */
 84  
     public SqlBuilder<E> variable(String name, Object value) {
 85  0
         getVariables().put(name, value);
 86  0
         return this;
 87  
     }
 88  
 
 89  
 
 90  
     // Properties
 91  
     //-----------------------------------------------------------------------
 92  
     public Map<String, Object> getVariables() {
 93  7
         return variables;
 94  
     }
 95  
 
 96  
     public void setVariables(Map<String, Object> properties) {
 97  0
         this.variables = properties;
 98  0
     }
 99  
 
 100  
 
 101  
     // Implementation methods
 102  
     //-----------------------------------------------------------------------
 103  
     protected boolean matches(E exchange, List list) {
 104  5
         return ObjectHelper.matches(list);
 105  
     }
 106  
 
 107  
     protected List evaluateQuery(E exchange) {
 108  7
         configureQuery(exchange);
 109  7
         Message in = exchange.getIn();
 110  7
         List list = in.getBody(List.class);
 111  7
         if (list == null) {
 112  0
             list = Collections.singletonList(in.getBody());
 113  
         }
 114  
         try {
 115  7
             return query.execute(list).getResults();
 116  
         }
 117  0
         catch (QueryExecutionException e) {
 118  0
             throw new RuntimeExpressionException(e);
 119  
         }
 120  
     }
 121  
 
 122  
     protected void configureQuery(E exchange) {
 123  
         // lets pass in the headers as variables that the SQL can use
 124  7
         addVariables(exchange.getProperties());
 125  7
         addVariables(exchange.getIn().getHeaders());
 126  7
         addVariables(getVariables());
 127  
 
 128  7
         query.setVariable("exchange", exchange);
 129  7
         query.setVariable("in", exchange.getIn());
 130  7
         query.setVariable("out", exchange.getOut());
 131  7
     }
 132  
 
 133  
     protected void addVariables(Map <String, Object> map) {
 134  21
         Set<Map.Entry<String, Object>> propertyEntries = map.entrySet();
 135  21
         for (Map.Entry<String, Object> entry : propertyEntries) {
 136  7
             query.setVariable(entry.getKey(), entry.getValue());
 137  7
         }
 138  21
     }
 139  
 }