This repository has been archived on 2023-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
dex/vendor/github.com/lib/pq/oid/gen.go

94 lines
1.7 KiB
Go
Raw Normal View History

2016-09-19 15:47:53 +00:00
// +build ignore
// Generate the table of OID values
// Run with 'go run gen.go'.
package main
import (
"database/sql"
"fmt"
"log"
"os"
"os/exec"
2018-11-16 19:49:39 +00:00
"strings"
2016-09-19 15:47:53 +00:00
_ "github.com/lib/pq"
)
2018-11-16 19:49:39 +00:00
// OID represent a postgres Object Identifier Type.
type OID struct {
ID int
Type string
}
// Name returns an upper case version of the oid type.
func (o OID) Name() string {
return strings.ToUpper(o.Type)
}
2016-09-19 15:47:53 +00:00
func main() {
datname := os.Getenv("PGDATABASE")
sslmode := os.Getenv("PGSSLMODE")
if datname == "" {
os.Setenv("PGDATABASE", "pqgotest")
}
if sslmode == "" {
os.Setenv("PGSSLMODE", "disable")
}
db, err := sql.Open("postgres", "")
if err != nil {
log.Fatal(err)
}
2018-11-16 19:49:39 +00:00
rows, err := db.Query(`
SELECT typname, oid
FROM pg_type WHERE oid < 10000
ORDER BY oid;
`)
if err != nil {
log.Fatal(err)
}
oids := make([]*OID, 0)
for rows.Next() {
var oid OID
if err = rows.Scan(&oid.Type, &oid.ID); err != nil {
log.Fatal(err)
}
oids = append(oids, &oid)
}
if err = rows.Err(); err != nil {
log.Fatal(err)
}
2016-09-19 15:47:53 +00:00
cmd := exec.Command("gofmt")
cmd.Stderr = os.Stderr
w, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
f, err := os.Create("types.go")
if err != nil {
log.Fatal(err)
}
cmd.Stdout = f
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
2018-11-16 19:49:39 +00:00
fmt.Fprintln(w, "// Code generated by gen.go. DO NOT EDIT.")
2016-09-19 15:47:53 +00:00
fmt.Fprintln(w, "\npackage oid")
fmt.Fprintln(w, "const (")
2018-11-16 19:49:39 +00:00
for _, oid := range oids {
fmt.Fprintf(w, "T_%s Oid = %d\n", oid.Type, oid.ID)
2018-11-29 07:24:13 +00:00
}
fmt.Fprintln(w, ")")
2018-11-16 19:49:39 +00:00
fmt.Fprintln(w, "var TypeName = map[Oid]string{")
for _, oid := range oids {
fmt.Fprintf(w, "T_%s: \"%s\",\n", oid.Type, oid.Name())
}
fmt.Fprintln(w, "}")
2016-09-19 15:47:53 +00:00
w.Close()
cmd.Wait()
}