added ? placeholder processing
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package batcher
|
package batcher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"btchrr/models"
|
"strings"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"btchrr/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Batcher - encapsulates the batch size for splitting a slice into batches
|
// Batcher - encapsulates the batch size for splitting a slice into batches
|
||||||
@@ -74,14 +76,14 @@ func (b *Batcher) BuildBatchQuery(singleQuery string, batchSize int, batchedItem
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return []models.BatchedQuery{}, err
|
return []models.BatchedQuery{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch placeholder {
|
switch placeholder {
|
||||||
case "?":
|
case "?":
|
||||||
return b.buildSqliteQuery(singleQuery, batchSize, batchedItems)
|
return b.buildSqliteQuery(singleQuery, batchedItems)
|
||||||
case "$":
|
case "$":
|
||||||
return b.buildPostgresQuery(singleQuery, batchSize, batchedItems)
|
return b.buildPostgresQuery(singleQuery, batchedItems)
|
||||||
default:
|
default:
|
||||||
return b.buildQueryWithNamedPlaceholders(singleQuery, batchSize, placeholder, batchedItems)
|
return b.buildQueryWithNamedPlaceholders(singleQuery, placeholder, batchedItems)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,14 +97,34 @@ func (b *Batcher) detectPlaceholders(query string) (string, error) {
|
|||||||
return "", models.ErrCannotDetectPlaceholder
|
return "", models.ErrCannotDetectPlaceholder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Batcher) buildSqliteQuery(singleQuery string, batchSize int, batchedItems [][]any) (batchedQueries []models.BatchedQuery, err error) {
|
func (b *Batcher) buildSqliteQuery(singleQuery string, batchedItems [][]any) (batchedQueries models.BatchedQuery, err error) {
|
||||||
|
// INSERT INTO users VALUES (?, ?);
|
||||||
|
idx := strings.Index(strings.ToLower(singleQuery), "values")
|
||||||
|
if idx == -1 {
|
||||||
|
return "", errors.New("no values found in query")
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix := singleQuery[:idx+6] // +6 - values
|
||||||
|
valuesPart := singleQuery[idx+6:]
|
||||||
|
|
||||||
|
numOfInserts := strings.Count(valuesPart, "?")
|
||||||
|
if numOfInserts == 0 {
|
||||||
|
return "", errors.New("no placeholders found in query")
|
||||||
|
}
|
||||||
|
|
||||||
|
singleItemValues := "(" + strings.Repeat("?, ", numOfInserts - 1) + "?)"
|
||||||
|
//INSERT INTO users (name, age) VALUES (?, ?), (?, ?), (?, ?);
|
||||||
|
newValues := strings.Repeat(singleItemValues + ", ", b.batchSize - 1) + singleItemValues
|
||||||
|
newQuery := prefix + " " + newValues + ";"
|
||||||
|
|
||||||
|
|
||||||
|
return models.BatchedQuery(newQuery), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Batcher) buildPostgresQuery(singleQuery string, batchedItems [][]any) (batchedQueries []models.BatchedQuery, err error) {
|
||||||
return []models.BatchedQuery{}, errors.New("not implemented")
|
return []models.BatchedQuery{}, errors.New("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Batcher) buildPostgresQuery(singleQuery string, batchSize int, batchedItems [][]any) (batchedQueries []models.BatchedQuery, err error) {
|
func (b *Batcher) buildQueryWithNamedPlaceholders(singleQuery string, placeholder string, batchedItems [][]any) (batchedQueries []models.BatchedQuery, err error) {
|
||||||
return []models.BatchedQuery{}, errors.New("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Batcher) buildQueryWithNamedPlaceholders(singleQuery string, batchSize int, placeholder string, batchedItems [][]any) (batchedQueries []models.BatchedQuery, err error) {
|
|
||||||
return []models.BatchedQuery{}, errors.New("not implemented")
|
return []models.BatchedQuery{}, errors.New("not implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
package Btchrr
|
package Btchrr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"btchrr/batcher"
|
|
||||||
"btchrr/dbadapter"
|
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
"btchrr/models"
|
"btchrr/models"
|
||||||
|
"btchrr/batcher"
|
||||||
|
"btchrr/dbadapter"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Btchrr - main struct for the package
|
// Btchrr - main struct for the package
|
||||||
type Btchrr struct {
|
type Btchrr struct {
|
||||||
batcher *batcher.Batcher
|
batcher *batcher.Batcher
|
||||||
executor Executor
|
executor Executor
|
||||||
|
//TODO: transactional bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executor - interface for the sql execution
|
// Executor - interface for the sql execution
|
||||||
|
|||||||
Reference in New Issue
Block a user