SQL query builder for Go. Might also be used as an extension for Squirrel.
Go to file
Reinaldy Rafli ca2d51bc7a feat: initial column definition types 2021-07-21 14:50:05 +07:00
.github fix: replacePlaceholder now do things properly 2021-06-26 09:57:08 +07:00
util refactor: moved args to util 2021-07-09 13:39:13 +07:00
.gitignore feat: initial 2021-06-24 14:43:28 +07:00
LICENSE feat: initial 2021-06-24 14:43:28 +07:00
README.md docs: examples for create table if not exists 2021-07-09 13:47:26 +07:00
bob.go fix(breaking): ToSQL > ToSql for compat with Squirrel 2021-07-09 13:41:34 +07:00
create.go feat: initial column definition types 2021-07-21 14:50:05 +07:00
create_test.go feat: initial column definition types 2021-07-21 14:50:05 +07:00
go.mod feat: initial 2021-06-24 14:43:28 +07:00
go.sum feat: initial 2021-06-24 14:43:28 +07:00
has.go fix(breaking): ToSQL > ToSql for compat with Squirrel 2021-07-09 13:41:34 +07:00
has_test.go fix(breaking): ToSQL > ToSql for compat with Squirrel 2021-07-09 13:41:34 +07:00
placeholder.go fix: replacePlaceholder now do things properly 2021-06-26 09:57:08 +07:00
placeholder_test.go fix: replacePlaceholder now do things properly 2021-06-26 09:57:08 +07:00

README.md

Bob - SQL Query Builder

Go Reference Go Report Card GitHub CodeFactor codecov Codacy Badge Build test Test and coverage

I really need a create table SQL builder, and I can't find one. So, like everything else, I made one. Heavily inspired by Squirrel and Knex. And of course, I still use Squirrel for other types of queries (insert, select, and all), think this package as an extension for Squirrel.

Oh, and of course, heavily inspired by Bob the Builder.

import "github.com/aldy505/bob"

Usage

It's not ready for production yet. But, the API is probably close to how you'd do things on Squirrel. This is an example for using with pgx.

import (
  "context"
  "log"
  "strings"

  "github.com/aldy505/bob"
  "github.com/jackc/pgx/v4"
)

func main() {
  db := pgx.Connect()

  // Check if a table is exists
  sql, args, err = bob.HasTable("users").PlaceholderFormat(bob.Dollar).ToSql()
  if err != nil {
    log.Fatal(err)
  }

  var hasTableUsers bool
  err = db.QueryRow(context.Background(), sql, args...).Scan(&hasTableUsers)
  if err != nil {
    if err.Error() == "no rows in result set" {
      hasTableUsers = false
    } else {
      log.Fatal(err)
    }
  }

  if !hasTableUsers {
    // Create "users" table
    // Note that this will return multiple query in a single string.
    sql, _, err := bob.CreateTable("users").
      Columns("id", "email", "name", "password", "date").
      Types("varchar(36)", "varchar(255)", "varchar(255)", "text", "date").
      Primary("id").
      Unique("email")
      ToSql()
    if err != nil {
      log.Fatal(err)
    }

    // If you don't do this, you will get the error:
    // ERROR: cannot insert multiple commands into a prepared statement (SQLSTATE 42601)
    splitQuery := strings.Split(sql, ";")
    for i := range splitQuery {
      _, err = db.Query(context.Background(), splitQuery[i])
      if err != nil {
        log.Fatal(err)
      }
    }

    // Create another table, this time with CREATE TABLE IF NOT EXISTS
    sql, _, err := bob.CreateTableIfNotExists("inventory").
      Columns("id", "userID", "items", "quantity").
      Types("varchar(36)", "varchar(36)", "json", "int").
      Primary("id").
      ToSql()
    if err != nil {
      log.Fatal(err)
    }
    
    inventoryQuery := strings.Split(sql, ";")
    for i := range inventoryQuery {
      _, err = db.Query(context.Background(), inventoryQuery[i])
      if err != nil {
        log.Fatal(err)
      }
    }
  }
}

Contributing

Contributions are always welcome! As long as you add a test for your changes.

License

Bob is licensed under MIT license