Quadcap Embeddable Database

com/quadcap/sql/meta/MetaColumns.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.ArrayList; 00044 import java.util.Iterator; 00045 import java.util.List; 00046 import java.util.Vector; 00047 00048 import java.sql.ResultSetMetaData; 00049 import java.sql.SQLException; 00050 00051 import com.quadcap.sql.Column; 00052 import com.quadcap.sql.Database; 00053 import com.quadcap.sql.Expression; 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>DatabaseMetaData.getColumns()</code> 00069 * operation. 00070 * 00071 * @author Stan Bailes 00072 */ 00073 public class MetaColumns extends MetaCursor { 00074 static Column[] cols = { 00075 new Column("TABLE_CAT", typeString), // 1 00076 new Column("TABLE_SCHEM", typeString), // 2 00077 new Column("TABLE_NAME", typeString), // 3 00078 new Column("COLUMN_NAME", typeString), // 4 00079 new Column("DATA_TYPE", typeShort), // 5 00080 new Column("TYPE_NAME", typeString), // 6 00081 new Column("COLUMN_SIZE", typeInt), // 7 00082 new Column("BUFFER_LENGTH", typeAny), // 8 00083 new Column("DECIMAL_DIGITS", typeInt), // 9 00084 new Column("NUM_PREC_RADIX", typeInt), // 10 00085 new Column("NULLABLE", typeInt), // 11 00086 new Column("REMARKS", typeString), // 12 00087 new Column("COLUMN_DEF", typeString), // 13 00088 new Column("SQL_DATA_TYPE", typeInt), // 14 00089 new Column("SQL_DATETIME_SUB", typeInt), // 15 00090 new Column("CHAR_OCTET_LENGTH", typeInt), // 16 00091 new Column("ORDINAL_POSITION", typeInt), // 17 00092 new Column("IS_NULLABLE", typeString) // 18 00093 }; 00094 00095 static int[] sortColumns = { 2, 3, 17 }; 00096 00097 public MetaColumns(Session session, Expression predicate) 00098 throws SQLException 00099 { 00100 super(session, predicate); 00101 try { 00102 addColumns(cols); 00103 Database db = session.getDatabase(); 00104 session.getTableWriteLock("#Schema"); 00105 synchronized (db.getFile().getLock()) { 00106 Iterator iter = db.getRelationNameIterator(); 00107 while (iter.hasNext()) { 00108 Relation r = db.getRelation(iter.next().toString()); 00109 if (r instanceof Table) { 00110 Table t = (Table)r; 00111 doTable(t); 00112 } 00113 } 00114 } 00115 sort(); 00116 } catch (ValueException e) { 00117 Debug.print(e); 00118 SQLException te = new SQLException(e.toString(), "Q000J"); 00119 te.setNextException(e); 00120 throw te; 00121 } catch (IOException e) { 00122 Debug.print(e); 00123 throw new SQLException(e.toString(), "Q000K"); 00124 } 00125 } 00126 00127 public int[] getSortColumns() { 00128 return sortColumns; 00129 } 00130 00131 void doTable(Table t) throws SQLException { 00132 for (int i = 1; i <= t.getColumnCount(); i++) { 00133 Column col = t.getColumn(i); 00134 Row row = doColumn(t, col); 00135 if (rowMatch(row)) { 00136 addRow(row); 00137 } 00138 } 00139 } 00140 00141 Row doColumn(Table t, Column col) throws SQLException { 00142 Type type = col.getType(); 00143 Row row = new Row(18); 00144 row.set(1, ValueNull.valueNull); 00145 doTableName(2, row, t.getName()); 00146 row.set(4, new ValueString(col.getShortName())); 00147 row.set(5, new ValueShort(type.getJDBCType())); 00148 row.set(6, new ValueString(type.getTypeName())); 00149 00150 Value size = new ValueInteger(type.getPrecision()); 00151 if (type instanceof TypeChar) { 00152 size = new ValueInteger(((TypeChar)type).getMax()); 00153 } else if (type instanceof TypeVarChar) { 00154 size = new ValueInteger(((TypeVarChar)type).getMax()); 00155 } 00156 row.set(7, size); 00157 row.set(8, ValueNull.valueNull); 00158 row.set(9, new ValueInteger(type.getScale())); 00159 row.set(10, new ValueInteger(10)); 00160 row.set(11, new ValueInteger(col.getNullable())); 00161 row.set(12, ValueNull.valueNull); 00162 Value defVal = ValueNull.valueNull; 00163 Expression def = col.getDefault(); 00164 if (def != null) { 00165 defVal = new ValueString(def.toString()); 00166 } 00167 row.set(13, defVal); 00168 row.set(14, ValueNull.valueNull); 00169 row.set(15, ValueNull.valueNull); 00170 00171 Value max = ValueNull.valueNull; 00172 if (type instanceof TypeChar) { 00173 max = new ValueInteger(2 * ((TypeChar)type).getMax()); 00174 } else if (type instanceof TypeVarChar) { 00175 max = new ValueInteger(2 * ((TypeVarChar)type).getMax()); 00176 } 00177 row.set(16, max); 00178 row.set(17, new ValueInteger(col.getColumn())); 00179 00180 String nullstr = ""; 00181 switch (col.getNullable()) { 00182 case ResultSetMetaData.columnNoNulls: 00183 nullstr = "NO"; 00184 break; 00185 case ResultSetMetaData.columnNullable: 00186 nullstr = "YES"; 00187 break; 00188 } 00189 row.set(18, new ValueString(nullstr)); 00190 return row; 00191 } 00192 00193 }