001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: View.java,v 1.4 2003/04/28 00:52:17 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import java.sql.Connection;
014: import java.sql.SQLException;
015: import java.util.ArrayList;
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.List;
019: import org.apache.log4j.Category;
020:
021: abstract class View extends AbstractTable {
022: private static final Category LOG = Category
023: .getInstance(View.class);
024:
025: public View(StoreManager storeMgr) {
026: super (storeMgr);
027: }
028:
029: public View(SQLIdentifier name, StoreManager storeMgr) {
030: super (name, storeMgr);
031: }
032:
033: public synchronized void addColumn(Column col) {
034: if (col.isPrimaryKeyPart())
035: throw new PrimaryKeyColumnNotAllowedException(this , col);
036:
037: super .addColumn(col);
038: }
039:
040: public void create(Connection conn) throws SQLException {
041: LOG.info("Creating view: " + this );
042:
043: super .create(conn);
044: }
045:
046: public boolean validate(int flags, Connection conn)
047: throws SQLException {
048: assertIsInitialized();
049:
050: boolean dbWasModified = false;
051:
052: if ((flags & VALIDATE) != 0) {
053: int tableType = storeMgr.getTableType(name, conn);
054:
055: if (tableType == TABLE_TYPE_MISSING) {
056: if ((flags & AUTO_CREATE) == 0)
057: throw new MissingTableException(this );
058:
059: create(conn);
060: dbWasModified = true;
061: } else {
062: LOG.info("Validating view: " + this );
063:
064: if (tableType != TABLE_TYPE_VIEW)
065: throw new NotAViewException(this );
066:
067: HashMap unvalidated = new HashMap(columnsByName);
068: Iterator i = storeMgr.getColumnInfo(name, conn)
069: .iterator();
070:
071: while (i.hasNext()) {
072: ColumnInfo ci = (ColumnInfo) i.next();
073: SQLIdentifier colName = new SQLIdentifier(dba,
074: ci.columnName);
075:
076: Column col = (Column) unvalidated.get(colName);
077:
078: if (col == null) {
079: if (!hasColumnName(colName))
080: throw new UnexpectedColumnException(this ,
081: colName);
082: /*
083: * Otherwise it's a duplicate column name in the
084: * metadata and we ignore it. Cloudscape is known to
085: * do this, although I think that's probably a bug.
086: */
087: } else {
088: col.validate(ci);
089: unvalidated.remove(colName);
090: }
091: }
092:
093: if (unvalidated.size() > 0)
094: throw new MissingColumnException(this , unvalidated
095: .values());
096: }
097: }
098:
099: state = TABLE_STATE_VALIDATED;
100:
101: return dbWasModified;
102: }
103:
104: public void drop(Connection conn) throws SQLException {
105: LOG.info("Dropping view: " + this );
106:
107: super .drop(conn);
108: }
109:
110: protected List getSQLDropStatements() {
111: assertIsInitialized();
112:
113: ArrayList stmts = new ArrayList();
114: stmts.add(dba.getDropViewStatement(this));
115:
116: return stmts;
117: }
118: }
|