Giter VIP home page Giter VIP logo

gen's People

Contributors

alfarih31 avatar antengye avatar ch3nnn avatar dawinia avatar dependabot[bot] avatar dino-ma avatar idersec avatar iluyuns avatar jeffmingup avatar jerrdasur avatar jinzhu avatar kaiiak avatar kukuke avatar liloupar avatar lluckyboi avatar ls-2018 avatar miseyu avatar mohenoo avatar myml avatar nm666 avatar normal-coder avatar qqxhb avatar radioinmyhead avatar stellarisw avatar taobig avatar tongyuantongyu avatar tr1v3r avatar unnamed42 avatar wulorn avatar zhouyusd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gen's Issues

DIY method模板的更多语法支持

Describe the feature

希望在DIY method中支持更多语法,例如数组遍历:

type Method interface {
    /* select * from @@table
      {{where}}
          {{ range $idx, $elem := @names }}
              {{ if $idx }} or {{ end }} name={{ $elem }}
          {{ end }}
      {{end}}
    */
    FindByNames(names []string) ([]gen.T, error)
}

如果能像go template一样注册自定义函数就更好了

Motivation

DIY method编写raw sql能力受限

Related Issues

查询条件判断问题

Describe the feature

当前端传递的查询参数不为空时,才查询该字段
如何拼接这种查询条件
尝试过使用变量Do去接收,但是有的返回时表达式Expr,无法使用

Snipaste_2021-12-01_17-23-59

Joins语句中支持select

Describe the feature

希望在关联查询时,可以指定select字段,例如有下面两个表:

type A struct {
	gorm.Model
	B *B
	... // other fields
}

type B struct {
	gorm.Model
	AID uint
	Config string
	... // other fields
}

关联查询b表:

ax := a.WithContext(ctx).Joins(a.B)
ax.Where(a.ID.Eq(1)).Take()

当b表字段较多时,希望select b表的指定字段,例如:

ax := a.WithContext(ctx).Joins(a.B.Select(b.Config))
ax.Where(a.ID.Eq(1)).Take()

Motivation

不支持这样的写法:

ax := a.WithContext(ctx).Select(a.ALL, b.Config).Joins(a.B)
ax.Where(a.ID.Eq(1)).Take()

Related Issues

How to distinguish func for different model in interface Method about DIY method

Your Question

type Method interface {
    // select * from @@table where id=@id
    FindUserById(id int64) (gen.T, error)
}

g.ApplyInterface(
	func(method models.Method) {},
	g.GenerateModel("user"),
	g.GenerateModel("role"),
)

The document you expected this should be explained

I want the interface Method to apply only to user, but now it will generate two func FindUserById applied to user and role, respectively.

Expected answer

How to distinguish func for different model in interface Method

Show some examples

Describe the feature

create examples folder in project

Motivation

Related Issues

Generate exists model only has

Describe the feature

Add exists model only has relationship, and gorm tag should be -

gen.FieldRelateModel("has", "A", model.A{}, &field.RelateConfig{GORMTag: "-"})

Wanna simplify this part of code, and change "has" to constant

Motivation

Related Issues

Simply update multi columns expr

Describe the feature

SQL: update a set b = b + 1, c = c + 1 where d = 4

Code:

Updates(map[string]interface{}{
	q.B.ColumnName().String():     q.B.Add(1)).RawExpr(),
	q.C.ColumnName().String():     q.C.Add(1).RawExpr(),
})

Motivation

Provide a way to simply this action to reduce code

Related Issues

Gorm-Gen支持二进制方式

批量生成数据表(所有表、部分表)的CURD。并支持go generate

提供一个二进制的可执行文件,在当前目录下执行,获取当前目录

通过cmd
传入:dbDSN tableList(为空则默认生成全部DB的) 输出路径(./Dal/query) 等Args来生成Safe CURD

依赖gorm 的go-gorm/gorm#4841

@dino-ma 自己提的Issue 自己解决。

如何组合类似闭包查询的效果

