Quadcap Embeddable Database

com/quadcap/sql/meta/MetaIndexInfo.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.Iterator; 00044 00045 import java.sql.DatabaseMetaData; 00046 import java.sql.ResultSetMetaData; 00047 import java.sql.SQLException; 00048 00049 import com.quadcap.sql.Column; 00050 import com.quadcap.sql.Constraint; 00051 import com.quadcap.sql.Database; 00052 import com.quadcap.sql.Expression; 00053 import com.quadcap.sql.IndexConstraint; 00054 import com.quadcap.sql.Relation; 00055 import com.quadcap.sql.Row; 00056 import com.quadcap.sql.Session; 00057 import com.quadcap.sql.StaticCursor; 00058 import com.quadcap.sql.Table; 00059 00060 import com.quadcap.sql.index.BCursor; 00061 import com.quadcap.sql.index.Btree; 00062 00063 import com.quadcap.sql.types.*; 00064 00065 import com.quadcap.util.Debug; 00066 00067 /** 00068 * A Cursor supporting the <code>getIndexInfo</code> function. 00069 * 00070 * @author Stan Bailes 00071 */ 00072 public class MetaIndexInfo extends MetaCursor { 00073 static Column[] cols = { 00074 new Column("TABLE_CAT", typeString), // 1 00075 new Column("TABLE_SCHEM", typeString), // 2 00076 new Column("TABLE_NAME", typeString), // 3 00077 new Column("NON_UNIQUE", typeBinary), // 4 00078 new Column("INDEX_QUALIFIER", typeString), // 5 00079 new Column("INDEX_NAME", typeString), // 6 00080 new Column("TYPE", typeShort), // 7 00081 new Column("ORDINAL_POSITION", typeInt), // 8 00082 new Column("COLUMN_NAME", typeString), // 9 00083 new Column("ASC_OR_DESC", typeString), // 10 00084 new Column("CARDINALITY", typeInt), // 11 00085 new Column("PAGES", typeInt), // 12 00086 new Column("FILTER_CONDITION", typeString) // 13 00087 }; 00088 00089 static int[] sortColumns = { 4, 7, 6, 8 }; 00090 00091 public MetaIndexInfo(Session session, Expression predicate) 00092 throws SQLException 00093 { 00094 super(session, predicate); 00095 try { 00096 addColumns(cols); 00097 Database db = session.getDatabase(); 00098 session.getTableWriteLock("#Schema"); 00099 synchronized (db.getFile().getLock()) { 00100 Iterator iter = db.getRelationNameIterator(); 00101 while (iter.hasNext()) { 00102 String name = (String)iter.next(); 00103 Relation r = db.getRelation(name); 00104 if (r instanceof Table) { 00105 Table t = (Table)r; 00106 int num = t.getNumConstraints(); 00107 for (int i = 0; i < num; i++) { 00108 Constraint c = t.getConstraint(i); 00109 if (c instanceof IndexConstraint) { 00110 doConstraint(t, (IndexConstraint)c); 00111 } 00112 } 00113 } 00114 } 00115 } 00116 sort(); 00117 } catch (ValueException e) { 00118 Debug.print(e); 00119 SQLException te = new SQLException(e.toString(), "Q000N"); 00120 te.setNextException(e); 00121 throw te; 00122 } catch (IOException e) { 00123 Debug.print(e); 00124 throw new SQLException(e.toString(), "Q000O"); 00125 } 00126 } 00127 00128 public int[] getSortColumns() { 00129 return sortColumns; 00130 } 00131 00132 void doConstraint(Table t, IndexConstraint c) throws SQLException { 00133 int[] colIndices = c.getColumns(); 00134 for (int i = 0; i < colIndices.length; i++) { 00135 Column col = t.getColumn(colIndices[i]); 00136 Row row = doColumn(t, c, i, col); 00137 if (rowMatch(row)) { 00138 addRow(row); 00139 } 00140 } 00141 } 00142 00143 Row doColumn(Table t, IndexConstraint c, int pos, Column col) 00144 throws SQLException 00145 { 00146 Type type = col.getType(); 00147 Row row = new Row(18); 00148 row.set(1, ValueNull.valueNull); 00149 doTableName(2, row, t.getName()); 00150 row.set(4, (c.isUnique() ? 00151 ValueBoolean.falseBoolean : 00152 ValueBoolean.trueBoolean)); 00153 if (c.isGlobal()) { 00154 row.set(5, new ValueString("global")); 00155 } else { 00156 row.set(5, ValueNull.valueNull); 00157 } 00158 String iname = c.getName(); 00159 if (iname == null) iname = ""; 00160 row.set(6, new ValueString(iname)); 00161 row.set(7, new ValueShort(DatabaseMetaData.tableIndexOther)); 00162 row.set(8, new ValueShort(pos+1)); 00163 row.set(9, new ValueString(col.getShortName())); 00164 row.set(10, new ValueString("A")); 00165 row.set(11, ValueNull.valueNull); 00166 row.set(12, ValueNull.valueNull); 00167 row.set(13, ValueNull.valueNull); 00168 return row; 00169 } 00170 00171 }