Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBX-1797 Several improvements: descriptions of classes are taken from comments of database tables and ... #1268

Open
wants to merge 3 commits into
base: 5.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/java/org/hibernate/cfg/reveng/RevEngUtils.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package org.hibernate.cfg.reveng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.MetaAttribute;
import org.hibernate.mapping.Table;

public class RevEngUtils {

public static List<String> getPrimaryKeyInfoInRevengStrategy(
public static final String CLASS_DB_DESCRIPTION = "class-db-description";
public static final String FIELD_DB_DESCRIPTION = "field-db-description";

public static List<String> getPrimaryKeyInfoInRevengStrategy(
ReverseEngineeringStrategy revengStrat,
Table table,
String defaultCatalog,
Expand Down Expand Up @@ -57,7 +63,9 @@ public static Map<String,MetaAttribute> getColumnToMetaAttributesInRevengStrateg
tableIdentifier = new TableIdentifier(catalog, schema, table.getName());
result = revengStrat.columnToMetaAttributes(tableIdentifier, column);
}
return result;

Column col = table.getColumn(new Column(column));
return putMetaAttributeDbDescription(result, FIELD_DB_DESCRIPTION, col == null ? null : col.getComment());
}

public static Map<String,MetaAttribute> getTableToMetaAttributesInRevengStrategy(
Expand All @@ -74,10 +82,24 @@ public static Map<String,MetaAttribute> getTableToMetaAttributesInRevengStrategy
tableIdentifier = new TableIdentifier(catalog, schema, table.getName());
result = revengStrat.tableToMetaAttributes(tableIdentifier);
}
return result;

return putMetaAttributeDbDescription(result, CLASS_DB_DESCRIPTION, table.getComment());
}