我是用的组合得到如下sql:
q.User.Preload(q.User.UserInfo).
Where(q.User.ID.Eq(1)).
Or(q.User.Username.Eq("a")).
Or(q.User.Token.Eq("b")).
Select(q.User.ID.Sum().As("count")).
Scan(&ud)

SELECT SUM(users.id) AS count FROM users WHERE (users.id = 1 OR users.username = 'a' OR users.token = 'b') AND users.deleted_at IS NULL

但是我想要得到这样的sql:
SELECT SUM(users.id) AS count FROM users WHERE users.id = 1 AND ( users.username = 'a' OR users.token = 'b') AND users.deleted_at IS NULL

将两个or与其余的条件用and连接

`Updates` query conditions overlay

GORM Playground Link

go-gorm/playground#1

Description

u := query.Use(db).User
do := u.WithContext(ctx)

do.Where(u.ID.Eq(1)).Updates(model.User{ID: 1, Name:"first"})
// UPDATE `users` SET `name`='first' WHERE `users`.`id` = 1

do.Where(u.ID.Eq(2)).Updates(model.User{Name:"second"})
// UPDATE `users` SET `name`='second' WHERE `users`.`id` = 1 AND `id` = 2

关于排序的疑问

生成的 GetFieldByName()方法返回的对象为field.Expr

无法调用

func (e expr) Desc() Expr {
	return e.setE(clause.Expr{SQL: "? DESC", Vars: []interface{}{e.RawExpr()}})
}

导致前端发送的对某个字段的Desc排序需求我无法实现,因为获取不到具体的 field.type。

I found that using Where and FindByPage at the same time will panic

Your Question

// gorm version: v1.21.15
// gen version: v0.0.16
user := query.NewUser(db)
_, _, _ = user.Model(&models.User{}).FindByPage(0, 5) // is ok
_, _, _ = user.FindByPage(0, 5) // is ok
_, _, _ = user.Model(&models.User{}).Where(user.ID.In(1, 2)).FindByPage(0, 5) // is ok
_, _, _ = user.Where(user.ID.In(1, 2)).FindByPage(0, 5) // panic

The document you expected this should be explained

I found that using Where and FindByPage at the same time will panic.

Expected answer

I hope to fix it ASAP.

是否可以仅生成 query

Your Question

非常赞的的发现已经加上 example 了, 同时使用了一下, 发现 model 也是一并生成的, 理解这个对于已经存在的使用 gorm 的项目不是很友好? 因为会重新生成 model, 所以想问下是否可以仅生成 query

  • 这里还有个问题, 在现有的项目中我们使用gorm model 有点类似 Django, model 是分散在不同的 app 内的, 如果可以仅生成query 的话, 理解这个问题也会能得到解决

The document you expected this should be explained

Expected answer

generated Do.FindInBatches func parameter issue

In the method func (t tDo) FindInBatches) generated for each of models by the template below:

func ({{.S}} {{.NewStructName}}Do) FindInBatches(result []*{{.StructInfo.Package}}.{{.StructInfo.Type}}, batchSize int, fc func(tx gen.Dao, batch int) error) error {
return {{.S}}.DO.FindInBatches(&result, batchSize, fc)
}

Could the 1st parameter result need to be an pointer to slice? Otherwise the caller can't read the result of each batch.

在where语句中支持raw sql

Describe the feature

希望可以在where语句中支持raw sql查询,例如对于q表:

id config
1 {"id_list": [1,2,3]}
2 {"id_list": [2,3,4]}

执行如下查询:

select * from q where JSON_CONTAINS(q.config, 1, "$.id_list")

一种预期调用方式:

id := 1
cond := q.Config.CondRaw(`JSON_CONTAINS(@col, ?, "$.id_list")`, id)
q.WithContext(ctx).Where(cond).Take()

这样就可以实现复杂条件的组合,例如:

qx := q.WithContext(ctx)
idList := []int{1, 2, 3}
for _, id := range idList {
    cond := q.Config.CondRaw(`JSON_CONTAINS(@col, ?, "$.id_list")`, id)
    qx = qx.Or(cond)
}
qx.Take()

