Author: jgbutler
Date: Mon Jul 24 14:50:36 2006
New Revision: 425193
URL: http://svn.apache.org/viewvc?rev=425193&view=rev
Log:
Abator changes - Support wildcards in table and schema names, improve build
Removed:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/devlib/ant-nodeps.jar
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/exception/UnknownTableException.java
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/build/version.properties
ibatis/trunk/java/mapper/mapper2/tools/abator/core/doc/ReleaseNotes.txt
ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/configreference/table.html
ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/whatsNew.html
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/api/GeneratedFile.java
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/AbatorContext.java
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/FullyQualifiedTable.java
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/TableConfiguration.java
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/xml/AbatorConfigurationParser.java
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/DatabaseIntrospector.java
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/IntrospectedTableImpl.java
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/StringUtility.java
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/messages/messages.properties
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/build/version.properties
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/build/version.properties?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/tools/abator/core/build/version.properties
(original)
+++ ibatis/trunk/java/mapper/mapper2/tools/abator/core/build/version.properties
Mon Jul 24 14:50:36 2006
@@ -1,4 +1,4 @@
#Abator build version info
-#Mon Jul 17 16:03:38 CDT 2006
+#Thu Jul 20 22:14:11 CDT 2006
version=0.7.0
-buildNum=139
+buildNum=142
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/doc/ReleaseNotes.txt
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/doc/ReleaseNotes.txt?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/tools/abator/core/doc/ReleaseNotes.txt
(original)
+++ ibatis/trunk/java/mapper/mapper2/tools/abator/core/doc/ReleaseNotes.txt Mon
Jul 24 14:50:36 2006
@@ -14,7 +14,7 @@
7. Added a typeHandler attribute to <columnOverride>
8. Added exampleMethodVisibility as a property for the DAO generator
9. Added methodNameCalculator as a property for the DAO generator
-10. (??) Added support for generating objects from a query
+10. Added the ability to specify wildcards for schema and/or tableName in
<table>
-------------------------------------------------------------------------------
Version 0.6.5:
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/configreference/table.html
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/configreference/table.html?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/configreference/table.html
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/configreference/table.html
Mon Jul 24 14:50:36 2006
@@ -43,7 +43,9 @@
</tr>
<tr>
<td valign="top">tableName</td>
- <td>The name of the database table (not including the schema or
catalog).</td>
+ <td>The name of the database table (not including the schema or catalog).
+ The specified value can contain SQL wildcards if so desired.
+ </td>
</tr>
</table>
@@ -56,7 +58,9 @@
<tr>
<td valign="top">schema</td>
<td>The database schema - not required if your database does not use
schemas,
- or if there is a default schema.</td>
+ or if there is a default schema.
+ The specified value can contain SQL wildcards if so desired.
+ </td>
</tr>
<tr>
<td>catalog</td>
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/whatsNew.html
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/whatsNew.html?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/whatsNew.html
(original)
+++ ibatis/trunk/java/mapper/mapper2/tools/abator/core/htmldoc/whatsNew.html
Mon Jul 24 14:50:36 2006
@@ -69,6 +69,11 @@
<li>Added the ability to override the naming convention for DAO method names.
See the <a
href="configreference/daoGenerator.html"><daoGenerator></a>
reference page for more information.</li>
+ <li>Added the ability to specify wildcards for schema or tableName in a table
+ configuration. This will allow generation of many tables with a simple
+ XML configuration.
+ See the <a href="configreference/table.html"><table></a>
+ reference page for more information.</li>
</ul>
</body>
</html>
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/api/GeneratedFile.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/api/GeneratedFile.java?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/api/GeneratedFile.java
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/api/GeneratedFile.java
Mon Jul 24 14:50:36 2006
@@ -67,4 +67,8 @@
* @return Returns the target project.
*/
public abstract String getTargetPackage();
+
+ public String toString() {
+ return getFormattedContent();
+ }
}
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/AbatorContext.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/AbatorContext.java?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/AbatorContext.java
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/AbatorContext.java
Mon Jul 24 14:50:36 2006
@@ -18,6 +18,7 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -28,7 +29,6 @@
import org.apache.ibatis.abator.api.ProgressCallback;
import org.apache.ibatis.abator.api.SqlMapGenerator;
import org.apache.ibatis.abator.exception.InvalidConfigurationException;
-import org.apache.ibatis.abator.exception.UnknownTableException;
import org.apache.ibatis.abator.internal.AbatorObjectFactory;
import org.apache.ibatis.abator.internal.NullProgressCallback;
import org.apache.ibatis.abator.internal.db.ConnectionFactory;
@@ -181,18 +181,18 @@
private void validateTableConfiguration(TableConfiguration tc, List
errors,
int listPosition) {
- if (tc.getTable() == null) {
- errors.add(Messages.getString("ValidationError.13", id));
//$NON-NLS-1$
- } else if
(!StringUtility.stringHasValue(tc.getTable().getTableName())) {
+ if (!StringUtility.stringHasValue(tc.getTableName())) {
errors.add(Messages.getString("ValidationError.6",
Integer.toString(listPosition))); //$NON-NLS-1$
}
if (tc.getGeneratedKey() != null
&&
!StringUtility.stringHasValue(tc.getGeneratedKey()
.getSqlStatement())) {
+ String tableName = StringUtility.composeFullyQualifiedTableName(
+ tc.getCatalog(), tc.getSchema(), tc.getTableName());
errors
.add(Messages.getString("ValidationError.7",
//$NON-NLS-1$
-
tc.getTable().getFullyQualifiedTableName()));
+ tableName));
}
}
@@ -236,7 +236,8 @@
Iterator iter = tableConfigurations.iterator();
while (iter.hasNext()) {
TableConfiguration tc = (TableConfiguration)
iter.next();
- String tableName =
tc.getTable().getFullyQualifiedTableName();
+ String tableName =
StringUtility.composeFullyQualifiedTableName(
+ tc.getCatalog(), tc.getSchema(), tc.getTableName());
if (!tc.areAnyStatementsEnabled()) {
warnings.add(Messages.getString("Warning.0", tableName)); //$NON-NLS-1$
@@ -244,21 +245,22 @@
}
- IntrospectedTable introspectedTable;
- try {
-
callback.startSubTask(Messages.getString("Progress.1", tableName));
//$NON-NLS-1$
- introspectedTable =
DatabaseIntrospector.introspectTable(connection, tc, javaTypeResolver,
warnings);
- callback.checkCancel();
- } catch (UnknownTableException e) {
-
warnings.add(Messages.getString("Warning.1", tableName)); //$NON-NLS-1$
- continue;
- }
-
- if (daoGenerator != null) {
-
generatedJavaFiles.addAll(daoGenerator.getGeneratedJavaFiles(introspectedTable,
callback));
- }
-
generatedJavaFiles.addAll(javaModelGenerator.getGeneratedJavaFiles(introspectedTable,
callback));
-
generatedXmlFiles.addAll(sqlMapGenerator.getGeneratedXMLFiles(introspectedTable,
callback));
+ Collection introspectedTables;
+
callback.startSubTask(Messages.getString("Progress.1", tableName));
//$NON-NLS-1$
+ introspectedTables =
DatabaseIntrospector.introspectTables(connection, tc, javaTypeResolver,
warnings);
+ callback.checkCancel();
+
+ Iterator iter2 = introspectedTables.iterator();
+ while (iter2.hasNext()) {
+ callback.checkCancel();
+ IntrospectedTable introspectedTable = (IntrospectedTable)
iter2.next();
+
+ if (daoGenerator != null) {
+
generatedJavaFiles.addAll(daoGenerator.getGeneratedJavaFiles(introspectedTable,
callback));
+ }
+
generatedJavaFiles.addAll(javaModelGenerator.getGeneratedJavaFiles(introspectedTable,
callback));
+
generatedXmlFiles.addAll(sqlMapGenerator.getGeneratedXMLFiles(introspectedTable,
callback));
+ }
}
} finally {
closeConnection(connection);
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/FullyQualifiedTable.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/FullyQualifiedTable.java?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/FullyQualifiedTable.java
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/FullyQualifiedTable.java
Mon Jul 24 14:50:36 2006
@@ -21,6 +21,7 @@
import org.apache.ibatis.abator.internal.util.StringUtility;
/**
+ * TODO - move to the API package
* @author Jeff Butler
*/
public class FullyQualifiedTable {
@@ -66,45 +67,13 @@
}
public String getFullyQualifiedTableName() {
- StringBuffer sb = new StringBuffer();
-
- if (StringUtility.stringHasValue(catalog)) {
- sb.append(catalog);
- sb.append('.');
- }
-
- if (StringUtility.stringHasValue(schema)) {
- sb.append(schema);
- sb.append('.');
- } else {
- if (sb.length() > 0) {
- sb.append('.');
- }
- }
-
- sb.append(tableName);
-
- return sb.toString();
+ return StringUtility.composeFullyQualifiedTableName(catalog, schema,
tableName);
}
public String getAliasedFullyQualifiedTableName() {
StringBuffer sb = new StringBuffer();
- if (StringUtility.stringHasValue(catalog)) {
- sb.append(catalog);
- sb.append('.');
- }
-
- if (StringUtility.stringHasValue(schema)) {
- sb.append(schema);
- sb.append('.');
- } else {
- if (sb.length() > 0) {
- sb.append('.');
- }
- }
-
- sb.append(tableName);
+ sb.append(getFullyQualifiedTableName());
if(StringUtility.stringHasValue(alias)) {
sb.append(' ');
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/TableConfiguration.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/TableConfiguration.java?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/TableConfiguration.java
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/TableConfiguration.java
Mon Jul 24 14:50:36 2006
@@ -16,22 +16,19 @@
package org.apache.ibatis.abator.config;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
-import java.util.List;
import java.util.Map;
+import java.util.Set;
-import org.apache.ibatis.abator.internal.db.ColumnDefinitions;
import org.apache.ibatis.abator.internal.util.EqualsUtil;
import org.apache.ibatis.abator.internal.util.HashCodeUtil;
-import org.apache.ibatis.abator.internal.util.messages.Messages;
/**
*
* @author Jeff Butler
*/
public class TableConfiguration extends PropertyHolder {
- private FullyQualifiedTable table;
-
private boolean insertStatementEnabled;
private boolean selectByPrimaryKeyStatementEnabled;
@@ -46,7 +43,7 @@
private Map columnOverrides;
- private Map ignoredColumns;
+ private Set ignoredColumns;
private GeneratedKey generatedKey;
@@ -54,11 +51,17 @@
private String selectByExampleQueryId;
+ private String catalog;
+ private String schema;
+ private String tableName;
+ private String domainObjectName;
+ private String alias;
+
public TableConfiguration() {
super();
columnOverrides = new HashMap();
- ignoredColumns = new HashMap();
+ ignoredColumns = new HashSet();
insertStatementEnabled = true;
selectByPrimaryKeyStatementEnabled = true;
@@ -105,22 +108,12 @@
public boolean isColumnIgnored(String column) {
String key = column.toUpperCase();
- boolean rc = false;
- if (ignoredColumns.containsKey(key)) {
- // column has been accessed, it must exist in the table
- rc = true;
- ignoredColumns.put(key, new Boolean(true));
- }
-
- return rc;
+ return ignoredColumns.contains(key);
}
public void addIgnoredColumn(String column) {
- // put a false in the map to designate that the column has not
- // been accessed yet. We use this to report warnings if the user
- // specifies an ignored column that does not exist in the table.
- ignoredColumns.put(column.toUpperCase(), new Boolean(false));
+ ignoredColumns.add(column.toUpperCase());
}
public void addColumnOverride(ColumnOverride columnOverride) {
@@ -139,12 +132,16 @@
TableConfiguration other = (TableConfiguration) obj;
- return EqualsUtil.areEqual(this.table, other.table);
+ return EqualsUtil.areEqual(this.catalog, other.catalog)
+ && EqualsUtil.areEqual(this.schema, other.schema)
+ && EqualsUtil.areEqual(this.tableName, other.tableName);
}
public int hashCode() {
int result = HashCodeUtil.SEED;
- result = HashCodeUtil.hash(result, table);
+ result = HashCodeUtil.hash(result, catalog);
+ result = HashCodeUtil.hash(result, schema);
+ result = HashCodeUtil.hash(result, tableName);
return result;
}
@@ -158,14 +155,6 @@
this.selectByExampleStatementEnabled =
selectByExampleStatementEnabled;
}
- public FullyQualifiedTable getTable() {
- return table;
- }
-
- public void setTable(FullyQualifiedTable tableName) {
- this.table = tableName;
- }
-
/**
* May return null if the column has not been overridden
*
@@ -205,40 +194,6 @@
this.deleteByExampleStatementEnabled =
deleteByExampleStatementEnabled;
}
- public void reportWarnings(ColumnDefinitions columnDefinitions, List
warnings) {
- Iterator iter = columnOverrides.values().iterator();
- while (iter.hasNext()) {
- ColumnOverride columnOverride = (ColumnOverride) iter.next();
- if
(columnDefinitions.getColumn(columnOverride.getColumnName().toUpperCase()) ==
null) {
- warnings.add(Messages.getString("Warning.3", //$NON-NLS-1$
- columnOverride.getColumnName(), table.toString()));
- }
- }
-
- iter = ignoredColumns.entrySet().iterator();
- while (iter.hasNext()) {
- Map.Entry entry = (Map.Entry) iter.next();
-
- Boolean value = (Boolean) entry.getValue();
-
- if (!value.booleanValue()) {
- warnings.add(Messages.getString("Warning.4", //$NON-NLS-1$
- entry.getKey().toString(), table.toString()));
- }
- }
-
- if (generatedKey != null
- &&
columnDefinitions.getColumn(generatedKey.getColumn().toUpperCase()) == null) {
- if (generatedKey.isIdentity()) {
- warnings.add(Messages.getString("Warning.5", //$NON-NLS-1$
- generatedKey.getColumn(), table.toString()));
- } else {
- warnings.add(Messages.getString("Warning.6", //$NON-NLS-1$
- generatedKey.getColumn(), table.toString()));
- }
- }
- }
-
public boolean areAnyStatementsEnabled() {
return selectByExampleStatementEnabled
|| selectByPrimaryKeyStatementEnabled
@@ -250,5 +205,53 @@
public void setGeneratedKey(GeneratedKey generatedKey) {
this.generatedKey = generatedKey;
+ }
+
+ public String getAlias() {
+ return alias;
+ }
+
+ public void setAlias(String alias) {
+ this.alias = alias;
+ }
+
+ public String getCatalog() {
+ return catalog;
+ }
+
+ public void setCatalog(String catalog) {
+ this.catalog = catalog;
+ }
+
+ public String getDomainObjectName() {
+ return domainObjectName;
+ }
+
+ public void setDomainObjectName(String domainObjectName) {
+ this.domainObjectName = domainObjectName;
+ }
+
+ public String getSchema() {
+ return schema;
+ }
+
+ public void setSchema(String schema) {
+ this.schema = schema;
+ }
+
+ public String getTableName() {
+ return tableName;
+ }
+
+ public void setTableName(String tableName) {
+ this.tableName = tableName;
+ }
+
+ public Iterator getColumnOverrides() {
+ return columnOverrides.values().iterator();
+ }
+
+ public Iterator getIgnoredColumns() {
+ return ignoredColumns.iterator();
}
}
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/xml/AbatorConfigurationParser.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/xml/AbatorConfigurationParser.java?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/xml/AbatorConfigurationParser.java
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/config/xml/AbatorConfigurationParser.java
Mon Jul 24 14:50:36 2006
@@ -31,7 +31,6 @@
import org.apache.ibatis.abator.config.AbatorContext;
import org.apache.ibatis.abator.config.ColumnOverride;
import org.apache.ibatis.abator.config.DAOGeneratorConfiguration;
-import org.apache.ibatis.abator.config.FullyQualifiedTable;
import org.apache.ibatis.abator.config.GeneratedKey;
import org.apache.ibatis.abator.config.JDBCConnectionConfiguration;
import org.apache.ibatis.abator.config.JavaModelGeneratorConfiguration;
@@ -69,7 +68,7 @@
throws IOException, XMLParserException {
FileReader fr = new FileReader(inputFile);
-
+
return parseAbatorConfiguration(fr);
}
@@ -85,7 +84,7 @@
throws IOException, XMLParserException {
InputSource is = new InputSource(inputStream);
-
+
return parseAbatorConfiguration(is);
}
@@ -247,25 +246,19 @@
NamedNodeMap nnm = node.getAttributes();
Node attribute = nnm.getNamedItem("catalog"); //$NON-NLS-1$
- String catalog = attribute == null ? null : attribute.getNodeValue();
+ tc.setCatalog(attribute == null ? null : attribute.getNodeValue());
attribute = nnm.getNamedItem("schema"); //$NON-NLS-1$
- String schema = attribute == null ? null : attribute.getNodeValue();
+ tc.setSchema(attribute == null ? null : attribute.getNodeValue());
attribute = nnm.getNamedItem("domainObjectName"); //$NON-NLS-1$
- String domainObjectName = attribute == null ? null : attribute
- .getNodeValue();
+ tc.setDomainObjectName(attribute == null ? null :
attribute.getNodeValue());
attribute = nnm.getNamedItem("tableName"); //$NON-NLS-1$
- String tableName = attribute.getNodeValue();
+ tc.setTableName(attribute.getNodeValue());
attribute = nnm.getNamedItem("alias"); //$NON-NLS-1$
- String alias = attribute == null ? null : attribute.getNodeValue();
-
- FullyQualifiedTable table = new FullyQualifiedTable(catalog, schema,
- tableName, domainObjectName, alias);
-
- tc.setTable(table);
+ tc.setAlias(attribute == null ? null : attribute.getNodeValue());
attribute = nnm.getNamedItem("enableInsert"); //$NON-NLS-1$
if (attribute != null) {
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/DatabaseIntrospector.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/DatabaseIntrospector.java?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/DatabaseIntrospector.java
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/DatabaseIntrospector.java
Mon Jul 24 14:50:36 2006
@@ -19,16 +19,18 @@
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
-import org.apache.ibatis.abator.api.IntrospectedTable;
import org.apache.ibatis.abator.api.JavaTypeResolver;
import org.apache.ibatis.abator.api.dom.java.FullyQualifiedJavaType;
import org.apache.ibatis.abator.config.ColumnOverride;
+import org.apache.ibatis.abator.config.FullyQualifiedTable;
+import org.apache.ibatis.abator.config.GeneratedKey;
import org.apache.ibatis.abator.config.TableConfiguration;
-import org.apache.ibatis.abator.exception.UnknownTableException;
import org.apache.ibatis.abator.exception.UnsupportedDataTypeException;
import org.apache.ibatis.abator.internal.util.JavaBeansUtil;
import org.apache.ibatis.abator.internal.util.StringUtility;
@@ -40,177 +42,249 @@
*/
public class DatabaseIntrospector {
- private DatabaseIntrospector() {
- super();
- }
-
- public static IntrospectedTable introspectTable(
- Connection connection, TableConfiguration tc,
- JavaTypeResolver javaTypeResolver, List warnings)
- throws SQLException, UnknownTableException {
-
- ColumnDefinitions cds = new ColumnDefinitions();
-
- DatabaseMetaData dbmd = connection.getMetaData();
-
- String localCatalog;
- String localSchema;
- String localTableName;
-
- if (dbmd.storesLowerCaseIdentifiers()) {
- localCatalog = tc.getTable().getCatalog() == null ? null :
tc.getTable().getCatalog().toLowerCase();
- localSchema = tc.getTable().getSchema() == null ? null :
tc.getTable().getSchema().toLowerCase();
- localTableName = tc.getTable().getTableName() == null ?
null : tc.getTable().getTableName().toLowerCase();
- } else if (dbmd.storesUpperCaseIdentifiers()) {
- localCatalog = tc.getTable().getCatalog() == null ? null :
tc.getTable().getCatalog().toUpperCase();
- localSchema = tc.getTable().getSchema() == null ? null :
tc.getTable().getSchema().toUpperCase();
- localTableName = tc.getTable().getTableName() == null ?
null : tc.getTable().getTableName().toUpperCase();
- } else {
- localCatalog = tc.getTable().getCatalog();
- localSchema = tc.getTable().getSchema();
- localTableName = tc.getTable().getTableName();
- }
-
- ResultSet rs = dbmd.getColumns(localCatalog, localSchema,
localTableName, null);
-
- int columnCount = 0;
- boolean hasNonBlobColumns = false;
- while (rs.next()) {
- columnCount++;
- ColumnDefinition cd = new
ColumnDefinition(tc.getTable().getAlias());
-
- cd.setJdbcType(rs.getInt("DATA_TYPE")); //$NON-NLS-1$
- cd.setLength(rs.getInt("COLUMN_SIZE")); //$NON-NLS-1$
- cd.setColumnName(rs.getString("COLUMN_NAME"));
//$NON-NLS-1$
- cd
- .setNullable(rs.getInt("NULLABLE") ==
DatabaseMetaData.columnNullable); //$NON-NLS-1$
- cd.setScale(rs.getInt("DECIMAL_DIGITS")); //$NON-NLS-1$
- cd.setTypeName(rs.getString("TYPE_NAME")); //$NON-NLS-1$
-
- ColumnOverride columnOverride = tc.getColumnOverride(cd
- .getColumnName());
-
- if (columnOverride == null
- ||
!StringUtility.stringHasValue(columnOverride
- .getJavaProperty())) {
- if
("true".equals(tc.getProperties().get("useActualColumnNames"))) { //$NON-NLS-1$
//$NON-NLS-2$
-
cd.setJavaProperty(JavaBeansUtil.getValidPropertyName(cd.getColumnName()));
- } else {
-
cd.setJavaProperty(JavaBeansUtil.getCamelCaseString(cd
- .getColumnName(),
false));
- }
- } else {
-
cd.setJavaProperty(columnOverride.getJavaProperty());
- }
-
- try {
- javaTypeResolver.initializeResolvedJavaType(cd);
- } catch (UnsupportedDataTypeException e) {
- // if the type is not supported, then we'll
report a warning and
- // ignore the column
- warnings.add(Messages.getString("Warning.14",
//$NON-NLS-1$
-
tc.getTable().getFullyQualifiedTableName(),
- cd.getColumnName()));
- continue;
- }
-
- if (columnOverride != null
- &&
StringUtility.stringHasValue(columnOverride
- .getJavaType())) {
-
cd.getResolvedJavaType().setFullyQualifiedJavaType(
- new
FullyQualifiedJavaType(columnOverride.getJavaType()));
- }
-
- if (columnOverride != null
- &&
StringUtility.stringHasValue(columnOverride
- .getJdbcType())) {
- cd.getResolvedJavaType().setJdbcTypeName(
- columnOverride.getJdbcType());
- }
+ private DatabaseIntrospector() {
+ super();
+ }
+
+ public static Collection introspectTables(Connection connection,
+ TableConfiguration tc, JavaTypeResolver javaTypeResolver,
+ List warnings) throws SQLException {
+
+ Map introspectedTables = new HashMap();
+ DatabaseMetaData dbmd = connection.getMetaData();
+
+ String localCatalog;
+ String localSchema;
+ String localTableName;
+
+ if (dbmd.storesLowerCaseIdentifiers()) {
+ localCatalog = tc.getCatalog() == null ? null : tc.getCatalog()
+ .toLowerCase();
+ localSchema = tc.getSchema() == null ? null : tc.getSchema()
+ .toLowerCase();
+ localTableName = tc.getTableName() == null ? null : tc
+ .getTableName().toLowerCase();
+ } else if (dbmd.storesUpperCaseIdentifiers()) {
+ localCatalog = tc.getCatalog() == null ? null : tc.getCatalog()
+ .toUpperCase();
+ localSchema = tc.getSchema() == null ? null : tc.getSchema()
+ .toUpperCase();
+ localTableName = tc.getTableName() == null ? null : tc
+ .getTableName().toUpperCase();
+ } else {
+ localCatalog = tc.getCatalog();
+ localSchema = tc.getSchema();
+ localTableName = tc.getTableName();
+ }
+
+ ResultSet rs = dbmd.getColumns(localCatalog, localSchema,
+ localTableName, null);
+
+ while (rs.next()) {
+ ColumnDefinition cd = new ColumnDefinition(tc.getAlias());
+
+ cd.setJdbcType(rs.getInt("DATA_TYPE")); //$NON-NLS-1$
+ cd.setLength(rs.getInt("COLUMN_SIZE")); //$NON-NLS-1$
+ cd.setColumnName(rs.getString("COLUMN_NAME")); //$NON-NLS-1$
+ cd
+ .setNullable(rs.getInt("NULLABLE") ==
DatabaseMetaData.columnNullable); //$NON-NLS-1$
+ cd.setScale(rs.getInt("DECIMAL_DIGITS")); //$NON-NLS-1$
+ cd.setTypeName(rs.getString("TYPE_NAME")); //$NON-NLS-1$
+
+ String tableName = rs.getString("TABLE_NAME"); //$NON-NLS-1$
+ String catalog = rs.getString("TABLE_CAT"); //$NON-NLS-1$
+ String schema = rs.getString("TABLE_SCHEM"); //$NON-NLS-1$
+
+ String fullyQualifiedTableName =
StringUtility.composeFullyQualifiedTableName(catalog, schema, tableName);
+
+ ColumnOverride columnOverride = tc.getColumnOverride(cd
+ .getColumnName());
+
+ if (columnOverride == null
+ || !StringUtility.stringHasValue(columnOverride
+ .getJavaProperty())) {
+ if
("true".equals(tc.getProperties().get("useActualColumnNames"))) { //$NON-NLS-1$
//$NON-NLS-2$
+ cd.setJavaProperty(JavaBeansUtil.getValidPropertyName(cd
+ .getColumnName()));
+ } else {
+ cd.setJavaProperty(JavaBeansUtil.getCamelCaseString(cd
+ .getColumnName(), false));
+ }
+ } else {
+ cd.setJavaProperty(columnOverride.getJavaProperty());
+ }
+
+ try {
+ javaTypeResolver.initializeResolvedJavaType(cd);
+ } catch (UnsupportedDataTypeException e) {
+ // if the type is not supported, then we'll report a warning
and
+ // ignore the column
+ warnings.add(Messages.getString("Warning.14", //$NON-NLS-1$
+ fullyQualifiedTableName, cd
+ .getColumnName()));
+ continue;
+ }
+
+ if (columnOverride != null
+ && StringUtility.stringHasValue(columnOverride
+ .getJavaType())) {
+ cd.getResolvedJavaType()
+ .setFullyQualifiedJavaType(
+ new FullyQualifiedJavaType(columnOverride
+ .getJavaType()));
+ }
+
+ if (columnOverride != null
+ && StringUtility.stringHasValue(columnOverride
+ .getJdbcType())) {
+ cd.getResolvedJavaType().setJdbcTypeName(
+ columnOverride.getJdbcType());
+ }
if (columnOverride != null
&& StringUtility.stringHasValue(columnOverride
.getTypeHandler())) {
cd.setTypeHandler(columnOverride.getTypeHandler());
}
+
+ if (tc.getGeneratedKey() != null
+ && tc.getGeneratedKey().isIdentity()
+ && cd.getColumnName().equalsIgnoreCase(
+ tc.getGeneratedKey().getColumn())) {
+ cd.setIdentity(true);
+ } else {
+ cd.setIdentity(false);
+ }
+
+ if (!tc.isColumnIgnored(cd.getColumnName())) {
+ IntrospectedTableImpl introspectedTable =
+ (IntrospectedTableImpl)
introspectedTables.get(fullyQualifiedTableName);
+ if (introspectedTable == null) {
+ FullyQualifiedTable table = new
FullyQualifiedTable(catalog,
+ schema, tableName, tc.getDomainObjectName(),
tc.getAlias());
+ introspectedTable = new IntrospectedTableImpl(tc, new
ColumnDefinitions(), table);
+ introspectedTables.put(fullyQualifiedTableName,
introspectedTable);
+ }
+
+ introspectedTable.getColumnDefinitions().addColumn(cd);
+ }
+ }
+
+ rs.close();
+
+ Iterator iter = introspectedTables.values().iterator();
+ while (iter.hasNext()) {
+ IntrospectedTableImpl it = (IntrospectedTableImpl) iter.next();
+ calculatePrimaryKey(dbmd, it, warnings);
+ }
+
+ // now introspectedTables has all the columns from all the
+ // tables in the configuration. Do some validation...
+
+ iter = introspectedTables.entrySet().iterator();
+ while (iter.hasNext()) {
+ Map.Entry entry = (Map.Entry) iter.next();
+
+ IntrospectedTableImpl introspectedTable =
+ (IntrospectedTableImpl) entry.getValue();
- if (tc.getGeneratedKey() != null
- && tc.getGeneratedKey().isIdentity()
- &&
cd.getColumnName().equalsIgnoreCase(tc.getGeneratedKey().getColumn())) {
- cd.setIdentity(true);
- } else {
- cd.setIdentity(false);
- }
-
- if (!tc.isColumnIgnored(cd.getColumnName())) {
- if (!cd.isBLOBColumn()) {
- hasNonBlobColumns = true;
- }
- cds.addColumn(cd);
- }
- }
-
- rs.close();
-
- if (columnCount == 0) {
- throw new UnknownTableException(tc);
- }
-
- if (!hasNonBlobColumns) {
- // we don't support tables that only have BLOB columns
- throw new UnknownTableException(tc);
- }
-
- // now make sure that all columns called out in the
configuration actually exist
- tc.reportWarnings(cds, warnings);
-
- // now calculate the primary key
- List primaryKeyColumns = findPrimaryKeyColumns(dbmd,
localCatalog, localSchema,
- localTableName, warnings);
- Iterator iter = primaryKeyColumns.iterator();
- while (iter.hasNext()) {
- cds.addPrimaryKeyColumn((String) iter.next());
- }
-
- IntrospectedTable answer = new IntrospectedTableImpl(tc, cds);
- return answer;
- }
-
- private static List findPrimaryKeyColumns(DatabaseMetaData dbmd, String
localCatalog,
- String localSchema, String localTableName, List warnings) {
- List answer = new ArrayList();
- ResultSet rs = null;
-
- try {
- rs = dbmd.getPrimaryKeys(localCatalog, localSchema,
localTableName);
- } catch (SQLException e) {
- warnings.add(Messages.getString("Warning.15"));
//$NON-NLS-1$
- }
-
- if (rs != null) {
- try {
- while (rs.next()) {
- answer.add(rs.getString("COLUMN_NAME"));
//$NON-NLS-1$
- }
- } catch (SQLException e) {
- // ignore the primary key if there's any error
- answer.clear();
- } finally {
- closeResultSet(rs);
- }
- }
-
- return answer;
- }
-
- private static void closeResultSet(ResultSet rs) {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- // ignore
- ;
- }
- }
- }
+ ColumnDefinitions cds = introspectedTable.getColumnDefinitions();
+
+ if (cds.getAllColumns().size() == 0) {
+ // add warning that the table has no columns, remove from the
list
+ warnings.add(Messages.getString("Warning.1",
introspectedTable.getTable().getFullyQualifiedTableName())); //$NON-NLS-1$
+ iter.remove();
+ } else if (cds.getPrimaryKey().size() == 0
+ && cds.getNonBLOBColumns().size() == 0) {
+ // add warning that the table has only BLOB columns, remove
from the list
+ warnings.add(Messages.getString("Warning.18",
introspectedTable.getTable().getFullyQualifiedTableName())); //$NON-NLS-1$
+ iter.remove();
+ } else {
+ // now make sure that all columns called out in the
configuration
+ // actually exist
+ reportIntrospectionWarnings(cds, tc,
introspectedTable.getTable(), warnings);
+ }
+ }
+
+ return introspectedTables.values();
+ }
+
+ private static void calculatePrimaryKey(DatabaseMetaData dbmd,
+ IntrospectedTableImpl introspectedTable, List warnings) {
+ ResultSet rs = null;
+
+ try {
+ rs = dbmd.getPrimaryKeys(introspectedTable.getTable().getCatalog(),
+ introspectedTable.getTable().getSchema(),
+ introspectedTable.getTable().getTableName());
+ } catch (SQLException e) {
+ closeResultSet(rs);
+ warnings.add(Messages.getString("Warning.15")); //$NON-NLS-1$
+ return;
+ }
+
+ try {
+ while (rs.next()) {
+ String columnName = rs.getString("COLUMN_NAME"); //$NON-NLS-1$
+
+
introspectedTable.getColumnDefinitions().addPrimaryKeyColumn(columnName);
+ }
+ } catch (SQLException e) {
+ // ignore the primary key if there's any error
+ } finally {
+ closeResultSet(rs);
+ }
+ }
+
+ private static void closeResultSet(ResultSet rs) {
+ if (rs != null) {
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ // ignore
+ ;
+ }
+ }
+ }
+
+ private static void reportIntrospectionWarnings(
+ ColumnDefinitions columnDefinitions,
+ TableConfiguration tableConfiguration,
+ FullyQualifiedTable table, List warnings) {
+ // make sure that every column listed in column overrides
+ // actually exists in the table
+ Iterator iter = tableConfiguration.getColumnOverrides();
+ while (iter.hasNext()) {
+ ColumnOverride columnOverride = (ColumnOverride) iter.next();
+ if (columnDefinitions.getColumn(columnOverride.getColumnName()) ==
null) {
+ warnings.add(Messages.getString("Warning.3", //$NON-NLS-1$
+ columnOverride.getColumnName(), table.toString()));
+ }
+ }
+
+ // make sure that every column listed in ignored columns
+ // actually exists in the table
+ iter = tableConfiguration.getIgnoredColumns();
+ while (iter.hasNext()) {
+ String ignoredColumn = (String) iter.next();
+
+ if (columnDefinitions.getColumn(ignoredColumn) == null) {
+ warnings.add(Messages.getString("Warning.4", //$NON-NLS-1$
+ ignoredColumn, table.toString()));
+ }
+ }
+
+ GeneratedKey generatedKey = tableConfiguration.getGeneratedKey();
+ if (generatedKey != null
+ && columnDefinitions.getColumn(generatedKey.getColumn()
+ .toUpperCase()) == null) {
+ if (generatedKey.isIdentity()) {
+ warnings.add(Messages.getString("Warning.5", //$NON-NLS-1$
+ generatedKey.getColumn(), table.toString()));
+ } else {
+ warnings.add(Messages.getString("Warning.6", //$NON-NLS-1$
+ generatedKey.getColumn(), table.toString()));
+ }
+ }
+ }
}
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/IntrospectedTableImpl.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/IntrospectedTableImpl.java?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/IntrospectedTableImpl.java
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/db/IntrospectedTableImpl.java
Mon Jul 24 14:50:36 2006
@@ -33,28 +33,33 @@
private TableConfiguration tableConfiguration;
private ColumnDefinitions columnDefinitions;
private TableType tableType;
+ private FullyQualifiedTable table;
/**
*
*/
- public IntrospectedTableImpl(TableConfiguration tableConfiguration,
ColumnDefinitions columnDefinitions) {
+ public IntrospectedTableImpl(TableConfiguration tableConfiguration,
ColumnDefinitions columnDefinitions,
+ FullyQualifiedTable table) {
super();
this.columnDefinitions = columnDefinitions;
this.tableConfiguration = tableConfiguration;
- this.tableType = TableType.calculateTableType(columnDefinitions);
+ this.table = table;
}
/* (non-Javadoc)
* @see org.apache.ibatis.abator.api.IntrospectedTable#getTable()
*/
public FullyQualifiedTable getTable() {
- return tableConfiguration.getTable();
+ return table;
}
/* (non-Javadoc)
* @see org.apache.ibatis.abator.api.IntrospectedTable#getTableType()
*/
public TableType getTableType() {
+ if (tableType == null) {
+ tableType = TableType.calculateTableType(columnDefinitions);
+ }
return tableType;
}
@@ -151,5 +156,9 @@
public boolean hasJDBCTimeColumns() {
return columnDefinitions.hasJDBCTimeColumns();
+ }
+
+ public ColumnDefinitions getColumnDefinitions() {
+ return columnDefinitions;
}
}
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/StringUtility.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/StringUtility.java?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/StringUtility.java
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/StringUtility.java
Mon Jul 24 14:50:36 2006
@@ -31,4 +31,27 @@
public static boolean stringHasValue(String s) {
return s != null && s.length() > 0;
}
+
+ public static String composeFullyQualifiedTableName(String catalog, String
schema,
+ String tableName) {
+ StringBuffer sb = new StringBuffer();
+
+ if (stringHasValue(catalog)) {
+ sb.append(catalog);
+ sb.append('.');
+ }
+
+ if (stringHasValue(schema)) {
+ sb.append(schema);
+ sb.append('.');
+ } else {
+ if (sb.length() > 0) {
+ sb.append('.');
+ }
+ }
+
+ sb.append(tableName);
+
+ return sb.toString();
+ }
}
Modified:
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/messages/messages.properties
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/messages/messages.properties?rev=425193&r1=425192&r2=425193&view=diff
==============================================================================
---
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/messages/messages.properties
(original)
+++
ibatis/trunk/java/mapper/mapper2/tools/abator/core/src/org/apache/ibatis/abator/internal/util/messages/messages.properties
Mon Jul 24 14:50:36 2006
@@ -11,7 +11,6 @@
ValidationError.10=JdbcConnectionConfiguration is required
ValidationError.11=At least one AbatorConfiguration element is required
ValidationError.12={0} Target Package is Required for context {1}
-ValidationError.13=TableConfiguration is missing a FullyQualifiedTable in
context {0}
RuntimeError.0=configfile is a required parameter
RuntimeError.1=configfile {0} does not exist
@@ -28,7 +27,7 @@
RuntimeError.12=Internal Error - Cannot calculate record type for
selectByExample method
Warning.0=There are no statements enabled for table {0}, this table will be
ignored.
-Warning.1=Table {0} does not exist, or contains only LOB fields, this table
will be ignored
+Warning.1=Table {0} does not exist, this table will be ignored
Warning.2=Existing file not overwritten, the generated Java file is saved as
{0}
Warning.3=Column {0}, specified for override in table {1}, does not exist in
the table.
Warning.4=Column {0}, specified to be ignored in table {1}, does not exist in
the table.
@@ -43,9 +42,10 @@
Warning.13=Exception while attempting to merge the XML file {0}. \
The existing file will not be changed.
Warning.14=Unsupported Data Type in table {0}, column: {1}, column ignored
-Warning.15=Cannot obtain primary key from database, generated objects may be
incomplete
+Warning.15=Cannot obtain primary key information from the database, generated
objects may be incomplete
Warning.16=Invalid value for exampleMethodVisibility specified ({0}),
defaulting to public
Warning.17=Cannot instantiate DAO method name calculator of type {0}, using
default calculator
+Warning.18=Table {0} contains only LOB fields, this table will be ignored
Progress.0=Connecting to the Database
Progress.1=Introspecting table {0}