| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| NaryConstraint |
|
| 0.0;0 |
| 1 | /* |
|
| 2 | * SQLElements object-oriented SQL parsing and generation library |
|
| 3 | * |
|
| 4 | * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation |
|
| 5 | * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber |
|
| 6 | * All Rights Reserved |
|
| 7 | * |
|
| 8 | * This library is free software; you can redistribute it and/or modify |
|
| 9 | * it under the terms of the GNU Lesser General Public License as published |
|
| 10 | * by the Free Software Foundation; either version 2.1 of the License, or |
|
| 11 | * (at your option) any later version. |
|
| 12 | * |
|
| 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
| 14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
| 15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
| 16 | * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
| 17 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
| 18 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
|
| 19 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
| 20 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
| 21 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
|
| 22 | * OF SUCH DAMAGE. |
|
| 23 | * |
|
| 24 | * You should have received a copy of the GNU Lesser General Public License |
|
| 25 | * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html |
|
| 26 | * |
|
| 27 | * $Id: NaryConstraint.java 27 2006-01-14 07:50:08 -0500 (Sat, 14 Jan 2006) cgruber $ |
|
| 28 | */ |
|
| 29 | package org.israfil.sqlelements.constraints; |
|
| 30 | ||
| 31 | import java.util.Arrays; |
|
| 32 | import java.util.Set; |
|
| 33 | ||
| 34 | import net.israfil.foundation.collections.ArraySet; |
|
| 35 | ||
| 36 | import org.israfil.sqlelements.render.SQLRenderContext; |
|
| 37 | ||
| 38 | /** |
|
| 39 | * N-ary Constraint |
|
| 40 | * A friendly parent for constraints that can have an infinite |
|
| 41 | * number of operands, such as and, or, etc. |
|
| 42 | * i.e.: "(a=b) and (b=c) and (c=d)" is a valid SQL construction, but |
|
| 43 | * can be represented (logically) by: and(condition1,condition2,condition3) |
|
| 44 | * |
|
| 45 | * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> |
|
| 46 | * @author Latest: $Author: cgruber $ |
|
| 47 | * @version $Revision: 27 $ |
|
| 48 | */ |
|
| 49 | public abstract class NaryConstraint implements ComplexConstraint |
|
| 50 | { |
|
| 51 | protected Constraint[] operands; |
|
| 52 | protected String operatorText; |
|
| 53 | ||
| 54 | protected NaryConstraint(String operatorText, Constraint ... operands) { |
|
| 55 | 20 | this(operatorText,prepArray(operands)); |
| 56 | 20 | } |
| 57 | ||
| 58 | protected static Set<Constraint> prepArray(Constraint[] array) { |
|
| 59 | 20 | Set<Constraint> s = new ArraySet<Constraint>(); |
| 60 | 20 | if (array != null) s.addAll(Arrays.asList(array)); |
| 61 | 20 | return s; |
| 62 | } |
|
| 63 | 32 | protected NaryConstraint(String operatorText, Set<Constraint> operands) { |
| 64 | 32 | if (operands == null) operands = new ArraySet<Constraint>(0); |
| 65 | 32 | this.operatorText = operatorText; |
| 66 | 32 | Set<Constraint> constraintSet = new ArraySet<Constraint>(); |
| 67 | 32 | for (Constraint c : operands) { |
| 68 | 63 | if (c == null) continue; |
| 69 | 61 | if (this.getClass().equals(c.getClass())) { |
| 70 | 27 | for(Constraint sub : ((NaryConstraint)c).operands){ |
| 71 | 18 | constraintSet.add(sub); |
| 72 | } |
|
| 73 | 9 | } |
| 74 | 52 | else constraintSet.add(c); |
| 75 | 61 | } |
| 76 | 32 | this.operands = constraintSet.toArray(new Constraint[constraintSet.size()]); |
| 77 | 32 | } |
| 78 | ||
| 79 | public String render(SQLRenderContext context) { |
|
| 80 | 18 | boolean first = true; |
| 81 | 18 | String sql = ""; |
| 82 | 63 | for (Constraint c : this.operands) { |
| 83 | 45 | if (!first) { |
| 84 | 29 | sql += " "; |
| 85 | 29 | sql += operatorText; |
| 86 | 29 | sql += " "; |
| 87 | 29 | } |
| 88 | 16 | else first = false; |
| 89 | 45 | sql += ConstraintUtils.renderInParentheses(c,context); |
| 90 | } |
|
| 91 | 18 | return sql; |
| 92 | } |
|
| 93 | } |