也可以融合到更复杂的join条件中,例如:

px := p.WithContext(ctx).LeftJoin(q, q.ID.EqCol(p.QID))
px = px.Where(p.ID.Eq(1))

idList := []int{1, 2, 3}
for _, id := range idList {
    cond := q.Config.CondRaw(`JSON_CONTAINS(@col, ?, "$.id_list")`, id)
    px = px.Where(cond)
}
px.Take()

Motivation

DIY method不能很好支持复杂SQL查询

Related Issues

关于Query对象的使用

通过 query.Use(db) 拿到的 Query 对象放在全局变量里使用,还是需要在每次使用的时候重新获取一下。
当我将
Query放到全局变量后,有时会在在构建查询时多出一些不在当前查询里的where条件出现。

All in one Field Option

Describe the feature

Provide a field option combine all field options like FieldRename/FieldType/FieldJSONTag...

Motivation

Configure table field more conveniently

Related Issues

Table has datetime filed type generate unit test error

SQL:
": syntax error [0.042ms] [rows:0] CREATE TABLEabc_table (id integer,create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,emr_time datetime NOT NULL DEFAULT 0000-00-00 00:00:00,PRIMARY KEY (id)) Error: AutoMigrate(&model.Diagnose{}) fail: near "-": syntax error=== RUN Test_diagnoseQuery

I think we need to support datetime

能否支持下字段属性的枚举生成

在编码过程中,经常会遇到用某个数值来表示某种状态、类型或者阶段的情况,比如有这样一个枚举:

public enum ComputerState {
    OPEN(10),         //开启
    CLOSE(11),         //关闭
    OFF_LINE(12),     //离线
    FAULT(200),     //故障
    UNKNOWN(255);     //未知

    private int code;
    ComputerState(int code) { this.code = code; }
}

通常我们希望将表示状态的数值存入数据库,即ComputerState.OPEN存入数据库取值为10。

能否通过field comment的中备注,来生成对应枚举值

比如可以约定注释检查规则的正则表达式如下

REMARKS_PATTERN = ".*\\s*\\[\\s*(\\w+\\s*\\(\\s*[\\u4e00-\\u9fa5_\\-a-zA-Z0-9]+\\s*\\)\\s*:\\s*[\\u4e00-\\u9fa5_\\-a-zA-Z0-9]+\\s*\\,?\\s*)+\\s*\\]\\s*.*";
CREATE TABLE `tb` (
  `type` smallint(3) COMMENT '注释[success(0):成功, fail(1):失败]',                       
);

提取出字段属性相应的枚举

type Type int

const (
        Success  Type = 0 //成功
        Fail Type = 1 //失败
)

How to use aggregation function with condition?

Your Question

database:Mysql

i want gen SQL like this:

SELECT
    IFNULL(AVG(IF(ad_d.fix_cost_time > 0, ad_d.duration, NULL)), 0)                AS avg_duration,
    IFNULL(AVG(IF(ad_d.fix_cost_time > 0, ad_d.fix_cost_time, NULL)), 0)           AS avg_fix_cost_time
FROM xxxx
Where 
  -- omit

i write code like:

query.Q.XXXX.WithContext(ctx).Select(XXX.FixCostTime.Avg())......

How do I pass conditions to aggregation function?

希望可以增加一个生成Paginate方法的功能

希望可以增加一个生成Paginate方法的功能
感觉分页数据的查询一直是一个头疼的问题

例如:

 g.ApplyPaginate(&model.User{})

生成的方法:

type Paginator struct {
  TotalItem int 
  TotalPage int 
  Page      int  
  Items     []gen.T 
}

type Param struct{
  Size int
  Page int
}

Paginate(param *Param) (result *Paginator, err error) {}

使用:

u := query.Use(db).User
u.Paginate(&query.Param{Size:10,Page:1})

`gentool` accept config file

Describe the feature

Accept config file and parse config from file. And accept command line parameters in the meanwhile.
Command line has higher priority.

