feat: add --batch to support batch mode

main
sudo pacman -Syu 2022-11-07 00:13:18 +07:00
parent 07a4563762
commit 85eede5e8b
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
4 changed files with 25 additions and 7 deletions

View File

@ -12,6 +12,7 @@ type action struct {
url string url string
table string table string
numberRecord int numberRecord int
batchMode bool
verbose bool verbose bool
dryRun bool dryRun bool
} }
@ -26,6 +27,7 @@ func (a *action) getFlags(c *cli.Context) {
a.flags.url = c.String(flagURLName) a.flags.url = c.String(flagURLName)
a.flags.table = c.String(flagTableName) a.flags.table = c.String(flagTableName)
a.flags.numberRecord = c.Int(flagNumberRecordName) a.flags.numberRecord = c.Int(flagNumberRecordName)
a.flags.batchMode = c.Bool(flagBatchModeName)
a.flags.verbose = c.Bool(flagVerboseName) a.flags.verbose = c.Bool(flagVerboseName)
a.flags.dryRun = c.Bool(flagDryRunName) a.flags.dryRun = c.Bool(flagDryRunName)

View File

@ -20,8 +20,14 @@ func (a *action) RunGenerate(c *cli.Context) error {
return fmt.Errorf("populatedb: failed to new populator: %w", err) return fmt.Errorf("populatedb: failed to new populator: %w", err)
} }
if err := populator.Insert(c.Context, a.flags.table, a.flags.numberRecord); err != nil { if a.flags.batchMode {
return fmt.Errorf("populatedb: failed to insert: %w", err) if err := populator.InsertBatch(c.Context, a.flags.table, a.flags.numberRecord); err != nil {
return fmt.Errorf("populatedb: failed to batch: %w", err)
}
} else {
if err := populator.Insert(c.Context, a.flags.table, a.flags.numberRecord); err != nil {
return fmt.Errorf("populatedb: failed to insert: %w", err)
}
} }
return nil return nil

View File

@ -26,6 +26,9 @@ const (
flagNumberRecordName = "number" flagNumberRecordName = "number"
flagNumberRecordUsage = "number of record to generate" flagNumberRecordUsage = "number of record to generate"
flagBatchModeName = "batch"
flagBatchModeUsage = "batch mode, insert data in batch"
flagVerboseName = "verbose" flagVerboseName = "verbose"
flagVerboseUsage = "show what is going on" flagVerboseUsage = "show what is going on"
@ -74,6 +77,10 @@ func NewApp() *App {
Usage: flagNumberRecordUsage, Usage: flagNumberRecordUsage,
Required: true, Required: true,
}, },
&cli.BoolFlag{
Name: flagBatchModeName,
Usage: flagBatchModeUsage,
},
&cli.BoolFlag{ &cli.BoolFlag{
Name: flagVerboseName, Name: flagVerboseName,
Aliases: flagVerboseAliases, Aliases: flagVerboseAliases,

View File

@ -32,6 +32,7 @@ var (
type Populator interface { type Populator interface {
Insert(ctx context.Context, tableName string, numberRecord int) error Insert(ctx context.Context, tableName string, numberRecord int) error
InsertBatch(ctx context.Context, tableName string, numberRecord int) error
} }
type populator struct { type populator struct {
@ -103,6 +104,7 @@ func (p *populator) Insert(ctx context.Context, tableName string, numberRecord i
return err return err
} }
// INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?);
queryInsert := fmt.Sprintf(stmtInsert, queryInsert := fmt.Sprintf(stmtInsert,
tableName, tableName,
strings.Join(columnNames, ", "), strings.Join(columnNames, ", "),
@ -117,7 +119,7 @@ func (p *populator) Insert(ctx context.Context, tableName string, numberRecord i
} }
if p.verbose { if p.verbose {
fmt.Println(i, queryInsert, args) fmt.Printf("Index: [%d], Query: [%s], LenArgs: [%d]\n", i, queryInsert, len(args))
} }
if !p.dryRun { if !p.dryRun {
@ -130,7 +132,7 @@ func (p *populator) Insert(ctx context.Context, tableName string, numberRecord i
return nil return nil
} }
func (p *populator) BatchInsert(ctx context.Context, tableName string, numberRecord int) error { func (p *populator) InsertBatch(ctx context.Context, tableName string, numberRecord int) error {
columnNames, questionMarks, argFns, err := p.prepareInsert(tableName) columnNames, questionMarks, argFns, err := p.prepareInsert(tableName)
if err != nil { if err != nil {
return err return err
@ -142,7 +144,7 @@ func (p *populator) BatchInsert(ctx context.Context, tableName string, numberRec
numberRecordEachBatch := maxQuestionMarks / len(questionMarks) numberRecordEachBatch := maxQuestionMarks / len(questionMarks)
if numberRecordEachBatch == 0 { if numberRecordEachBatch == 0 {
return fmt.Errorf("maxium question marks [%d]: %w", len(questionMarks), ErrMaximumQuestionMarks) return fmt.Errorf("maximum question marks [%d]: %w", len(questionMarks), ErrMaximumQuestionMarks)
} }
numberBatch := numberRecord/numberRecordEachBatch + 1 numberBatch := numberRecord/numberRecordEachBatch + 1
@ -162,6 +164,7 @@ func (p *populator) BatchInsert(ctx context.Context, tableName string, numberRec
argsInsert = append(argsInsert, args...) argsInsert = append(argsInsert, args...)
} }
// INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?);
queryInsert := fmt.Sprintf(stmtInsert, queryInsert := fmt.Sprintf(stmtInsert,
tableName, tableName,
strings.Join(columnNames, ", "), strings.Join(columnNames, ", "),
@ -175,7 +178,7 @@ func (p *populator) BatchInsert(ctx context.Context, tableName string, numberRec
queryInsert, argsInsert := generateQueryArgsInsertFn(numberRecordEachBatch) queryInsert, argsInsert := generateQueryArgsInsertFn(numberRecordEachBatch)
if p.verbose { if p.verbose {
fmt.Println(i, queryInsert, argsInsert) fmt.Printf("Index: [%d], Query: [%s], LenArgs: [%d]\n", i, queryInsert, len(argsInsert))
} }
if !p.dryRun { if !p.dryRun {
@ -190,7 +193,7 @@ func (p *populator) BatchInsert(ctx context.Context, tableName string, numberRec
queryInsert, argsInsert := generateQueryArgsInsertFn(numberRecordLastBatch) queryInsert, argsInsert := generateQueryArgsInsertFn(numberRecordLastBatch)
if p.verbose { if p.verbose {
fmt.Println(numberBatch-1, queryInsert, argsInsert) fmt.Printf("Index: [%d], Query: [%s], LenArgs: [%d]\n", numberBatch-1, queryInsert, len(argsInsert))
} }
if !p.dryRun { if !p.dryRun {