Skip to content

Instantly share code, notes, and snippets.

@edwinlab
Last active April 11, 2019 07:43
Show Gist options
  • Save edwinlab/54f2f73bc1d3e79a7f90e949811414cf to your computer and use it in GitHub Desktop.
Save edwinlab/54f2f73bc1d3e79a7f90e949811414cf to your computer and use it in GitHub Desktop.
package query
import (
"fmt"
"strings"
)
type Builder struct {
table string
query string
}
func Table(name string) *Builder {
return &Builder{table: name}
}
func (b *Builder) Select(q ...string) *Builder {
b.query += fmt.Sprintf("SELECT %s FROM %s WHERE 1 = 1", strings.Join(q, ","), b.table)
return b
}
func (b *Builder) Limit(page, limit int32) *Builder {
if page != 0 && limit != 0 {
b.query += " LIMIT ? OFFSET ?"
}
return b
}
func (b *Builder) Query(sql string, args []interface{}) *Builder {
b.query += sql
return b
}
func (b *Builder) String() string {
return b.query
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment