gen.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }