|
1 | 1 | package mssql |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "math" |
4 | 5 | "strconv" |
5 | 6 | "strings" |
6 | 7 | "unicode/utf16" |
@@ -780,16 +781,16 @@ func (c *cc) convertScalarExpression(expr tsql.ScalarExpression) ast.Node { |
780 | 781 | case *tsql.VariableReference: |
781 | 782 | return c.convertVariableReference(e) |
782 | 783 | case *tsql.IntegerLiteral: |
783 | | - ival, _ := strconv.ParseInt(e.Value, 10, 64) |
| 784 | + ival, err := strconv.ParseInt(e.Value, 10, 64) |
| 785 | + if err != nil || ival > math.MaxInt32 { |
| 786 | + return c.numericLiteral(e.Value, e) |
| 787 | + } |
784 | 788 | return &ast.A_Const{ |
785 | 789 | Val: &ast.Integer{Ival: ival}, |
786 | 790 | Location: c.loc(e), |
787 | 791 | } |
788 | 792 | case *tsql.NumericLiteral: |
789 | | - return &ast.A_Const{ |
790 | | - Val: &ast.Float{Str: e.Value}, |
791 | | - Location: c.loc(e), |
792 | | - } |
| 793 | + return c.numericLiteral(e.Value, e) |
793 | 794 | case *tsql.RealLiteral: |
794 | 795 | return &ast.A_Const{ |
795 | 796 | Val: &ast.Float{Str: e.Value}, |
@@ -1594,6 +1595,22 @@ func (c *cc) convertCreateFunctionStatement(n *tsql.CreateFunctionStatement) ast |
1594 | 1595 | } |
1595 | 1596 | } |
1596 | 1597 |
|
| 1598 | +// numericLiteral is a number written with a decimal point, or too large |
| 1599 | +// for an int, which SQL Server types as the decimal holding its digits: |
| 1600 | +// 1.50 is a decimal(3,2), 2147483648 a decimal(10,0). |
| 1601 | +func (c *cc) numericLiteral(value string, n tsql.Node) ast.Node { |
| 1602 | + whole, frac, _ := strings.Cut(value, ".") |
| 1603 | + whole = strings.TrimLeft(whole, "0") |
| 1604 | + scale := int64(len(frac)) |
| 1605 | + precision := max(int64(len(whole))+scale, 1) |
| 1606 | + return &ast.TypeCast{ |
| 1607 | + Arg: &ast.A_Const{Val: &ast.Float{Str: value}, Location: c.loc(n)}, |
| 1608 | + TypeName: &ast.TypeName{Name: "decimal", Typmods: appendTypmod(appendTypmod(nil, |
| 1609 | + &ast.A_Const{Val: &ast.Integer{Ival: precision}}), &ast.A_Const{Val: &ast.Integer{Ival: scale}})}, |
| 1610 | + Location: c.loc(n), |
| 1611 | + } |
| 1612 | +} |
| 1613 | + |
1597 | 1614 | // typeSpelling spells a type so that it parses back as the same type, which |
1598 | 1615 | // for a nullable one is the Nullable(T) it was spelled as. |
1599 | 1616 | func typeSpelling(tn *ast.TypeName, t *core.TypeExpr) string { |
|
0 commit comments