|
6 | 6 | */
|
7 | 7 | package org.hibernate.tool.schema.extract.internal;
|
8 | 8 |
|
| 9 | +import java.sql.SQLException; |
9 | 10 | import java.util.HashMap;
|
10 | 11 | import java.util.Map;
|
11 | 12 |
|
12 | 13 | import org.hibernate.boot.model.naming.Identifier;
|
13 | 14 | import org.hibernate.boot.model.relational.Namespace;
|
14 | 15 | import org.hibernate.boot.model.relational.QualifiedSequenceName;
|
15 | 16 | import org.hibernate.boot.model.relational.QualifiedTableName;
|
| 17 | +import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; |
| 18 | +import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; |
| 19 | +import org.hibernate.service.ServiceRegistry; |
16 | 20 | import org.hibernate.tool.schema.extract.spi.DatabaseInformation;
|
17 | 21 | import org.hibernate.tool.schema.extract.spi.ExtractionContext;
|
| 22 | +import org.hibernate.tool.schema.extract.spi.InformationExtractor; |
18 | 23 | import org.hibernate.tool.schema.extract.spi.SequenceInformation;
|
19 | 24 | import org.hibernate.tool.schema.extract.spi.TableInformation;
|
20 | 25 |
|
21 | 26 | /**
|
| 27 | + * Access to information from the existing database. |
| 28 | + * |
22 | 29 | * @author Steve Ebersole
|
23 | 30 | */
|
24 | 31 | public class DatabaseInformationImpl implements DatabaseInformation, ExtractionContext.DatabaseObjectAccess {
|
25 |
| - private final Map<QualifiedTableName,TableInformation> tableInformationMap = new HashMap<QualifiedTableName, TableInformation>(); |
| 32 | + private final InformationExtractor extractor; |
| 33 | + private final ExtractionContext extractionContext; |
| 34 | + |
| 35 | + private final JdbcEnvironment jdbcEnvironment; |
| 36 | + |
26 | 37 | private final Map<QualifiedSequenceName,SequenceInformation> sequenceInformationMap = new HashMap<QualifiedSequenceName, SequenceInformation>();
|
27 | 38 |
|
28 |
| - public DatabaseInformationImpl() { |
| 39 | + public DatabaseInformationImpl( |
| 40 | + ServiceRegistry serviceRegistry, |
| 41 | + JdbcEnvironment jdbcEnvironment, |
| 42 | + JdbcConnectionAccess jdbcConnectionAccess, |
| 43 | + Identifier defaultCatalogName, |
| 44 | + Identifier defaultSchemaName) throws SQLException { |
| 45 | + this.jdbcEnvironment = jdbcEnvironment; |
| 46 | + |
| 47 | + this.extractionContext = new ExtractionContextImpl( |
| 48 | + serviceRegistry, |
| 49 | + jdbcEnvironment, |
| 50 | + jdbcConnectionAccess, |
| 51 | + this, |
| 52 | + defaultCatalogName, |
| 53 | + defaultSchemaName |
| 54 | + ); |
| 55 | + |
| 56 | + // todo : make this pluggable |
| 57 | + this.extractor = new InformationExtractorJdbcDatabaseMetaDataImpl( extractionContext ); |
| 58 | + |
| 59 | + // legacy code did initialize sequences... |
| 60 | + initializeSequences(); |
29 | 61 | }
|
30 | 62 |
|
31 |
| - |
32 |
| - // DatabaseInformation implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 63 | + private void initializeSequences() throws SQLException { |
| 64 | + Iterable<SequenceInformation> itr = jdbcEnvironment.getDialect().getSequenceInformationExtractor().extractMetadata( extractionContext ); |
| 65 | + for ( SequenceInformation sequenceInformation : itr ) { |
| 66 | + sequenceInformationMap.put( |
| 67 | + // for now, follow the legacy behavior of storing just the |
| 68 | + // unqualified sequence name. |
| 69 | + new QualifiedSequenceName( |
| 70 | + null, |
| 71 | + null, |
| 72 | + sequenceInformation.getSequenceName().getSequenceName() |
| 73 | + ), |
| 74 | + sequenceInformation |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
33 | 78 |
|
34 | 79 | @Override
|
35 | 80 | public boolean schemaExists(Namespace.Name schema) {
|
36 |
| - return false; |
| 81 | + return extractor.schemaExists( schema.getCatalog(), schema.getSchema() ); |
37 | 82 | }
|
38 | 83 |
|
39 | 84 | @Override
|
40 |
| - public TableInformation getTableInformation(Identifier catalogName, Identifier schemaName, Identifier tableName) { |
41 |
| - return locateTableInformation( new QualifiedTableName( catalogName, schemaName, tableName ) ); |
| 85 | + public TableInformation getTableInformation( |
| 86 | + Identifier catalogName, |
| 87 | + Identifier schemaName, |
| 88 | + Identifier tableName) { |
| 89 | + return getTableInformation( new QualifiedTableName( catalogName, schemaName, tableName ) ); |
42 | 90 | }
|
43 | 91 |
|
44 | 92 | @Override
|
45 |
| - public TableInformation getTableInformation(Namespace.Name schemaName, Identifier tableName) { |
46 |
| - return locateTableInformation( new QualifiedTableName( schemaName, tableName ) ); |
| 93 | + public TableInformation getTableInformation( |
| 94 | + Namespace.Name schemaName, |
| 95 | + Identifier tableName) { |
| 96 | + return getTableInformation( new QualifiedTableName( schemaName, tableName ) ); |
47 | 97 | }
|
48 | 98 |
|
49 | 99 | @Override
|
50 |
| - public TableInformation getTableInformation(QualifiedTableName tableName) { |
51 |
| - return locateTableInformation( tableName ); |
| 100 | + public TableInformation getTableInformation(QualifiedTableName qualifiedTableName) { |
| 101 | + if ( qualifiedTableName.getObjectName() == null ) { |
| 102 | + throw new IllegalArgumentException( "Passed table name cannot be null" ); |
| 103 | + } |
| 104 | + |
| 105 | + return extractor.getTable( |
| 106 | + qualifiedTableName.getCatalogName(), |
| 107 | + qualifiedTableName.getSchemaName(), |
| 108 | + qualifiedTableName.getTableName() |
| 109 | + ); |
52 | 110 | }
|
53 | 111 |
|
54 | 112 | @Override
|
55 |
| - public SequenceInformation getSequenceInformation(Identifier catalogName, Identifier schemaName, Identifier sequenceName) { |
56 |
| - return locateSequenceInformation( new QualifiedSequenceName( catalogName, schemaName, sequenceName ) ); |
| 113 | + public void registerTable(TableInformation tableInformation) { |
57 | 114 | }
|
58 | 115 |
|
59 | 116 | @Override
|
60 |
| - public SequenceInformation getSequenceInformation(Namespace.Name schemaName, Identifier sequenceName) { |
61 |
| - return locateSequenceInformation( new QualifiedSequenceName( schemaName, sequenceName ) ); |
| 117 | + public SequenceInformation getSequenceInformation( |
| 118 | + Identifier catalogName, |
| 119 | + Identifier schemaName, |
| 120 | + Identifier sequenceName) { |
| 121 | + return getSequenceInformation( new QualifiedSequenceName( catalogName, schemaName, sequenceName ) ); |
62 | 122 | }
|
63 | 123 |
|
64 | 124 | @Override
|
65 |
| - public SequenceInformation getSequenceInformation(QualifiedSequenceName sequenceName) { |
66 |
| - return locateSequenceInformation( sequenceName ); |
| 125 | + public SequenceInformation getSequenceInformation( |
| 126 | + Namespace.Name schemaName, |
| 127 | + Identifier sequenceName) { |
| 128 | + return getSequenceInformation( new QualifiedSequenceName( schemaName, sequenceName ) ); |
67 | 129 | }
|
68 | 130 |
|
69 | 131 | @Override
|
70 |
| - public boolean catalogExists(Identifier catalog) { |
71 |
| - return false; |
72 |
| - } |
73 |
| - |
74 |
| - |
75 |
| - // RegisteredObjectAccess implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
76 |
| - |
77 |
| - public void registerTableInformation(TableInformation tableInformation) { |
78 |
| - tableInformationMap.put( tableInformation.getName(), tableInformation ); |
| 132 | + public SequenceInformation getSequenceInformation(QualifiedSequenceName qualifiedSequenceName) { |
| 133 | + return locateSequenceInformation( qualifiedSequenceName ); |
79 | 134 | }
|
80 | 135 |
|
81 |
| - public void registerSequenceInformation(SequenceInformation sequenceInformation) { |
82 |
| - sequenceInformationMap.put( sequenceInformation.getSequenceName(), sequenceInformation ); |
| 136 | + @Override |
| 137 | + public boolean catalogExists(Identifier catalog) { |
| 138 | + return extractor.catalogExists( catalog ); |
83 | 139 | }
|
84 | 140 |
|
85 | 141 | @Override
|
86 | 142 | public TableInformation locateTableInformation(QualifiedTableName tableName) {
|
87 |
| - return tableInformationMap.get( tableName ); |
| 143 | + return getTableInformation( tableName ); |
88 | 144 | }
|
89 | 145 |
|
90 | 146 | @Override
|
91 | 147 | public SequenceInformation locateSequenceInformation(QualifiedSequenceName sequenceName) {
|
92 |
| - return sequenceInformationMap.get( sequenceName ); |
93 |
| - } |
| 148 | + // again, follow legacy behavior |
| 149 | + if ( sequenceName.getCatalogName() != null || sequenceName.getSchemaName() != null ) { |
| 150 | + sequenceName = new QualifiedSequenceName( null, null, sequenceName.getSequenceName() ); |
| 151 | + } |
94 | 152 |
|
95 |
| - @Override |
96 |
| - public void registerTable(TableInformation tableInformation) { |
97 |
| - tableInformationMap.put( tableInformation.getName(), tableInformation ); |
| 153 | + return sequenceInformationMap.get( sequenceName ); |
98 | 154 | }
|
99 | 155 | }
|
0 commit comments