1 package com.quadcap.sql.meta; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.Iterator ; 44 45 import java.sql.DatabaseMetaData ; 46 import java.sql.ResultSetMetaData ; 47 import java.sql.SQLException ; 48 49 import com.quadcap.sql.Column; 50 import com.quadcap.sql.Constraint; 51 import com.quadcap.sql.Database; 52 import com.quadcap.sql.Expression; 53 import com.quadcap.sql.PrimaryKeyConstraint; 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.Btree; 61 import com.quadcap.sql.index.BCursor; 62 63 import com.quadcap.sql.types.*; 64 65 import com.quadcap.util.Debug; 66 67 72 public class MetaPrimaryKeys extends MetaCursor { 73 static Column[] cols = { 74 new Column("TABLE_CAT", typeString), new Column("TABLE_SCHEM", typeString), new Column("TABLE_NAME", typeString), new Column("COLUMN_NAME", typeString), new Column("KEY_SEQ", typeShort), new Column("PK_NAME", typeString) }; 81 82 static int[] sortColumns = { 4 }; 83 84 public MetaPrimaryKeys(Session session, Expression predicate) 85 throws SQLException 86 { 87 super(session, predicate); 88 try { 89 addColumns(cols); 90 Database db = session.getDatabase(); 91 session.getTableWriteLock("#Schema"); 92 synchronized (db.getFile().getLock()) { 93 Iterator iter = db.getRelationNameIterator(); 94 while (iter.hasNext()) { 95 String name = (String )iter.next(); 96 Relation r = db.getRelation(name); 97 if (r instanceof Table) { 98 Table t = (Table)r; 99 int num = t.getNumConstraints(); 100 for (int i = 0; i < num; i++) { 101 Constraint c = t.getConstraint(i); 102 if (c instanceof PrimaryKeyConstraint) { 103 doConstraint(t, (PrimaryKeyConstraint)c); 104 } 105 } 106 } 107 } 108 } 109 sort(); 110 } catch (ValueException e) { 111 Debug.print(e); 112 SQLException te = new SQLException (e.toString(), "Q000N"); 113 te.setNextException(e); 114 throw te; 115 } catch (IOException e) { 116 Debug.print(e); 117 throw new SQLException (e.toString(), "Q000O"); 118 } 119 } 120 121 public int[] getSortColumns() { 122 return sortColumns; 123 } 124 125 void doConstraint(Table t, PrimaryKeyConstraint c) throws SQLException { 126 int[] colIndices = c.getColumns(); 127 for (int i = 0; i < colIndices.length; i++) { 128 Column col = t.getColumn(colIndices[i]); 129 Row row = doColumn(t, c, i, col); 130 if (rowMatch(row)) { 131 addRow(row); 132 } 133 } 134 } 135 136 Row doColumn(Table t, PrimaryKeyConstraint c, int pos, Column col) 137 throws SQLException 138 { 139 Type type = col.getType(); 140 Row row = new Row(6); 141 row.set(1, ValueNull.valueNull); 142 doTableName(2, row, t.getName()); 143 row.set(4, new ValueString(col.getShortName())); 144 row.set(5, new ValueShort(pos+1)); 145 row.set(6, new ValueString(c.getName())); 146 return row; 147 } 148 149 } 150 | Popular Tags |