Raw sql statements

Although qb has a great support for building many different queries, there might be cases where it may be needed to run raw sql statements. qb provides that too. Here's an example

package main

import (
	"fmt"
	"github.com/aacanakin/qb"
)

func main() {

	db, err := qb.New("postgres", "user=postgres dbname=qb_test sslmode=disable")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	statement := qb.Statement()

	sql := `

			SELECT id, name, email
FROM user
				WHERE id = ?

	`

	statement.Text(sql)

	fmt.Println(statement.SQL())

  // execute the select
	var id int
	var name, email string
	db.Engine().DB().QueryRow(statement.SQL(), 5).Scan(&id, &name, &email)

}

📘

The indendation is intentional

The example intentionally defines sql with shitty indentation to make a point.

The output would be;

SELECT id, name, email
FROM user
WHERE id = ?;

Here, you can build the same sql without using Statement() call. However, if you don't use Statement() constructor, your query will have \t \n characters that is not needed. Moreover, Statement() checks if you added ";" character and ensures the sql has ";". You can put any tabs at the start of sql statement. Statement().Text() would clean them for you.