feat: add --batch to support batch mode
parent
07a4563762
commit
85eede5e8b
|
@ -12,6 +12,7 @@ type action struct {
|
|||
url string
|
||||
table string
|
||||
numberRecord int
|
||||
batchMode bool
|
||||
verbose bool
|
||||
dryRun bool
|
||||
}
|
||||
|
@ -26,6 +27,7 @@ func (a *action) getFlags(c *cli.Context) {
|
|||
a.flags.url = c.String(flagURLName)
|
||||
a.flags.table = c.String(flagTableName)
|
||||
a.flags.numberRecord = c.Int(flagNumberRecordName)
|
||||
a.flags.batchMode = c.Bool(flagBatchModeName)
|
||||
|
||||
a.flags.verbose = c.Bool(flagVerboseName)
|
||||
a.flags.dryRun = c.Bool(flagDryRunName)
|
||||
|
|
|
@ -20,8 +20,14 @@ func (a *action) RunGenerate(c *cli.Context) error {
|
|||
return fmt.Errorf("populatedb: failed to new populator: %w", err)
|
||||
}
|
||||
|
||||
if err := populator.Insert(c.Context, a.flags.table, a.flags.numberRecord); err != nil {
|
||||
return fmt.Errorf("populatedb: failed to insert: %w", err)
|
||||
if a.flags.batchMode {
|
||||
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
|
||||
|
|
|
@ -26,6 +26,9 @@ const (
|
|||
flagNumberRecordName = "number"
|
||||
flagNumberRecordUsage = "number of record to generate"
|
||||
|
||||
flagBatchModeName = "batch"
|
||||
flagBatchModeUsage = "batch mode, insert data in batch"
|
||||
|
||||
flagVerboseName = "verbose"
|
||||
flagVerboseUsage = "show what is going on"
|
||||
|
||||
|
@ -74,6 +77,10 @@ func NewApp() *App {
|
|||
Usage: flagNumberRecordUsage,
|
||||
Required: true,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: flagBatchModeName,
|
||||
Usage: flagBatchModeUsage,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: flagVerboseName,
|
||||
Aliases: flagVerboseAliases,
|
||||
|
|
|
@ -32,6 +32,7 @@ var (
|
|||
|
||||
type Populator interface {
|
||||
Insert(ctx context.Context, tableName string, numberRecord int) error
|
||||
InsertBatch(ctx context.Context, tableName string, numberRecord int) error
|
||||
}
|
||||
|
||||
type populator struct {
|
||||
|
@ -103,6 +104,7 @@ func (p *populator) Insert(ctx context.Context, tableName string, numberRecord i
|
|||
return err
|
||||
}
|
||||
|
||||
// INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?);
|
||||
queryInsert := fmt.Sprintf(stmtInsert,
|
||||
tableName,
|
||||
strings.Join(columnNames, ", "),
|
||||
|
@ -117,7 +119,7 @@ func (p *populator) Insert(ctx context.Context, tableName string, numberRecord i
|
|||
}
|
||||
|
||||
if p.verbose {
|
||||
fmt.Println(i, queryInsert, args)
|
||||
fmt.Printf("Index: [%d], Query: [%s], LenArgs: [%d]\n", i, queryInsert, len(args))
|
||||
}
|
||||
|
||||
if !p.dryRun {
|
||||
|
@ -130,7 +132,7 @@ func (p *populator) Insert(ctx context.Context, tableName string, numberRecord i
|
|||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -142,7 +144,7 @@ func (p *populator) BatchInsert(ctx context.Context, tableName string, numberRec
|
|||
|
||||
numberRecordEachBatch := maxQuestionMarks / len(questionMarks)
|
||||
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
|
||||
|
@ -162,6 +164,7 @@ func (p *populator) BatchInsert(ctx context.Context, tableName string, numberRec
|
|||
argsInsert = append(argsInsert, args...)
|
||||
}
|
||||
|
||||
// INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?);
|
||||
queryInsert := fmt.Sprintf(stmtInsert,
|
||||
tableName,
|
||||
strings.Join(columnNames, ", "),
|
||||
|
@ -175,7 +178,7 @@ func (p *populator) BatchInsert(ctx context.Context, tableName string, numberRec
|
|||
queryInsert, argsInsert := generateQueryArgsInsertFn(numberRecordEachBatch)
|
||||
|
||||
if p.verbose {
|
||||
fmt.Println(i, queryInsert, argsInsert)
|
||||
fmt.Printf("Index: [%d], Query: [%s], LenArgs: [%d]\n", i, queryInsert, len(argsInsert))
|
||||
}
|
||||
|
||||
if !p.dryRun {
|
||||
|
@ -190,7 +193,7 @@ func (p *populator) BatchInsert(ctx context.Context, tableName string, numberRec
|
|||
queryInsert, argsInsert := generateQueryArgsInsertFn(numberRecordLastBatch)
|
||||
|
||||
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 {
|
||||
|
|
Loading…
Reference in New Issue