1 10 11 package com.triactive.jdo.store; 12 13 import java.sql.Connection ; 14 import java.sql.SQLException ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import org.apache.log4j.Category; 20 21 22 abstract class View extends AbstractTable 23 { 24 private static final Category LOG = Category.getInstance(View.class); 25 26 27 public View(StoreManager storeMgr) 28 { 29 super(storeMgr); 30 } 31 32 33 public View(SQLIdentifier name, StoreManager storeMgr) 34 { 35 super(name, storeMgr); 36 } 37 38 39 public synchronized void addColumn(Column col) 40 { 41 if (col.isPrimaryKeyPart()) 42 throw new PrimaryKeyColumnNotAllowedException(this, col); 43 44 super.addColumn(col); 45 } 46 47 48 public void create(Connection conn) throws SQLException 49 { 50 LOG.info("Creating view: " + this); 51 52 super.create(conn); 53 } 54 55 56 public boolean validate(int flags, Connection conn) throws SQLException 57 { 58 assertIsInitialized(); 59 60 boolean dbWasModified = false; 61 62 if ((flags & VALIDATE) != 0) 63 { 64 int tableType = storeMgr.getTableType(name, conn); 65 66 if (tableType == TABLE_TYPE_MISSING) 67 { 68 if ((flags & AUTO_CREATE) == 0) 69 throw new MissingTableException(this); 70 71 create(conn); 72 dbWasModified = true; 73 } 74 else 75 { 76 LOG.info("Validating view: " + this); 77 78 if (tableType != TABLE_TYPE_VIEW) 79 throw new NotAViewException(this); 80 81 HashMap unvalidated = new HashMap (columnsByName); 82 Iterator i = storeMgr.getColumnInfo(name, conn).iterator(); 83 84 while (i.hasNext()) 85 { 86 ColumnInfo ci = (ColumnInfo)i.next(); 87 SQLIdentifier colName = new SQLIdentifier(dba, ci.columnName); 88 89 Column col = (Column)unvalidated.get(colName); 90 91 if (col == null) 92 { 93 if (!hasColumnName(colName)) 94 throw new UnexpectedColumnException(this, colName); 95 100 } 101 else 102 { 103 col.validate(ci); 104 unvalidated.remove(colName); 105 } 106 } 107 108 if (unvalidated.size() > 0) 109 throw new MissingColumnException(this, unvalidated.values()); 110 } 111 } 112 113 state = TABLE_STATE_VALIDATED; 114 115 return dbWasModified; 116 } 117 118 119 public void drop(Connection conn) throws SQLException 120 { 121 LOG.info("Dropping view: " + this); 122 123 super.drop(conn); 124 } 125 126 127 protected List getSQLDropStatements() 128 { 129 assertIsInitialized(); 130 131 ArrayList stmts = new ArrayList (); 132 stmts.add(dba.getDropViewStatement(this)); 133 134 return stmts; 135 } 136 } 137 | Popular Tags |