|
24 | 24 | import java.sql.DatabaseMetaData; |
25 | 25 | import java.sql.PreparedStatement; |
26 | 26 | import java.sql.ResultSet; |
| 27 | +import java.util.ArrayList; |
27 | 28 | import java.util.List; |
28 | 29 |
|
29 | 30 | import org.fugerit.java.core.db.connect.ConnectionFactory; |
@@ -59,14 +60,20 @@ public static DataBaseModel createModel( ConnectionFactory cf ) throws Exception |
59 | 60 | } |
60 | 61 |
|
61 | 62 | 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 ); |
63 | 70 | } |
64 | 71 |
|
65 | 72 | private static final int MODE_LOOSE = 1; |
66 | 73 |
|
67 | 74 | private static final int MODE_STRICT = 2; |
68 | 75 |
|
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 { |
70 | 77 |
|
71 | 78 | DataBaseModel dataBaseModel = new DataBaseModel(); |
72 | 79 |
|
@@ -108,99 +115,117 @@ private static DataBaseModel createModel( ConnectionFactory cf, String catalog, |
108 | 115 | tableId.setTableCatalog( tableRS.getString( "TABLE_CAT" ) ); |
109 | 116 | tableId.setTableName( tableRS.getString( "TABLE_NAME" ) ); |
110 | 117 | 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 ); |
119 | 118 |
|
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; |
139 | 129 | } |
140 | | - columnModel.setComment( columnComment ); |
141 | | - columnModel.setExtra( jdbcAdaptor.getColumnExtraInfo( tableId, columnModel.getName() ) ); |
142 | | - tableModel.addColumn( columnModel ); |
143 | 130 | } |
144 | | - columnsRS.close(); |
145 | 131 |
|
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 ); |
159 | 133 |
|
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 ); |
169 | 140 | } |
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 ); |
173 | 166 | } |
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 ); |
192 | 177 | } |
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" ); |
196 | 181 | } |
197 | | - foreignRS.close(); |
198 | | - } else { |
199 | | - LogFacade.getLog().debug( "DataBaseModel createModel() : SKIPPING FOREIGN KEYS" ); |
200 | | - } |
201 | 182 |
|
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 | + } |
204 | 229 | } |
205 | 230 | tableRS.close(); |
206 | 231 |
|
|
0 commit comments