Skip to content

Conversation

myml
Copy link
Contributor

@myml myml commented Aug 7, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

给 field.String 添加了 Lower 方法 和 Upper 方法,个人认为他们虽然是两个方法,但功能类似,是作用相反的一对,所以放到一个 pr 中了

添加这两个方法的原因是目前 gen 好像无法方便的使用除生成外的其他 sql 方法

以下是我在Issues搜索时看到的,作为参考

二是 Lower 和 Upper 在 sql-92 compatible 中有定义,有广泛的兼容性

User Case Description

在使用 like 搜索的场景时,经常会有“不区分大小写”的场景,除了使用各sql方言的特殊方法,比如 postgres 的 'ilike',另一种简单粗暴的方法就是用 Lower 将关键词和内容都转为小写,以下是该方法使用例子

keyword := fmt.Sprintf("%s%%", strings.ToLower(opt.Keyword))
count, err := user.WithContext(ctx).
    Where(user.Nickname.Lower().Like(keyword)).
    Or(user.Username.Lower().Like(keyword)).
    FindByPage(opt.Offset, opt.Limit)

@jinzhu jinzhu merged commit 03f8b50 into go-gorm:master Aug 16, 2023
jeepc pushed a commit to jeepc/gen that referenced this pull request Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants