added ? placeholder processing

This commit is contained in:
2025-10-12 23:47:10 +03:00
parent 4ecc170179
commit b104ff31b6
3 changed files with 37 additions and 14 deletions

View File

@@ -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
@@ -77,11 +79,11 @@ func (b *Batcher) BuildBatchQuery(singleQuery string, batchSize int, batchedItem
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")
} }

View File

@@ -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

2
go.mod
View File

@@ -1,3 +1,3 @@
module btchrr module btchrr
go 1.25.1 go 1.25.2