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

queryx: unset empty values #206

Merged
merged 5 commits into from
Nov 26, 2021
Merged

Conversation

N1cOs
Copy link
Contributor

@N1cOs N1cOs commented Nov 20, 2021

Resolve #202

@N1cOs N1cOs changed the title #202 queryx: unset empty values queryx: unset empty values Nov 20, 2021
Makefile Outdated
@@ -13,7 +13,7 @@ GOTEST_CPU := 1
endif

ifndef GOBIN
GOBIN := $(GOPATH)/bin
GOBIN := $(shell go env GOPATH)/bin
Copy link
Contributor

Choose a reason for hiding this comment

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

If missing, let's define GOPATH like GOBIN.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

example_test.go Outdated
@@ -106,17 +107,30 @@ func basicCreateAndPopulateKeyspace(t *testing.T, session gocqlx.Session) {

// Insert song using query builder.
insertSong := qb.Insert("examples.songs").
Columns("id", "title", "album", "artist", "tags", "data").Query(session)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add new examples if appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remained your insert statement, just save it in the separate variable. I suppose it's a good example how one statement can be used in situations with all filled params and partially filled.

If you think it's inappropriate in this example, I can remove it.

Copy link
Contributor

@mmatczuk mmatczuk Nov 24, 2021

Choose a reason for hiding this comment

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

It breaks the convention and history.
Feel free to copy the example and fully adjust it to present your usecase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, added separate example

transformer.go Outdated
)

// bindTransformer is called right before binding a value to a named parameter.
type bindTransformer func(name string, val interface{}) interface{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be exported.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can leave is as Transformer, it looks generic enough to use it with parsing rows if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Named it as Transformer

transformer.go Outdated
// UnsetEmpty unsets all empty parameters.
// It helps to avoid tombstones when using the same insert/update
// statement for filled and partially filled parameters.
var UnsetEmpty = func(name string, val interface{}) interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be more explicit that this is a transformer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initially I though it would be some redundancy in word transformer but ok

queryx.go Outdated
@@ -136,7 +144,7 @@ func (q *Queryx) BindStructMap(arg0 interface{}, arg1 map[string]interface{}) *Q
return q
}

func bindStructArgs(names []string, arg0 interface{}, arg1 map[string]interface{}, m *reflectx.Mapper) ([]interface{}, error) {
func bindStructArgs(names []string, arg0 interface{}, arg1 map[string]interface{}, m *reflectx.Mapper, tr bindTransformer) ([]interface{}, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It already has 2 Query fields passed in let's make it a method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@mmatczuk mmatczuk left a comment

Choose a reason for hiding this comment

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

Some comments inline, overall it looks ok.

I'd suggest you add a var DefaultBindTransformer similar to DefaultUnsafe.
This way it can be applied to the whole app without setting it each time.

// Transformer transforms the value of the named parameter to another value.
type Transformer func(name string, val interface{}) interface{}

// DefaultBindTransformer just do nothing.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It sounds a bit strange but I couldn't come up with anything better :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I set it as nil to save compatibility with older versions. Maybe someone uses insert/update statements with nil values to explicitly (or implicitly, who knows) delete columns

@N1cOs N1cOs requested a review from mmatczuk November 23, 2021 19:29
@mmatczuk
Copy link
Contributor

All is good let's fix the example. The current example is called basicCreateAndPopulateKeyspace please add something proper so that folks can grasp the idea.

@mmatczuk mmatczuk merged commit e502c7c into scylladb:master Nov 26, 2021
@mmatczuk
Copy link
Contributor

Thanks for the contribution!

@N1cOs N1cOs deleted the queryx/unset_values branch November 26, 2021 09:48
@N1cOs
Copy link
Contributor Author

N1cOs commented Nov 26, 2021

@mmatczuk can we tag a new release, so we can update our dependencies?

@mmatczuk
Copy link
Contributor

Let's do that.

@mmatczuk
Copy link
Contributor

Here it is @N1cOs https://github.com/scylladb/gocqlx/releases/tag/v2.6.0

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.

using of gocql.UnsetValue
2 participants