001: /*
002: * User: mrettig
003: * Date: Jun 25, 2002
004: * Time: 11:38:42 AM
005: */
006: package net.sourceforge.jaxor;
007:
008: import net.sourceforge.jaxor.util.SystemException;
009: import net.sourceforge.jaxor.util.Validation;
010:
011: import java.io.Serializable;
012: import java.sql.Connection;
013: import java.sql.DatabaseMetaData;
014: import java.sql.ResultSet;
015: import java.sql.SQLException;
016: import java.util.*;
017:
018: public class MetaRow implements Serializable {
019: private final List _list = new ArrayList();
020: private final String _table;
021: private MetaParser _parser;
022: private Class _implClass;
023: private Class _listImpl;
024:
025: public MetaRow(String table, Class impl, Class listImpl) {
026: _table = table;
027: _implClass = impl;
028: _listImpl = listImpl;
029: }
030:
031: public Class getImplClass() {
032: return _implClass;
033: }
034:
035: public Class getListImplClass() {
036: return _listImpl;
037: }
038:
039: public void setImplClass(Class implClass) {
040: _implClass = implClass;
041: }
042:
043: public void add(MetaField field) {
044: _list.add(field);
045: _parser = null;
046: }
047:
048: public String getTableName() {
049: return _table;
050: }
051:
052: public Validation validateColumns(Connection conn,
053: boolean capitalizeTable) {
054: return findInvalidColumns(conn, capitalizeTable);
055: }
056:
057: private Validation findInvalidColumns(Connection conn,
058: boolean capitalizeTable) {
059: try {
060: Validation val = new Validation();
061: DatabaseMetaData meta = conn.getMetaData();
062: Set allColumns = getColumnNamesInUpperCase();
063:
064: String tableName = capitalizeTable ? getTableName()
065: .toUpperCase() : getTableName();
066:
067: ResultSet set = meta.getColumns(null, null, tableName, "%");
068: while (set.next()) {
069: String columnName = set.getString("COLUMN_NAME");
070: if (!allColumns.remove(columnName.toUpperCase())) {
071: val.addInfo("Table " + tableName
072: + " Contains Column: " + columnName
073: + " not found in SQLGEN");
074: } else {
075: boolean nullable = "YES".equals(set
076: .getString("IS_NULLABLE"));
077: MetaField field = findMetaByColumn(columnName);
078: if (!nullable && field.canBeNull()) {
079: String msg = " Jaxor nullable="
080: + field.canBeNull()
081: + " database nullable=" + nullable;
082: val.addError("Table: " + tableName
083: + " Column: " + columnName + msg);
084: } else if (!nullable) {
085: //fine we insert first time to initialize but then we never update it
086: } else if (nullable != field.canBeNull()) {
087: String msg = " Jaxor nullable="
088: + field.canBeNull()
089: + " database nullable=" + nullable;
090: val.addInfo("Table: " + tableName + " Column: "
091: + columnName + msg);
092: }
093: }
094: }
095: set.close();
096: for (Iterator iterator = allColumns.iterator(); iterator
097: .hasNext();) {
098: String s = (String) iterator.next();
099: val.addError(s + " column defined in " + tableName
100: + "Entity not found in database");
101: }
102: return val;
103: } catch (SQLException e) {
104: throw new SystemException(
105: "Failed to find Columns for table: "
106: + getTableName(), e);
107: }
108: }
109:
110: public MetaField findMetaByColumn(String columnName) {
111: for (Iterator iterator = _list.iterator(); iterator.hasNext();) {
112: MetaField metaField = (MetaField) iterator.next();
113: if (metaField.getColumn().equalsIgnoreCase(columnName))
114: return metaField;
115: }
116: return null;
117: }
118:
119: private Set getColumnNamesInUpperCase() {
120: Set set = new HashSet();
121: for (Iterator iterator = _list.iterator(); iterator.hasNext();) {
122: MetaField metaField = (MetaField) iterator.next();
123: set.add(metaField.getColumn().toUpperCase());
124: }
125: return set;
126: }
127:
128: public List getColumns() {
129: return Collections.unmodifiableList(_list);
130: }
131:
132: public List getPrimaryKey() {
133: List key = new ArrayList();
134: for (int i = 0; i < _list.size(); i++) {
135: MetaField field = (MetaField) _list.get(i);
136: if (field.isPrimaryKey())
137: key.add(field);
138: }
139: return key;
140: }
141:
142: public List getInsertColumns() {
143: List insert = new ArrayList();
144: for (Iterator iter = _list.iterator(); iter.hasNext();) {
145: MetaField element = (MetaField) iter.next();
146: if (!element.isAutoAssign())
147: insert.add(element);
148: }
149: return insert;
150: }
151:
152: public List getUpdateColumns() {
153: List all = new ArrayList();
154: for (Iterator iterator = _list.iterator(); iterator.hasNext();) {
155: MetaField attribute = (MetaField) iterator.next();
156: if (!attribute.isPrimaryKey())
157: all.add(attribute);
158: }
159: return all;
160: }
161:
162: public List getUpdateMatchColumns() {
163: List all = new ArrayList();
164: for (Iterator iterator = _list.iterator(); iterator.hasNext();) {
165: MetaField attribute = (MetaField) iterator.next();
166: if (attribute.isMatchOnUpdate())
167: all.add(attribute);
168: }
169: return all;
170: }
171:
172: public MetaParser getParser() {
173: if (_parser == null)
174: _parser = new MetaParser(this );
175: return _parser;
176: }
177:
178: public List getNonPrimaryKeyColumns() {
179: List results = new ArrayList();
180: for (Iterator iterator = _list.iterator(); iterator.hasNext();) {
181: MetaField metaField = (MetaField) iterator.next();
182: if (!metaField.isPrimaryKey())
183: results.add(metaField);
184: }
185: return results;
186: }
187: }
|