KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > UnboundIdentifierExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.sql;
30
31 import com.caucho.db.table.Column;
32 import com.caucho.db.table.Table;
33 import com.caucho.log.Log;
34 import com.caucho.util.L10N;
35
36 import java.sql.SQLException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 class UnboundIdentifierExpr extends Expr {
41   private static final L10N L = new L10N(UnboundIdentifierExpr.class);
42   private static final Logger JavaDoc log = Log.open(UnboundIdentifierExpr.class);
43
44   private String JavaDoc _table;
45   private String JavaDoc _column;
46
47   /**
48    * Creates an unbound identifier with just a column name.
49    */

50   UnboundIdentifierExpr(String JavaDoc column)
51   {
52     _column = column;
53   }
54
55   /**
56    * Creates an unbound identifier with a table and a column name.
57    */

58   UnboundIdentifierExpr(String JavaDoc table, String JavaDoc column)
59   {
60     _table = table;
61     _column = column;
62   }
63
64   /**
65    * The cost of a match of the expr.
66    */

67   protected long lookupCost(ArrayList JavaDoc<FromItem> fromList)
68   {
69     Column column = findColumn(fromList);
70     if (column == null)
71       return Integer.MAX_VALUE;
72     
73     FromItem fromItem = fromList.get(fromList.size() - 1);
74     
75     Table table = fromItem.getTable();
76
77     column = table.getColumn(_column);
78
79     if (column == null)
80       return 100 * 100 * 100;
81     else if (column.isPrimaryKey())
82       return 100;
83     else if (column.isUnique())
84       return 100 * 100;
85     else
86       return 100 * 100 * 100;
87   }
88
89   /**
90    * The cost of a match of the expr.
91    */

92   public long subCost(ArrayList JavaDoc<FromItem> fromList)
93   {
94     Column column = findColumn(fromList);
95
96     if (column == null)
97       return Integer.MAX_VALUE;
98     else
99       return 10 * 100 * 100 * 100;
100   }
101
102   /**
103    * Finds the column in the from list.
104    */

105   private Column findColumn(ArrayList JavaDoc<FromItem> fromItems)
106   {
107     if (_table == null) {
108       for (int i = 0; i < fromItems.size(); i++) {
109     FromItem fromItem = fromItems.get(i);
110     
111     Table table = fromItem.getTable();
112
113     Column column = table.getColumn(_column);
114
115     if (column != null)
116       return column;
117       }
118
119       return null;
120     }
121     else {
122       for (int i = 0; i < fromItems.size(); i++) {
123     FromItem fromItem = fromItems.get(i);
124     
125     if (_table.equals(fromItem.getName())) {
126       Table table = fromItem.getTable();
127
128       return table.getColumn(_column);
129     }
130       }
131       
132       return null;
133     }
134   }
135                 
136   protected Expr bind(Query query)
137     throws SQLException JavaDoc
138   {
139     return query.bind(_table, _column);
140   }
141
142   public String JavaDoc toString()
143   {
144     if (_table != null)
145       return "UnboundIdentifier[" + _table + "," + _column + "]";
146     else
147       return "UnboundIdentifier[" + _column + "]";
148   }
149 }
150
Popular Tags