1 package com.quadcap.sql.meta; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.Enumeration ; 44 import java.util.Vector ; 45 46 import java.sql.DatabaseMetaData ; 47 import java.sql.ResultSetMetaData ; 48 import java.sql.SQLException ; 49 50 import com.quadcap.sql.Column; 51 import com.quadcap.sql.Constraint; 52 import com.quadcap.sql.Database; 53 import com.quadcap.sql.Expression; 54 import com.quadcap.sql.PrimaryKeyConstraint; 55 import com.quadcap.sql.Relation; 56 import com.quadcap.sql.Row; 57 import com.quadcap.sql.Session; 58 import com.quadcap.sql.StaticCursor; 59 import com.quadcap.sql.Table; 60 import com.quadcap.sql.UniqueConstraint; 61 62 import com.quadcap.sql.index.BCursor; 63 import com.quadcap.sql.index.Btree; 64 65 import com.quadcap.sql.types.*; 66 67 import com.quadcap.util.Debug; 68 69 75 public class MetaBestRowId extends MetaCursor { 76 static Column[] cols = { 77 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", typeAny), new Column("DECIMAL_DIGITS", typeInt), new Column("PSEUDO_COLUMN", typeShort) }; 86 87 static int[] sortColumns = { 2 }; 88 89 95 public MetaBestRowId(Session session, String tableName, int scope, 96 boolean nullable) 97 throws SQLException 98 { 99 super(session, null); 100 try { 101 addColumns(cols); 102 Database db = session.getDatabase(); 103 session.getTableWriteLock("#Schema"); 104 tableName = tableName.toUpperCase(); 105 Table t = (Table)db.getRelation(tableName); 106 if (t != null) { 107 UniqueConstraint uc = null; 108 int num = t.getNumConstraints(); 109 for (int i = 0; i < num; i++) { 110 Constraint c = t.getConstraint(i); 111 if (c instanceof UniqueConstraint) { 112 if (nullable || checkNullable(c)) { 113 uc = (UniqueConstraint)c; 114 if (c instanceof PrimaryKeyConstraint) break; 115 } 116 } 117 } 118 if (uc != null) { 119 int cnt = uc.getColumnCount(); 120 for (int i = 0; i < cnt; i++) { 121 addRow(doColumn(uc.getColumn(i))); 122 } 123 } 124 } 125 sort(); 126 } catch (ValueException e) { 127 Debug.print(e); 128 SQLException te = new SQLException (e.toString(), "Q000J"); 129 te.setNextException(e); 130 throw te; 131 } catch (IOException e) { 132 Debug.print(e); 133 throw new SQLException (e.toString(), "Q000K"); 134 } 135 } 136 137 140 public int[] getSortColumns() { 141 return sortColumns; 142 } 143 144 147 public boolean checkNullable(Constraint c) throws SQLException { 148 boolean ret = !(c instanceof PrimaryKeyConstraint); 149 int cnt = c.getColumnCount(); 150 for (int i = 0; ret && i < cnt; i++) { 151 ret = c.getColumn(i).isNullable(); 152 } 153 return ret; 154 } 155 156 159 Row doColumn(Column col) throws SQLException { 160 Type type = col.getType(); 161 Row row = new Row(18); 162 row.set(1, new ValueShort(DatabaseMetaData.bestRowSession)); 163 row.set(2, new ValueString(col.getShortName())); 164 row.set(3, new ValueShort(type.getJDBCType())); 165 row.set(4, new ValueString(type.getTypeName())); 166 167 Value size = new ValueInteger(type.getPrecision()); 168 if (type instanceof TypeChar) { 169 size = new ValueInteger(((TypeChar)type).getMax()); 170 } else if (type instanceof TypeVarChar) { 171 size = new ValueInteger(((TypeVarChar)type).getMax()); 172 } 173 row.set(5, size); 174 row.set(6, ValueNull.valueNull); 175 row.set(7, new ValueShort(type.getScale())); 176 row.set(8, new ValueShort(DatabaseMetaData.bestRowUnknown)); 177 return row; 178 } 179 } 180 | Popular Tags |