diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2009-05-28 02:57:33 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2009-05-28 02:57:33 +0000 |
commit | cd23b435686872ea0abd59568be5bd2bd1994858 (patch) | |
tree | 70923292c775828b5da4348f19edca4168607a62 /src | |
parent | 9ee53ade6d1056a259a7472fa3ba8116f708a7cb (diff) | |
download | jackcess-cd23b435686872ea0abd59568be5bd2bd1994858.tar.gz jackcess-cd23b435686872ea0abd59568be5bd2bd1994858.zip |
add initial test for reading queries; add support for multicolumn joins
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@393 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src')
-rw-r--r-- | src/java/com/healthmarketscience/jackcess/query/Query.java | 58 |
1 files changed, 55 insertions, 3 deletions
diff --git a/src/java/com/healthmarketscience/jackcess/query/Query.java b/src/java/com/healthmarketscience/jackcess/query/Query.java index 7853859..08f6f48 100644 --- a/src/java/com/healthmarketscience/jackcess/query/Query.java +++ b/src/java/com/healthmarketscience/jackcess/query/Query.java @@ -28,10 +28,12 @@ King of Prussia, PA 19406 package com.healthmarketscience.jackcess.query; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -224,7 +226,31 @@ public abstract class Query List<Row> joins = getJoinRows(); if(!joins.isEmpty()) { - for(Row join : joins) { + // combine any multi-column joins + Collection<List<Row>> comboJoins = combineJoins(joins); + + for(List<Row> comboJoin : comboJoins) { + + Row join = comboJoin.get(0); + String joinExpr = join.expression; + + if(comboJoin.size() > 1) { + + // combine all the join expressions with "AND" + AppendableList<String> comboExprs = new AppendableList<String>() { + private static final long serialVersionUID = 0L; + @Override + protected String getSeparator() { + return ") AND ("; + } + }; + for(Row tmpJoin : comboJoin) { + comboExprs.add(tmpJoin.expression); + } + + joinExpr = new StringBuilder().append("(") + .append(comboExprs).append(")").toString(); + } String fromTable = join.name1; String toTable = join.name2; @@ -238,7 +264,7 @@ public abstract class Query String expr = new StringBuilder().append(fromExpr) .append(joinType).append(toExpr).append(" ON ") - .append(join.expression).toString(); + .append(joinExpr).toString(); fromExpr.join(toExpr, expr); joinExprs.add(fromExpr); @@ -265,6 +291,28 @@ public abstract class Query throw new IllegalStateException("Cannot find join table " + table); } + private Collection<List<Row>> combineJoins(List<Row> joins) + { + // combine joins with the same to/from tables + Map<List<String>,List<Row>> comboJoinMap = + new LinkedHashMap<List<String>,List<Row>>(); + for(Row join : joins) { + List<String> key = Arrays.asList(join.name1, join.name2); + List<Row> comboJoins = comboJoinMap.get(key); + if(comboJoins == null) { + comboJoins = new ArrayList<Row>(); + comboJoinMap.put(key, comboJoins); + } else { + if(comboJoins.get(0).flag != join.flag) { + throw new IllegalStateException( + "Mismatched join flags for combo joins"); + } + } + comboJoins.add(join); + } + return comboJoinMap.values(); + } + protected String getFromRemoteDbPath() { return getRemoteDatabaseRow().name1; @@ -610,13 +658,17 @@ public abstract class Query super(c); } + protected String getSeparator() { + return ", "; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); for(Iterator<E> iter = iterator(); iter.hasNext(); ) { builder.append(iter.next().toString()); if(iter.hasNext()) { - builder.append(", "); + builder.append(getSeparator()); } } return builder.toString(); |