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

Support generate sql for postgres #6

Open
miramir opened this issue Jul 17, 2021 · 6 comments
Open

Support generate sql for postgres #6

miramir opened this issue Jul 17, 2021 · 6 comments

Comments

@miramir
Copy link

miramir commented Jul 17, 2021

In Postgres substitutions params look like $1. In this library only "?"

@timsolov
Copy link
Owner

@miramir hi, what kind of library do you use to interact with Postgres? Some ORM ?

@miramir
Copy link
Author

miramir commented Jul 19, 2021

I use in pgx.

@timsolov
Copy link
Owner

@miramir The library is agnostic for driver so default placeholders for SQL parameters is ? (question symbol).
So, you can use something like this func for your purpose:

package main

import (
	"fmt"
	"strconv"
	"strings"
)

const (
	UNKNOWN = iota
	QUESTION
	DOLLAR
	NAMED
	AT
)

// Rebind a query from the default bindtype (QUESTION) to the target bindtype.
func Rebind(bindType int, query string) string {
	switch bindType {
	case QUESTION, UNKNOWN:
		return query
	}

	// Add space enough for 10 params before we have to allocate
	rqb := make([]byte, 0, len(query)+10)

	var i, j int

	for i = strings.Index(query, "?"); i != -1; i = strings.Index(query, "?") {
		rqb = append(rqb, query[:i]...)

		switch bindType {
		case DOLLAR:
			rqb = append(rqb, '$')
		case NAMED:
			rqb = append(rqb, ':', 'a', 'r', 'g')
		case AT:
			rqb = append(rqb, '@', 'p')
		}

		j++
		rqb = strconv.AppendInt(rqb, int64(j), 10)

		query = query[i+1:]
	}

	return string(append(rqb, query...))
}

func main() {
	fmt.Println(Rebind(DOLLAR, "SELECT * FROM table WHERE id = ? AND uid = ?"))
}

This func is taken from the sqlx library.

@miramir
Copy link
Author

miramir commented Jul 22, 2021

The problem with this solution is: what if I want to find a string in the database with the symbol '?' ?

@miramir
Copy link
Author

miramir commented Jul 22, 2021

There was even an idea to take only url parsing from your library , and give the query construction https://pkg.go.dev/github.com/huandu/go-sqlbuilder . But for now, I'm just wasting time tormented by perfectionism. :)

@timsolov
Copy link
Owner

The problem with this solution is: what if I want to find a string in the database with the symbol '?' ?

Then you've to add feature to escape '?' in this func.

There was even an idea to take only url parsing from your library , and give the query construction https://pkg.go.dev/github.com/huandu/go-sqlbuilder . But for now, I'm just wasting time tormented by perfectionism. :)

Good idea, but it's not necessary for my purposes. But you can fork repo and implement your dreams ;-)

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