-
Notifications
You must be signed in to change notification settings - Fork 82
Add ASCII table query output format #609
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
Open
0x7FFFFFFFFFFFFFFF
wants to merge
12
commits into
microsoft:main
Choose a base branch
from
0x7FFFFFFFFFFFFFFF:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8e2fffb
Add ASCII table output format
0x7FFFFFFFFFFFFFFF c886ce4
Add test for ascii output
0x7FFFFFFFFFFFFFFF bd336bd
Add help text to disable ASCII table output format
0x7FFFFFFFFFFFFFFF dcdaa09
Add ascii parameter. Disable ascii output format by default
0x7FFFFFFFFFFFFFFF e496801
Mark --vertical and --ascii as mutually exclusive
0x7FFFFFFFFFFFFFFF 7740be6
Iterated over all rows just once, and some other small fixes
0x7FFFFFFFFFFFFFFF 9e46b91
Follow the instruction convention by removing ending period
0x7FFFFFFFFFFFFFFF 98d332c
Update the document of the function NewSQLCmdDefaultFormatter
0x7FFFFFFFFFFFFFFF f53daed
Enhance ASCII table format output documentation
0x7FFFFFFFFFFFFFFF 551734d
Remove temp build script
0x7FFFFFFFFFFFFFFF 0cb7e50
Update README to correctly document which variables are used vs ignor…
0x7FFFFFFFFFFFFFFF dc27dfb
Merge branch 'main' into main
0x7FFFFFFFFFFFFFFF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| package sqlcmd | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "os" | ||
| "strings" | ||
| "unicode/utf8" | ||
|
|
||
| "github.com/microsoft/go-sqlcmd/internal/color" | ||
| "golang.org/x/term" | ||
| ) | ||
|
|
||
| type asciiFormatter struct { | ||
| *sqlCmdFormatterType | ||
| rows [][]string | ||
| colWidths []int | ||
| } | ||
|
|
||
| func NewSQLCmdAsciiFormatter(vars *Variables, removeTrailingSpaces bool, ccb ControlCharacterBehavior) Formatter { | ||
| return &asciiFormatter{ | ||
| sqlCmdFormatterType: &sqlCmdFormatterType{ | ||
| removeTrailingSpaces: removeTrailingSpaces, | ||
| format: "ascii", | ||
| colorizer: color.New(false), | ||
| ccb: ccb, | ||
| vars: vars, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (f *asciiFormatter) BeginResultSet(cols []*sql.ColumnType) { | ||
| f.sqlCmdFormatterType.BeginResultSet(cols) | ||
| f.rows = make([][]string, 0) | ||
| f.colWidths = make([]int, len(f.columnDetails)) | ||
| for i, c := range f.columnDetails { | ||
| f.colWidths[i] = utf8.RuneCountInString(c.col.Name()) | ||
| } | ||
| } | ||
|
|
||
| func (f *asciiFormatter) AddRow(row *sql.Rows) string { | ||
| values, err := f.scanRow(row) | ||
| if err != nil { | ||
| f.mustWriteErr(err.Error()) | ||
| return "" | ||
| } | ||
| f.rows = append(f.rows, values) | ||
| for i, val := range values { | ||
| if i < len(f.colWidths) { | ||
| l := utf8.RuneCountInString(val) | ||
| if l > f.colWidths[i] { | ||
| f.colWidths[i] = l | ||
| } | ||
| } | ||
| } | ||
| if len(values) > 0 { | ||
| return values[0] | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func (f *asciiFormatter) EndResultSet() { | ||
| if len(f.rows) > 0 || len(f.columnDetails) > 0 { | ||
| f.printAsciiTable() | ||
| } | ||
| f.rows = nil | ||
| f.colWidths = nil | ||
| } | ||
|
|
||
| func (f *asciiFormatter) printAsciiTable() { | ||
| maxWidth := int(f.vars.ScreenWidth()) | ||
| if maxWidth <= 0 { | ||
| if w, _, err := term.GetSize(int(os.Stdout.Fd())); err == nil { | ||
| maxWidth = w - 1 | ||
| } else { | ||
| maxWidth = 1000000 | ||
| } | ||
| } | ||
|
|
||
| totalWidth := 1 | ||
| for _, w := range f.colWidths { | ||
| totalWidth += w + 3 | ||
| } | ||
|
|
||
| if totalWidth <= maxWidth { | ||
| f.printTableSegment(f.colWidths, 0, len(f.colWidths)-1) | ||
| } else { | ||
| startCol := 0 | ||
| for startCol < len(f.colWidths) { | ||
| currentWidth := 1 | ||
| endCol := startCol | ||
| for endCol < len(f.colWidths) { | ||
| w := f.colWidths[endCol] + 3 | ||
| if currentWidth+w > maxWidth { | ||
| break | ||
| } | ||
| currentWidth += w | ||
| endCol++ | ||
| } | ||
|
|
||
| if endCol == startCol { | ||
| endCol++ | ||
| } | ||
|
|
||
| f.printTableSegment(f.colWidths, startCol, endCol-1) | ||
| startCol = endCol | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (f *asciiFormatter) printTableSegment(colWidths []int, startCol, endCol int) { | ||
| if startCol > endCol { | ||
| return | ||
| } | ||
|
|
||
| sep := f.vars.ColumnSeparator() | ||
| if sep == "" || sep == " " { | ||
| sep = "|" | ||
| } | ||
|
|
||
| divider := "+" | ||
| for i := startCol; i <= endCol; i++ { | ||
| divider += strings.Repeat("-", colWidths[i]+2) + "+" | ||
| } | ||
| f.writeOut(divider+SqlcmdEol, color.TextTypeSeparator) | ||
|
|
||
| header := sep | ||
| for i := startCol; i <= endCol; i++ { | ||
| name := f.columnDetails[i].col.Name() | ||
| header += " " + padRightString(name, colWidths[i]) + " " + sep | ||
| } | ||
| f.writeOut(header+SqlcmdEol, color.TextTypeHeader) | ||
| f.writeOut(divider+SqlcmdEol, color.TextTypeSeparator) | ||
|
|
||
| for _, row := range f.rows { | ||
| line := sep | ||
| for i := startCol; i <= endCol; i++ { | ||
| val := "" | ||
| if i < len(row) { | ||
| val = row[i] | ||
| } | ||
| isNumeric := isNumericType(f.columnDetails[i].col.DatabaseTypeName()) | ||
|
|
||
| if isNumeric { | ||
| line += " " + padLeftString(val, colWidths[i]) + " " + sep | ||
| } else { | ||
| line += " " + padRightString(val, colWidths[i]) + " " + sep | ||
| } | ||
| } | ||
| f.writeOut(line+SqlcmdEol, color.TextTypeCell) | ||
| } | ||
| f.writeOut(divider+SqlcmdEol, color.TextTypeSeparator) | ||
| } | ||
|
|
||
| func padRightString(s string, width int) string { | ||
| l := utf8.RuneCountInString(s) | ||
| if l >= width { | ||
| return s | ||
| } | ||
| return s + strings.Repeat(" ", width-l) | ||
| } | ||
|
|
||
| func padLeftString(s string, width int) string { | ||
| l := utf8.RuneCountInString(s) | ||
| if l >= width { | ||
| return s | ||
| } | ||
| return strings.Repeat(" ", width-l) + s | ||
| } | ||
|
|
||
| func isNumericType(typeName string) bool { | ||
| switch typeName { | ||
| case "TINYINT", "SMALLINT", "INT", "BIGINT", "REAL", "FLOAT", "DECIMAL", "NUMERIC", "MONEY", "SMALLMONEY": | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package sqlcmd | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestAsciiFormatter(t *testing.T) { | ||
| s, buf := setupSqlCmdWithMemoryOutput(t) | ||
| if s.db == nil { | ||
| t.Skip("No database connection available") | ||
| } | ||
| defer buf.Close() | ||
|
|
||
| // Set format to ascii | ||
| s.vars.Set(SQLCMDFORMAT, "ascii") | ||
| s.Format = NewSQLCmdDefaultFormatter(s.vars, false, ControlIgnore) | ||
|
|
||
| err := runSqlCmd(t, s, []string{"select 1 as id, 'test' as name", "GO"}) | ||
| assert.NoError(t, err, "runSqlCmd returned error") | ||
|
|
||
| expected := `+----+------+` + SqlcmdEol + | ||
| `| id | name |` + SqlcmdEol + | ||
| `+----+------+` + SqlcmdEol + | ||
| `| 1 | test |` + SqlcmdEol + | ||
| `+----+------+` + SqlcmdEol + | ||
| `(1 row affected)` + SqlcmdEol | ||
|
|
||
| assert.Equal(t, expected, buf.buf.String()) | ||
| } | ||
|
|
||
| func TestAsciiFormatterWrapping(t *testing.T) { | ||
| s, buf := setupSqlCmdWithMemoryOutput(t) | ||
| if s.db == nil { | ||
| t.Skip("No database connection available") | ||
| } | ||
| defer buf.Close() | ||
|
|
||
| s.vars.Set(SQLCMDFORMAT, "ascii") | ||
| s.vars.Set(SQLCMDCOLWIDTH, "20") // Small width to force wrapping | ||
| s.Format = NewSQLCmdDefaultFormatter(s.vars, false, ControlIgnore) | ||
|
|
||
| // Select 3 columns that won't fit in 20 chars | ||
| err := runSqlCmd(t, s, []string{"select 1 as id, 'test' as name, '0123456789' as descr", "GO"}) | ||
| assert.NoError(t, err, "runSqlCmd returned error") | ||
|
|
||
| expectedPart1 := `+----+------+` + SqlcmdEol + | ||
| `| id | name |` + SqlcmdEol + | ||
| `+----+------+` + SqlcmdEol + | ||
| `| 1 | test |` + SqlcmdEol + | ||
| `+----+------+` + SqlcmdEol | ||
|
|
||
| expectedPart2 := `+------------+` + SqlcmdEol + | ||
| `| descr |` + SqlcmdEol + | ||
| `+------------+` + SqlcmdEol + | ||
| `| 0123456789 |` + SqlcmdEol + | ||
| `+------------+` + SqlcmdEol + | ||
| `(1 row affected)` + SqlcmdEol | ||
|
|
||
| assert.Contains(t, buf.buf.String(), expectedPart1) | ||
| assert.Contains(t, buf.buf.String(), expectedPart2) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.