Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init command #4

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ permissions:
contents: write

jobs:
build:
release:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
Expand Down
12 changes: 6 additions & 6 deletions e2e/mds/gencoder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ databases:
dsn: 'mysql://root:root@localhost:3306/testdb'
schema: testdb
properties:
package: defaultpackage
entityPkg: com.example.entity
mapperPkg: com.example.mapper
dynamicSQLPkg: com.example.mapper
tables:
- name: 'user'
properties:
package: mysqlpackage
ignoreColumns:
- deleted_at
- name: postgres
dsn: 'postgres://root:root@localhost:5432/testdb?sslmode=disable'
schema: public
properties:
package: defaultpackage
entityPkg: com.example.pg.entity
mapperPkg: com.example.pg.mapper
dynamicSQLPkg: com.example.pg.mapper
tables:
- name: 'user'
properties:
package: postgrespackage
ignoreColumns:
- deleted_at
4 changes: 3 additions & 1 deletion e2e/mds/templates/dynamic_support.java.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* @gencoder.generated: src/main/java/{{replaceAll properties.package '\.' '/'}}/{{pascalCase table.name}}DynamicSqlSupport.java
* @gencoder.generated: src/main/java/{{replaceAll properties.dynamicSQLPkg '\.' '/'}}/{{pascalCase table.name}}DynamicSqlSupport.java
*/

package {{properties.dynamicSQLPkg}};

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.AliasableSqlTable;

Expand Down
4 changes: 2 additions & 2 deletions e2e/mds/templates/entity.java.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @gencoder.generated: src/main/java/{{replaceAll properties.package '\.' '/'}}/{{pascalCase table.name}}.java
* @gencoder.generated: src/main/java/{{replaceAll properties.entityPkg '\.' '/'}}/{{pascalCase table.name}}.java
*/

package {{properties.package}};
package {{properties.entityPkg}};

/**
* @gencoder.block.start: table
Expand Down
8 changes: 0 additions & 8 deletions e2e/mds/templates/java_type.partial.hbs

This file was deleted.

160 changes: 160 additions & 0 deletions e2e/mds/templates/mapper.java.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/**
* @gencoder.generated: src/main/java/{{replaceAll properties.mapperPkg '\.' '/'}}/{{pascalCase table.name}}Mapper.java
*/

package {{properties.mapperPkg}};

import static {{properties.dynamicSQLPkg}}.{{pascalCase table.name}}DynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

import {{properties.entityPkg}}.{{pascalCase table.name}};
import jakarta.annotation.Generated;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper;
import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper;
import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

