Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Use proper reader for long file lines #441

Merged
merged 3 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions pkg/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ var (
}
)

const maxLineBuffer = 2 * 1024 * 1024 // 2Mb

// Console defines methods used for console input and output
type Console interface {
// Readline returns the next line of input.
Expand Down Expand Up @@ -327,24 +325,29 @@ func (s *Sqlcmd) IncludeFile(path string, processAll bool) error {
b := s.batch.batchline
utf16bom := unicode.BOMOverride(unicode.UTF8.NewDecoder())
unicodeReader := transform.NewReader(f, utf16bom)
scanner := bufio.NewScanner(unicodeReader)
buf := make([]byte, maxLineBuffer)
scanner.Buffer(buf, maxLineBuffer)
scanner := bufio.NewReader(unicodeReader)
curLine := s.batch.read
echoFileLines := s.echoFileLines
ln := make([]byte, 0, 2*1024*1024)
s.batch.read = func() (string, error) {
if !scanner.Scan() {
err := scanner.Err()
if err == nil {
return "", io.EOF
}
return "", err
var (
isPrefix bool = true
err error = nil
line []byte
)

for isPrefix && err == nil {
line, isPrefix, err = scanner.ReadLine()
ln = append(ln, line...)
}
t := scanner.Text()
if echoFileLines {
_, _ = s.GetOutput().Write([]byte(s.Prompt() + t + SqlcmdEol))
if err == nil && echoFileLines {
_, _ = s.GetOutput().Write([]byte(s.Prompt()))
_, _ = s.GetOutput().Write(ln)
_, _ = s.GetOutput().Write([]byte(SqlcmdEol))
}
return t, nil
t := string(ln)
ln = ln[:0]
return t, err
}
err = s.Run(false, processAll)
s.batch.read = curLine
Expand Down
16 changes: 16 additions & 0 deletions pkg/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,22 @@ func TestSqlCmdOutputAndError(t *testing.T) {
}
}

func TestVeryLongLineInFile(t *testing.T) {
s, buf := setupSqlCmdWithMemoryOutput(t)
val := strings.Repeat("a1b", (3*1024*1024)/3)
line := "set nocount on" + SqlcmdEol + "select('" + val + "')"
file, err := os.CreateTemp("", "sqlcmdlongline")
assert.NoError(t, err, "os.CreateTemp")
defer os.Remove(file.Name())
_, err = file.WriteString(line)
assert.NoError(t, err, "Unable to write temp file")
err = s.IncludeFile(file.Name(), true)
if assert.NoError(t, err, "runSqlCmd") {
actual := strings.TrimRight(buf.buf.String(), "\r\n")
assert.Equal(t, val, actual, "Query result")
}
}

// runSqlCmd uses lines as input for sqlcmd instead of relying on file or console input
func runSqlCmd(t testing.TB, s *Sqlcmd, lines []string) error {
t.Helper()
Expand Down
Loading