(p *PrettyCfg)
| 1025 | } |
| 1026 | |
| 1027 | func (node *CastExpr) doc(p *PrettyCfg) pretty.Doc { |
| 1028 | typ := p.formatType(node.Type) |
| 1029 | |
| 1030 | switch node.SyntaxMode { |
| 1031 | case CastPrepend: |
| 1032 | // This is a special case for things like INTERVAL '1s'. These only work |
| 1033 | // with string constats; if the underlying expression was changed, we fall |
| 1034 | // back to the short syntax. |
| 1035 | if _, ok := node.Expr.(*StrVal); ok { |
| 1036 | return pretty.Fold(pretty.Concat, |
| 1037 | typ, |
| 1038 | pretty.Text(" "), |
| 1039 | p.Doc(node.Expr), |
| 1040 | ) |
| 1041 | } |
| 1042 | fallthrough |
| 1043 | case CastShort: |
| 1044 | if typ, ok := GetStaticallyKnownType(node.Type); ok { |
| 1045 | switch typ.Family() { |
| 1046 | case types.JsonFamily: |
| 1047 | if sv, ok := node.Expr.(*StrVal); ok && p.JSONFmt { |
| 1048 | return p.jsonCast(sv, "::", typ) |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | return pretty.Fold(pretty.Concat, |
| 1053 | p.exprDocWithParen(node.Expr), |
| 1054 | pretty.Text("::"), |
| 1055 | typ, |
| 1056 | ) |
| 1057 | default: |
| 1058 | if nTyp, ok := GetStaticallyKnownType(node.Type); ok && nTyp.Family() == types.CollatedStringFamily { |
| 1059 | // COLLATE clause needs to go after CAST expression, so create |
| 1060 | // equivalent string type without the locale to get name of string |
| 1061 | // type without the COLLATE. |
| 1062 | strTyp := types.MakeScalar( |
| 1063 | types.StringFamily, |
| 1064 | nTyp.Oid(), |
| 1065 | nTyp.Precision(), |
| 1066 | nTyp.Width(), |
| 1067 | "", /* locale */ |
| 1068 | ) |
| 1069 | typ = pretty.Text(strTyp.SQLString()) |
| 1070 | } |
| 1071 | |
| 1072 | ret := pretty.Fold(pretty.Concat, |
| 1073 | pretty.Keyword("CAST"), |
| 1074 | p.bracket( |
| 1075 | "(", |
| 1076 | p.nestUnder( |
| 1077 | p.Doc(node.Expr), |
| 1078 | pretty.Concat( |
| 1079 | p.keywordWithText("", "AS", " "), |
| 1080 | typ, |
| 1081 | ), |
| 1082 | ), |
| 1083 | ")", |
| 1084 | ), |
nothing calls this directly
no test coverage detected