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.
()
| 1311 | // reproduce the type via parsing the string as a type. It is used in error |
| 1312 | // messages and also to produce the output of SHOW CREATE. |
| 1313 | func (t *T) SQLString() string { |
| 1314 | switch t.Family() { |
| 1315 | case BitFamily: |
| 1316 | o := t.Oid() |
| 1317 | typName := "BIT" |
| 1318 | if o == oid.T_varbit { |
| 1319 | typName = "VARBIT" |
| 1320 | } |
| 1321 | // BIT(1) pretty-prints as just BIT. |
| 1322 | if (o != oid.T_varbit && t.Width() > 1) || |
| 1323 | (o == oid.T_varbit && t.Width() > 0) { |
| 1324 | typName = fmt.Sprintf("%s(%d)", typName, t.Width()) |
| 1325 | } |
| 1326 | return typName |
| 1327 | case IntFamily: |
| 1328 | switch t.Width() { |
| 1329 | case 16: |
| 1330 | return "INT2" |
| 1331 | case 32: |
| 1332 | return "INT4" |
| 1333 | case 64: |
| 1334 | return "INT8" |
| 1335 | default: |
| 1336 | panic(errors.AssertionFailedf("programming error: unknown int width: %d", t.Width())) |
| 1337 | } |
| 1338 | case StringFamily: |
| 1339 | return t.stringTypeSQL() |
| 1340 | case CollatedStringFamily: |
| 1341 | return t.collatedStringTypeSQL(false /* isArray */) |
| 1342 | case FloatFamily: |
| 1343 | const realName = "FLOAT4" |
| 1344 | const doubleName = "FLOAT8" |
| 1345 | if t.Width() == 32 { |
| 1346 | return realName |
| 1347 | } |
| 1348 | return doubleName |
| 1349 | case DecimalFamily: |
| 1350 | if t.Precision() > 0 { |
| 1351 | if t.Width() > 0 { |
| 1352 | return fmt.Sprintf("DECIMAL(%d,%d)", t.Precision(), t.Scale()) |
| 1353 | } |
| 1354 | return fmt.Sprintf("DECIMAL(%d)", t.Precision()) |
| 1355 | } |
| 1356 | case JsonFamily: |
| 1357 | // Only binary JSON is currently supported. |
| 1358 | return "JSONB" |
| 1359 | case TimestampFamily, TimestampTZFamily, TimeFamily, TimeTZFamily: |
| 1360 | if t.InternalType.Precision > 0 || t.InternalType.TimePrecisionIsSet { |
| 1361 | return fmt.Sprintf("%s(%d)", strings.ToUpper(t.Name()), t.Precision()) |
| 1362 | } |
| 1363 | case IntervalFamily: |
| 1364 | switch t.InternalType.IntervalDurationField.DurationType { |
| 1365 | case IntervalDurationType_UNSET: |
| 1366 | if t.InternalType.Precision > 0 || t.InternalType.TimePrecisionIsSet { |
| 1367 | return fmt.Sprintf("%s(%d)", strings.ToUpper(t.Name()), t.Precision()) |
| 1368 | } |
| 1369 | default: |
| 1370 | fromStr := "" |
no test coverage detected