Quadcap Embeddable Database

com/quadcap/sql/meta/MetaBestRowId.java

Go to the documentation of this file.
00001 package com.quadcap.sql.meta; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 00043 import java.util.Enumeration; 00044 import java.util.Vector; 00045 00046 import java.sql.DatabaseMetaData; 00047 import java.sql.ResultSetMetaData; 00048 import java.sql.SQLException; 00049 00050 import com.quadcap.sql.Column; 00051 import com.quadcap.sql.Constraint; 00052 import com.quadcap.sql.Database; 00053 import com.quadcap.sql.Expression; 00054 import com.quadcap.sql.PrimaryKeyConstraint; 00055 import com.quadcap.sql.Relation; 00056 import com.quadcap.sql.Row; 00057 import com.quadcap.sql.Session; 00058 import com.quadcap.sql.StaticCursor; 00059 import com.quadcap.sql.Table; 00060 import com.quadcap.sql.UniqueConstraint; 00061 00062 import com.quadcap.sql.index.BCursor; 00063 import com.quadcap.sql.index.Btree; 00064 00065 import com.quadcap.sql.types.*; 00066 00067 import com.quadcap.util.Debug; 00068 00069 /** 00070 * A Cursor supporting the <code>DatabaseMetaData.getBestRowId()</code> 00071 * operation. 00072 * 00073 * @author Stan Bailes 00074 */ 00075 public class MetaBestRowId extends MetaCursor { 00076 static Column[] cols = { 00077 new Column("SCOPE", typeShort), // 1 00078 new Column("COLUMN_NAME", typeString), // 2 00079 new Column("DATA_TYPE", typeShort), // 3 00080 new Column("TYPE_NAME", typeString), // 4 00081 new Column("COLUMN_SIZE", typeInt), // 5 00082 new Column("BUFFER_LENGTH", typeAny), // 6 00083 new Column("DECIMAL_DIGITS", typeInt), // 7 00084 new Column("PSEUDO_COLUMN", typeShort) // 8 00085 }; 00086 00087 static int[] sortColumns = { 2 }; 00088 00089 /** 00090 * The constructor for this meta cursor finds the "best" index 00091 * to use for this table, purely based on the index type 00092 * (and possibly the 'nullable' flag). We don't do anything with 00093 * scope -- best is best for us. 00094 */ 00095 public MetaBestRowId(Session session, String tableName, int scope, 00096 boolean nullable) 00097 throws SQLException 00098 { 00099 super(session, null); 00100 try { 00101 addColumns(cols); 00102 Database db = session.getDatabase(); 00103 session.getTableWriteLock("#Schema"); 00104 tableName = tableName.toUpperCase(); 00105 Table t = (Table)db.getRelation(tableName); 00106 if (t != null) { 00107 UniqueConstraint uc = null; 00108 int num = t.getNumConstraints(); 00109 for (int i = 0; i < num; i++) { 00110 Constraint c = t.getConstraint(i); 00111 if (c instanceof UniqueConstraint) { 00112 if (nullable || checkNullable(c)) { 00113 uc = (UniqueConstraint)c; 00114 if (c instanceof PrimaryKeyConstraint) break; 00115 } 00116 } 00117 } 00118 if (uc != null) { 00119 int cnt = uc.getColumnCount(); 00120 for (int i = 0; i < cnt; i++) { 00121 addRow(doColumn(uc.getColumn(i))); 00122 } 00123 } 00124 } 00125 sort(); 00126 } catch (ValueException e) { 00127 Debug.print(e); 00128 SQLException te = new SQLException(e.toString(), "Q000J"); 00129 te.setNextException(e); 00130 throw te; 00131 } catch (IOException e) { 00132 Debug.print(e); 00133 throw new SQLException(e.toString(), "Q000K"); 00134 } 00135 } 00136 00137 /** 00138 * Sort by name, I guess... 00139 */ 00140 public int[] getSortColumns() { 00141 return sortColumns; 00142 } 00143 00144 /** 00145 * Do any of the columns in this constraint allow nulls? 00146 */ 00147 public boolean checkNullable(Constraint c) throws SQLException { 00148 boolean ret = !(c instanceof PrimaryKeyConstraint); 00149 int cnt = c.getColumnCount(); 00150 for (int i = 0; ret && i < cnt; i++) { 00151 ret = c.getColumn(i).isNullable(); 00152 } 00153 return ret; 00154 } 00155 00156 /** 00157 * Add one cursor row to describe the specified column. 00158 */ 00159 Row doColumn(Column col) throws SQLException { 00160 Type type = col.getType(); 00161 Row row = new Row(18); 00162 row.set(1, new ValueShort(DatabaseMetaData.bestRowSession)); 00163 row.set(2, new ValueString(col.getShortName())); 00164 row.set(3, new ValueShort(type.getJDBCType())); 00165 row.set(4, new ValueString(type.getTypeName())); 00166 00167 Value size = new ValueInteger(type.getPrecision()); 00168 if (type instanceof TypeChar) { 00169 size = new ValueInteger(((TypeChar)type).getMax()); 00170 } else if (type instanceof TypeVarChar) { 00171 size = new ValueInteger(((TypeVarChar)type).getMax()); 00172 } 00173 row.set(5, size); 00174 row.set(6, ValueNull.valueNull); 00175 row.set(7, new ValueShort(type.getScale())); 00176 row.set(8, new ValueShort(DatabaseMetaData.bestRowUnknown)); 00177 return row; 00178 } 00179 }