/
H2DdlReader.java
98 lines (87 loc) · 3.76 KB
/
H2DdlReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package org.jumpmind.db.platform.h2;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.ForeignKey;
import org.jumpmind.db.model.IIndex;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.platform.AbstractJdbcDdlReader;
import org.jumpmind.db.platform.DatabaseMetaDataWrapper;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.MetaDataColumnDescriptor;
/*
* Reads a database model from a H2 database. From patch <a
* href="https://issues.apache.org/jira/browse/DDLUTILS-185"
* >https://issues.apache.org/jira/browse/DDLUTILS-185</a>
*/
public class H2DdlReader extends AbstractJdbcDdlReader {
public H2DdlReader(IDatabasePlatform platform) {
super(platform);
setDefaultCatalogPattern(null);
setDefaultSchemaPattern(null);
}
@Override
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object> values)
throws SQLException {
Column column = super.readColumn(metaData, values);
if (values.get("CHARACTER_MAXIMUM_LENGTH") != null) {
column.setSize(values.get("CHARACTER_MAXIMUM_LENGTH").toString());
}
if (values.get("COLUMN_DEFAULT") != null) {
column.setDefaultValue(values.get("COLUMN_DEFAULT").toString());
}
if (values.get("NUMERIC_SCALE") != null) {
column.setScale((Integer) values.get("NUMERIC_SCALE"));
}
if (TypeMap.isTextType(column.getTypeCode()) && (column.getDefaultValue() != null)) {
column.setDefaultValue(unescape(column.getDefaultValue(), "'", "''"));
}
String autoIncrement = (String) values.get("IS_AUTOINCREMENT");
if (autoIncrement != null) {
column.setAutoIncrement("YES".equalsIgnoreCase(autoIncrement.trim()));
column.setDefaultValue(null);
}
return column;
}
@Override
protected List<MetaDataColumnDescriptor> initColumnsForColumn() {
List<MetaDataColumnDescriptor> result = super.initColumnsForColumn();
result.add(new MetaDataColumnDescriptor("COLUMN_DEFAULT", 12));
result.add(new MetaDataColumnDescriptor("NUMERIC_SCALE", 4, new Integer(0)));
result.add(new MetaDataColumnDescriptor("CHARACTER_MAXIMUM_LENGTH", 12));
return result;
}
@Override
protected boolean isInternalForeignKeyIndex(Connection connection,
DatabaseMetaDataWrapper metaData, Table table, ForeignKey fk, IIndex index) {
String name = index.getName();
return name != null && name.startsWith("CONSTRAINT_INDEX_");
}
@Override
protected boolean isInternalPrimaryKeyIndex(Connection connection,
DatabaseMetaDataWrapper metaData, Table table, IIndex index) {
String name = index.getName();
return name != null && name.startsWith("PRIMARY_KEY_");
}
}