File: gen.rb

package info (click to toggle)
golang-github-jackc-pgerrcode 0.0~git20240316.6e2875d-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104 kB
  • sloc: ruby: 82; makefile: 2
file content (108 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Run this script against the data in table A.1. on https://www.postgresql.org/docs/16/errcodes-appendix.html.
#
# Source data should be formatted like the following:
#
# Class 00 — Successful Completion
# 00000 	successful_completion
# Class 01 — Warning
# 01000 	warning
# 0100C 	dynamic_result_sets_returned
#
# for best results pass through gofmt
# ruby gen.rb < tablecontents.txt  | gofmt > errcode.go

code_name_overrides = {
  # Some error code names are repeated. In those cases add the error class as a suffix.
  "01004" => "StringDataRightTruncationWarning",
  "22001" => "StringDataRightTruncationDataException",
  "22004" => "NullValueNotAllowedDataException",
  "2F002" => "ModifyingSQLDataNotPermittedSQLRoutineException",
  "2F003" => "ProhibitedSQLStatementAttemptedSQLRoutineException",
  "2F004" => "ReadingSQLDataNotPermittedSQLRoutineException",
	"38002" => "ModifyingSQLDataNotPermittedExternalRoutineException",
	"38003" => "ProhibitedSQLStatementAttemptedExternalRoutineException",
  "38004" => "ReadingSQLDataNotPermittedExternalRoutineException",
  "39004" => "NullValueNotAllowedExternalRoutineInvocationException",

  # Go casing corrections
	"08001" => "SQLClientUnableToEstablishSQLConnection",
  "08004" => "SQLServerRejectedEstablishmentOfSQLConnection",
  "P0000" => "PLpgSQLError"
}

class_name_overrides = {
  # Go casing corrections
  "WITHCHECKOPTIONViolation" => "WithCheckOptionViolation"
}

cls_errs = Array.new
cls_assertions = Array.new
last_cls = ""
last_cls_full = ""

def build_assert_func(last_cls, last_cls_full, cls_errs)
  <<~GO
  // Is#{last_cls} asserts the error code class is #{last_cls_full}
  func Is#{last_cls} (code string) bool {
      switch code{
          case #{cls_errs.join(", ")}:
              return true
      }
      return false
  }
  GO
end

puts <<~STR
// Package pgerrcode contains constants for PostgreSQL error codes.
package pgerrcode

// Source: https://www.postgresql.org/docs/16/errcodes-appendix.html
// See gen.rb for script that can convert the error code table to Go code.

const (
STR

ARGF.each do |line|
  case line
  when /^Class/
    if cls_errs.length > 0 && last_cls != ""
      assert_func = build_assert_func(class_name_overrides.fetch(last_cls) { last_cls }, last_cls_full, cls_errs)
      cls_assertions.push(assert_func)
    end
    last_cls = line.split("—")[1]
    .gsub(" ", "")
    .gsub("/", "")
    .gsub("\n", "")
    .sub(/\(\w*\)/, "")
    last_cls_full = line.gsub("\n", "")
    cls_errs.clear
    puts
    puts "// #{line}"
  when /^(\w{5})\s+(\w+)/
    code = $1
    name = code_name_overrides.fetch(code) do
      $2.split("_").map(&:capitalize).join
        .gsub("Sql", "SQL")
        .gsub("Xml", "XML")
        .gsub("Fdw", "FDW")
        .gsub("Srf", "SRF")
        .gsub("Io", "IO")
        .gsub("Json", "JSON")
    end
    cls_errs.push(name)
    puts %Q[#{name} = "#{code}"]
  else
    puts line
  end
end
puts ")"

if cls_errs.length > 0
  assert_func = build_assert_func(class_name_overrides.fetch(last_cls) { last_cls }, last_cls_full, cls_errs)
  cls_assertions.push(assert_func)
end

cls_assertions.each do |cls_assertion|
  puts cls_assertion
end