Skip to content

Commit 0fa6024

Browse files
junit optimization
1 parent fe60e45 commit 0fa6024

File tree

12 files changed

+523
-90
lines changed

12 files changed

+523
-90
lines changed

fj-core/src/main/java/org/fugerit/java/core/cfg/xml/BasicIdConfigType.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,12 @@ public String getId() {
2525
public void setId(String id) {
2626
this.id = id;
2727
}
28+
29+
@Override
30+
public String toString() {
31+
return this.getClass().getSimpleName()+"[id:"+this.getId()+"]";
32+
}
33+
34+
2835

2936
}

fj-core/src/main/java/org/fugerit/java/core/cfg/xml/ListMapConfig.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import java.util.Properties;
55

66
import org.fugerit.java.core.util.collection.KeyMapper;
7+
import org.fugerit.java.core.util.collection.KeyObject;
78
import org.fugerit.java.core.util.collection.ListMapStringKey;
89
import org.fugerit.java.core.xml.dom.DOMUtils;
910
import org.w3c.dom.Element;
1011

11-
public class ListMapConfig<T> extends ListMapStringKey<T> {
12+
public class ListMapConfig<T> extends ListMapStringKey<T> implements IdConfigType, KeyObject<String> {
1213

1314
/**
1415
*
@@ -44,5 +45,22 @@ public Properties getConfig() {
4445
public void initFromElementAttributes( Element tag ) {
4546
DOMUtils.attributesToProperties( tag , this.getConfig() );
4647
}
48+
49+
private String id;
50+
51+
@Override
52+
public String getKey() {
53+
return this.getId();
54+
}
55+
56+
@Override
57+
public void setId(String id) {
58+
this.id = id;
59+
}
60+
61+
@Override
62+
public String getId() {
63+
return this.id;
64+
}
4765

4866
}

fj-core/src/main/java/org/fugerit/java/core/db/metadata/MetaDataUtils.java

Lines changed: 110 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.sql.DatabaseMetaData;
2525
import java.sql.PreparedStatement;
2626
import java.sql.ResultSet;
27+
import java.util.ArrayList;
2728
import java.util.List;
2829

2930
import org.fugerit.java.core.db.connect.ConnectionFactory;
@@ -59,14 +60,20 @@ public static DataBaseModel createModel( ConnectionFactory cf ) throws Exception
5960
}
6061

6162
public static DataBaseModel createModel( ConnectionFactory cf, String catalog, String schema ) throws Exception {
62-
return createModel( cf , catalog, schema, (JdbcAdaptor) new DefaulJdbcdaptor( cf ) );
63+
List<String> tableNameList = new ArrayList<String>();
64+
tableNameList.add( "*" );
65+
return createModel(cf, catalog, schema, tableNameList);
66+
}
67+
68+
public static DataBaseModel createModel( ConnectionFactory cf, String catalog, String schema, List<String> tableNameList ) throws Exception {
69+
return createModel( cf , catalog, schema, (JdbcAdaptor) new DefaulJdbcdaptor( cf ), tableNameList );
6370
}
6471

6572
private static final int MODE_LOOSE = 1;
6673

6774
private static final int MODE_STRICT = 2;
6875

69-
private static DataBaseModel createModel( ConnectionFactory cf, String catalog, String schema, JdbcAdaptor jdbcAdaptor ) throws Exception {
76+
private static DataBaseModel createModel( ConnectionFactory cf, String catalog, String schema, JdbcAdaptor jdbcAdaptor, List<String> tableNameList ) throws Exception {
7077

7178
DataBaseModel dataBaseModel = new DataBaseModel();
7279

@@ -108,99 +115,117 @@ private static DataBaseModel createModel( ConnectionFactory cf, String catalog,
108115
tableId.setTableCatalog( tableRS.getString( "TABLE_CAT" ) );
109116
tableId.setTableName( tableRS.getString( "TABLE_NAME" ) );
110117
tableId.setTableSchema( tableRS.getString( "TABLE_SCHEM" ) );
111-
LogFacade.getLog().debug( "DataBaseModel.DataBaseModel() tableId : "+tableId );
112-
tableModel.setTableId( tableId );
113-
LogFacade.getLog().debug( "TABLE EX : "+tableId);
114-
String tableComment = tableRS.getString( "REMARKS" );
115-
if ( tableComment == null || tableComment.length() == 0 ) {
116-
tableComment = jdbcAdaptor.getTableComment( tableId );
117-
}
118-
tableModel.setComment( tableComment );
119118

120-
ResultSet columnsRS = dbmd.getColumns( tableModel.getCatalog(), tableModel.getSchema(), tableModel.getName(), null );
121-
while ( columnsRS.next() ) {
122-
ColumnModel columnModel = new ColumnModel();
123-
columnModel.setName( columnsRS.getString( "COLUMN_NAME" ) );
124-
columnModel.setTypeSql( columnsRS.getInt( "DATA_TYPE" ) );
125-
columnModel.setTypeName( columnsRS.getString( "TYPE_NAME" ) );
126-
//System.out.println( "columnModel : "+columnModel );
127-
String isNullable = columnsRS.getString( "IS_NULLABLE" );
128-
if ( "NO".equalsIgnoreCase( isNullable ) ) {
129-
columnModel.setNullable( ColumnModel.NULLABLE_FALSE );
130-
} else if ( "YES".equalsIgnoreCase( isNullable ) ) {
131-
columnModel.setNullable( ColumnModel.NULLABLE_TRUE );
132-
} else {
133-
columnModel.setNullable( ColumnModel.NULLABLE_UNKNOWN );
134-
}
135-
columnModel.setSize( columnsRS.getInt( "CHAR_OCTET_LENGTH" ) );
136-
String columnComment = columnsRS.getString( "REMARKS" );
137-
if ( columnComment == null || columnComment.length() == 0 ) {
138-
columnComment = jdbcAdaptor.getColumnComment( tableId, columnModel.getName() );
119+
boolean doTable = false;
120+
for ( String checkTableName : tableNameList ) {
121+
if ( checkTableName.endsWith( "*" ) ) {
122+
String check1 = checkTableName.substring( 0 , checkTableName.length()-1 ).toLowerCase();
123+
String check2 = tableId.getTableName().toLowerCase();
124+
if ( check2.toLowerCase().startsWith( check1 ) ) {
125+
doTable = true;
126+
}
127+
} else if ( checkTableName.equalsIgnoreCase( tableId.getTableName() ) ) {
128+
doTable = true;
139129
}
140-
columnModel.setComment( columnComment );
141-
columnModel.setExtra( jdbcAdaptor.getColumnExtraInfo( tableId, columnModel.getName() ) );
142-
tableModel.addColumn( columnModel );
143130
}
144-
columnsRS.close();
145131

146-
if ( mode == MODE_STRICT ) {
147-
ResultSet pkRS = dbmd.getPrimaryKeys( tableModel.getCatalog(), tableModel.getSchema(), tableModel.getName() );
148-
IndexModel primaryKey = new IndexModel();
149-
while ( pkRS.next() ) {
150-
primaryKey.setName( pkRS.getString( "PK_NAME" ) );
151-
primaryKey.addColumn( (ColumnModel)tableModel.getColumnMap().get( pkRS.getString( "COLUMN_NAME" ) ) );
152-
}
153-
tableModel.setPrimaryKey( primaryKey );
154-
pkRS.close();
155-
} else {
156-
LogFacade.getLog().debug( "DataBaseModel createModel() : SKIPPING PRIMARY KEY" );
157-
}
158-
132+
LogFacade.getLog().info( "DataBaseModel.DataBaseModel() tableId : "+tableId+" doTable : "+doTable );
159133

160-
// estrazione indici
161-
ResultSet indexRS = dbmd.getIndexInfo( tableModel.getCatalog(), tableModel.getSchema(), tableModel.getName(), true, true );
162-
while ( indexRS.next() ) {
163-
String indexName = indexRS.getString( "INDEX_NAME" );
164-
IndexModel indexModel = (IndexModel)tableModel.getIndexMap().get( indexName );
165-
if ( indexModel==null ) {
166-
indexModel = new IndexModel();
167-
indexModel.setName( indexName );
168-
tableModel.addIndex( indexModel );
134+
if ( doTable ) {
135+
tableModel.setTableId( tableId );
136+
LogFacade.getLog().debug( "TABLE EX : "+tableId);
137+
String tableComment = tableRS.getString( "REMARKS" );
138+
if ( tableComment == null || tableComment.length() == 0 ) {
139+
tableComment = jdbcAdaptor.getTableComment( tableId );
169140
}
170-
String columnName = indexRS.getString( "COLUMN_NAME" );
171-
if ( columnName != null ) {
172-
indexModel.addColumn( (ColumnModel)tableModel.getColumnMap().get( columnName ) );
141+
tableModel.setComment( tableComment );
142+
143+
ResultSet columnsRS = dbmd.getColumns( tableModel.getCatalog(), tableModel.getSchema(), tableModel.getName(), null );
144+
while ( columnsRS.next() ) {
145+
ColumnModel columnModel = new ColumnModel();
146+
columnModel.setName( columnsRS.getString( "COLUMN_NAME" ) );
147+
columnModel.setTypeSql( columnsRS.getInt( "DATA_TYPE" ) );
148+
columnModel.setTypeName( columnsRS.getString( "TYPE_NAME" ) );
149+
//System.out.println( "columnModel : "+columnModel );
150+
String isNullable = columnsRS.getString( "IS_NULLABLE" );
151+
if ( "NO".equalsIgnoreCase( isNullable ) ) {
152+
columnModel.setNullable( ColumnModel.NULLABLE_FALSE );
153+
} else if ( "YES".equalsIgnoreCase( isNullable ) ) {
154+
columnModel.setNullable( ColumnModel.NULLABLE_TRUE );
155+
} else {
156+
columnModel.setNullable( ColumnModel.NULLABLE_UNKNOWN );
157+
}
158+
columnModel.setSize( columnsRS.getInt( "CHAR_OCTET_LENGTH" ) );
159+
String columnComment = columnsRS.getString( "REMARKS" );
160+
if ( columnComment == null || columnComment.length() == 0 ) {
161+
columnComment = jdbcAdaptor.getColumnComment( tableId, columnModel.getName() );
162+
}
163+
columnModel.setComment( columnComment );
164+
columnModel.setExtra( jdbcAdaptor.getColumnExtraInfo( tableId, columnModel.getName() ) );
165+
tableModel.addColumn( columnModel );
173166
}
174-
}
175-
indexRS.close();
176-
177-
if ( mode == MODE_STRICT ) {
178-
// estrazione chiavi esterne
179-
ResultSet foreignRS = dbmd.getImportedKeys( tableModel.getCatalog(), tableModel.getSchema(), tableModel.getName() );
180-
while ( foreignRS.next() ) {
181-
String foreignName = foreignRS.getString( "FK_NAME" );
182-
ForeignKeyModel foreignKeyModel = (ForeignKeyModel)tableModel.getForeignKeyMap().get( foreignName );
183-
if ( foreignKeyModel == null ) {
184-
foreignKeyModel = new ForeignKeyModel();
185-
foreignKeyModel.setName( foreignName );
186-
TableId foreignTableId = new TableId();
187-
foreignTableId.setTableName( foreignRS.getString( "PKTABLE_NAME" ) );
188-
foreignTableId.setTableCatalog( foreignRS.getString( "PKTABLE_CAT" ) );
189-
foreignTableId.setTableSchema( foreignRS.getString( "PKTABLE_SCHEM" ) );
190-
foreignKeyModel.setForeignTableId( foreignTableId );
191-
tableModel.addForeignKey( foreignKeyModel );
167+
columnsRS.close();
168+
169+
if ( mode == MODE_STRICT ) {
170+
ResultSet pkRS = dbmd.getPrimaryKeys( tableModel.getCatalog(), tableModel.getSchema(), tableModel.getName() );
171+
if ( pkRS.next() ) {
172+
IndexModel primaryKey = new IndexModel();
173+
primaryKey.setName( pkRS.getString( "PK_NAME" ) );
174+
ColumnModel column = tableModel.getColumn( pkRS.getString( "COLUMN_NAME" ) );
175+
primaryKey.addColumn( column );
176+
tableModel.setPrimaryKey( primaryKey );
192177
}
193-
String columnNamePk = foreignRS.getString( "PKCOLUMN_NAME" );
194-
String columnNameFk = foreignRS.getString( "FKCOLUMN_NAME" );
195-
foreignKeyModel.getColumnMap().setProperty( columnNamePk , columnNameFk );
178+
pkRS.close();
179+
} else {
180+
LogFacade.getLog().debug( "DataBaseModel createModel() : SKIPPING PRIMARY KEY" );
196181
}
197-
foreignRS.close();
198-
} else {
199-
LogFacade.getLog().debug( "DataBaseModel createModel() : SKIPPING FOREIGN KEYS" );
200-
}
201182

202-
dataBaseModel.addTable( tableModel );
203-
183+
184+
// estrazione indici
185+
ResultSet indexRS = dbmd.getIndexInfo( tableModel.getCatalog(), tableModel.getSchema(), tableModel.getName(), true, true );
186+
while ( indexRS.next() ) {
187+
String indexName = indexRS.getString( "INDEX_NAME" );
188+
IndexModel indexModel = (IndexModel)tableModel.getIndexMap().get( indexName );
189+
if ( indexModel==null ) {
190+
indexModel = new IndexModel();
191+
indexModel.setName( indexName );
192+
tableModel.addIndex( indexModel );
193+
}
194+
String columnName = indexRS.getString( "COLUMN_NAME" );
195+
if ( columnName != null ) {
196+
indexModel.addColumn( (ColumnModel)tableModel.getColumnMap().get( columnName ) );
197+
}
198+
}
199+
indexRS.close();
200+
201+
if ( mode == MODE_STRICT ) {
202+
// estrazione chiavi esterne
203+
ResultSet foreignRS = dbmd.getImportedKeys( tableModel.getCatalog(), tableModel.getSchema(), tableModel.getName() );
204+
while ( foreignRS.next() ) {
205+
String foreignName = foreignRS.getString( "FK_NAME" );
206+
ForeignKeyModel foreignKeyModel = (ForeignKeyModel)tableModel.getForeignKeyMap().get( foreignName );
207+
if ( foreignKeyModel == null ) {
208+
foreignKeyModel = new ForeignKeyModel();
209+
foreignKeyModel.setName( foreignName );
210+
TableId foreignTableId = new TableId();
211+
foreignTableId.setTableName( foreignRS.getString( "PKTABLE_NAME" ) );
212+
foreignTableId.setTableCatalog( foreignRS.getString( "PKTABLE_CAT" ) );
213+
foreignTableId.setTableSchema( foreignRS.getString( "PKTABLE_SCHEM" ) );
214+
foreignKeyModel.setForeignTableId( foreignTableId );
215+
tableModel.addForeignKey( foreignKeyModel );
216+
}
217+
String columnNamePk = foreignRS.getString( "PKCOLUMN_NAME" );
218+
String columnNameFk = foreignRS.getString( "FKCOLUMN_NAME" );
219+
foreignKeyModel.getColumnMap().setProperty( columnNamePk , columnNameFk );
220+
}
221+
foreignRS.close();
222+
} else {
223+
LogFacade.getLog().debug( "DataBaseModel createModel() : SKIPPING FOREIGN KEYS" );
224+
}
225+
226+
dataBaseModel.addTable( tableModel );
227+
228+
}
204229
}
205230
tableRS.close();
206231

fj-core/src/main/java/org/fugerit/java/core/db/metadata/TableId.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
*/
2121
package org.fugerit.java.core.db.metadata;
2222

23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.fugerit.java.core.lang.helpers.CollectionUtils;
2327
import org.fugerit.java.core.lang.helpers.ConcatHelper;
28+
import org.fugerit.java.core.lang.helpers.StringUtils;
2429
import org.fugerit.java.core.util.collection.KeyObject;
2530

2631
/**
@@ -110,4 +115,12 @@ public int hashCode() {
110115
return h;
111116
}
112117

118+
public String toIdString() {
119+
List<String> list = new ArrayList<String>();
120+
CollectionUtils.addIfNotNull( list , this.getTableCatalog() );
121+
CollectionUtils.addIfNotNull( list , this.getTableSchema() );
122+
CollectionUtils.addIfNotNull( list , this.getTableName() );
123+
return StringUtils.concat( ".", list );
124+
}
125+
113126
}

0 commit comments

Comments
 (0)