gen.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // +build ignore
  2. // Generate the table of OID values
  3. // Run with 'go run gen.go'.
  4. package main
  5. import (
  6. "database/sql"
  7. "fmt"
  8. "log"
  9. "os"
  10. "os/exec"
  11. _ "github.com/lib/pq"
  12. )
  13. func main() {
  14. datname := os.Getenv("PGDATABASE")
  15. sslmode := os.Getenv("PGSSLMODE")
  16. if datname == "" {
  17. os.Setenv("PGDATABASE", "pqgotest")
  18. }
  19. if sslmode == "" {
  20. os.Setenv("PGSSLMODE", "disable")
  21. }
  22. db, err := sql.Open("postgres", "")
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. cmd := exec.Command("gofmt")
  27. cmd.Stderr = os.Stderr
  28. w, err := cmd.StdinPipe()
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. f, err := os.Create("types.go")
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. cmd.Stdout = f
  37. err = cmd.Start()
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. fmt.Fprintln(w, "// generated by 'go run gen.go'; do not edit")
  42. fmt.Fprintln(w, "\npackage oid")
  43. fmt.Fprintln(w, "const (")
  44. rows, err := db.Query(`
  45. SELECT typname, oid
  46. FROM pg_type WHERE oid < 10000
  47. ORDER BY oid;
  48. `)
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. var name string
  53. var oid int
  54. for rows.Next() {
  55. err = rows.Scan(&name, &oid)
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. fmt.Fprintf(w, "T_%s Oid = %d\n", name, oid)
  60. }
  61. if err = rows.Err(); err != nil {
  62. log.Fatal(err)
  63. }
  64. fmt.Fprintln(w, ")")
  65. w.Close()
  66. cmd.Wait()
  67. }