public static String getColumnToPropertyNameInRevengStrategy(

private static Map<String,MetaAttribute> putMetaAttributeDbDescription(Map<String, MetaAttribute> map,
String metaAttributeName, String description) {
if (metaAttributeName != null && description != null) {
if (map == null) {
map = new HashMap<>();
}
List<SimpleMetaAttribute> comment = new ArrayList<>();
comment.add(new SimpleMetaAttribute(description, true));
map.put(metaAttributeName, MetaAttributeBinder.toRealMetaAttribute(metaAttributeName, comment));
}
return map;
}

public static String getColumnToPropertyNameInRevengStrategy(
ReverseEngineeringStrategy revengStrat,
Table table,
String defaultCatalog,
Expand Down
142 changes: 88 additions & 54 deletions src/java/org/hibernate/cfg/reveng/dialect/OracleMetaDataDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public OracleMetaDataDialect() {

/* ******* TABLE QUERIES ******* */
private static final String SQL_TABLE_BASE =
"select a.table_name, a.owner, b.comments, 'TABLE' "
+ "from all_tables a left join all_tab_comments b "
+ "on (a.owner=b.owner and a.table_name=b.table_name) ";
"select a.table_name, a.owner, "
+ "(SELECT b.comments\n"
+ " FROM all_tab_comments b\n"
+ " WHERE a.owner = b.owner\n"
+ " AND a.table_name = b.table_name) AS comments, "
+ "'TABLE' "
+ "from all_tables a ";

private static final String SQL_TABLE_VIEW =
" union all select view_name, owner, NULL, 'VIEW' from all_views ";
Expand All @@ -46,9 +50,11 @@ public OracleMetaDataDialect() {
private static final String SQL_TABLE_TABLE = SQL_TABLE_BASE
+ "where a.table_name like ?" + SQL_TABLE_VIEW + "where view_name like ?";

private static final String SQL_TABLE_SCHEMA_AND_TABLE = SQL_TABLE_BASE
+ "where a.owner like ? and a.table_name like ?" + SQL_TABLE_VIEW
+ "where owner like ? and view_name like ?";
private static final String SQL_TABLE_SCHEMA_AND_TABLE =
SQL_TABLE_BASE
+ "where a.owner like ? and a.table_name like ?"
+ SQL_TABLE_VIEW
+ "where owner like ? and view_name like ?";

private PreparedStatement prepTableNone;

Expand All @@ -61,14 +67,19 @@ public OracleMetaDataDialect() {
/* ***************************** */
/* ******* INDEX QUERIES ******* */
/* ***************************** */
private static final String SQL_INDEX_BASE = "select a.column_name, "
+ "decode(b.uniqueness,'UNIQUE','false','true'), "
+ "a.index_owner, a.index_name, a.table_name "
+ "from all_ind_columns a left join all_indexes b on "
+ "(a.table_name = b.table_name "
+ " AND a.table_owner = b.table_owner "
+ " AND a.index_name = b.index_name) "
+ "where b.index_type not like 'FUNCTION-BASED%' ";
private static final String SQL_INDEX_BASE =
"SELECT a.column_name\n" +
" ,decode((SELECT b.uniqueness\n" +
" FROM all_indexes b\n" +
" WHERE a.table_name = b.table_name\n" +
" AND a.table_owner = b.table_owner\n" +
" AND a.index_name = b.index_name\n" +
" AND b.index_type NOT LIKE 'FUNCTION-BASED%'), 'UNIQUE', 'false', 'true') AS uniqueness\n" +
" ,a.index_owner\n" +
" ,a.index_name\n" +
" ,a.table_name\n" +
" FROM all_ind_columns a\n " +
" WHERE 1 = 1\n ";

private static final String SQL_INDEX_ORDER = " order by a.table_name, a.column_position";

Expand All @@ -93,26 +104,30 @@ public OracleMetaDataDialect() {
private PreparedStatement prepIndexSchemaAndTable;

/* ****** COLUMN QUERIES ******* */
private static final String SQL_COLUMN_BASE = "select a.column_name as COLUMN_NAME, a.owner as TABLE_SCHEM, "
+ "decode(a.nullable,'N',0,1) as NULLABLE, "
+ "decode(a.data_type, "
+ "'FLOAT', decode(a.data_precision, null, a.data_length, a.data_precision), "
+ "'NUMBER', decode(a.data_precision, null, a.data_length, a.data_precision), "
+ "'VARCHAR2', a.char_length, "
+ "'VARCHAR', a.char_length, "
+ "'NVARCHAR2', a.char_length, "
+ "'CHAR', a.char_length, "
+ "'NCHAR', a.char_length, "
+ "a.data_length) as COLUMN_SIZE, "
+ "decode(a.data_type,'CHAR',1, 'DATE',91, 'FLOAT',6, "
+ "'LONG',-1, 'NUMBER',2, 'VARCHAR2',12, 'BFILE',-13, "
+ "'BLOB',2004, 'CLOB',2005, 'MLSLABEL',1111, 'NCHAR',1, 'NCLOB',2005, 'NVARCHAR2',12, "
+ "'RAW',-3, 'ROWID',1111, 'UROWID',1111, 'LONG RAW', -4, "
+ "'TIMESTAMP', 93, 'TIMESTAMP(6)', 93, 'XMLTYPE',2005, 1111) as DATA_TYPE, "
+ "a.table_name as TABLE_NAME, a.data_type as TYPE_NAME, "
+ "decode(a.data_scale, null, 0 ,a.data_scale) as DECIMAL_DIGITS, b.comments "
+ "from all_tab_columns a left join all_col_comments b on "
+ "(a.owner=b.owner and a.table_name=b.table_name and a.column_name=b.column_name) ";
private static final String SQL_COLUMN_BASE =
"SELECT a.column_name AS COLUMN_NAME\n" +
" ,a.owner AS TABLE_SCHEM\n" +
" ,decode(a.nullable, 'N', 0, 1) AS NULLABLE\n" +
" ,decode(a.data_type, 'FLOAT', decode(a.data_precision, NULL, a.data_length, a.data_precision), 'NUMBER',\n" +
" decode(a.data_precision, NULL, a.data_length, a.data_precision), 'VARCHAR2', a.char_length, 'VARCHAR',\n" +
" a.char_length, 'NVARCHAR2', a.char_length, 'CHAR', a.char_length, 'NCHAR', a.char_length, a.data_length) AS COLUMN_SIZE\n" +
" ,CASE\n" +
" WHEN a.data_type LIKE 'TIMESTAMP%' THEN\n" +
" 93\n" +
" ELSE\n" +
" decode(a.data_type, 'CHAR', 1, 'DATE', 91, 'FLOAT', 6, 'LONG', -1, 'NUMBER', 2, 'VARCHAR2', 12, 'BFILE', -13,\n" +
" 'BLOB', 2004, 'CLOB', 2005, 'MLSLABEL', 1111, 'NCHAR', 1, 'NCLOB', 2005, 'NVARCHAR2', 12, 'RAW', -3,\n" +
" 'ROWID', 1111, 'UROWID', 1111, 'LONG RAW', -4, 'XMLTYPE', 2005, 1111)\n" +
" END AS DATA_TYPE\n" +
" ,a.table_name AS TABLE_NAME\n" +
" ,a.data_type AS TYPE_NAME\n" +
" ,decode(a.data_scale, NULL, 0, a.data_scale) AS DECIMAL_DIGITS\n" +
" ,(SELECT b.comments\n" +
" FROM all_col_comments b\n" +
" WHERE a.owner = b.owner\n" +
" AND a.table_name = b.table_name\n" +
" AND a.column_name = b.column_name) AS COMMENTS\n" +
" FROM all_tab_columns a\n";

private static final String SQL_COLUMN_ORDER = " order by column_id ";

Expand Down Expand Up @@ -161,8 +176,9 @@ public OracleMetaDataDialect() {
/* ***************************** */
/* ******** PK QUERIES ********* */
/* ***************************** */
private static final String SQL_PK_BASE = "select c.table_name, c.column_name, c.position, c.constraint_name, "
+ "c.owner from all_cons_columns c left join all_constraints k on "
private static final String SQL_PK_BASE =
"select c.table_name, c.column_name, c.position, c.constraint_name, "
+ "c.owner from all_cons_columns c join all_constraints k on "
+ "(k.owner = c.owner AND k.table_name = c.table_name AND k.constraint_name = c.constraint_name) "
+ "where k.constraint_type = 'P' ";

Expand Down Expand Up @@ -190,16 +206,37 @@ public OracleMetaDataDialect() {
/* ***************************** */
/* ******** FK QUERIES ********* */
/* ***************************** */
private static final String SQL_FK_BASE = "select p.table_name, p.owner, f.owner, f.table_name, "
+ "fc.column_name, pc.column_name, f.constraint_name, fc.position "
+ "from all_constraints p left join all_cons_columns pc on "
+ "(pc.owner = p.owner and pc.constraint_name = p.constraint_name and pc.table_name = p.table_name) "
+ "left join all_constraints f on (p.owner = f.r_owner and p.constraint_name = f.r_constraint_name) "
+ "left join all_cons_columns fc on "
+ "(fc.owner = f.owner and fc.constraint_name = f.constraint_name and fc.table_name = f.table_name and"
+ " fc.position = pc.position) where f.constraint_type = 'R' AND p.constraint_type = 'P' ";

private static final String SQL_FK_ORDER = " order by f.table_name, f.constraint_name, fc.position ";
private static final String SQL_FK_BASE =
"SELECT p.table_name as p_table_name\n" +
" ,p.owner as p_owner\n" +
" ,f.owner as f_owner\n" +
" ,f.table_name as f_table_name\n" +
" ,(SELECT fc.column_name\n" +
" FROM all_cons_columns fc\n" +
" WHERE fc.owner = f.owner\n" +
" AND fc.constraint_name = f.constraint_name\n" +
" AND fc.table_name = f.table_name\n" +
" AND fc.position = pc.position) AS fc_column_name\n" +
" ,pc.column_name as pc_column_name\n" +
" ,f.constraint_name\n" +
" ,(SELECT fc.position\n" +
" FROM all_cons_columns fc\n" +
" WHERE fc.owner = f.owner\n" +
" AND fc.constraint_name = f.constraint_name\n" +
" AND fc.table_name = f.table_name\n" +
" AND fc.position = pc.position) AS fc_position\n" +
" FROM all_constraints p\n" +
" JOIN all_cons_columns pc\n" +
" ON pc.owner = p.owner\n" +
" AND pc.constraint_name = p.constraint_name\n" +
" AND pc.table_name = p.table_name\n" +
" JOIN all_constraints f\n" +
" ON p.owner = f.r_owner\n" +
" AND p.constraint_name = f.r_constraint_name\n" +
" WHERE f.constraint_type = 'R'\n" +
" AND p.constraint_type = 'P'\n";

private static final String SQL_FK_ORDER = " order by f.table_name, f.constraint_name, position ";

private static final String SQL_FK_NONE = SQL_FK_BASE + SQL_FK_ORDER;

Expand All @@ -225,8 +262,7 @@ public Iterator<Map<String,Object>> getTables(final String catalog, final String
try {
log.debug("getTables(" + catalog + "." + schema + "." + table + ")");

ResultSet tableRs;
tableRs = getTableResultSet( schema, table );
ResultSet tableRs = getTableResultSet( schema, table );

return new ResultSetIterator(null, tableRs,
getSQLExceptionConverter()) {
Expand All @@ -241,7 +277,7 @@ protected Map<String, Object> convertRow(ResultSet tableResultSet)
element.put("TABLE_SCHEM", tableResultSet.getString(2));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't there be an A in TABLE_SCHEM, I also noticed it in SQLServerDialect ?

element.put("TABLE_CAT", null);
element.put("TABLE_TYPE", tableResultSet.getString(4));
element.put("REMARKS", tableResultSet.getString(3));
element.put("REMARKS", tableResultSet.getString(3));
log.info( element.toString() );
return element;
}
Expand Down Expand Up @@ -309,7 +345,7 @@ protected Throwable handleSQLException(SQLException e) {
throw getSQLExceptionConverter().convert(
e,
"Exception while getting index info for "
+ TableNameQualifier.qualify(catalog, schema, table), null);
+ TableNameQualifier.qualify(catalog, schema, table) + ": " + e.getMessage(), null);
}
}

Expand Down Expand Up @@ -409,8 +445,7 @@ public Iterator<Map<String, Object>> getExportedKeys(final String catalog, final
log.debug("getExportedKeys(" + catalog + "." + schema + "." + table
+ ")");

ResultSet pExportRs;
pExportRs = getExportedKeysResultSet( schema, table );
ResultSet pExportRs = getExportedKeysResultSet( schema, table );

return new ResultSetIterator(null, pExportRs,
getSQLExceptionConverter()) {
Expand Down Expand Up @@ -573,8 +608,7 @@ private ResultSet getExportedKeysResultSet(final String schema, final String tab
prepFkNone = con .prepareStatement(SQL_FK_NONE);
prepFkSchema = con.prepareStatement(SQL_FK_SCHEMA);
prepFkTable = con.prepareStatement(SQL_FK_TABLE);
prepFkSchemaAndTable = con
.prepareStatement(SQL_FK_SCHEMA_AND_TABLE);
prepFkSchemaAndTable = con.prepareStatement(SQL_FK_SCHEMA_AND_TABLE);
log.debug(" foreign key queries prepared!");
}

Expand Down
18 changes: 16 additions & 2 deletions src/java/org/hibernate/tool/ant/Hbm2JavaExporterTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class Hbm2JavaExporterTask extends ExporterTask {

boolean jdk5 = false;

boolean annotateFields = false;

boolean javaDocFromDbComments = false;

public Hbm2JavaExporterTask(HibernateToolTask parent) {
super( parent );
}
Expand All @@ -29,11 +33,21 @@ public void setJdk5(boolean b) {
jdk5 = b;
}

protected Exporter configureExporter(Exporter exp) {
public void setAnnotateFields(boolean annotateFields) {
this.annotateFields = annotateFields;
}

public void setJavaDocFromDbComments(boolean javaDocFromDbComments) {
this.javaDocFromDbComments = javaDocFromDbComments;
}

protected Exporter configureExporter(Exporter exp) {
POJOExporter exporter = (POJOExporter) exp;
super.configureExporter( exp );
exporter.getProperties().setProperty("ejb3", ""+ejb3);
exporter.getProperties().setProperty("ejb3", "" + ejb3);
exporter.getProperties().setProperty("jdk5", ""+jdk5);
exporter.getProperties().setProperty("annotateFields", "" + annotateFields);
exporter.getProperties().setProperty("javaDocFromDbComments", "" + javaDocFromDbComments);
return exporter;
}

Expand Down
3 changes: 3 additions & 0 deletions src/java/org/hibernate/tool/hbm2x/MetaAttributeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public interface MetaAttributeConstants {
static final String CLASS_MODIFIER = "class-modifier";
static final String SCOPE_CLASS = "scope-class";
static final String CLASS_DESCRIPTION = "class-description";
static final String CLASS_DB_DESCRIPTION = "class-db-description";
static final String FIELD_DESCRIPTION = "field-description";
static final String FIELD_DB_DESCRIPTION = "field-db-description";
static final String IMPLEMENTS = "implements";
static final String EXTENDS = "extends";
static final String GEN_PROPERTY = "gen-property"; // in templates.
Expand Down
6 changes: 6 additions & 0 deletions src/java/org/hibernate/tool/hbm2x/POJOExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ protected void setupContext() {
if(!getProperties().containsKey("jdk5")) {
getProperties().put("jdk5", "false");
}
if(!getProperties().containsKey("annotateFields")) {
getProperties().put("annotateFields", "false");
}
if(!getProperties().containsKey("javaDocFromDbComments")) {
getProperties().put("javaDocFromDbComments", "false");
}
super.setupContext();
}
}
Loading