gentool -c ./conf/gen.yml

# specify dsn instead of using configuration in file
gentool -c ./conf/gen.yml -dsn <dsn>

Scopes 多个查询条件只有最后个生效,且重复最后个请求条件

使用 gorm.DB 执行没有问题
func (r Repo) GetSimpleInfo1(ctx context.Context, search ...func(db *gorm.DB) *gorm.DB) ([]*model.Staffs, error) {
db := r.data.GetDb(ctx)
var result []model.Staffs
db.WithContext(ctx).
Scopes(search...).Find(&result)
fmt.Printf("result:%v", result)
return nil, apiErrors.ErrorAuthErr("ssss")
}
sql : SELECT * FROM oa_staffs WHERE ((real_name like '%' or username like '%')) AND id IN (1,2) LIMIT 5

使用gen.Scopes 出现 search 查询条件重复,重复参数对应 search请求次数
func (r Repo) GetStaffsTotal(ctx context.Context, search ...func(dao gen.Dao) gen.Dao) (int32, error) {
this := r.data.Query(ctx).Staffs
total, err := this.WithContext(ctx).Scopes(search...).Count()
if err != nil {
return 0, err
}
return int32(total), nil
}
sql SELECT count(*) FROM oa_staffs WHERE oa_staffs.id IN (1,2) AND oa_staffs.id IN (1,2) AND oa_staffs.id IN (1,2)"

Generation support association

Describe the feature

The association already supports existing models, so how to support generation?

Motivation

Able to complete the use of association

Related Issues

#109

update multiple columns from subquery

Describe the feature

add a new function to support update multiple columns from subquery

Motivation

we always need to update multiple columns of one table and set these columns' value from a subquery, like following sql:
UPDATE table1
SET (column_name1, column_name2... ) = (
SELECT (column_name1, column_name2...)
FROM table2
WHERE column_name = value )
WHERE column_name = value

Related Issues

Separate gorm_generated file?

Describe the feature

Separate gorm_generated.go file into query.go and others.

Motivation

Just want to separate the files for easy opening and viewing, its file row count will expand rapidly as the table grows now

Related Issues

传入*gorm_adapter.GormDB无法读取DB生成代码

对象有效,可以从DB中获取数据。
但是传入gen后,没办法读取表结构。

//查询返回的数据
orderData.ID: 1384821646614859776

//gen执行的过程
2021/11/03 17:47:50.269758 generator.go:262: got 0 columns from table
2021/11/03 17:47:50.320858 generator.go:262: got 0 columns from table <orders_medical_advice>
2021/11/03 17:47:50.336705 generator.go:262: generate query file: /Users/mashengjie03/Work/code/heytea/cmd/dal/query/gen.go
2021/11/03 17:47:50.336723 generator.go:262: Generate code done.

wechat 9393103

try clickhouse database generate has panic

panic: generate struct fail

goroutine 1 [running]:
gorm.io/gen.(*Generator).GenerateModelAs(0xc000166f00, {0xbf0003, 0x31}, {0xc00032b810, 0x0}, {0x0, 0x0, 0xc000322150})
/home/xxx/xxx/go/pkg/mod/gorm.io/[email protected]/generator.go:166 +0x2cd
gorm.io/gen.(*Generator).GenerateModel(0xc000166f00, {0xbf0003, 0x12}, {0x0, 0x0, 0x0})
/home/xxx/xxx/go/pkg/mod/gorm.io/[email protected]/generator.go:150 +0x73
main.main()
/home/xxx/xxx/GolandProjects/sdk-server/cmd/generate/generate.go:19 +0xc5

Does it support clickhouse, i used driver that gorm/clickhouse.

Is there a way to update multi incr/decr fields?

Your Question

SQL: update a set b = b + 1, c = c + 1 where d = 4
I checked the docs and I can't seem to find the relevant method

The document you expected this should be explained

Expected answer

A solution to build

是否可以把单测一起生成了

很多公司都要求单测90%,而其中DB这一层要么使用Mock要么使用SQLLite去写单测,既然已经有了GEN这个基于GORM的工具,那么是否可以把单测也直接搞定。

