SQL query builder for Go. Might also be used as an extension for Squirrel.
Go to file
Reinaldy Rafli 9c7a6285ca docs: correct example with current feature 2021-07-21 16:00: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: correct example with current feature 2021-07-21 16:00:05 +07:00
bob.go chore: error definition var 2021-07-21 15:59:53 +07:00
create.go feat: initial column definition types 2021-07-21 14:50:05 +07:00
create_test.go test: more tests 2021-07-21 15:35:57 +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

Think of this as an extension of Squirrel with functionability like Knex. I still use Squirrel for other types of queries (insert, select, and all that), but I needed some SQL builder for create table and some other stuffs.

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 == bob.ErrEmptyTablePg {
      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").
      IntegerColumn("id", "PRIMARY KEY", "SERIAL").
      StringColumn("name", "NOT NULL").
      TextColumn("password", "NOT NULL").
      DateColumn("created_at").
      ToSql()
    if err != nil {
      log.Fatal(err)
    }

    _, 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").
      UUIDColumn("id", "PRIMARY KEY").
      IntegerColumn("userID", "FOREIGN KEY REFERENCES users(id)").
      JSONColumn("items").
      IntegerColumn("quantity").
      ToSql()
    if err != nil {
      log.Fatal(err)
    }
    
    _, err = db.Query(context.Background(), inventoryQuery[i])
    if err != nil {
      log.Fatal(err)
    }
  }
}

Features

  • bob.CreateTable(tableName) - Basic SQL create table
  • bob.CreateTableIfNotExists(tableName) - Create table if not exists
  • bob.HasTable(tableName) - Checks if column exists (return error if false, check example above for error handling)
  • bob.HasColumn(columnName) - Check if a column exists on current table

TODO

Meaning these are some ideas for the future development of Bob.

  • bob.DropTable(tableName) - Drop a table (drop table "users")
  • bob.DropTableIfExists(tableName) - Drop a table if exists (drop table if exists "users")
  • bob.RenameTable(tableName) - Rename a table (rename table "users" to "old_users")
  • bob.Truncate(tableName) - Truncate a table (truncate "users")
  • bob.Upsert(tableName) - UPSERT function (insert into "users" ("name", "email") values (?, ?) on duplicate key update email = ?)
  • bob.ExecWith() - Just like Squirrel's ExecWith
  • bob.Count(tableName, columnName) - Count query (select count("active") from "users")

Contributing

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

License

Bob is licensed under MIT license