public class {{pascalCase table.name}}Mapper extends CommonCountMapper, CommonDeleteMapper, CommonUpdateMapper {

// @gencoder.block.start: mapper

BasicColumn[] selectList = BasicColumn.columnList({{#each table.columns}}{{camelCase name}}{{#unless @last}}, {{/unless}}{{/each}});

@InsertProvider(type=SqlProviderAdapter.class, method="insert")
@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="row.id", before=false, resultType={{> 'id_type.partial.hbs' columns=table.columns}}.class)
int insert(InsertStatementProvider<{{pascalCase table.name}}> insertStatement);

@SelectProvider(type=SqlProviderAdapter.class, method="select")
@Results(id="{{pascalCase table.name}}Result", value = {
{{#each table.columns}}
@Result(column="{{name}}", property="{{camelCase name}}"/**, jdbcType={{jdbcType}}*/{{#if isPrimaryKey}}, id=true{{/if}}){{#unless @last}},{{/unless}}
{{/each}}
})
List<{{pascalCase table.name}}> selectMany(SelectStatementProvider selectStatement);

@SelectProvider(type=SqlProviderAdapter.class, method="select")
@ResultMap("{{pascalCase table.name}}Result")
Optional<{{pascalCase table.name}}> selectOne(SelectStatementProvider selectStatement);

default long count(CountDSLCompleter completer) {
return MyBatis3Utils.countFrom(this::count, {{camelCase table.name}}, completer);
}

default int delete(DeleteDSLCompleter completer) {
return MyBatis3Utils.deleteFrom(this::delete, {{camelCase table.name}}, completer);
}

default int deleteByPrimaryKey(Integer id_) {
return delete(c ->
c.where(id, isEqualTo(id_))
);
}

default int insert({{pascalCase table.name}} row) {
return MyBatis3Utils.insert(this::insert, row, {{camelCase table.name}}, c -> c
{{#each table.columns}}
{{#if (ne name 'id')}}
.map({{camelCase name}}).toProperty("{{camelCase name}}")
{{/if}}
{{/each}}
);
}

default int insertSelective({{pascalCase table.name}} row) {
return MyBatis3Utils.insert(this::insert, row, user, c -> c
{{#each table.columns}}
{{#if (ne name 'id')}}
.map({{camelCase name}}).toPropertyWhenPresent("{{camelCase name}}")
{{/if}}
{{/each}}
);
}

default Optional<{{pascalCase table.name}}> selectOne(SelectDSLCompleter completer) {
return MyBatis3Utils.selectOne(this::selectOne, selectList, {{camelCase table.name}}, completer);
}

default List<{{pascalCase table.name}}> select(SelectDSLCompleter completer) {
return MyBatis3Utils.selectList(this::selectMany, selectList, {{camelCase table.name}}, completer);
}

default List<{{pascalCase table.name}}> selectDistinct(SelectDSLCompleter completer) {
return MyBatis3Utils.selectDistinct(this::selectMany, selectList, {{camelCase table.name}}, completer);
}

default Optional<{{pascalCase table.name}}> selectByPrimaryKey(Integer id_) {
return selectOne(c ->
c.where(id, isEqualTo(id_))
);
}

default int update(UpdateDSLCompleter completer) {
return MyBatis3Utils.update(this::update, {{camelCase table.name}}, completer);
}

static UpdateDSL<UpdateModel> updateAllColumns({{pascalCase table.name}} row, UpdateDSL<UpdateModel> dsl) {
return dsl
{{#each table.columns}}
{{#if (ne name 'id')}}
.set({{camelCase name}}).equalTo(row::get{{upperFirst (camelCase name)}}){{#if @last}};{{/if}}
{{/if}}
{{/each}}
}

static UpdateDSL<UpdateModel> updateSelectiveColumns({{pascalCase table.name}} row, UpdateDSL<UpdateModel> dsl) {
return dsl
{{#each table.columns}}
{{#if (ne name 'id')}}
.set({{camelCase name}}).equalToWhenPresent(row::get{{upperFirst (camelCase name)}}){{#if @last}};{{/if}}
{{/if}}
{{/each}}
}

default int updateByPrimaryKey({{pascalCase table.name}} row) {
return update(c -> c
{{#each table.columns}}
{{#if (ne name 'id')}}
.set({{camelCase name}}).equalTo(row::get{{upperFirst (camelCase name)}})
{{/if}}
{{/each}}
.where(id, isEqualTo(row::getId))
);
}

default int updateByPrimaryKeySelective({{pascalCase table.name}} row) {
return update(c -> c
{{#each table.columns}}
{{#unless isPrimaryKey}}
.set({{camelCase name}}).equalToWhenPresent(row::get{{upperFirst (camelCase name)}})
{{/unless}}
{{/each}}
.where({{> 'id_name.partial.hbs' columns=table.columns}}, isEqualTo(row::getId))
);
}

List<Map<String, Object>> generalSelect(SelectStatementProvider selectStatement);

// @gencoder.block.end: mapper

}
5 changes: 5 additions & 0 deletions e2e/mds/templates/partials/id_name.partial.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#each table.columns}}
{{~#if isPrimaryKey}}
{{name}}
{{~/if}}
{{/each}}
5 changes: 5 additions & 0 deletions e2e/mds/templates/partials/id_type.partial.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#each columns}}
{{~#if isPrimaryKey}}
{{> 'java_type.partial.hbs' columnType=type}}
{{~/if}}
{{/each}}
16 changes: 16 additions & 0 deletions e2e/mds/templates/partials/java_type.partial.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{~#if (match 'varchar\(\d+\)|char|tinytext|text|mediumtext|longtext' columnType)}}String
{{~else if (match 'bigint' columnType)}}Long
{{~else if (match 'int|integer|mediumint' columnType)}}Integer
{{~else if (match 'smallint' columnType)}}Short
{{~else if (match 'tinyint' columnType)}}Byte
{{~else if (match 'bit|bool|boolean' columnType)}}Boolean
{{~else if (match 'decimal' columnType)}}java.math.BigDecimal
{{~else if (match 'float' columnType)}}Double
{{~else if (match 'datetime' columnType)}}java.time.LocalDateTime
{{~else if (match 'date' columnType)}}java.time.LocalDate
{{~else if (match 'time' columnType)}}java.time.LocalTime
{{~else if (match 'timestamp' columnType)}}java.time.LocalDateTime
{{~else if (match 'varbinary' columnType)}}byte[]
{{~else if (match 'enum.*' columnType)}}String
{{~else}}Object
{{~/if}}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/DanielLiu1123/gencoder
go 1.23.1

require (
github.com/dop251/goja v0.0.0-20240828124009-016eb7256539
github.com/dop251/goja v0.0.0-20240919115326-6c7d1df7ff05
github.com/go-sql-driver/mysql v1.8.1
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.23
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yA
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dop251/goja v0.0.0-20240828124009-016eb7256539 h1:YIxvsQAoCLGScK2c9ag+4sFCgiQFpMzywJG6dQZFu9k=
github.com/dop251/goja v0.0.0-20240828124009-016eb7256539/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
github.com/dop251/goja v0.0.0-20240919115326-6c7d1df7ff05 h1:oK4+QcKsczZjHYTHD0JAdkvq5w74JEkG95J0XNBx/BI=
github.com/dop251/goja v0.0.0-20240919115326-6c7d1df7ff05/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
github.com/go-sourcemap/sourcemap v2.1.4+incompatible h1:a+iTbH5auLKxaNwQFg0B+TCYl6lbukKPc7b5x0n1s6Q=
github.com/go-sourcemap/sourcemap v2.1.4+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
Expand Down
Loading