-
Notifications
You must be signed in to change notification settings - Fork 5
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 ON CONFLICT DO NOTHING for insert #6
Conversation
Can @mstg also review this? |
I'm not fully against merging this, just not sure if introducing further operations is the best way to go. What if we used something like
That way we could continue using @JasonYangShadow @WestleyK What do you think? We could just merge this too and get back to it later. This library is due for some changes that is going to simplify its use but that is going to be long term and gradual. I'm OK with both approaches at the moment |
@mstg I like that approach better. I think we can include that in this PR if its okay with @JasonYangShadow. |
c09f067
to
bddf2a0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple small comments, otherwise looks good.
pika_psql.go
Outdated
@@ -67,6 +67,10 @@ type basePsql[T any] struct { | |||
psql *PostgreSQL | |||
} | |||
|
|||
type PikaOption byte | |||
|
|||
const InsertOnConflictionDoNothing PikaOption = (iota + 1) << 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if this is intended to be a bitbask type option(s), I think this byte shift will collide, we can use this instead for unique bitmask values:
1 << iota
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, this one looks better. thanks!
pika_psql.go
Outdated
@@ -67,6 +67,10 @@ type basePsql[T any] struct { | |||
psql *PostgreSQL | |||
} | |||
|
|||
type PikaOption byte |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to call this PikaCreateOption
? This is specifically create options, this is a very generic name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
though I feel like this originally is designed for any pika option. but PikaCreateOption
looks more clear in this case
pika_psql.go
Outdated
b.ignoreOrderBy = origIgnoreOrderBy | ||
|
||
// Execute query | ||
err := b.psql.Queryable().Get(x, q, args...) | ||
if err != nil { | ||
// ignore no rows in resultset error when ignoreConflict is set to true, this is a normal case | ||
if errors.Is(err, sql.ErrNoRows) && insertOnConflict(options...) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a suggestion, maybe you could do something like this:
if errors.Is(err, sql.ErrNoRows) && getOptions(opts...)&InsertOnConflictionDoNothing > 0 {
...
func getOptions(opts ...PikaOption) PikaOption {
o := 0
if opts == nil {
return o
}
for _, opt := range opts {
o = o | opt
}
return o
}
You dont need to change this, just thinking, this could make it easier when (if) theres lots of options, since we dont need a dedicated function to return each option.
bddf2a0
to
d73c6ba
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, can @mstg take another look?
@JasonYangShadow Could you rename |
Signed-off-by: jason yang <[email protected]>
d73c6ba
to
9c37a40
Compare
Thanks! done rename. |
Awesome, thank you! |
in this PR, I added a new method named
CreateIgnore
, which will addON CONFLICT DO NOTHING
toINSERT
query (https://www.postgresql.org/docs/current/sql-insert.html), it'll help avoid returning the duplicated key violation error thus application does not need to do this check at each place whereCreate
method is called.