ulule/loukoum

Document usage for database/sql

jawnsy opened this issue ยท 4 comments

Hello,

Thanks for writing this library, it looks great! Is it usable with database/sql directly, rather than sqlx? The way the bind parameters are defined makes it look like this wouldn't work with database/sql, and I can't find any examples that just use database/sql.

novln commented

Hi,

Thanks for writing this library, it looks great!

With pleasure ๐Ÿ˜„

Is it usable with database/sql directly [....]

No you're right, it won't.
This is maybe the most essential part that doesn't work with database/sql. We made this decision because we use exclusively sqlx.

At the moment it's not a priority for us but we'll be glad to review pull request.
However, if there is a good amount of ๐Ÿ‘, I could start working on a solution for it ๐Ÿ™‚

Cheers,

thoas commented

You can still use loukoum with database/sql but without the builder.Prepare() mechanism since it relies on Named queries.

Like so:

// GetUser retrieves user using database/sql.
func GetUser(db *sql.DB, id int) (*User, error) {
	builder := lk.Select("id", "first_name", "last_name", "email").
		From("users").
		Where(lk.Condition("deleted_at").IsNull(true)).Where(lk.Condition("id").Is(lk.Raw("?")))

	user := &User{}
	err := db.QueryRow(builder.String(), id).Scan(user)
	if err != nil {
		return nil, err
	}

	return user, nil
}

It's not documented at the moment but we should.

@thoas the String method should not be used for anything except debugging. See #30

Related to #38