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

goquの導入 #899

Merged
merged 4 commits into from
Dec 30, 2024
Merged

goquの導入 #899

merged 4 commits into from
Dec 30, 2024

Conversation

Kubosaka
Copy link
Collaborator

@Kubosaka Kubosaka commented Dec 25, 2024

https://nut-m-e-g.slack.com/archives/C020WQ3GY07/p1734923921611839

概要

  • sqlを直接記述するのは、sqlインジェクションなどセキュリティ的にも良くないので、クエリビルダーgoquを導入しました。
  • ORMを使うことも有用だったが、既存コードのリファクタリング箇所が大きくなる(dbの接続周りなど)ので、クエリビルダーを使うことで対応します。
  • クエリビルダーにはgoquを使いました
  • sponsorsのみリファクタリングしました

goquの採用理由

テスト項目

  • 変更箇所(/sponsors)のapiが機能するか確認してください

備考

@Kubosaka Kubosaka self-assigned this Dec 25, 2024
Copy link
Collaborator

@hikahana hikahana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goquの構文はよさそう

query := "SELECT * FROM sponsors WHERE id = " + id
dialect := goqu.Dialect("mysql")
query, _, err := dialect.From("sponsors").Where(goqu.Ex{"id": id}).ToSQL()
fmt.Println(query)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fmt.Println(query)

消し忘れっぽそう

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

@Kubosaka Kubosaka Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repositoryパッケージで共有
dialectをrepo内で共有するようにしました

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm
学びです。

sponsors (name, tel, email, address, representative)
VALUES ("` + name + `","` + tel + `","` + email + `","` + address + `","` + representative + `")`
dialect := goqu.Dialect("mysql")
ds := dialect.Insert("sponsors").Rows(goqu.Record{"name": name, "tel": tel, "email": email, "address": address, "representative": representative})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question
dsって何の略称ですか?
dialect sponsorとか?

Copy link
Collaborator Author

@Kubosaka Kubosaka Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なんだろう
公式が使っていたから他に良い名前が思いつかずという感じです
https://github.com/doug-martin/goqu

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

datasetっぽそう

image

sponsors (name, tel, email, address, representative)
VALUES ("` + name + `","` + tel + `","` + email + `","` + address + `","` + representative + `")`
dialect := goqu.Dialect("mysql")
ds := dialect.Insert("sponsors").Rows(goqu.Record{"name": name, "tel": tel, "email": email, "address": address, "representative": representative})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ds := dialect.Insert("sponsors").Rows(goqu.Record{"name": name, "tel": tel, "email": email, "address": address, "representative": representative})
ds := dialect.Insert("sponsors").
Rows(goqu.Record{
"name": name,
"tel": tel,
"email": email,
"address": address,
"representative": representative})

長くなりそうならこんな感じにしてもよさそう
lintされないかな勝手に

Copy link
Collaborator Author

@Kubosaka Kubosaka Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

長くなりそうだなー、
フォーマッターあるならこっちでもいいけど、整形するのにコストかかるな
既存でもそこまで読みにくくないけどどう?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/segmentio/golines
長くなるの折り返しならこれ良さそう

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
innerjoinが若干見切れるのが悲しい気持ちになるけど、だいぶ改善されてよさそう

`", representative = "` + representative +
`" WHERE id = ` + id
dialect := goqu.Dialect("mysql")
ds := dialect.Update("sponsors").Set(goqu.Record{"name": name, "tel": tel, "email": email, "address": address, "representative": representative}).Where(goqu.Ex{"id": id})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ds := dialect.Update("sponsors").Set(goqu.Record{"name": name, "tel": tel, "email": email, "address": address, "representative": representative}).Where(goqu.Ex{"id": id})
ds := dialect.Update("sponsors").
Set(goqu.Record{
"name": name,
"tel": tel,
"email": email,
"address": address,
"representative": representative}).
Where(goqu.Ex{"id": id})

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

years.year = ` + year +
" ORDER BY sponsors.id;"
dialect := goqu.Dialect("mysql")
query, _, err := dialect.Select("sponsors.*").From("sponsors").InnerJoin(goqu.I("year_periods"), goqu.On(goqu.I("sponsors.created_at").Gt(goqu.I("year_periods.started_at")), goqu.I("sponsors.created_at").Lt(goqu.I("year_periods.ended_at")))).InnerJoin(goqu.I("years"), goqu.On(goqu.I("year_periods.year_id").Eq(goqu.I("years.id")))).Where(goqu.Ex{"years.year": year}).Order(goqu.I("sponsors.id").Desc()).ToSQL()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
query, _, err := dialect.Select("sponsors.*").From("sponsors").InnerJoin(goqu.I("year_periods"), goqu.On(goqu.I("sponsors.created_at").Gt(goqu.I("year_periods.started_at")), goqu.I("sponsors.created_at").Lt(goqu.I("year_periods.ended_at")))).InnerJoin(goqu.I("years"), goqu.On(goqu.I("year_periods.year_id").Eq(goqu.I("years.id")))).Where(goqu.Ex{"years.year": year}).Order(goqu.I("sponsors.id").Desc()).ToSQL()
query, _, err := dialect.Select("sponsors.*").
From("sponsors").
InnerJoin(goqu.I("year_periods"),
goqu.On(goqu.I("sponsors.created_at").Gt(goqu.I("year_periods.started_at")),
goqu.I("sponsors.created_at").Lt(goqu.I("year_periods.ended_at")))).InnerJoin(goqu.I("years"),
goqu.On(goqu.I("year_periods.year_id").
Eq(goqu.I("years.id")))).
Where(goqu.Ex{"years.year": year}).
Order(goqu.I("sponsors.id").Desc()).ToSQL()

ここいい感じにならない。。。

Copy link
Collaborator

@hikahana hikahana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

sponsors (name, tel, email, address, representative)
VALUES ("` + name + `","` + tel + `","` + email + `","` + address + `","` + representative + `")`
dialect := goqu.Dialect("mysql")
ds := dialect.Insert("sponsors").Rows(goqu.Record{"name": name, "tel": tel, "email": email, "address": address, "representative": representative})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
innerjoinが若干見切れるのが悲しい気持ちになるけど、だいぶ改善されてよさそう

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm
学びです。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm
run on saveっていう拡張機能なんですね
これ他の人に共有してもいいかも

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mergeされたらslackで共有します
prの備考にも追記しておきました

@Kubosaka Kubosaka merged commit dcc36ad into develop Dec 30, 2024
1 check passed
@Kubosaka Kubosaka deleted the feat/kubosaka/intro-query-builder branch December 30, 2024 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants