Skip to content

Commit 6c6206c

Browse files
committed
First commit
0 parents  commit 6c6206c

File tree

156 files changed

+11846
-0
lines changed

Some content is hidden

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

156 files changed

+11846
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/.classpath
2+
/.gradle/
3+
/.project
4+
/.settings/
5+
/bin/
6+
/build/
7+
/target/
8+
/.metadata
9+
/.idea/
10+
.factorypath

README.md

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
Doma CodeGen Plugin
2+
===================
3+
4+
Doma CodeGen Plugin is a gradle plugin.
5+
It generates Java source files and SQL files from Database.
6+
7+
How to use
8+
----------
9+
10+
First, see [Gradle Plugin Portal](https://plugins.gradle.org/plugin/org.seasar.doma.codegen).
11+
12+
The basic usage example is as follows:
13+
14+
```groovy
15+
buildscript {
16+
repositories {
17+
mavenCentral()
18+
}
19+
dependencies {
20+
// specify your JDBC driver
21+
classpath 'com.h2database:h2:1.3.175'
22+
}
23+
}
24+
25+
plugins {
26+
id 'java'
27+
// specify the Doma CodeGen Plugin with correct version
28+
id 'org.seasar.doma.codegen' version 'x.x.x'
29+
}
30+
31+
domaCodeGen {
32+
// make an arbitrary named block
33+
dev {
34+
// JDBC url
35+
url = '...'
36+
// JDBC user
37+
user = '...'
38+
// JDBC password
39+
password = '...'
40+
// configuration for generated entity source files
41+
entity {
42+
packageName = 'org.example.entity'
43+
}
44+
// configuration for generated DAO source files
45+
dao {
46+
packageName = 'org.example.dao'
47+
}
48+
}
49+
}
50+
```
51+
52+
To generate all files, run `domaCodeGenDevAll` task:
53+
54+
```bash
55+
$ ./gradlew domaCodeGenDevAll
56+
```
57+
58+
Gradle Tasks
59+
------------
60+
61+
The Doma CodeGen Plugin provides following tasks:
62+
63+
- domaCodeGen*Name*All - Generate all.
64+
- domaCodeGen*Name*Dao - Generates DAO source files.
65+
- domaCodeGen*Name*Dto - Read resultSet metadata and generate a DTO source file.
66+
- domaCodeGen*Name*Entity - Generates entity source files.
67+
- domaCodeGen*Name*Sql - Generates SQL files.
68+
- domaCodeGen*Name*SqlTest - Generates SQL test source files.
69+
70+
Note that each *Name* part in the above task names is replaced with the block name defined under the `domaCodeGen` block.
71+
In the above usage example, the *Dev* part is corresponding to the `dev` block.
72+
73+
To check all defined task names, run the `tasks` task:
74+
75+
```bash
76+
$ ./gradlew tasks
77+
```
78+
79+
Config Options
80+
--------------
81+
82+
### named config
83+
84+
A named config must be under the `domaCodeGen` block.
85+
The name of the config is arbitrary.
86+
You can make multiple configs under the `domaCodeGen` block.
87+
88+
In the following example, we define two configs - `sales` and `account`:
89+
90+
```groovy
91+
domaCodeGen {
92+
sales {
93+
url = "jdbc:h2:mem:sales"
94+
}
95+
account {
96+
url = "jdbc:h2:mem:account"
97+
}
98+
}
99+
```
100+
101+
| Option | Description | Values | Default |
102+
| :--- | :--- | :--- | :--- |
103+
| url | JDBC url | | |
104+
| user | JDBC user | | |
105+
| password | JDBC password | | |
106+
| dataSource | database data source | | inferred by the url |
107+
| codeGenDialect | database dialect | | inferred by the url |
108+
| schemaName | database schema name | | |
109+
| tableNamePattern | database table pattern (Regex) | | `.*` |
110+
| ignoredTableNamePattern | database ignored table pattern (Regex) | | `.*\$.*` |
111+
| tableTypes | database table type | such as TABLE, VIEW and so on | `TABLE` |
112+
| versionColumnNamePattern | database version column pattern (Regex) | | `VERSION([_]?NO)?` |
113+
| templateEncoding | encoding for freeMarker template files | | `UTF-8` |
114+
| templateDir | directory for user customized template files | | |
115+
| encoding | encoding for generated Java source files | | `UTF-8` |
116+
| sourceDir | directory for generated Java source files | | `src/main/java` |
117+
| testSourceDir | directory for generated Java test source files | | `src/test/java` |
118+
| resourceDir | directory for generated SQL files | | src/main/resources |
119+
| globalFactory | entry point to customize plugin behavior | | `new org.seasar.doma.gradle.codegen.GlobalFactory()` |
120+
121+
### entity
122+
123+
An `entity` block must be under a named config:
124+
125+
```groovy
126+
domaCodeGen {
127+
sales {
128+
entity {
129+
useAccessor = false
130+
}
131+
}
132+
}
133+
```
134+
135+
| Option | Description | Values | Default |
136+
| :--- | :--- | :--- | :--- |
137+
| overwrite | where to overwrite generated entity files or not | | `true` |
138+
| overwriteListener | allow to overwrite listeners or not | | `false` |
139+
| superclassName | common superclass for generated entity classes | | |
140+
| listenerSuperclassName | common superclass for generated entity listener classes | | |
141+
| packageName | package name for generated entity class | | `example.entity` |
142+
| generationType | generation type for entity identities | enum value of `org.seasar.doma.gradle.codegen.desc.GenerationType` | |
143+
| namingType | naming convention | enum value of org.`seasar.doma.gradle.codegen.desc.NamingType` | |
144+
| initialValue | initial value for entity identities | | |
145+
| allocationSize | allocation size for entity identities | | |
146+
| showCatalogName | whether to show catalog names or not | | `false` |
147+
| showSchemaName | whether to show schema names or not | | `false` |
148+
| showTableName | whether to show table names or not | | `true` |
149+
| showColumnName | whether to show column names or not | | `true` |
150+
| showDbComment | whether to show database comments or not | | `true` |
151+
| useAccessor | whether to use accessors or not | | `true` |
152+
| useListener | whether to use listeners or not | | `true` |
153+
| originalStatesPropertyName | property to be annotated with `@OriginalStates` | | |
154+
| entityPropertyClassNamesFile | file used to resolve entity property classes | | |
155+
| prefix | prefix for entity classes | | |
156+
| suffix | suffix for entity classes | | |
157+
158+
### dao
159+
160+
A `dao` block must be under a named config:
161+
162+
```groovy
163+
domaCodeGen {
164+
sales {
165+
dao {
166+
packageName = 'org.example.sales.dao'
167+
}
168+
}
169+
}
170+
```
171+
172+
| Option | Description | Values | Default |
173+
| :--- | :--- | :--- | :--- |
174+
| overwrite | whether to overwrite generated DAO files or not | | `false` |
175+
| packageName | package name for generated DAO classes | | `example.dao` |
176+
| suffix | suffix for Dao classes | | `Dao` |
177+
| configClassName | `org.seasar.doma.jdbc.Config` implemented class name. Tha name is used at @Dao | | `false` |
178+
179+
### sql
180+
181+
A `sql` block must be under a named config:
182+
183+
```groovy
184+
domaCodeGen {
185+
sales {
186+
sql {
187+
overwrite = false
188+
}
189+
}
190+
}
191+
```
192+
193+
| Option | Description | Values | Default |
194+
| :--- | :--- | :--- | :--- |
195+
| overwrite | whether to overwrite generated sql files or not | | `true` |
196+
197+
198+
Customization
199+
-------------
200+
201+
### Using custom template files
202+
203+
Default template files are located in the source code repository of the Doma CodeGen Plugin.
204+
They are listed as follows:
205+
206+
| Template File | Data Model Class | Generated Files |
207+
| :--- | :--- | :--- |
208+
| entity.ftl | org.seasar.doma.gradle.codege.desc.EntityDesc | entity source files|
209+
| entityListener.ftl | org.seasar.doma.gradle.codege.desc.EntityListenerDesc | entity listener source files |
210+
| dao.ftl | org.seasar.doma.gradle.codege.desc.DaoDesc | DAO source files |
211+
| sqlTestCase.ftl | org.seasar.doma.gradle.codege.desc.SqlTestDesc | test source files for SQL |
212+
| selectById.sql.ftl | org.seasar.doma.gradle.codege.desc.SqlDesc | SQL files |
213+
| selectByIdAndVersion.sql.ftl | org.seasar.doma.gradle.codege.desc.SqlDesc | SQL files |
214+
215+
To create custom template files, copy them and modify their contents without changing file names.
216+
Then put them in the directory which is specified to the `templateDir` option.
217+
218+
```groovy
219+
domaCodeGen {
220+
dev {
221+
url = '...'
222+
user = '...'
223+
password = '...'
224+
// specify the directory including your custom template files
225+
templateDir = "$projectDir/template"
226+
entity {
227+
packageName = 'org.example.entity'
228+
}
229+
dao {
230+
packageName = 'org.example.dao'
231+
}
232+
}
233+
}
234+
```
235+
236+
The Doma CodeGen Plugin uses [Apache FreeMarker](https://freemarker.apache.org/) to process the template files.
237+
238+
Sample Project
239+
--------------
240+
241+
- [codegen-sample](https://github.com/domaframework/codegen-sample)
242+
243+
License
244+
-------
245+
246+
```
247+
Copyright 2020 domaframework.org
248+
249+
Licensed under the Apache License, Version 2.0 (the "License");
250+
you may not use this file except in compliance with the License.
251+
You may obtain a copy of the License at
252+
253+
http://www.apache.org/licenses/LICENSE-2.0
254+
255+
Unless required by applicable law or agreed to in writing, software
256+
distributed under the License is distributed on an "AS IS" BASIS,
257+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
258+
See the License for the specific language governing permissions and
259+
limitations under the License.
260+
```

codegen-test/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/.classpath
2+
/.gradle/
3+
/.project
4+
/.settings/
5+
/bin/
6+
/build/
7+
/src/
8+
/target/
9+
/data/
10+
/.metadata
11+
/.idea/
12+
/.apt_generated/

codegen-test/build.gradle

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import org.h2.Driver
2+
import org.seasar.doma.gradle.codegen.jdbc.SimpleDataSource
3+
4+
buildscript {
5+
ext.domaVersion = '2.28.0'
6+
ext.h2Version= '1.4.200'
7+
repositories {
8+
mavenCentral()
9+
mavenLocal()
10+
maven {url 'https://oss.sonatype.org/content/repositories/snapshots/'}
11+
}
12+
dependencies {
13+
classpath "org.seasar.doma:codegen"
14+
classpath "com.h2database:h2:$h2Version"
15+
}
16+
}
17+
18+
plugins {
19+
id 'java'
20+
id 'org.seasar.doma.compile' version '1.0.0'
21+
}
22+
23+
apply plugin: 'org.seasar.doma.codegen'
24+
25+
java {
26+
sourceCompatibility = JavaVersion.VERSION_1_8
27+
targetCompatibility = JavaVersion.VERSION_1_8
28+
}
29+
30+
test {
31+
useJUnitPlatform()
32+
}
33+
34+
repositories {
35+
mavenLocal()
36+
mavenCentral()
37+
}
38+
39+
dependencies {
40+
implementation "org.seasar.doma:doma:$domaVersion"
41+
annotationProcessor "org.seasar.doma:doma:$domaVersion"
42+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.1'
43+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.1'
44+
testRuntimeOnly "com.h2database:h2:$h2Version"
45+
}
46+
47+
def _url = "jdbc:h2:file:$projectDir/data/db"
48+
def _user = ''
49+
def _password = ''
50+
51+
domaCodeGen {
52+
dev {
53+
def basePackage = 'codegen'
54+
url = _url.toString()
55+
user = _user
56+
password = _password
57+
entity {
58+
packageName = "${basePackage}.entity"
59+
}
60+
dao {
61+
packageName = "${basePackage}.dao"
62+
}
63+
}
64+
}
65+
66+
task deleteSrc {
67+
delete 'src'
68+
}
69+
70+
task createDb {
71+
doLast {
72+
def ds = new SimpleDataSource()
73+
ds.setDriver(new Driver())
74+
ds.setUrl(_url)
75+
ds.setUser(_user)
76+
ds.setPassword(_password)
77+
78+
def sql = """
79+
drop all objects;
80+
81+
create table department(
82+
department_id integer not null primary key,
83+
department_no integer not null,
84+
department_name varchar(20),
85+
location varchar(20) default 'tokyo',
86+
version integer
87+
);
88+
89+
create table employee(
90+
employee_id integer not null primary key,
91+
employee_no integer not null,
92+
employee_name varchar(20)
93+
);
94+
"""
95+
ds.connection.withCloseable {
96+
it.createStatement().withCloseable {
97+
it.execute(sql)
98+
}
99+
}
100+
}
101+
}
102+
103+
tasks.getByName('domaCodeGenDevDbMeta').dependsOn(deleteSrc, createDb)
57.3 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sat Apr 04 07:31:40 JST 2020
2+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
3+
distributionBase=GRADLE_USER_HOME
4+
distributionPath=wrapper/dists
5+
zipStorePath=wrapper/dists
6+
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)