SQLString returns the native SQL string that can be used to reproduce the type via parsing the string as a type. It is used in error messages and also to produce the output of SHOW CREATE.
()
| 1615 | // reproduce the type via parsing the string as a type. It is used in error |
| 1616 | // messages and also to produce the output of SHOW CREATE. |
| 1617 | func (t *T) SQLString() string { |
| 1618 | switch t.Family() { |
| 1619 | case BitFamily: |
| 1620 | o := t.Oid() |
| 1621 | typName := "BIT" |
| 1622 | if o == oid.T_varbit { |
| 1623 | typName = "VARBIT" |
| 1624 | } |
| 1625 | // BIT(1) pretty-prints as just BIT. |
| 1626 | if (o != oid.T_varbit && t.Width() > 1) || |
| 1627 | (o == oid.T_varbit && t.Width() > 0) { |
| 1628 | typName = fmt.Sprintf("%s(%d)", typName, t.Width()) |
| 1629 | } |
| 1630 | return typName |
| 1631 | case IntFamily: |
| 1632 | switch t.Width() { |
| 1633 | case 16: |
| 1634 | return "SMALLINT" // TODO: Return commented version, different function for MySQL string -> "INT2" |
| 1635 | case 32: |
| 1636 | return "INTEGER" // TODO: Return commented version, different function for MySQL string -> "INT4" |
| 1637 | case 64: |
| 1638 | return "BIGINT" // TODO: Return commented version, different function for MySQL string -> "INT8" |
| 1639 | default: |
| 1640 | panic(errors.AssertionFailedf("programming error: unknown int width: %d", t.Width())) |
| 1641 | } |
| 1642 | case StringFamily: |
| 1643 | return t.stringTypeSQL() |
| 1644 | case CollatedStringFamily: |
| 1645 | return t.collatedStringTypeSQL(false /* isArray */) |
| 1646 | case FloatFamily: |
| 1647 | const realName = "REAL" // TODO: Return commented version, different function for MySQL string -> "FLOAT4" |
| 1648 | const doubleName = "DOUBLE PRECISION" // TODO: Return commented version, different function for MySQL string -> "FLOAT8" |
| 1649 | if t.Width() == 32 { |
| 1650 | return realName |
| 1651 | } |
| 1652 | return doubleName |
| 1653 | case DecimalFamily: |
| 1654 | if t.Precision() > 0 { |
| 1655 | if t.Width() > 0 { |
| 1656 | return fmt.Sprintf("DECIMAL(%d,%d)", t.Precision(), t.Scale()) |
| 1657 | } |
| 1658 | return fmt.Sprintf("DECIMAL(%d)", t.Precision()) |
| 1659 | } |
| 1660 | case JsonFamily: |
| 1661 | return "JSON" |
| 1662 | case TimestampFamily, TimestampTZFamily, TimeFamily, TimeTZFamily: |
| 1663 | if t.InternalType.Precision > 0 || t.InternalType.TimePrecisionIsSet { |
| 1664 | return fmt.Sprintf("%s(%d)", strings.ToUpper(t.Name()), t.Precision()) |
| 1665 | } |
| 1666 | case GeometryFamily, GeographyFamily: |
| 1667 | return strings.ToUpper(t.Name() + t.InternalType.GeoMetadata.SQLString()) |
| 1668 | case IntervalFamily: |
| 1669 | switch t.InternalType.IntervalDurationField.DurationType { |
| 1670 | case IntervalDurationType_UNSET: |
| 1671 | if t.InternalType.Precision > 0 || t.InternalType.TimePrecisionIsSet { |
| 1672 | return fmt.Sprintf("%s(%d)", strings.ToUpper(t.Name()), t.Precision()) |
| 1673 | } |
| 1674 | default: |
no test coverage detected