Associations references

Your Question

how to Override References?

When you assign credit cards to a user, GORM will save the user’s ID into credit cards’ UserNumber field.

type User struct {
gorm.Model
MemberNumber string
CreditCards []CreditCard gorm:"foreignKey:UserNumber"
}

type CreditCard struct {
gorm.Model
Number string
UserNumber string
}

Expected answer

i want able to change it with tag references, e.g:

type User struct {
gorm.Model
MemberNumber string
CreditCards []CreditCard gorm:"foreignKey:UserNumber;references:MemberNumber"
}

type CreditCard struct {
gorm.Model
Number string
UserNumber string
}

FieldRelate

Description

gen.FieldRelate use check.BaseStruct which is in an internal package.
So end users will get use of internal package gorm.io/gen/internal/check not allowed

model生成时,int类型问题

### int类型设置问题,长度不够用

数据库设置的类型为int, 长度为11
生成的model类型为int32

Snipaste_2021-11-12_18-59-34

另外,发现一个单词的bug
image

Why does MySQL report an error when using bool

Your Question

// definition
// ...
Visible   bool `json:"visible" gorm:"column:visible"`
Shareable bool `json:"shareable" gorm:"column:shareable"`
// ...

problem, err := p.Where(p.Visible.Is(true)).Where(p.ID.Eq(id)).First()

err:

Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? AND `problem`.`id` = ? ORDER BY `problem`.`id` LIMIT 1' at line 1

debug sql:

SELECT * FROM `problem` WHERE `problem`.`visible` IS true AND `problem`.`id` = 1 ORDER BY `problem`.`id` LIMIT 1

I use this sql in cli is ok.

Expected answer

How to use bool type in query?

关于之前我说*Query对象使用全局变量,会导致where查询条件叠加的问题复现了

当我使用全局变量*Query是,preload预加载的on条件会错误:

q := query.Use(db)
go func() {
u := q.User
ui := q.UserInfo
ctx1 := context.Background()
  u.WithContext(ctx1).
	  Preload(u.UserInfo.WithContext(ctx1).On(ui.ShopID.Eq(11))).
	  Where(u.ID.Eq(1)).
	  First()
}()
go func() {
  u := q.User
  ui := q.UserInfo
  ctx2 := context.Background()
  u.WithContext(ctx2).
	  Preload(u.UserInfo.WithContext(ctx2).On(ui.ShopID.Eq(12))).
	  Where(u.ID.Eq(2)).
	  First()
}()
这时的sql是这样的:
	SELECT * FROM `user_infos` WHERE `user_infos`.`user_id` = 2 AND `user_infos`.`shop_id` = 12
	SELECT * FROM `users` WHERE `users`.`id` = 2 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
	SELECT * FROM `user_infos` WHERE `user_infos`.`user_id` = 1 AND `user_infos`.`shop_id` = 12 AND `user_infos`.`shop_id` = 11
	SELECT * FROM `users` WHERE `users`.`id` = 1 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
可以看到,其中一个user_infos的查询条件中带入了另一个的条件。
但是当我在每个协程中都重新拿到query对象时就是正常的,比如:
go func() {
  q := query.Use(db)
  u := q.User
  ui := q.UserInfo
  ctx1 := context.Background()
  u.WithContext(ctx1).
	  Preload(u.UserInfo.WithContext(ctx1).On(ui.ShopID.Eq(11))).
	  Where(u.ID.Eq(1)).
	  First()
}()
go func() {
  q := query.Use(db)
  u := q.User
  ui := q.UserInfo
  ctx2 := context.Background()
  u.WithContext(ctx2).
	  Preload(u.UserInfo.WithContext(ctx2).On(ui.ShopID.Eq(12))).
	  Where(u.ID.Eq(2)).
	  First()
}()
这样的话表现就一切正常

Custom tag

Describe the feature

Support adding custom tag, and optional naming strategy(camelcase, underscore etc)

Motivation

Share model to support bson

Related Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.