Skip to content

Commit

Permalink
fix load sql
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Jun 5, 2024
1 parent 9808e78 commit 31ac0d9
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions load_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package toolbox

import (
"bufio"
"errors"
"fmt"
"os"
"strings"
)
Expand All @@ -19,9 +19,9 @@ func (t *Tools) LoadSQLQueries(fileName string) (map[string]string, error) {
if err != nil {
return query, err
}
defer func(file *os.File) {
defer func() {
_ = file.Close()
}(file)
}()

query, err = parseSQLQueries(file, query)
return query, err
Expand All @@ -31,23 +31,25 @@ func (t *Tools) LoadSQLQueries(fileName string) (map[string]string, error) {
func parseSQLQueries(file *os.File, query map[string]string) (map[string]string, error) {
scanner := bufio.NewScanner(file)
var key string
var queries []string
var queryBuilder strings.Builder
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if isSQLQuery(line) || len(key) > 0 {
if len(key) > 0 {
queries = append(queries, line)
if strings.HasSuffix(line, ";") {
query[key] = strings.Join(queries, " ")
key, queries = "", nil
queryBuilder.WriteString(line)
query[key] = queryBuilder.String()
key, queryBuilder = "", strings.Builder{}
} else {
queryBuilder.WriteString(line + " ")
}
} else {
key = extractKey(line)
}
}
}
if err := scanner.Err(); err != nil {
return query, errors.New("error reading file: " + err.Error())
return query, fmt.Errorf("error reading file: %w", err)
}
return query, nil
}
Expand Down

0 comments on commit 31ac0d9

Please sign in to comment.