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 1 commit
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
28 changes: 15 additions & 13 deletions pkg/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,24 +327,26 @@ 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
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, ln []byte
shueybubbles marked this conversation as resolved.
Show resolved Hide resolved
)

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
return string(ln), 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