| 1 | /* ============================================================= |
| 2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
| 3 | * ============================================================= |
| 4 | * |
| 5 | * (C) Copyright 2004-2006, by Volker Berlin. |
| 6 | * |
| 7 | * Project Info: http://www.smallsql.de/ |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU Lesser General Public License as published by |
| 11 | * the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 17 | * License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 22 | * USA. |
| 23 | * |
| 24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
| 25 | * in the United States and other countries.] |
| 26 | * |
| 27 | * --------------- |
| 28 | * ExpressionName.java |
| 29 | * --------------- |
| 30 | * Author: Volker Berlin |
| 31 | * |
| 32 | */ |
| 33 | package smallsql.database; |
| 34 | |
| 35 | |
| 36 | public class ExpressionName extends Expression { |
| 37 | |
| 38 | private String tableAlias; |
| 39 | private DataSource fromEntry; |
| 40 | private int colIdx; |
| 41 | private TableView table; |
| 42 | private Column column; |
| 43 | |
| 44 | // field name Expression i.e. abc, mytable.abc, "ab c" |
| 45 | ExpressionName(String name){ |
| 46 | super(NAME); |
| 47 | setName( name ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Constructor used for aggregate functions from parser. |
| 52 | * @param type the type of the aggregate function |
| 53 | */ |
| 54 | ExpressionName(int type){ |
| 55 | super(type); |
| 56 | //setName( "" ); if null it will be generat a automatic name |
| 57 | } |
| 58 | |
| 59 | void setNameAfterTableAlias(String name){ |
| 60 | tableAlias = getName(); |
| 61 | setName( name ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Is used in GroupResult. |
| 66 | */ |
| 67 | public boolean equals(Object expr){ |
| 68 | if(!super.equals(expr)) return false; |
| 69 | if(!(expr instanceof ExpressionName)) return false; |
| 70 | if( ((ExpressionName)expr).fromEntry != fromEntry) return false; |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | boolean isNull() throws Exception{ |
| 75 | return fromEntry.isNull(colIdx); |
| 76 | } |
| 77 | |
| 78 | boolean getBoolean() throws Exception{ |
| 79 | return fromEntry.getBoolean(colIdx); |
| 80 | } |
| 81 | |
| 82 | int getInt() throws Exception{ |
| 83 | return fromEntry.getInt(colIdx); |
| 84 | } |
| 85 | |
| 86 | long getLong() throws Exception{ |
| 87 | return fromEntry.getLong(colIdx); |
| 88 | } |
| 89 | |
| 90 | float getFloat() throws Exception{ |
| 91 | return fromEntry.getFloat(colIdx); |
| 92 | } |
| 93 | |
| 94 | double getDouble() throws Exception{ |
| 95 | return fromEntry.getDouble(colIdx); |
| 96 | } |
| 97 | |
| 98 | long getMoney() throws Exception{ |
| 99 | return fromEntry.getMoney(colIdx); |
| 100 | } |
| 101 | |
| 102 | MutableNumeric getNumeric() throws Exception{ |
| 103 | return fromEntry.getNumeric(colIdx); |
| 104 | } |
| 105 | |
| 106 | Object getObject() throws Exception{ |
| 107 | return fromEntry.getObject(colIdx); |
| 108 | } |
| 109 | |
| 110 | String getString() throws Exception{ |
| 111 | return fromEntry.getString(colIdx); |
| 112 | } |
| 113 | |
| 114 | byte[] getBytes() throws Exception{ |
| 115 | return fromEntry.getBytes(colIdx); |
| 116 | } |
| 117 | |
| 118 | int getDataType(){ |
| 119 | switch(getType()){ |
| 120 | case NAME: |
| 121 | case GROUP_BY: |
| 122 | return fromEntry.getDataType(colIdx); |
| 123 | case FIRST: |
| 124 | case LAST: |
| 125 | case MAX: |
| 126 | case MIN: |
| 127 | case SUM: |
| 128 | return getParams()[0].getDataType(); |
| 129 | case COUNT: |
| 130 | return SQLTokenizer.INT; |
| 131 | default: throw new Error(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Set the DataSource and the index in the DataSource. The first column has the index 0. |
| 137 | * The Table object is using to request the Column description. |
| 138 | */ |
| 139 | void setFrom( DataSource fromEntry, int colIdx, TableView table ){ |
| 140 | this.fromEntry = fromEntry; |
| 141 | this.colIdx = colIdx; |
| 142 | this.table = table; |
| 143 | // Because the DataSource is a TableResult the colIdx of both is identical |
| 144 | this.column = table.columns.get(colIdx); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Set the DataSource and the index in the DataSource. The first column has the index 0. |
| 149 | * The Table object is using to request the Column description. |
| 150 | */ |
| 151 | void setFrom( DataSource fromEntry, int colIdx, Column column ){ |
| 152 | this.fromEntry = fromEntry; |
| 153 | this.colIdx = colIdx; |
| 154 | this.column = column; |
| 155 | } |
| 156 | |
| 157 | String getTableAlias(){ return tableAlias; } |
| 158 | |
| 159 | /** |
| 160 | * Get the table of this column |
| 161 | * @return |
| 162 | */ |
| 163 | final TableView getTable(){ |
| 164 | return table; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get index of the column in the table |
| 169 | * @return |
| 170 | */ |
| 171 | final int getColumnIndex(){ |
| 172 | return colIdx; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | final Column getColumn(){ |
| 177 | return column; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | final public String toString(){ |
| 182 | if(tableAlias == null) return String.valueOf(getAlias()); |
| 183 | return tableAlias + "." + getAlias(); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /*======================================================================= |
| 188 | |
| 189 | Methods for ResultSetMetaData |
| 190 | |
| 191 | =======================================================================*/ |
| 192 | |
| 193 | String getTableName(){ |
| 194 | if(table != null){ |
| 195 | return table.getName(); |
| 196 | } |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | int getPrecision(){ |
| 201 | return column.getPrecision(); |
| 202 | } |
| 203 | |
| 204 | int getScale(){ |
| 205 | return column.getScale(); |
| 206 | } |
| 207 | |
| 208 | int getDisplaySize(){ |
| 209 | return column.getDisplaySize(); |
| 210 | } |
| 211 | |
| 212 | boolean isAutoIncrement(){ |
| 213 | return column.isAutoIncrement(); |
| 214 | } |
| 215 | |
| 216 | boolean isCaseSensitive(){ |
| 217 | return column.isCaseSensitive(); |
| 218 | } |
| 219 | |
| 220 | boolean isNullable(){ |
| 221 | return column.isNullable(); |
| 222 | } |
| 223 | |
| 224 | boolean isDefinitelyWritable(){ |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | } |