Skip to content

Commit ced9108

Browse files
committed
Merge pull request #74 from domaframework/issue-73
PostgresDialectでID生成エラーが発生する問題を修正
2 parents 52b1182 + 21609bc commit ced9108

14 files changed

+432
-73
lines changed

src/main/java/org/seasar/doma/jdbc/dialect/Dialect.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,23 @@ boolean supportsSelectForUpdate(SelectForUpdateType type,
147147
* <p>
148148
* {@link #supportsIdentity()} が {@code true} を返す場合にのみ呼び出し可能です。
149149
*
150-
* @param qualifiedTableName
151-
* テーブルの完全修飾名
150+
* @param catalogName
151+
* カタログの名前
152+
* @param schemaName
153+
* スキーマの名前
154+
* @param tableName
155+
* テーブルの名前
152156
* @param columnName
153157
* IDENTITYカラムの名前
158+
* @param isQuoteRequired
159+
* 引用符が必要かどうか
154160
* @return IDENTITYを取得するためのSQL
155161
* @throws DomaNullPointerException
156-
* 引数のいずれかが {@code null} の場合
162+
* {@code tableName} と {@code columnName} のいずれかが {@code null}
163+
* の場合
157164
*/
158-
Sql<?> getIdentitySelectSql(String qualifiedTableName, String columnName);
165+
Sql<?> getIdentitySelectSql(String catalogName, String schemaName,
166+
String tableName, String columnName, boolean isQuoteRequired);
159167

160168
/**
161169
* シーケンスの次の値を取得するためのSQLを返します。

src/main/java/org/seasar/doma/jdbc/dialect/H212126Dialect.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ public boolean includesIdentityColumn() {
131131
}
132132

133133
@Override
134-
public PreparedSql getIdentitySelectSql(String qualifiedTableName,
135-
String columnName) {
136-
if (qualifiedTableName == null) {
137-
throw new DomaNullPointerException("qualifiedTableName");
134+
public PreparedSql getIdentitySelectSql(String catalogName,
135+
String schemaName, String tableName, String columnName,
136+
boolean isQuoteRequired) {
137+
if (tableName == null) {
138+
throw new DomaNullPointerException("tableName");
138139
}
139140
if (columnName == null) {
140141
throw new DomaNullPointerException("columnName");

src/main/java/org/seasar/doma/jdbc/dialect/HsqldbDialect.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,11 @@ public boolean includesIdentityColumn() {
130130
}
131131

132132
@Override
133-
public PreparedSql getIdentitySelectSql(String qualifiedTableName,
134-
String columnName) {
135-
if (qualifiedTableName == null) {
136-
throw new DomaNullPointerException("qualifiedTableName");
133+
public PreparedSql getIdentitySelectSql(String catalogName,
134+
String schemaName, String tableName, String columnName,
135+
boolean isQuoteRequired) {
136+
if (tableName == null) {
137+
throw new DomaNullPointerException("tableName");
137138
}
138139
if (columnName == null) {
139140
throw new DomaNullPointerException("columnName");

src/main/java/org/seasar/doma/jdbc/dialect/PostgresDialect.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
import java.sql.SQLException;
2020
import java.sql.Types;
2121
import java.util.Collections;
22+
import java.util.function.Function;
2223

2324
import org.seasar.doma.DomaNullPointerException;
2425
import org.seasar.doma.expr.ExpressionFunctions;
2526
import org.seasar.doma.internal.jdbc.dialect.PostgresForUpdateTransformer;
2627
import org.seasar.doma.internal.jdbc.dialect.PostgresPagingTransformer;
2728
import org.seasar.doma.internal.jdbc.sql.InParameter;
2829
import org.seasar.doma.internal.jdbc.sql.PreparedSql;
30+
import org.seasar.doma.internal.jdbc.util.DatabaseObjectUtil;
2931
import org.seasar.doma.jdbc.JdbcMappingVisitor;
3032
import org.seasar.doma.jdbc.ScriptBlockContext;
3133
import org.seasar.doma.jdbc.SelectForUpdateType;
@@ -159,19 +161,22 @@ public boolean isUniqueConstraintViolated(SQLException sqlException) {
159161
}
160162

161163
@Override
162-
public PreparedSql getIdentitySelectSql(String qualifiedTableName,
163-
String columnName) {
164-
if (qualifiedTableName == null) {
165-
throw new DomaNullPointerException("qualifiedTableName");
164+
public PreparedSql getIdentitySelectSql(String catalogName,
165+
String schemaName, String tableName, String columnName,
166+
boolean isQuoteRequired) {
167+
if (tableName == null) {
168+
throw new DomaNullPointerException("tableName");
166169
}
167170
if (columnName == null) {
168171
throw new DomaNullPointerException("columnName");
169172
}
173+
String qualifiedTableName = DatabaseObjectUtil.getQualifiedName(
174+
isQuoteRequired ? this::applyQuote : Function.identity(),
175+
catalogName, schemaName, tableName + "_" + columnName + "_seq");
170176
StringBuilder buf = new StringBuilder(64);
171177
buf.append("select currval('");
172178
buf.append(qualifiedTableName);
173-
buf.append('_').append(columnName);
174-
buf.append("_seq')");
179+
buf.append("')");
175180
String rawSql = buf.toString();
176181
return new PreparedSql(SqlKind.SELECT, rawSql, rawSql, null,
177182
Collections.<InParameter<?>> emptyList(), SqlLogType.FORMATTED);

src/main/java/org/seasar/doma/jdbc/dialect/SqliteDialect.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ public boolean includesIdentityColumn() {
127127
}
128128

129129
@Override
130-
public PreparedSql getIdentitySelectSql(String qualifiedTableName,
131-
String columnName) {
132-
if (qualifiedTableName == null) {
133-
throw new DomaNullPointerException("qualifiedTableName");
130+
public PreparedSql getIdentitySelectSql(String catalogName,
131+
String schemaName, String tableName, String columnName,
132+
boolean isQuoteRequired) {
133+
if (tableName == null) {
134+
throw new DomaNullPointerException("tableName");
134135
}
135136
if (columnName == null) {
136137
throw new DomaNullPointerException("columnName");

src/main/java/org/seasar/doma/jdbc/dialect/StandardDialect.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.seasar.doma.jdbc.SelectForUpdateType;
4747
import org.seasar.doma.jdbc.SelectOptions;
4848
import org.seasar.doma.jdbc.SelectOptionsAccessor;
49+
import org.seasar.doma.jdbc.Sql;
4950
import org.seasar.doma.jdbc.SqlLogFormattingFunction;
5051
import org.seasar.doma.jdbc.SqlLogFormattingVisitor;
5152
import org.seasar.doma.jdbc.SqlNode;
@@ -432,8 +433,8 @@ public JdbcType<ResultSet> getResultSetType() {
432433
}
433434

434435
@Override
435-
public PreparedSql getIdentitySelectSql(String qualifiedTableName,
436-
String columnName) {
436+
public Sql<?> getIdentitySelectSql(String catalogName, String schemaName,
437+
String tableName, String columnName, boolean isQuoteRequired) {
437438
throw new JdbcUnsupportedOperationException(getClass().getName(),
438439
"getIdentitySelectSql");
439440
}

src/main/java/org/seasar/doma/jdbc/id/BuiltinIdentityIdGenerator.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
import org.seasar.doma.GenerationType;
2323
import org.seasar.doma.jdbc.JdbcException;
24+
import org.seasar.doma.jdbc.Naming;
2425
import org.seasar.doma.jdbc.Sql;
26+
import org.seasar.doma.jdbc.entity.EntityType;
2527
import org.seasar.doma.message.Message;
2628

2729
/**
@@ -94,10 +96,16 @@ protected long getGeneratedValue(IdGenerationConfig config,
9496
* 識別子の取得に失敗した場合
9597
*/
9698
protected long getGeneratedValue(IdGenerationConfig config) {
97-
String qualifiedTableName = config.getQualifiedTableName();
98-
String idColumnName = config.getIdColumnName();
99-
Sql<?> sql = config.getDialect().getIdentitySelectSql(
100-
qualifiedTableName, idColumnName);
99+
Naming naming = config.getNaming();
100+
EntityType<?> entityType = config.getEntityType();
101+
String catalogName = entityType.getCatalogName();
102+
String schemaName = entityType.getSchemaName();
103+
String tableName = entityType.getTableName(naming::apply);
104+
String idColumnName = entityType.getGeneratedIdPropertyType()
105+
.getColumnName(naming::apply);
106+
Sql<?> sql = config.getDialect().getIdentitySelectSql(catalogName,
107+
schemaName, tableName, idColumnName,
108+
entityType.isQuoteRequired());
101109
return getGeneratedValue(config, sql);
102110
}
103111

src/main/java/org/seasar/doma/jdbc/id/IdGenerationConfig.java

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.seasar.doma.jdbc.Config;
2323
import org.seasar.doma.jdbc.JdbcLogger;
24+
import org.seasar.doma.jdbc.Naming;
2425
import org.seasar.doma.jdbc.RequiresNewController;
2526
import org.seasar.doma.jdbc.dialect.Dialect;
2627
import org.seasar.doma.jdbc.entity.EntityType;
@@ -39,12 +40,6 @@ public class IdGenerationConfig {
3940
/** 識別子が属するエンティティ */
4041
protected final EntityType<?> entityType;
4142

42-
/** 識別子が属するエンティティに対応するテーブルの完全修飾名 */
43-
protected final String qualifiedTableName;
44-
45-
/** 識別子にマッピングされたカラムの名前 */
46-
protected final String idColumnName;
47-
4843
/**
4944
* インスタンスを構築します。
5045
*
@@ -54,32 +49,9 @@ public class IdGenerationConfig {
5449
* 識別子が属するエンティティ
5550
*/
5651
public IdGenerationConfig(Config config, EntityType<?> entityType) {
57-
this(config, entityType, entityType.getQualifiedTableName(
58-
config.getNaming()::apply, config.getDialect()::applyQuote),
59-
entityType.getGeneratedIdPropertyType().getColumnName(
60-
config.getNaming()::apply,
61-
config.getDialect()::applyQuote));
62-
}
63-
64-
/**
65-
* インスタンスを構築します。
66-
*
67-
* @param config
68-
* JDBCの設定
69-
* @param entityType
70-
* 識別子が属するエンティティ
71-
* @param qualifiedTableName
72-
* 識別子が属するエンティティに対応するテーブルの完全修飾名
73-
* @param idColumnName
74-
* 識別子にマッピングされたカラムの名前
75-
*/
76-
protected IdGenerationConfig(Config config, EntityType<?> entityType,
77-
String qualifiedTableName, String idColumnName) {
78-
assertNotNull(config, entityType, qualifiedTableName, idColumnName);
52+
assertNotNull(config, entityType);
7953
this.config = config;
8054
this.entityType = entityType;
81-
this.qualifiedTableName = qualifiedTableName;
82-
this.idColumnName = idColumnName;
8355
}
8456

8557
public DataSource getDataSource() {
@@ -102,6 +74,10 @@ public RequiresNewController getRequiresNewController() {
10274
return config.getRequiresNewController();
10375
}
10476

77+
public Naming getNaming() {
78+
return config.getNaming();
79+
}
80+
10581
public int getFetchSize() {
10682
return config.getFetchSize();
10783
}
@@ -118,12 +94,4 @@ public EntityType<?> getEntityType() {
11894
return entityType;
11995
}
12096

121-
public String getQualifiedTableName() {
122-
return qualifiedTableName;
123-
}
124-
125-
public String getIdColumnName() {
126-
return idColumnName;
127-
}
128-
12997
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2004-2010 the Seasar Foundation and the Others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package example.entity;
17+
18+
import java.io.Serializable;
19+
import java.math.BigDecimal;
20+
21+
import org.seasar.doma.Entity;
22+
import org.seasar.doma.GeneratedValue;
23+
import org.seasar.doma.GenerationType;
24+
import org.seasar.doma.Id;
25+
import org.seasar.doma.OriginalStates;
26+
import org.seasar.doma.Table;
27+
import org.seasar.doma.Version;
28+
import org.seasar.doma.jdbc.entity.NamingType;
29+
30+
@Entity(naming = NamingType.SNAKE_UPPER_CASE)
31+
@Table(catalog = "CATA", quote = true)
32+
public class IdGeneratedEmp implements Serializable {
33+
34+
private static final long serialVersionUID = -6511179565163144602L;
35+
36+
@Id
37+
@GeneratedValue(strategy = GenerationType.IDENTITY)
38+
Integer id;
39+
40+
String name;
41+
42+
BigDecimal salary;
43+
44+
@Version
45+
Integer version;
46+
47+
@OriginalStates
48+
public IdGeneratedEmp originalStates;
49+
50+
public Integer getId() {
51+
return id;
52+
}
53+
54+
public void setId(Integer id) {
55+
this.id = id;
56+
}
57+
58+
public String getName() {
59+
return name;
60+
}
61+
62+
public void setName(String name) {
63+
this.name = name;
64+
}
65+
66+
public BigDecimal getSalary() {
67+
return salary;
68+
}
69+
70+
public void setSalary(BigDecimal salary) {
71+
this.salary = salary;
72+
}
73+
74+
public Integer getVersion() {
75+
return version;
76+
}
77+
78+
public void setVersion(Integer version) {
79+
this.version = version;
80+
}
81+
82+
}

0 commit comments

Comments
 (0)