KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > View


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: View.java,v 1.4 2003/04/28 00:52:17 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.sql.Connection JavaDoc;
14 import java.sql.SQLException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
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 JavaDoc conn) throws SQLException JavaDoc
49     {
50         LOG.info("Creating view: " + this);
51
52         super.create(conn);
53     }
54
55
56     public boolean validate(int flags, Connection JavaDoc conn) throws SQLException JavaDoc
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 JavaDoc unvalidated = new HashMap JavaDoc(columnsByName);
82                 Iterator JavaDoc 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                         /*
96                          * Otherwise it's a duplicate column name in the
97                          * metadata and we ignore it. Cloudscape is known to
98                          * do this, although I think that's probably a bug.
99                          */

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 JavaDoc conn) throws SQLException JavaDoc
120     {
121         LOG.info("Dropping view: " + this);
122
123         super.drop(conn);
124     }
125
126
127     protected List JavaDoc getSQLDropStatements()
128     {
129         assertIsInitialized();
130
131         ArrayList JavaDoc stmts = new ArrayList JavaDoc();
132         stmts.add(dba.getDropViewStatement(this));
133
134         return stmts;
135     }
136 }
137
Popular Tags