1 package com.quadcap.sql.meta; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.ArrayList ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Vector ; 47 48 import java.sql.ResultSetMetaData ; 49 import java.sql.SQLException ; 50 51 import com.quadcap.sql.Column; 52 import com.quadcap.sql.Database; 53 import com.quadcap.sql.Expression; 54 import com.quadcap.sql.Relation; 55 import com.quadcap.sql.Row; 56 import com.quadcap.sql.Session; 57 import com.quadcap.sql.StaticCursor; 58 import com.quadcap.sql.Table; 59 60 import com.quadcap.sql.index.BCursor; 61 import com.quadcap.sql.index.Btree; 62 63 import com.quadcap.sql.types.*; 64 65 import com.quadcap.util.Debug; 66 67 73 public class MetaColumns extends MetaCursor { 74 static Column[] cols = { 75 new Column("TABLE_CAT", typeString), new Column("TABLE_SCHEM", typeString), new Column("TABLE_NAME", typeString), new Column("COLUMN_NAME", typeString), new Column("DATA_TYPE", typeShort), new Column("TYPE_NAME", typeString), new Column("COLUMN_SIZE", typeInt), new Column("BUFFER_LENGTH", typeAny), new Column("DECIMAL_DIGITS", typeInt), new Column("NUM_PREC_RADIX", typeInt), new Column("NULLABLE", typeInt), new Column("REMARKS", typeString), new Column("COLUMN_DEF", typeString), new Column("SQL_DATA_TYPE", typeInt), new Column("SQL_DATETIME_SUB", typeInt), new Column("CHAR_OCTET_LENGTH", typeInt), new Column("ORDINAL_POSITION", typeInt), new Column("IS_NULLABLE", typeString) }; 94 95 static int[] sortColumns = { 2, 3, 17 }; 96 97 public MetaColumns(Session session, Expression predicate) 98 throws SQLException 99 { 100 super(session, predicate); 101 try { 102 addColumns(cols); 103 Database db = session.getDatabase(); 104 session.getTableWriteLock("#Schema"); 105 synchronized (db.getFile().getLock()) { 106 Iterator iter = db.getRelationNameIterator(); 107 while (iter.hasNext()) { 108 Relation r = db.getRelation(iter.next().toString()); 109 if (r instanceof Table) { 110 Table t = (Table)r; 111 doTable(t); 112 } 113 } 114 } 115 sort(); 116 } catch (ValueException e) { 117 Debug.print(e); 118 SQLException te = new SQLException (e.toString(), "Q000J"); 119 te.setNextException(e); 120 throw te; 121 } catch (IOException e) { 122 Debug.print(e); 123 throw new SQLException (e.toString(), "Q000K"); 124 } 125 } 126 127 public int[] getSortColumns() { 128 return sortColumns; 129 } 130 131 void doTable(Table t) throws SQLException { 132 for (int i = 1; i <= t.getColumnCount(); i++) { 133 Column col = t.getColumn(i); 134 Row row = doColumn(t, col); 135 if (rowMatch(row)) { 136 addRow(row); 137 } 138 } 139 } 140 141 Row doColumn(Table t, Column col) throws SQLException { 142 Type type = col.getType(); 143 Row row = new Row(18); 144 row.set(1, ValueNull.valueNull); 145 doTableName(2, row, t.getName()); 146 row.set(4, new ValueString(col.getShortName())); 147 row.set(5, new ValueShort(type.getJDBCType())); 148 row.set(6, new ValueString(type.getTypeName())); 149 150 Value size = new ValueInteger(type.getPrecision()); 151 if (type instanceof TypeChar) { 152 size = new ValueInteger(((TypeChar)type).getMax()); 153 } else if (type instanceof TypeVarChar) { 154 size = new ValueInteger(((TypeVarChar)type).getMax()); 155 } 156 row.set(7, size); 157 row.set(8, ValueNull.valueNull); 158 row.set(9, new ValueInteger(type.getScale())); 159 row.set(10, new ValueInteger(10)); 160 row.set(11, new ValueInteger(col.getNullable())); 161 row.set(12, ValueNull.valueNull); 162 Value defVal = ValueNull.valueNull; 163 Expression def = col.getDefault(); 164 if (def != null) { 165 defVal = new ValueString(def.toString()); 166 } 167 row.set(13, defVal); 168 row.set(14, ValueNull.valueNull); 169 row.set(15, ValueNull.valueNull); 170 171 Value max = ValueNull.valueNull; 172 if (type instanceof TypeChar) { 173 max = new ValueInteger(2 * ((TypeChar)type).getMax()); 174 } else if (type instanceof TypeVarChar) { 175 max = new ValueInteger(2 * ((TypeVarChar)type).getMax()); 176 } 177 row.set(16, max); 178 row.set(17, new ValueInteger(col.getColumn())); 179 180 String nullstr = ""; 181 switch (col.getNullable()) { 182 case ResultSetMetaData.columnNoNulls: 183 nullstr = "NO"; 184 break; 185 case ResultSetMetaData.columnNullable: 186 nullstr = "YES"; 187 break; 188 } 189 row.set(18, new ValueString(nullstr)); 190 return row; 191 } 192 193 } 194 | Popular Tags |