-
Notifications
You must be signed in to change notification settings - Fork 84
feat: 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
13
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
13 commits
Select commit
Hold shift + click to select a range
cd77d3f
Add ASCII table output format
0x7FFFFFFFFFFFFFFF ef6f7c6
Add test for ascii output
0x7FFFFFFFFFFFFFFF a6e0f33
Add help text to disable ASCII table output format
0x7FFFFFFFFFFFFFFF 6a4cb39
Add ascii parameter. Disable ascii output format by default
0x7FFFFFFFFFFFFFFF b23d59a
Mark --vertical and --ascii as mutually exclusive
0x7FFFFFFFFFFFFFFF 0f2fa63
Iterated over all rows just once, and some other small fixes
0x7FFFFFFFFFFFFFFF 8409a37
Follow the instruction convention by removing ending period
0x7FFFFFFFFFFFFFFF 01b00c7
Update the document of the function NewSQLCmdDefaultFormatter
0x7FFFFFFFFFFFFFFF bf39320
Enhance ASCII table format output documentation
0x7FFFFFFFFFFFFFFF 444496d
Remove temp build script
0x7FFFFFFFFFFFFFFF 870c65b
Update README to correctly document which variables are used vs ignor…
0x7FFFFFFFFFFFFFFF f84516b
Refactor buffer closure in ASCII format tests
dlevy-msft-sql f3c205d
Truncate wide column
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,197 @@ | ||
| 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 | ||
| } | ||
| } | ||
|
|
||
| // Limit column width to maxWidth - 4 (border + padding) | ||
| // 1 (left border) + 1 (space) + content + 1 (space) + 1 (right border) = content + 4 | ||
| maxColContentWidth := maxWidth - 4 | ||
| if maxColContentWidth < 1 { | ||
| maxColContentWidth = 1 | ||
| } | ||
|
|
||
| for i := range f.colWidths { | ||
| if f.colWidths[i] > maxColContentWidth { | ||
| f.colWidths[i] = maxColContentWidth | ||
| } | ||
| } | ||
|
|
||
| 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 { | ||
| r := []rune(s) | ||
| if width >= 3 { | ||
| return string(r[:width-3]) + "..." | ||
| } | ||
| return string(r[:width]) | ||
| } | ||
| return s + strings.Repeat(" ", width-l) | ||
| } | ||
|
|
||
| func padLeftString(s string, width int) string { | ||
| l := utf8.RuneCountInString(s) | ||
| if l > width { | ||
| r := []rune(s) | ||
| if width >= 3 { | ||
| return string(r[:width-3]) + "..." | ||
| } | ||
| return string(r[:width]) | ||
| } | ||
| 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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.