| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
on |
| 6 |
|
| 7 |
ng |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
code_name_overrides = { |
| 15 |
|
| 16 |
"01004" => "StringDataRightTruncationWarning", |
| 17 |
"22001" => "StringDataRightTruncationDataException", |
| 18 |
"22004" => "NullValueNotAllowedDataException", |
| 19 |
"2F002" => "ModifyingSQLDataNotPermittedSQLRoutineException", |
| 20 |
"2F003" => "ProhibitedSQLStatementAttemptedSQLRoutineException", |
| 21 |
"2F004" => "ReadingSQLDataNotPermittedSQLRoutineException", |
| 22 |
"38002" => "ModifyingSQLDataNotPermittedExternalRoutineException", |
| 23 |
"38003" => "ProhibitedSQLStatementAttemptedExternalRoutineException", |
| 24 |
"38004" => "ReadingSQLDataNotPermittedExternalRoutineException", |
| 25 |
"39004" => "NullValueNotAllowedExternalRoutineInvocationException", |
| 26 |
|
| 27 |
|
| 28 |
"08001" => "SQLClientUnableToEstablishSQLConnection", |
| 29 |
"08004" => "SQLServerRejectedEstablishmentOfSQLConnection", |
| 30 |
"P0000" => "PLpgSQLError" |
| 31 |
} |
| 32 |
|
| 33 |
class_name_overrides = { |
| 34 |
|
| 35 |
"WITHCHECKOPTIONViolation" => "WithCheckOptionViolation" |
| 36 |
} |
| 37 |
|
| 38 |
cls_errs = Array.new |
| 39 |
cls_assertions = Array.new |
| 40 |
all_error_mappings = Array.new |
| 41 |
last_cls = "" |
| 42 |
last_cls_full = "" |
| 43 |
|
| 44 |
def build_assert_func(last_cls, last_cls_full, cls_errs) |
| 45 |
<<~GO |
| 46 |
// Is#{last_cls} asserts the error code class is #{last_cls_full} |
| 47 |
func Is#{last_cls} (code string) bool { |
| 48 |
switch code{ |
| 49 |
case #{cls_errs.join(", ")}: |
| 50 |
return true |
| 51 |
} |
| 52 |
return false |
| 53 |
} |
| 54 |
GO |
| 55 |
end |
| 56 |
|
| 57 |
def build_name_func(all_error_mappings) |
| 58 |
<<~GO |
| 59 |
// Name returns the string name for a given PostgreSQL error code. |
| 60 |
// Returns an empty string if the error code is not recognized. |
| 61 |
func Name(code string) string { |
| 62 |
switch code { |
| 63 |
#{all_error_mappings.map { |code, name| " case \"#{code}\":\n return \"#{name}\"" }.join("\n")} |
| 64 |
} |
| 65 |
return "" |
| 66 |
} |
| 67 |
GO |
| 68 |
end |
| 69 |
|
| 70 |
puts <<~STR |
| 71 |
// Package pgerrcode contains constants for PostgreSQL error codes. |
| 72 |
package pgerrcode |
| 73 |
|
| 74 |
// Source: https://www.postgresql.org/docs/16/errcodes-appendix.html |
| 75 |
// See gen.rb for script that can convert the error code table to Go code. |
| 76 |
|
| 77 |
const ( |
| 78 |
STR |
| 79 |
|
| 80 |
ARGF.each do |line| |
| 81 |
case line |
| 82 |
when /^Class/ |
| 83 |
if cls_errs.length > 0 && last_cls != "" |
| 84 |
assert_func = build_assert_func(class_name_overrides.fetch(last_cls) { last_cls }, last_cls_full, cls_errs) |
| 85 |
cls_assertions.push(assert_func) |
| 86 |
end |
| 87 |
last_cls = line.split("—")[1] |
| 88 |
.gsub(" ", "") |
| 89 |
.gsub("/", "") |
| 90 |
.gsub("\n", "") |
| 91 |
.sub(/\(\w*\)/, "") |
| 92 |
last_cls_full = line.gsub("\n", "") |
| 93 |
cls_errs.clear |
| 94 |
puts |
| 95 |
puts "// #{line}" |
| 96 |
when /^(\w{5})\s+(\w+)/ |
| 97 |
code = $1 |
| 98 |
name = code_name_overrides.fetch(code) do |
| 99 |
$2.split("_").map(&:capitalize).join |
| 100 |
.gsub("Sql", "SQL") |
| 101 |
.gsub("Xml", "XML") |
| 102 |
.gsub("Fdw", "FDW") |
| 103 |
.gsub("Srf", "SRF") |
| 104 |
.gsub("Io", "IO") |
| 105 |
.gsub("Json", "JSON") |
| 106 |
end |
| 107 |
cls_errs.push(name) |
| 108 |
all_error_mappings.push([code, name]) |
| 109 |
puts %Q[#{name} = "#{code}"] |
| 110 |
else |
| 111 |
puts line |
| 112 |
end |
| 113 |
end |
| 114 |
puts ")" |
| 115 |
|
| 116 |
if cls_errs.length > 0 |
| 117 |
assert_func = build_assert_func(class_name_overrides.fetch(last_cls) { last_cls }, last_cls_full, cls_errs) |
| 118 |
cls_assertions.push(assert_func) |
| 119 |
end |
| 120 |
|
| 121 |
cls_assertions.each do |cls_assertion| |
| 122 |
puts cls_assertion |
| 123 |
end |
| 124 |
|
| 125 |
puts build_name_func(all_error_mappings) |