01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdbc.sql.exp;
12:
13: import com.versant.core.jdbc.sql.SqlDriver;
14:
15: /**
16: * An expression with two args.
17: */
18: public class BinaryExp extends SqlExp {
19:
20: public BinaryExp(SqlExp left, SqlExp right) {
21: super (left);
22: left.next = right;
23: right.next = null;
24: }
25:
26: public BinaryExp() {
27: }
28:
29: public SqlExp createInstance() {
30: return new BinaryExp();
31: }
32:
33: /**
34: * Create an aliases for any subtables we may have.
35: */
36: public int createAlias(int index) {
37: return childList.next.createAlias(childList.createAlias(index));
38: }
39:
40: /**
41: * Replace any references to old with nw. This is used when redundant
42: * joins are removed.
43: */
44: public void replaceSelectExpRef(SelectExp old, SelectExp nw) {
45: childList.replaceSelectExpRef(old, nw);
46: childList.next.replaceSelectExpRef(old, nw);
47: }
48:
49: /**
50: * Normalize this node i.e. transform it into its simplist possible form.
51: * This will turn sub selects into joins and so on.
52: */
53: public SqlExp normalize(SqlDriver driver, SelectExp sel,
54: boolean convertExists) {
55: SqlExp r = childList.normalize(driver, sel, convertExists);
56: if (r != null) {
57: r.next = childList.next;
58: childList = r;
59: }
60: r = childList.next.normalize(driver, sel, convertExists);
61: if (r != null) {
62: childList.next = r;
63: }
64: return null;
65: }
66:
67: /**
68: * If this expression involves a single table only then return the
69: * SelectExp for the table (e.g. a.col1 == 10 returns a, a.col1 = b.col2
70: * returns null). This is used to detect expressions that can be moved
71: * into the ON list for the join to the table involved.
72: * @param exclude
73: */
74: public SelectExp getSingleSelectExp(SelectExp exclude) {
75: SelectExp a = childList.getSingleSelectExp(exclude);
76: SelectExp b = childList.next.getSingleSelectExp(exclude);
77: if (b == null || b == exclude)
78: return a;
79: if (a == null || a == exclude)
80: return b;
81: return a == b ? a : null;
82: }
83:
84: }
|