SQLString returns the CockroachDB 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.
()
| 1976 | // reproduce the type via parsing the string as a type. It is used in error |
| 1977 | // messages and also to produce the output of SHOW CREATE. |
| 1978 | func (t *T) SQLString() string { |
| 1979 | switch t.Family() { |
| 1980 | case BitFamily: |
| 1981 | switch t.Oid() { |
| 1982 | case oid.T_bit: |
| 1983 | typName := "BIT" |
| 1984 | // BIT(1) pretty-prints as just BIT. |
| 1985 | // BIT(0) represents a BIT type with unspecified length. This is a |
| 1986 | // divergence from Postgres which does not allow this type and has |
| 1987 | // no way to represent it in SQL. It is required in order for it to |
| 1988 | // be correctly serialized into SQL and evaluated during distributed |
| 1989 | // query execution. VARBIT cannot be used because it has a different |
| 1990 | // OID. |
| 1991 | if t.Width() != 1 { |
| 1992 | typName = fmt.Sprintf("%s(%d)", typName, t.Width()) |
| 1993 | } |
| 1994 | return typName |
| 1995 | default: |
| 1996 | typName := "VARBIT" |
| 1997 | if t.Width() > 0 { |
| 1998 | typName = fmt.Sprintf("%s(%d)", typName, t.Width()) |
| 1999 | } |
| 2000 | return typName |
| 2001 | } |
| 2002 | case IntFamily: |
| 2003 | switch t.Width() { |
| 2004 | case 16: |
| 2005 | return "INT2" |
| 2006 | case 32: |
| 2007 | return "INT4" |
| 2008 | case 64: |
| 2009 | return "INT8" |
| 2010 | default: |
| 2011 | panic(errors.AssertionFailedf("programming error: unknown int width: %d", t.Width())) |
| 2012 | } |
| 2013 | case StringFamily: |
| 2014 | return t.stringTypeSQL() |
| 2015 | case CollatedStringFamily: |
| 2016 | return t.collatedStringTypeSQL(false /* isArray */) |
| 2017 | case FloatFamily: |
| 2018 | const realName = "FLOAT4" |
| 2019 | const doubleName = "FLOAT8" |
| 2020 | if t.Width() == 32 { |
| 2021 | return realName |
| 2022 | } |
| 2023 | return doubleName |
| 2024 | case DecimalFamily: |
| 2025 | if t.Precision() > 0 { |
| 2026 | if t.Width() > 0 { |
| 2027 | return fmt.Sprintf("DECIMAL(%d,%d)", t.Precision(), t.Scale()) |
| 2028 | } |
| 2029 | return fmt.Sprintf("DECIMAL(%d)", t.Precision()) |
| 2030 | } |
| 2031 | case JsonFamily: |
| 2032 | // Only binary JSON is currently supported. |
| 2033 | return "JSONB" |
| 2034 | case JsonpathFamily: |
| 2035 | return "JSONPATH" |
no test coverage detected