1 package com.quadcap.sql.meta; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.Vector ; 47 49 import java.sql.ResultSet ; 50 import java.sql.SQLException ; 51 52 import com.quadcap.sql.Column; 53 import com.quadcap.sql.Cursor; 54 import com.quadcap.sql.Database; 55 import com.quadcap.sql.Expression; 56 import com.quadcap.sql.Relation; 57 import com.quadcap.sql.Row; 58 import com.quadcap.sql.Session; 59 import com.quadcap.sql.StaticCursor; 60 import com.quadcap.sql.Table; 61 62 import com.quadcap.sql.types.*; 63 64 import com.quadcap.util.Debug; 65 66 71 public class MetaCursor extends StaticCursor { 72 static TypeVarChar typeString = new TypeVarChar(-1); 73 static TypeInt typeInt = new TypeInt(); 74 static TypeSmallInt typeShort = new TypeSmallInt(); 75 static TypeAny typeAny = new TypeAny(); 76 static TypeBinary typeBinary = new TypeBinary(1); 77 78 StaticCursor sc; 79 Expression predicate; 80 81 84 public MetaCursor(Session session, Expression predicate) 85 throws SQLException  86 { 87 super(session, new Vector ()); 88 try { 89 session.getTableWriteLock("#Schema"); 90 } catch (IOException e) { 91 throw new SQLException (e.toString()); 92 } 93 this.session = session; 94 this.predicate = predicate; 95 Vector vec = new Vector (); 96 vec.addElement(null); 97 sc = new StaticCursor(session, vec); 98 } 99 100 103 public static MetaCursor create(Session session, Column[] cols) 104 throws SQLException { 105 MetaCursor c = new MetaCursor(session, null); 106 c.addColumns(cols); 107 return c; 108 } 109 110 113 boolean rowMatch(Row row) throws SQLException { 114 if (predicate == null) return true; 115 sc.updateRow(0, row); 116 sc.absolute(1); 117 Value v = predicate.getValue(session, sc); 118 return (v instanceof ValueBoolean && ((ValueBoolean)v).isTrue()); 119 } 120 121 124 void addColumns(Column[] cols) throws SQLException { 125 for (int i = 0; i < cols.length; i++) { 126 addColumn(cols[i]); 127 sc.addColumn(cols[i]); 128 } 129 } 130 131 135 void doTableName(int col, Row row, String name) throws SQLException { 136 int idx = name.indexOf('.'); 137 if (idx > 0) { 138 row.set(col, new ValueString(name.substring(0, idx))); 139 row.set(col+1, new ValueString(name.substring(idx+1))); 140 } else { 141 row.set(col, new ValueString("")); 142 row.set(col+1, new ValueString(name)); 143 } 144 } 145 146 static Column[] cColumnPrivileges = { 147 new Column("TABLE_CAT", typeString), new Column("TABLE_SCHEM", typeString), new Column("TABLE_NAME", typeString), new Column("COLUMN_NAME", typeString), new Column("GRANTOR", typeString), new Column("GRANTEE", typeString), new Column("PRIVILEGE", typeString), new Column("IS_GRANTABLE", typeString) }; 156 157 static Column[] cCatalogs = { 158 new Column("TABLE_CAT", typeString) }; 160 161 static Column[] cProcedureColumns = { 162 new Column("PROCEDURE_CAT", typeString), new Column("PROCEDURE_SCHEM", typeString), new Column("PROCEDURE_NAME", typeString), new Column("COLUMN_NAME", typeString), new Column("COLUMN_TYPE", typeShort), new Column("DATA_TYPE", typeShort), new Column("TYPE_NAME", typeString), new Column("PRECISION", typeInt), new Column("LENGTH", typeInt), new Column("SCALE", typeShort), new Column("RADIX", typeInt), new Column("NULLABLE", typeShort), new Column("REMARKS", typeString), }; 176 177 static Column[] cProcedures = { 178 new Column("PROCEDURE_CAT", typeString), new Column("PROCEDURE_SCHEM", typeString), new Column("PROCEDURE_NAME", typeString), new Column("reserved1", typeString), new Column("reserved2", typeString), new Column("reserved3", typeString), new Column("REMARKS", typeString), new Column("PROCEDURE_TYPE", typeShort) }; 187 188 static Column[] cTablePrivileges = { 189 new Column("TABLE_CAT", typeString), new Column("TABLE_SCHEM", typeString), new Column("TABLE_NAME", typeString), new Column("GRANTOR", typeString), new Column("GRANTEE", typeString), new Column("PRIVILEGE", typeString), new Column("IS_GRANTABLE", typeString) }; 197 198 static Column[] cUdts= { 199 new Column("TYPE_CAT", typeString), new Column("TYPE_SCHEM", typeString), new Column("TYPE_NAME", typeString), new Column("CLASS_NAME", typeString), new Column("DATA_TYPE", typeShort), new Column("REMARKS", typeString) }; 206 207 static Column[] cVersionColumns = { 208 new Column("SCOPE", typeShort), new Column("COLUMN_NAME", typeString), new Column("DATA_TYPE", typeShort), new Column("TYPE_NAME", typeString), new Column("COLUMN_SIZE", typeInt), new Column("BUFFER_LENGTH", typeInt), new Column("DECIMAL_DIGITS", typeInt), new Column("PSEUDO_COLUMN", typeShort) }; 217 218 221 public static Cursor find(Session session, String name) 222 throws SQLException  223 { 224 if (name.equalsIgnoreCase("SYSTEM.CATALOGS")) { 225 return create(session, cCatalogs); 226 } else if (name.equalsIgnoreCase("SYSTEM.COLUMNS")) { 227 return new MetaColumns(session, null); 228 } else if (name.equalsIgnoreCase("SYSTEM.COLUMNPRIVILEGES")) { 229 return create(session, cColumnPrivileges); 230 } else if (name.equalsIgnoreCase("SYSTEM.CROSSREFERENCE")) { 231 return new MetaCrossReference(session, null); 232 } else if (name.equalsIgnoreCase("SYSTEM.INDEXINFO")) { 233 return new MetaIndexInfo(session, null); 234 } else if (name.equalsIgnoreCase("SYSTEM.PRIMARYKEYS")) { 235 return new MetaPrimaryKeys(session, null); 236 } else if (name.equalsIgnoreCase("SYSTEM.PROCEDURECOLUMNS")) { 237 return create(session, cProcedureColumns); 238 } else if (name.equalsIgnoreCase("SYSTEM.PROCEDURES")) { 239 return create(session, cProcedures); 240 } else if (name.equalsIgnoreCase("SYSTEM.SCHEMAS")) { 241 return new MetaSchemas(session); 242 } else if (name.equalsIgnoreCase("SYSTEM.TABLES")) { 243 return new MetaTables(session, null); 244 } else if (name.equalsIgnoreCase("SYSTEM.TABLEPRIVILEGES")) { 245 return create(session, cTablePrivileges); 246 } else if (name.equalsIgnoreCase("SYSTEM.TABLETYPES")) { 247 return new MetaTableTypes(session); 248 } else if (name.equalsIgnoreCase("SYSTEM.VERSIONCOLUMNS")) { 249 return create(session, cVersionColumns); 250 } else if (name.equalsIgnoreCase("SYSTEM.TYPEINFO")) { 251 return new MetaTypes(session); 252 } else if (name.equalsIgnoreCase("SYSTEM.UDTS")) { 253 return create(session, cUdts); 254 } 255 return null; 256 } 257 } 258
| Popular Tags
|