Quadcap Embeddable Database

com/quadcap/sql/meta/MetaCursor.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 //-//#ifdef JDK11 00044 //-import com.sun.java.util.collections.Vector; 00045 //#else 00046 import java.util.Vector; 00047 //#endif 00048 00049 import java.sql.ResultSet; 00050 import java.sql.SQLException; 00051 00052 import com.quadcap.sql.Column; 00053 import com.quadcap.sql.Cursor; 00054 import com.quadcap.sql.Database; 00055 import com.quadcap.sql.Expression; 00056 import com.quadcap.sql.Relation; 00057 import com.quadcap.sql.Row; 00058 import com.quadcap.sql.Session; 00059 import com.quadcap.sql.StaticCursor; 00060 import com.quadcap.sql.Table; 00061 00062 import com.quadcap.sql.types.*; 00063 00064 import com.quadcap.util.Debug; 00065 00066 /** 00067 * Base class for all cursors used in this package. 00068 * 00069 * @author Stan Bailes 00070 */ 00071 public class MetaCursor extends StaticCursor { 00072 static TypeVarChar typeString = new TypeVarChar(-1); 00073 static TypeInt typeInt = new TypeInt(); 00074 static TypeSmallInt typeShort = new TypeSmallInt(); 00075 static TypeAny typeAny = new TypeAny(); 00076 static TypeBinary typeBinary = new TypeBinary(1); 00077 00078 StaticCursor sc; 00079 Expression predicate; 00080 00081 /** 00082 * Constructor for a typical meta cursor with a predicate 00083 */ 00084 public MetaCursor(Session session, Expression predicate) 00085 throws SQLException 00086 { 00087 super(session, new Vector()); 00088 try { 00089 session.getTableWriteLock("#Schema"); 00090 } catch (IOException e) { 00091 throw new SQLException(e.toString()); 00092 } 00093 this.session = session; 00094 this.predicate = predicate; 00095 Vector vec = new Vector(); 00096 vec.addElement(null); 00097 sc = new StaticCursor(session, vec); 00098 } 00099 00100 /** 00101 * Constructor for empty metacursor 00102 */ 00103 public static MetaCursor create(Session session, Column[] cols) 00104 throws SQLException { 00105 MetaCursor c = new MetaCursor(session, null); 00106 c.addColumns(cols); 00107 return c; 00108 } 00109 00110 /** 00111 * Matcher function (compare the row against the supplied predicate) 00112 */ 00113 boolean rowMatch(Row row) throws SQLException { 00114 if (predicate == null) return true; 00115 sc.updateRow(0, row); 00116 sc.absolute(1); 00117 Value v = predicate.getValue(session, sc); 00118 return (v instanceof ValueBoolean && ((ValueBoolean)v).isTrue()); 00119 } 00120 00121 /** 00122 * Add a list of columns 00123 */ 00124 void addColumns(Column[] cols) throws SQLException { 00125 for (int i = 0; i < cols.length; i++) { 00126 addColumn(cols[i]); 00127 sc.addColumn(cols[i]); 00128 } 00129 } 00130 00131 /** 00132 * Break down schema and table name into adjacent columns in the 00133 * result set 00134 */ 00135 void doTableName(int col, Row row, String name) throws SQLException { 00136 int idx = name.indexOf('.'); 00137 if (idx > 0) { 00138 row.set(col, new ValueString(name.substring(0, idx))); 00139 row.set(col+1, new ValueString(name.substring(idx+1))); 00140 } else { 00141 row.set(col, new ValueString("")); 00142 row.set(col+1, new ValueString(name)); 00143 } 00144 } 00145 00146 static Column[] cColumnPrivileges = { 00147 new Column("TABLE_CAT", typeString), // 1 00148 new Column("TABLE_SCHEM", typeString), // 2 00149 new Column("TABLE_NAME", typeString), // 3 00150 new Column("COLUMN_NAME", typeString), // 4 00151 new Column("GRANTOR", typeString), // 5 00152 new Column("GRANTEE", typeString), // 6 00153 new Column("PRIVILEGE", typeString), // 7 00154 new Column("IS_GRANTABLE", typeString) // 8 00155 }; 00156 00157 static Column[] cCatalogs = { 00158 new Column("TABLE_CAT", typeString) // 1 00159 }; 00160 00161 static Column[] cProcedureColumns = { 00162 new Column("PROCEDURE_CAT", typeString), // 1 00163 new Column("PROCEDURE_SCHEM", typeString), // 2 00164 new Column("PROCEDURE_NAME", typeString), // 3 00165 new Column("COLUMN_NAME", typeString), // 4 00166 new Column("COLUMN_TYPE", typeShort), // 5 00167 new Column("DATA_TYPE", typeShort), // 6 00168 new Column("TYPE_NAME", typeString), // 7 00169 new Column("PRECISION", typeInt), // 8 00170 new Column("LENGTH", typeInt), // 9 00171 new Column("SCALE", typeShort), // 10 00172 new Column("RADIX", typeInt), // 11 00173 new Column("NULLABLE", typeShort), // 12 00174 new Column("REMARKS", typeString), // 13 00175 }; 00176 00177 static Column[] cProcedures = { 00178 new Column("PROCEDURE_CAT", typeString), // 1 00179 new Column("PROCEDURE_SCHEM", typeString), // 2 00180 new Column("PROCEDURE_NAME", typeString), // 3 00181 new Column("reserved1", typeString), // 4 00182 new Column("reserved2", typeString), // 5 00183 new Column("reserved3", typeString), // 6 00184 new Column("REMARKS", typeString), // 7 00185 new Column("PROCEDURE_TYPE", typeShort) // 8 00186 }; 00187 00188 static Column[] cTablePrivileges = { 00189 new Column("TABLE_CAT", typeString), // 1 00190 new Column("TABLE_SCHEM", typeString), // 2 00191 new Column("TABLE_NAME", typeString), // 3 00192 new Column("GRANTOR", typeString), // 4 00193 new Column("GRANTEE", typeString), // 5 00194 new Column("PRIVILEGE", typeString), // 6 00195 new Column("IS_GRANTABLE", typeString) // 7 00196 }; 00197 00198 static Column[] cUdts= { 00199 new Column("TYPE_CAT", typeString), // 1 00200 new Column("TYPE_SCHEM", typeString), // 2 00201 new Column("TYPE_NAME", typeString), // 3 00202 new Column("CLASS_NAME", typeString), // 4 00203 new Column("DATA_TYPE", typeShort), // 5 00204 new Column("REMARKS", typeString) // 6 00205 }; 00206 00207 static Column[] cVersionColumns = { 00208 new Column("SCOPE", typeShort), // 1 00209 new Column("COLUMN_NAME", typeString), // 2 00210 new Column("DATA_TYPE", typeShort), // 3 00211 new Column("TYPE_NAME", typeString), // 4 00212 new Column("COLUMN_SIZE", typeInt), // 5 00213 new Column("BUFFER_LENGTH", typeInt), // 6 00214 new Column("DECIMAL_DIGITS", typeInt), // 7 00215 new Column("PSEUDO_COLUMN", typeShort) // 8 00216 }; 00217 00218 /** 00219 * Information schema 00220 */ 00221 public static Cursor find(Session session, String name) 00222 throws SQLException 00223 { 00224 if (name.equalsIgnoreCase("SYSTEM.CATALOGS")) { 00225 return create(session, cCatalogs); 00226 } else if (name.equalsIgnoreCase("SYSTEM.COLUMNS")) { 00227 return new MetaColumns(session, null); 00228 } else if (name.equalsIgnoreCase("SYSTEM.COLUMNPRIVILEGES")) { 00229 return create(session, cColumnPrivileges); 00230 } else if (name.equalsIgnoreCase("SYSTEM.CROSSREFERENCE")) { 00231 return new MetaCrossReference(session, null); 00232 } else if (name.equalsIgnoreCase("SYSTEM.INDEXINFO")) { 00233 return new MetaIndexInfo(session, null); 00234 } else if (name.equalsIgnoreCase("SYSTEM.PRIMARYKEYS")) { 00235 return new MetaPrimaryKeys(session, null); 00236 } else if (name.equalsIgnoreCase("SYSTEM.PROCEDURECOLUMNS")) { 00237 return create(session, cProcedureColumns); 00238 } else if (name.equalsIgnoreCase("SYSTEM.PROCEDURES")) { 00239 return create(session, cProcedures); 00240 } else if (name.equalsIgnoreCase("SYSTEM.SCHEMAS")) { 00241 return new MetaSchemas(session); 00242 } else if (name.equalsIgnoreCase("SYSTEM.TABLES")) { 00243 return new MetaTables(session, null); 00244 } else if (name.equalsIgnoreCase("SYSTEM.TABLEPRIVILEGES")) { 00245 return create(session, cTablePrivileges); 00246 } else if (name.equalsIgnoreCase("SYSTEM.TABLETYPES")) { 00247 return new MetaTableTypes(session); 00248 } else if (name.equalsIgnoreCase("SYSTEM.VERSIONCOLUMNS")) { 00249 return create(session, cVersionColumns); 00250 } else if (name.equalsIgnoreCase("SYSTEM.TYPEINFO")) { 00251 return new MetaTypes(session); 00252 } else if (name.equalsIgnoreCase("SYSTEM.UDTS")) { 00253 return create(session, cUdts); 00254 } 00255 return null; 00256 } 00257 }