001: /*
002: * $Id: AxionColumnsMetaTableUpdater.java,v 1.5 2005/12/20 18:32:57 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.metaupdaters;
042:
043: import java.sql.DatabaseMetaData;
044: import java.util.Iterator;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048: import org.axiondb.AxionCommand;
049: import org.axiondb.AxionException;
050: import org.axiondb.Column;
051: import org.axiondb.ColumnIdentifier;
052: import org.axiondb.Constraint;
053: import org.axiondb.Database;
054: import org.axiondb.Function;
055: import org.axiondb.Literal;
056: import org.axiondb.Row;
057: import org.axiondb.Selectable;
058: import org.axiondb.SelectableBasedConstraint;
059: import org.axiondb.Table;
060: import org.axiondb.TableIdentifier;
061: import org.axiondb.constraints.NotNullConstraint;
062: import org.axiondb.constraints.PrimaryKeyConstraint;
063: import org.axiondb.engine.commands.DeleteCommand;
064: import org.axiondb.engine.commands.UpdateCommand;
065: import org.axiondb.engine.rows.SimpleRow;
066: import org.axiondb.event.BaseDatabaseModificationListener;
067: import org.axiondb.event.ColumnEvent;
068: import org.axiondb.event.ConstraintEvent;
069: import org.axiondb.event.DatabaseModificationListener;
070: import org.axiondb.event.DatabaseModifiedEvent;
071: import org.axiondb.event.RowEvent;
072: import org.axiondb.event.TableModificationListener;
073: import org.axiondb.functions.FunctionIdentifier;
074: import org.axiondb.util.ValuePool;
075:
076: /**
077: * Updates the <code>AXION_TABLES</code> meta table
078: *
079: * @version $Revision: 1.5 $ $Date: 2005/12/20 18:32:57 $
080: * @author Chuck Burdick
081: * @author Rodney Waldhoff
082: * @author Ahimanikya Satapathy
083: */
084: public class AxionColumnsMetaTableUpdater extends
085: BaseDatabaseModificationListener implements
086: DatabaseModificationListener, TableModificationListener {
087: private static Log _log = LogFactory
088: .getLog(AxionColumnsMetaTableUpdater.class);
089: private Database _db = null;
090:
091: public AxionColumnsMetaTableUpdater(Database db) {
092: _db = db;
093: }
094:
095: public void tableAdded(DatabaseModifiedEvent e) {
096: try {
097: for (int i = 0, I = e.getTable().getColumnCount(); i < I; i++) {
098: Column col = e.getTable().getColumn(i);
099: Row row = createRowForColumnAdded(e.getTable(), col);
100: _db.getTable("AXION_COLUMNS").addRow(row);
101: }
102: } catch (AxionException ex) {
103: _log.error("Unable to mention table in system tables", ex);
104: }
105: }
106:
107: public void tableDropped(DatabaseModifiedEvent e) {
108: Function where = makeEqualFunction(new ColumnIdentifier(
109: "TABLE_NAME"), new Literal(e.getTable().getName()));
110: AxionCommand cmd = new DeleteCommand("AXION_COLUMNS", where);
111: try {
112: cmd.execute(_db);
113: } catch (AxionException ex) {
114: _log
115: .error(
116: "Unable to remove mention of table in system tables",
117: ex);
118: }
119: }
120:
121: //===================================== TableModificationListener Interface
122:
123: public void columnAdded(ColumnEvent e) throws AxionException {
124: Row row = createRowForColumnAdded(e.getTable(), e.getColumn());
125: _db.getTable("AXION_COLUMNS").addRow(row);
126: }
127:
128: public void rowInserted(RowEvent event) throws AxionException {
129: }
130:
131: public void rowDeleted(RowEvent event) throws AxionException {
132: }
133:
134: public void rowUpdated(RowEvent event) throws AxionException {
135: }
136:
137: public void constraintAdded(ConstraintEvent event)
138: throws AxionException {
139: }
140:
141: public void constraintRemoved(ConstraintEvent event)
142: throws AxionException {
143: }
144:
145: public void updateNullableStatus(ConstraintEvent event,
146: boolean changeNullableTo) {
147: Constraint c = event.getConstraint();
148: if (c instanceof NotNullConstraint
149: || c instanceof PrimaryKeyConstraint) {
150: SelectableBasedConstraint nn = (SelectableBasedConstraint) c;
151: for (int i = 0; i < nn.getSelectableCount(); i++) {
152: try {
153: AxionCommand cmd = createUpdateNullableCmd(event
154: .getTable(),
155: nn.getSelectable(i).getLabel(),
156: changeNullableTo);
157: cmd.execute(_db);
158: } catch (AxionException ex) {
159: _log
160: .error(
161: "Unable to mark nullable status in system tables",
162: ex);
163: }
164: }
165: }
166: }
167:
168: protected Row createRowForColumnAdded(Table t, Column col)
169: throws AxionException {
170: boolean isnullable = isNullable(t, col.getName());
171: Integer nullableInt = ValuePool
172: .getInt(isnullable ? DatabaseMetaData.columnNullable
173: : DatabaseMetaData.columnNoNulls);
174: String nullableString = isnullable ? "YES" : "NO";
175:
176: Short typeVal = new Short((short) col.getDataType()
177: .getJdbcType());
178: Integer size = ValuePool.getInt(col.getDataType()
179: .getPrecision());
180: Integer scale = ValuePool.getInt(col.getDataType().getScale());
181: ;
182: Integer radix = ValuePool.getInt(col.getDataType()
183: .getPrecisionRadix());
184:
185: String typeName = col.getDataType().getClass().getName();
186: String sqlType = col.getSqlType();
187: if (sqlType == null) {
188: sqlType = typeName;
189: } else {
190: sqlType = sqlType.toUpperCase();
191: }
192: int ord = t.getColumnIndex(col.getName());
193:
194: SimpleRow row = new SimpleRow(22);
195: row.set(0, ""); // table_cat
196: row.set(1, ""); // table schem
197: row.set(2, t.getName()); // table_name
198: row.set(3, col.getName().toUpperCase()); // column_name
199: row.set(4, typeVal); // data_type
200: row.set(5, sqlType); // type_name
201: row.set(6, size); // column_size
202: row.set(7, null); // buffer_length (unused)
203: row.set(8, scale); // decimal_digits
204: row.set(9, radix); // num_prec_radix
205: row.set(10, nullableInt); // nullable
206: row.set(11, null); // remarks
207: row.set(12, null); // column_def
208: row.set(13, null); // sql_data_type (unused)
209: row.set(14, null); // sql_datetime_sub (unused)
210: row.set(15, null); // char_octet_length
211: row.set(16, ValuePool.getInt(ord + 1)); // ordinal_position
212: row.set(17, nullableString); // is_nullable
213: row.set(18, null); // scope_catalog
214: row.set(19, null); // scope_schema
215: row.set(20, null); // scope_table
216: row.set(21, null); // source_data_type
217: return row;
218: }
219:
220: private UpdateCommand createUpdateNullableCmd(Table t,
221: String colName, boolean isnullable) {
222: Integer nullableInt = ValuePool
223: .getInt(isnullable ? DatabaseMetaData.columnNullable
224: : DatabaseMetaData.columnNoNulls);
225: String nullableString = isnullable ? "YES" : "NO";
226:
227: Selectable tableMatch = makeEqualFunction(new ColumnIdentifier(
228: "TABLE_NAME"), new Literal(t.getName()));
229:
230: Selectable colMatch = makeEqualFunction(new ColumnIdentifier(
231: "COLUMN_NAME"), new Literal(colName.toUpperCase()));
232:
233: FunctionIdentifier where = new FunctionIdentifier("and");
234: where.addArgument(tableMatch);
235: where.addArgument(colMatch);
236:
237: UpdateCommand cmd = new UpdateCommand();
238: cmd.setTable(new TableIdentifier("AXION_COLUMNS"));
239: cmd.addColumn(new ColumnIdentifier("NULLABLE"));
240: cmd.addValue(new Literal(nullableInt));
241: cmd.addColumn(new ColumnIdentifier("IS_NULLABLE"));
242: cmd.addValue(new Literal(nullableString));
243: cmd.setWhere(where);
244: return cmd;
245: }
246:
247: private boolean isNullable(Table table, String column) {
248: // FIXME: this is a little hack
249: for (Iterator iter = table.getConstraints(); iter.hasNext();) {
250: Constraint c = (Constraint) iter.next();
251: if (c instanceof NotNullConstraint
252: || c instanceof PrimaryKeyConstraint) {
253: SelectableBasedConstraint nn = (SelectableBasedConstraint) c;
254: for (int i = 0; i < nn.getSelectableCount(); i++) {
255: if (column.equals(nn.getSelectable(i).getLabel())) {
256: return false;
257: }
258: }
259: }
260: }
261: return true;
262: }
263:
264: private FunctionIdentifier makeEqualFunction(Selectable left,
265: Selectable right) {
266: FunctionIdentifier function = new FunctionIdentifier("=");
267: function.addArgument(left);
268: function.addArgument(right);
269: return function;
270: }
271: }
|