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

api improve suggestion #7

Closed
wongoo opened this issue Oct 24, 2019 · 4 comments
Closed

api improve suggestion #7

wongoo opened this issue Oct 24, 2019 · 4 comments

Comments

@wongoo
Copy link

wongoo commented Oct 24, 2019

Actual API

spo := orchestra2{Cello: "Strad", Bass: "Cecilio"}
query := sx.InsertQuery("symphony", &spo)  // INSERT INTO symphony (cello,contrabass) VALUES (?,?)
tx.MustExec(query, sx.Values(&spo)...)

Expected API

spo := orchestra2{Cello: "Strad", Bass: "Cecilio"}
tx.MustInsert("symphony", &spo)

And the same for update api.

@ders
Copy link
Member

ders commented Oct 24, 2019

Thank you for the suggestion. This is indeed a cleaner API, but it also assumes that you want to regenerate the query for every row inserted, which is often not the case.

As an example, let's suppose we want to read orchestra2 objects from a channel and insert them. The code might look like this:

cacophony := make(chan orchestra2)

...

query := sx.InsertQuery("symphony", &orchestra2{})
tx.MustPrepare(query).Do(func(stmt *sx.Stmt) {
    for o := range cacophony {
        stmt.MustExec(query, sx.Values(&o)...)
    }
})

One could write similar code to insert objects from a slice, from a map, etc., but each use case would be slightly different.

If you find yourself using a particular pattern a lot, then it may be worth writing a helper function, e.g.

func quickInsert(tx *sx.Tx, table string, data interface{}) {
    tx.MustExec(sx.InsertQuery(table, data), sx.Values(data)...)
}

So the question we need to ask is: How useful would this particular helper function be, and should we add it as a method on tx? We'd like to be cautious about cluttering up the API with too many helpers.

@wongoo
Copy link
Author

wongoo commented Oct 25, 2019

@ders so do u suggestion to create the query only once and use it repeatedly? if so, your example may need change to use a global variable, or add some comment to notice. if the query may be created every time when needed , the helper function api could be very useful.

@ders
Copy link
Member

ders commented Oct 25, 2019

It's completely up to you whether you want to create the query once and reuse it, or create the query in each function or method that needs it. (It's just a string, after all!)

The point about MustInsert is this: There are a lot of shortcuts that we could add to make certain usage patterns easier. Choosing which shortcuts to include and which to leave out is often difficult. Our feeling at this point is that MustInsert is not something we need in the library: it wouldn't get used that often, and it's a one-liner to reproduce.

@wongoo
Copy link
Author

wongoo commented Oct 25, 2019

get it, keep it simple, tks~

@wongoo wongoo closed this as completed Oct 25, 2019
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

No branches or pull requests

2 participants