Skip to content

Commit b1939fa

Browse files
committed
Merge pull request #66 from domaframework/runtime-naming
ネーミング規約の適用をコンパイル時から実行時に変更
2 parents 8d016af + 6a15c1f commit b1939fa

File tree

70 files changed

+1306
-276
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1306
-276
lines changed

docs/sources/config.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,29 @@ REQUIRES_NEW 属性のトランザクションとの連動
142142

143143
デフォルトでは、 ``UnknownColumnException`` がスローされます。
144144

145+
テーブルやカラムにおけるネーミング規約の制御
146+
--------------------------------------------
147+
148+
``Naming`` を ``getNaming`` メソッドで返してください。
149+
150+
``Naming`` は、 ``@Entity`` の ``name`` 要素に指定された(もしくは指定されない) ``NamingType``
151+
をどのように適用するかについて制御するインタフェースです。
152+
このインタフェースを使うことで、個別のエンティティクラスに ``NamingType`` を指定しなくても
153+
エンティティのクラス名とプロパティ名からデータベースのテーブル名とカラム名を解決できます。
154+
155+
``Naming`` が使用される条件は以下の通りです。
156+
157+
* ``@Table`` や ``@Column`` の ``name`` 要素に値が指定されていない。
158+
159+
一般的なユースケースを実現するための実装は、 ``Naming`` の ``static`` なメンバに定義されています。
160+
161+
デフォルトでは、 ``Naming.NONE`` が使用されます。
162+
この実装は、エンティティクラスに指定された ``NamingType`` を使い、
163+
指定がない場合は何の規約も適用しません。
164+
165+
例えば、指定がない場合にスネークケースの大文字を適用したいというケースでは、
166+
``Naming.SNAKE_UPPER_CASE`` を使用できます。
167+
145168
マップのキーのネーミング規約の制御
146169
----------------------------------
147170

src/main/java/org/seasar/doma/internal/RuntimeConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.seasar.doma.jdbc.ConfigException;
2929
import org.seasar.doma.jdbc.JdbcLogger;
3030
import org.seasar.doma.jdbc.MapKeyNaming;
31+
import org.seasar.doma.jdbc.Naming;
3132
import org.seasar.doma.jdbc.QueryImplementors;
3233
import org.seasar.doma.jdbc.RequiresNewController;
3334
import org.seasar.doma.jdbc.SqlFileRepository;
@@ -123,6 +124,11 @@ public Commenter getCommenter() {
123124
return config.getCommenter();
124125
}
125126

