Skip to content

Commit

Permalink
Minor cosmetichan PR #14.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Apr 27, 2021
1 parent 03767ff commit 16e030d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
28 changes: 16 additions & 12 deletions backends/sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ const (
dbTypeMysql = "mysql"
)

// Opt represents SQL DB backend's options.
type Opt struct {
DbType string
ResultsTable string
UnloggedTable bool
DBType string
ResultsTable string
UnloggedTables bool
}

// sqlDB represents the sqlDB backend.
type sqlDB struct {
db *sql.DB
opt *Opt
opt Opt
logger *log.Logger

// The result schemas (CREATE TABLE ...) are dynamically
Expand Down Expand Up @@ -58,20 +59,20 @@ type insertSchema struct {

// NewSQLBackend returns a new sqlDB result backend instance.
// It accepts an *sql.DB connection
func NewSQLBackend(db *sql.DB, options *Opt, l *log.Logger) (ResultBackend, error) {
func NewSQLBackend(db *sql.DB, opt Opt, l *log.Logger) (ResultBackend, error) {
var (
r = sqlDB{
db: db,
opt: options,
opt: opt,
resTableSchemas: make(map[string]insertSchema),
schemaMutex: sync.RWMutex{},
logger: l,
}
)

// Config.
if options.ResultsTable != "" {
r.opt.ResultsTable = options.ResultsTable
if opt.ResultsTable != "" {
r.opt.ResultsTable = opt.ResultsTable
} else {
r.opt.ResultsTable = "results_%s"
}
Expand Down Expand Up @@ -120,7 +121,7 @@ func (w *sqlDBWriter) RegisterColTypes(cols []string, colTypes []*sql.ColumnType
colNameHolder[i] = fmt.Sprintf(`"%s"`, w.cols[i])

// This will be filled by the driver.
if w.backend.opt.DbType == dbTypePostgres {
if w.backend.opt.DBType == dbTypePostgres {
// Postgres placeholders are $1, $2 ...
colValHolder[i] = fmt.Sprintf("$%d", i+1)
} else {
Expand Down Expand Up @@ -233,7 +234,7 @@ func (s *sqlDB) createTableSchema(cols []string, colTypes []*sql.ColumnType) ins
colNameHolder[i] = fmt.Sprintf(`"%s"`, cols[i])

// This will be filled by the driver.
if s.opt.DbType == dbTypePostgres {
if s.opt.DBType == dbTypePostgres {
// Postgres placeholders are $1, $2 ...
colValHolder[i] = fmt.Sprintf("$%d", i+1)
} else {
Expand Down Expand Up @@ -264,7 +265,7 @@ func (s *sqlDB) createTableSchema(cols []string, colTypes []*sql.ColumnType) ins
case "BOOLEAN": // Postgres, MySQL
typ = "BOOLEAN"
case "JSON", "JSONB": // Postgres
if s.opt.DbType != dbTypePostgres {
if s.opt.DBType != dbTypePostgres {
typ = "TEXT"
}
// _INT4, _INT8, _TEXT represent array types in Postgres
Expand All @@ -285,7 +286,10 @@ func (s *sqlDB) createTableSchema(cols []string, colTypes []*sql.ColumnType) ins
fields[i] = fmt.Sprintf(`"%s" %s`, cols[i], typ)
}

if s.opt.DbType == dbTypePostgres && s.opt.UnloggedTable {
// If the DB is Postgres, optionally create an "unlogged" table that disables
// WAL, improving performance of throw-away cache tables.
// https://www.postgresql.org/docs/9.1/sql-createtable.html
if s.opt.DBType == dbTypePostgres && s.opt.UnloggedTables {
unlogged = "UNLOGGED"
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/jobber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ func setup() {

var (
opt = backends.Opt{
DbType: cfg.Type,
ResultsTable: ko.String(fmt.Sprintf("results.%s.results_table", dbName)),
UnloggedTable: cfg.Unlogged,
DBType: cfg.Type,
ResultsTable: ko.String(fmt.Sprintf("results.%s.results_table", dbName)),
UnloggedTables: cfg.Unlogged,
}
)

// Create a new backend instance.
backend, err := backends.NewSQLBackend(conn, &opt, sysLog)
backend, err := backends.NewSQLBackend(conn, opt, sysLog)
if err != nil {
sysLog.Fatalf("error initializing result backend: %v", err)
}
Expand Down
14 changes: 6 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,14 @@ func main() {
log.Fatal(err)
}

var (
opt = backends.Opt{
DbType: cfg.Type,
ResultsTable: ko.String(fmt.Sprintf("results.%s.results_table", dbName)),
UnloggedTable: cfg.Unlogged,
}
)
opt := backends.Opt{
DBType: cfg.Type,
ResultsTable: ko.String(fmt.Sprintf("results.%s.results_table", dbName)),
UnloggedTables: cfg.Unlogged,
}

// Create a new backend instance.
backend, err := backends.NewSQLBackend(conn, &opt, sysLog)
backend, err := backends.NewSQLBackend(conn, opt, sysLog)
if err != nil {
log.Fatalf("error initializing result backend: %v", err)
}
Expand Down

0 comments on commit 16e030d

Please sign in to comment.