(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode)
| 1247 | } |
| 1248 | |
| 1249 | func PostgresNodeFormatter(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode) { |
| 1250 | switch node := node.(type) { |
| 1251 | case sqlparser.ColIdent: |
| 1252 | // MySQL `@var` and `@@var` references map to postgres' current_setting(). |
| 1253 | // All other identifiers go through tree.AutoQuoteIdent so that names |
| 1254 | // containing special characters or matching a reserved keyword come out |
| 1255 | // double-quoted (postgres' quoting style) rather than backticked |
| 1256 | // (vitess' default formatID behavior). |
| 1257 | switch { |
| 1258 | case strings.HasPrefix(node.String(), "@@"): |
| 1259 | buf.Myprintf("current_setting('.%s')", strings.TrimLeft(node.String(), "@")) |
| 1260 | case strings.HasPrefix(node.String(), "@"): |
| 1261 | buf.Myprintf("current_setting('doltgres_enginetest.%s')", strings.TrimLeft(node.String(), "@")) |
| 1262 | default: |
| 1263 | buf.WriteString(AutoQuoteIdent(node.Lowered())) |
| 1264 | } |
| 1265 | case sqlparser.TableIdent: |
| 1266 | // Same auto-quoting story as ColIdent; vitess' formatID would |
| 1267 | // otherwise wrap special-character identifiers in backticks. |
| 1268 | buf.WriteString(AutoQuoteIdent(node.String())) |
| 1269 | case sqlparser.TableName: |
| 1270 | // MySQL `db.tbl` → postgres `db.public.tbl`. MySQL has no schema |
| 1271 | // concept, so we insert `public` (postgres' default schema) between |
| 1272 | // the catalog and table when a database qualifier is present — |
| 1273 | // except for well-known schemas like `information_schema`, where the |
| 1274 | // MySQL qualifier already lines up with a postgres schema. |
| 1275 | switch { |
| 1276 | case node.DbQualifier.IsEmpty(): |
| 1277 | buf.Myprintf("%v", node.Name) |
| 1278 | case isWellKnownSchema(node.DbQualifier.String()): |
| 1279 | buf.Myprintf("%v.%v", node.DbQualifier, node.Name) |
| 1280 | default: |
| 1281 | buf.Myprintf("%v.public.%v", node.DbQualifier, node.Name) |
| 1282 | } |
| 1283 | case *sqlparser.ColName: |
| 1284 | // Same translation for qualified column references: `db.tbl.col` → |
| 1285 | // `db.public.tbl.col`, leaving `information_schema.tbl.col` alone. |
| 1286 | switch { |
| 1287 | case node.Qualifier.DbQualifier.IsEmpty() && node.Qualifier.Name.IsEmpty(): |
| 1288 | buf.Myprintf("%v", node.Name) |
| 1289 | case node.Qualifier.DbQualifier.IsEmpty(): |
| 1290 | buf.Myprintf("%v.%v", node.Qualifier.Name, node.Name) |
| 1291 | case isWellKnownSchema(node.Qualifier.DbQualifier.String()): |
| 1292 | buf.Myprintf("%v.%v.%v", node.Qualifier.DbQualifier, node.Qualifier.Name, node.Name) |
| 1293 | default: |
| 1294 | buf.Myprintf("%v.public.%v.%v", node.Qualifier.DbQualifier, node.Qualifier.Name, node.Name) |
| 1295 | } |
| 1296 | case *sqlparser.UnaryExpr: |
| 1297 | if node.Operator == "binary " { |
| 1298 | buf.Myprintf("%v::text::bytea", node.Expr) |
| 1299 | } else { |
| 1300 | buf.Myprintf("%v", node) |
| 1301 | } |
| 1302 | case *sqlparser.Limit: |
| 1303 | if node == nil { |
| 1304 | return |
| 1305 | } |
| 1306 | buf.Myprintf(" limit %v", node.Rowcount) |
nothing calls this directly
no test coverage detected