Skip to content
Open
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
17 changes: 14 additions & 3 deletions drivers/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ SELECT
c.name,
t.name AS type,
c.max_length,
c.precision,
c.scale,
c.is_nullable,
c.is_identity,
object_definition(c.default_object_id),
Expand All @@ -244,18 +246,20 @@ ORDER BY c.column_id
columnName string
dataType string
maxLength int
precision int
scale int
isNullable bool
isIdentity bool
columnDefault sql.NullString
columnComment sql.NullString
)
err = columnRows.Scan(&columnName, &dataType, &maxLength, &isNullable, &isIdentity, &columnDefault, &columnComment)
err = columnRows.Scan(&columnName, &dataType, &maxLength, &precision, &scale, &isNullable, &isIdentity, &columnDefault, &columnComment)
if err != nil {
return nil, nil, errors.WithStack(err)
}
column := &schema.Column{
Name: columnName,
Type: convertColumnType(dataType, maxLength),
Type: convertColumnType(dataType, maxLength, precision, scale),
Nullable: isNullable,
Default: columnDefault,
Comment: columnComment.String,
Expand Down Expand Up @@ -661,14 +665,21 @@ func convertTableType(t string) string {
}
}

func convertColumnType(t string, maxLength int) string {
func convertColumnType(t string, maxLength, precision, scale int) string {
switch t {
case "varchar":
var length = strconv.Itoa(maxLength)
if maxLength == -1 {
length = "MAX"
}
return fmt.Sprintf("varchar(%s)", length)
case "char", "binary":
return fmt.Sprintf("%s(%d)", t, maxLength)
case "nchar":
//nchar length is 2 byte, return character length
return fmt.Sprintf("nchar(%d)", maxLength/2)
case "decimal", "numeric":
return fmt.Sprintf("%s(%d,%d)", t, precision, scale)
case "nvarchar":
//nvarchar length is 2 byte, return character length
var length = strconv.Itoa(maxLength / 2)
Expand Down
48 changes: 48 additions & 0 deletions drivers/mssql/mssql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,51 @@ func TestObjectExists(t *testing.T) {
t.Error("got true for a dropped table, want false")
}
}

func TestColumnTypePrecisionAndScale(t *testing.T) {
const name = "tbls_column_types"
if _, err := db.Exec(`DROP TABLE IF EXISTS ` + name); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_, _ = db.Exec(`DROP TABLE IF EXISTS ` + name)
})
// d_default has no explicit precision, so SQL Server applies decimal(18,0);
// it checks that the values come from sys.columns, not the DDL text.
if _, err := db.Exec(`CREATE TABLE ` + name + ` (
d decimal(9,2),
n numeric(18,4),
d_default decimal,
c char(5),
nc nchar(5),
b binary(16)
)`); err != nil {
t.Fatal(err)
}

driver := New(db)
sc := &schema.Schema{Name: "mssqltestdb"}
if err := driver.Analyze(sc); err != nil {
t.Fatal(err)
}

tbl, err := sc.FindTableByName(name)
if err != nil {
t.Fatal(err)
}
got := map[string]string{}
for _, c := range tbl.Columns {
got[c.Name] = c.Type
}
want := map[string]string{
"d": "decimal(9,2)",
"n": "numeric(18,4)",
"d_default": "decimal(18,0)",
"c": "char(5)",
"nc": "nchar(5)",
"b": "binary(16)",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("column types mismatch (-want +got):\n%s", diff)
}
}
2 changes: 1 addition & 1 deletion sample/azuresql/order_items.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
| order_id | int | | false | | [orders](orders.md) | |
| product_id | int | | false | | [products](products.md) | |
| quantity | int | | false | | | |
| unit_price | decimal | | false | | | |
| unit_price | decimal(10,2) | | false | | | |
| created | date | | false | | | |

## Constraints
Expand Down
148 changes: 74 additions & 74 deletions sample/azuresql/order_items.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample/azuresql/order_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CREATE VIEW order_summary AS (
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | int | | false | | | |
| username | varchar(50) | | true | | | |
| total_amount | decimal | | false | | | |
| total_amount | decimal(12,2) | | false | | | |
| status | varchar(20) | | false | | | |
| created | date | | false | | | |

Expand Down
2 changes: 1 addition & 1 deletion sample/azuresql/order_summary.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample/azuresql/orders.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Customer orders
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | int | | false | [order_items](order_items.md) | | |
| user_id | int | | false | | [users](users.md) | |
| total_amount | decimal | | false | | | |
| total_amount | decimal(12,2) | | false | | | |
| status | varchar(20) | ('pending') | false | | | pending/processing/shipped/delivered |
| created | date | | false | | | |
| updated | date | | true | | | |
Expand Down
58 changes: 29 additions & 29 deletions sample/azuresql/orders.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample/azuresql/products.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Product catalog
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | int | | false | [order_items](order_items.md) | | |
| name | nvarchar(255) | | false | | | |
| price | decimal | | false | | | Unit price in USD |
| price | decimal(10,2) | | false | | | Unit price in USD |
| stock_quantity | int | ((0)) | false | | | |
| created | date | | false | | | |

Expand Down
4 changes: 2 additions & 2 deletions sample/azuresql/products.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions sample/azuresql/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
},
{
"name": "price",
"type": "decimal",
"type": "decimal(10,2)",
"nullable": false,
"comment": "Unit price in USD"
},
Expand Down Expand Up @@ -230,7 +230,7 @@
},
{
"name": "total_amount",
"type": "decimal",
"type": "decimal(12,2)",
"nullable": false
},
{
Expand Down Expand Up @@ -312,7 +312,7 @@
},
{
"name": "unit_price",
"type": "decimal",
"type": "decimal(10,2)",
"nullable": false
},
{
Expand Down Expand Up @@ -419,7 +419,7 @@
},
{
"name": "total_amount",
"type": "decimal",
"type": "decimal(12,2)",
"nullable": false
},
{
Expand Down Expand Up @@ -496,7 +496,7 @@
],
"driver": {
"name": "sqlserver",
"database_version": "Microsoft Azure SQL Edge Developer (RTM) - 15.0.2000.1574 (ARM64) \n\tJan 25 2023 10:36:08 \n\tCopyright (C) 2019 Microsoft Corporation\n\tLinux (Ubuntu 18.04.6 LTS aarch64) \u003cARM64\u003e",
"database_version": "Microsoft SQL Server 2019 (RTM-CU32-GDR) (KB5128494) - 15.0.4500.2 (X64) \n\tSep 26 2026 20:15:06 \n\tCopyright (C) 2019 Microsoft Corporation\n\tDeveloper Edition (64-bit) on Linux (Ubuntu 20.04.6 LTS) \u003cX64\u003e",
"meta": {
"dict": {
"Functions": "Stored procedures and functions"
Expand Down
Loading