127+
@Override
128+
public Naming getNaming() {
129+
return config.getNaming();
130+
}
131+
126132
@Override
127133
public MapKeyNaming getMapKeyNaming() {
128134
return config.getMapKeyNaming();

src/main/java/org/seasar/doma/internal/apt/EntityTypeGenerator.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.util.Iterator;
2222
import java.util.Map;
23+
import java.util.function.BiFunction;
2324

2425
import javax.annotation.processing.ProcessingEnvironment;
2526
import javax.lang.model.element.TypeElement;
@@ -43,6 +44,7 @@
4344
import org.seasar.doma.internal.apt.meta.SequenceIdGeneratorMeta;
4445
import org.seasar.doma.internal.apt.meta.TableIdGeneratorMeta;
4546
import org.seasar.doma.internal.apt.util.TypeMirrorUtil;
47+
import org.seasar.doma.jdbc.Naming;
4648
import org.seasar.doma.jdbc.entity.AbstractEntityType;
4749
import org.seasar.doma.jdbc.entity.AssignedIdPropertyType;
4850
import org.seasar.doma.jdbc.entity.DefaultPropertyType;
@@ -112,10 +114,10 @@ protected void printClass() {
112114
protected void printFields() {
113115
printSingletonField();
114116
printOriginalStatesAccessorField();
117+
printNamingTypeField();
115118
printIdGeneratorField();
116119
printPropertyTypeFields();
117120
printListenerSupplierField();
118-
printNamingTypeField();
119121
printImmutableField();
120122
printCatalogNameField();
121123
printSchemaNameField();
@@ -188,7 +190,7 @@ protected void printPropertyTypeFields() {
188190
iprint("/** the %1$s */%n", pm.getName());
189191
if (pm.isId()) {
190192
if (pm.getIdGeneratorMeta() != null) {
191-
iprint("public final %1$s<%11$s, %2$s, %3$s, %14$s> %12$s = new %1$s<>(%6$s.class, %13$s.class, %3$s.class, () -> %7$s, %10$s, %8$s, \"%4$s\", \"%5$s\", %15$s, __idGenerator);%n",
193+
iprint("public final %1$s<%11$s, %2$s, %3$s, %14$s> %12$s = new %1$s<>(%6$s.class, %13$s.class, %3$s.class, () -> %7$s, %10$s, %8$s, \"%4$s\", \"%5$s\", __namingType, %15$s, __idGenerator);%n",
192194
/* 1 */GeneratedIdPropertyType.class.getName(),
193195
/* 2 */entityMeta.getEntityTypeName(),
194196
/* 3 */basicCtType.getBoxedTypeName(),
@@ -205,7 +207,7 @@ protected void printPropertyTypeFields() {
205207
/* 14 */domainTypeName,
206208
/* 15 */pm.isColumnQuoteRequired());
207209
} else {
208-
iprint("public final %1$s<%11$s, %2$s, %3$s, %14$s> %12$s = new %1$s<>(%6$s.class, %13$s.class, %3$s.class, () -> %7$s, %10$s, %8$s, \"%4$s\", \"%5$s\", %15$s);%n",
210+
iprint("public final %1$s<%11$s, %2$s, %3$s, %14$s> %12$s = new %1$s<>(%6$s.class, %13$s.class, %3$s.class, () -> %7$s, %10$s, %8$s, \"%4$s\", \"%5$s\", __namingType, %15$s);%n",
209211
/* 1 */AssignedIdPropertyType.class.getName(),
210212
/* 2 */entityMeta.getEntityTypeName(),
211213
/* 3 */basicCtType.getBoxedTypeName(),
@@ -223,7 +225,7 @@ protected void printPropertyTypeFields() {
223225
/* 15 */pm.isColumnQuoteRequired());
224226
}
225227
} else if (pm.isVersion()) {
226-
iprint("public final %1$s<%11$s, %2$s, %3$s, %14$s> %12$s = new %1$s<>(%6$s.class, %13$s.class, %3$s.class, () -> %7$s, %10$s, %8$s, \"%4$s\", \"%5$s\", %15$s);%n",
228+
iprint("public final %1$s<%11$s, %2$s, %3$s, %14$s> %12$s = new %1$s<>(%6$s.class, %13$s.class, %3$s.class, () -> %7$s, %10$s, %8$s, \"%4$s\", \"%5$s\", __namingType, %15$s);%n",
227229
/* 1 */VersionPropertyType.class.getName(),
228230
/* 2 */entityMeta.getEntityTypeName(),
229231
/* 3 */basicCtType.getBoxedTypeName(),
@@ -240,7 +242,7 @@ protected void printPropertyTypeFields() {
240242
/* 14 */domainTypeName,
241243
/* 15 */pm.isColumnQuoteRequired());
242244
} else {
243-
iprint("public final %1$s<%13$s, %2$s, %3$s, %16$s> %14$s = new %1$s<>(%8$s.class, %15$s.class, %3$s.class, () -> %9$s, %12$s, %10$s, \"%4$s\", \"%5$s\", %6$s, %7$s, %17$s);%n",
245+
iprint("public final %1$s<%13$s, %2$s, %3$s, %16$s> %14$s = new %1$s<>(%8$s.class, %15$s.class, %3$s.class, () -> %9$s, %12$s, %10$s, \"%4$s\", \"%5$s\", __namingType, %6$s, %7$s, %17$s);%n",
244246
/* 1 */DefaultPropertyType.class.getName(),
245247
/* 2 */entityMeta.getEntityTypeName(),
246248
/* 3 */basicCtType.getBoxedTypeName(),
@@ -263,6 +265,13 @@ protected void printPropertyTypeFields() {
263265
}
264266
}
265267

268+
protected String getNamingTypeExpression(boolean defined) {
269+
if (defined) {
270+
return NamingType.class.getName() + "." + NamingType.NONE;
271+
}
272+
return "__namingType";
273+
}
274+
266275
protected void printListenerSupplierField() {
267276
if (entityMeta.isGenericEntityListener()) {
268277
iprint("private final java.util.function.Supplier<%1$s<%2$s>> __listenerSupplier;%n",
@@ -276,7 +285,14 @@ protected void printListenerSupplierField() {
276285
}
277286

278287
protected void printNamingTypeField() {
279-
iprint("private final %1$s __namingType;%n", NamingType.class.getName());
288+
NamingType namingType = entityMeta.getNamingType();
289+
if (namingType == null) {
290+
iprint("private final %1$s __namingType = null;%n",
291+
NamingType.class.getName());
292+
} else {
293+
iprint("private final %1$s __namingType = %1$s.%2$s;%n",
294+
NamingType.class.getName(), namingType.name());
295+
}
280296
print("%n");
281297
}
282298

@@ -334,8 +350,6 @@ protected void printEntityPropertyTypeMapField() {
334350
protected void printConstructor() {
335351
iprint("private %1$s() {%n", simpleName);
336352
iprint(" __listenerSupplier = () -> ListenerHolder.listener;%n");
337-
iprint(" __namingType = %1$s.%2$s;%n", NamingType.class.getName(),
338-
entityMeta.getNamingType().name());
339353
iprint(" __immutable = %1$s;%n", entityMeta.isImmutable());
340354
iprint(" __name = \"%1$s\";%n", entityMeta.getEntityName());
341355
iprint(" __catalogName = \"%1$s\";%n", entityMeta.getCatalogName());
@@ -438,6 +452,16 @@ protected void printGetSchemaNameMethod() {
438452
protected void printGetTableNameMethod() {
439453
iprint("@Override%n");
440454
iprint("public String getTableName() {%n");
455+
iprint(" return getTableName(%1$s.DEFAULT::apply);%n",
456+
Naming.class.getName());
457+
iprint("}%n");
458+
print("%n");
459+
iprint("@Override%n");
460+
iprint("public String getTableName(%1$s<%2$s, String, String> namingFunction) {%n",
461+
BiFunction.class.getName(), NamingType.class.getName());
462+
iprint(" if (__tableName.isEmpty()) {%n");
463+
iprint(" return namingFunction.apply(__namingType, __name);%n");
464+
iprint(" }%n");
441465
iprint(" return __tableName;%n");
442466
iprint("}%n");
443467
print("%n");

src/main/java/org/seasar/doma/internal/apt/meta/EntityMeta.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,7 @@ public String getSchemaName() {
228228
}
229229

230230
public String getTableName() {
231-
String tableName = tableMirror != null ? tableMirror.getNameValue()
232-
: "";
233-
return !tableName.isEmpty() ? tableName : namingType.apply(entityName);
231+
return tableMirror != null ? tableMirror.getNameValue() : "";
234232
}
235233

236234
public boolean isQuoteRequired() {

src/main/java/org/seasar/doma/internal/apt/meta/EntityMetaFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.seasar.doma.internal.apt.meta;
1717

18-
import static org.seasar.doma.internal.util.AssertionUtil.*;
18+
import static org.seasar.doma.internal.util.AssertionUtil.assertNotNull;
1919

2020
import java.util.ArrayList;
2121
import java.util.Collections;
@@ -124,7 +124,7 @@ protected TypeMirror resolveEntityListener(TypeElement classElement) {
124124
}
125125

126126
protected NamingType resolveNamingType(TypeElement classElement) {
127-
NamingType result = NamingType.NONE;
127+
NamingType result = null;
128128
for (AnnotationValue value : getEntityElementValueList(classElement,
129129
"naming")) {
130130
if (value != null) {

src/main/java/org/seasar/doma/internal/apt/meta/EntityPropertyMeta.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class EntityPropertyMeta {
7171
public EntityPropertyMeta(TypeElement entityElement,
7272
VariableElement propertyElement, NamingType namingType,
7373
boolean ownProperty, ProcessingEnvironment env) {
74-
assertNotNull(entityElement, propertyElement, namingType, env);
74+
assertNotNull(entityElement, propertyElement, env);
7575
this.entityName = entityElement.getSimpleName().toString();
7676
this.entityTypeName = entityElement.getQualifiedName().toString();
7777
this.entityMetaTypeName = MetaUtil.getMetaTypeName(entityTypeName);
@@ -165,9 +165,7 @@ public void setColumnMirror(ColumnMirror columnMirror) {
165165
}
166166

167167
public String getColumnName() {
168-
String columnName = columnMirror != null ? columnMirror.getNameValue()
169-
: "";
170-
return !columnName.isEmpty() ? columnName : namingType.apply(name);
168+
return columnMirror != null ? columnMirror.getNameValue() : "";
171169
}
172170

173171
public boolean isColumnInsertable() {

src/main/java/org/seasar/doma/internal/jdbc/command/EntityProvider.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Set;
3131

3232
import org.seasar.doma.jdbc.JdbcMappingVisitor;
33+
import org.seasar.doma.jdbc.Naming;
3334
import org.seasar.doma.jdbc.ResultMappingException;
3435
import org.seasar.doma.jdbc.Sql;
3536
import org.seasar.doma.jdbc.UnknownColumnHandler;
@@ -42,8 +43,7 @@
4243
* @author nakamura-to
4344
*
4445
*/
45-
public class EntityProvider<ENTITY> extends
46-
AbstractObjectProvider<ENTITY> {
46+
public class EntityProvider<ENTITY> extends AbstractObjectProvider<ENTITY> {
4747

4848
protected final EntityType<ENTITY> entityType;
4949

@@ -129,25 +129,27 @@ protected ENTITY build(ResultSet resultSet) throws SQLException {
129129

130130
protected HashMap<String, EntityPropertyType<ENTITY, ?>> createColumnNameMap(
131131
EntityType<ENTITY> entityType) {
132+
Naming naming = query.getConfig().getNaming();
132133
List<EntityPropertyType<ENTITY, ?>> propertyTypes = entityType
133134
.getEntityPropertyTypes();
134135
HashMap<String, EntityPropertyType<ENTITY, ?>> result = new HashMap<>(
135136
propertyTypes.size());
136137
for (EntityPropertyType<ENTITY, ?> propertyType : propertyTypes) {
137-
String columnName = propertyType.getColumnName();
138+
String columnName = propertyType.getColumnName(naming::apply);
138139
result.put(columnName.toLowerCase(), propertyType);
139140
}
140141
return result;
141142
}
142143

143144
protected void throwResultMappingException(
144145
Set<EntityPropertyType<ENTITY, ?>> unmappedPropertySet) {
146+
Naming naming = query.getConfig().getNaming();
145147
int size = unmappedPropertySet.size();
146148
List<String> unmappedPropertyNames = new ArrayList<>(size);
147149
List<String> expectedColumnNames = new ArrayList<>(size);
148150
for (EntityPropertyType<ENTITY, ?> propertyType : unmappedPropertySet) {
149151
unmappedPropertyNames.add(propertyType.getName());
150-
expectedColumnNames.add(propertyType.getColumnName());
152+
expectedColumnNames.add(propertyType.getColumnName(naming::apply));
151153
}
152154
Sql<?> sql = query.getSql();
153155
throw new ResultMappingException(query.getConfig()

src/main/java/org/seasar/doma/internal/jdbc/dao/AbstractDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ private void validateConfig(Config config, DataSource dataSource) {
159159
throw new ConfigException(config.getClass().getName(),
160160
"getUnknownColumnHandler");
161161
}
162+
if (config.getNaming() == null) {
163+
throw new ConfigException(config.getClass().getName(), "getNaming");
164+
}
162165
if (config.getMapKeyNaming() == null) {
163166
throw new ConfigException(config.getClass().getName(),
164167
"getMapKeyNaming");

src/main/java/org/seasar/doma/jdbc/Config.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ default UnknownColumnHandler getUnknownColumnHandler() {
145145
return ConfigSupport.defaultUnknownColumnHandler;
146146
}
147147

148+
/**
149+
* ネーミング規約のコントローラを返します。
150+
*
151+
* @return ネーミング規約のコントローラ
152+
* @since 2.2.0
153+
*/
154+
default Naming getNaming() {
155+
return ConfigSupport.defaultNaming;
156+
}
157+
148158
/**
149159
* マップのキーのネーミング規約のコントローラを返します。
150160
*

src/main/java/org/seasar/doma/jdbc/ConfigSupport.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public final class ConfigSupport {
4242
public static UnknownColumnHandler defaultUnknownColumnHandler = new UnknownColumnHandler() {
4343
};
4444

45+
/**
46+
* @since 2.2.0
47+
*/
48+
public static Naming defaultNaming = Naming.DEFAULT;
49+
4550
public static MapKeyNaming defaultMapKeyNaming = new MapKeyNaming() {
4651
};
4752

0 commit comments

Comments
 (0)