Skip to content

Commit

Permalink
Merge branch 'main' into apoorvdeshmukh/fix-loc1
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvdeshmukh committed Aug 1, 2023
2 parents f877f65 + f58da1a commit 3d6bf91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
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

0 comments on commit 3d6bf91

Please sign in to comment.