| 1 |
// Package internal contains tblfmt internals. |
| 2 |
package internal |
| 3 |
|
| 4 |
import ( |
| 5 |
"bytes" |
| 6 |
"crypto/md5" |
| 7 |
"encoding/json" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"math/rand" |
| 11 |
"os/exec" |
| 12 |
"runtime" |
| 13 |
"strconv" |
| 14 |
"strings" |
| 15 |
"time" |
| 16 |
"unicode" |
| 17 |
) |
| 18 |
|
| 19 |
func init() { |
| 20 |
if runtime.GOOS == "windows" { |
| 21 |
newline = []byte("\r\n") |
| 22 |
} else { |
| 23 |
newline = []byte("\n") |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
// Divider is a divider. |
| 28 |
const Divider = "===============================================" |
| 29 |
|
| 30 |
// RS is a result set. |
| 31 |
type RS struct { |
| 32 |
rs int |
| 33 |
pos int |
| 34 |
cols []string |
| 35 |
vals [][][]any |
| 36 |
} |
| 37 |
|
| 38 |
// New creates a new result set. |
| 39 |
func New(cols []string, vals ...[][]any) *RS { |
| 40 |
return &RS{ |
| 41 |
cols: cols, |
| 42 |
vals: vals, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// Multi creates a result set with multiple result sets. |
| 47 |
func Multi() *RS { |
| 48 |
s, t := rset(14), rset(38) |
| 49 |
r := &RS{ |
| 50 |
cols: []string{"author_id", "name", "z"}, |
| 51 |
vals: [][][]any{ |
| 52 |
s[:2], s[2:], t, |
| 53 |
}, |
| 54 |
} |
| 55 |
return r |
| 56 |
} |
| 57 |
|
| 58 |
// Big creates a random, big result set using the provided seed. |
| 59 |
func Big(seed int64) *RS { |
| 60 |
src := rand.New(rand.NewSource(seed)) |
| 61 |
count := src.Intn(1000) |
| 62 |
// generate rows |
| 63 |
vals := make([][]any, count) |
| 64 |
for i := range count { |
| 65 |
p := newRrow(src) |
| 66 |
vals[i] = []any{i + 1, p.name, p.dob, p.f, p.hash, p.char, p.z} |
| 67 |
} |
| 68 |
return &RS{ |
| 69 |
cols: []string{"id", "name", "dob", "float", "hash", "", "z"}, |
| 70 |
vals: [][][]any{vals}, |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Tiny creates a tiny result set. |
| 75 |
func Tiny() *RS { |
| 76 |
return &RS{ |
| 77 |
cols: []string{"z"}, |
| 78 |
vals: [][][]any{ |
| 79 |
{ |
| 80 |
{"x"}, |
| 81 |
}, |
| 82 |
}, |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Wide creates a wide result set. |
| 87 |
func Wide() *RS { |
| 88 |
return &RS{ |
| 89 |
cols: []string{ |
| 90 |
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 91 |
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 92 |
"cccccccccccccccccccccccccccccc", |
| 93 |
"dddddddddddddddddddddddddddddd", |
| 94 |
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeee", |
| 95 |
"ffffffffffffffffffffffffffffff", |
| 96 |
"gggggggggggggggggggggggggggggg", |
| 97 |
"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", |
| 98 |
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiii", |
| 99 |
"jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", |
| 100 |
}, |
| 101 |
vals: [][][]any{ |
| 102 |
{ |
| 103 |
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, |
| 104 |
}, |
| 105 |
}, |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Err satisfies the ResultSet interface. |
| 110 |
func (*RS) Err() error { |
| 111 |
return nil |
| 112 |
} |
| 113 |
|
| 114 |
// Close satisfies the ResultSet interface. |
| 115 |
func (*RS) Close() error { |
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
// Columns satisfies the ResultSet interface. |
| 120 |
func (r *RS) Columns() ([]string, error) { |
| 121 |
return r.cols, nil |
| 122 |
} |
| 123 |
|
| 124 |
// Next satisfies the ResultSet interface. |
| 125 |
func (r *RS) Next() bool { |
| 126 |
return r.pos < len(r.vals[r.rs]) |
| 127 |
} |
| 128 |
|
| 129 |
// Scan satisfies the ResultSet interface. |
| 130 |
func (r *RS) Scan(vals ...any) error { |
| 131 |
for i := range vals { |
| 132 |
x, ok := vals[i].(*any) |
| 133 |
if !ok { |
| 134 |
return fmt.Errorf("scan for col %d expected *interface{}, got: %T", i, vals[i]) |
| 135 |
} |
| 136 |
*x = r.vals[r.rs][r.pos][i] |
| 137 |
} |
| 138 |
r.pos++ |
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
// NextResultSet satisfies the ResultSet interface. |
| 143 |
func (r *RS) NextResultSet() bool { |
| 144 |
r.rs, r.pos = r.rs+1, 0 |
| 145 |
return r.rs < len(r.vals) |
| 146 |
} |
| 147 |
|
| 148 |
// Reset resets the rset so that it can be used repeatedly. |
| 149 |
func (r *RS) Reset() { |
| 150 |
r.pos, r.rs = 0, 0 |
| 151 |
} |
| 152 |
|
| 153 |
// rrow is a random row. |
| 154 |
type rrow struct { |
| 155 |
name string |
| 156 |
dob time.Time |
| 157 |
f float64 |
| 158 |
hash []byte |
| 159 |
char []byte |
| 160 |
z any |
| 161 |
} |
| 162 |
|
| 163 |
// newRrow creates a new random row using the rand source. |
| 164 |
func newRrow(src *rand.Rand) rrow { |
| 165 |
hash := md5.Sum([]byte(rstr(src))) |
| 166 |
var char []byte |
| 167 |
if src.Intn(2) == 1 { |
| 168 |
char = []byte{byte(int('a') + src.Intn(26))} |
| 169 |
} |
| 170 |
var z any |
| 171 |
switch src.Intn(4) { |
| 172 |
case 0, 1: |
| 173 |
case 2: |
| 174 |
c := 1 + src.Intn(5) |
| 175 |
m := make(map[string]any, c) |
| 176 |
for range c { |
| 177 |
r := []rune(rstr(src)) |
| 178 |
m[string(r[0:3])] = string(r[3:]) |
| 179 |
} |
| 180 |
z = m |
| 181 |
case 3: |
| 182 |
y := make([]any, 1+src.Intn(5)) |
| 183 |
for i := range y { |
| 184 |
r := []rune(rstr(src)) |
| 185 |
y[i] = string(r[0 : 1+src.Intn(6)]) |
| 186 |
} |
| 187 |
z = y |
| 188 |
} |
| 189 |
return rrow{ |
| 190 |
name: rstr(src), |
| 191 |
dob: rtime(src), |
| 192 |
f: src.Float64(), |
| 193 |
hash: fmt.Appendf(nil, "%x", hash[:]), |
| 194 |
char: char, |
| 195 |
z: z, |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// rstr creates a random string using the rand source. |
| 200 |
func rstr(src *rand.Rand) string { |
| 201 |
l := 6 + src.Intn(32) |
| 202 |
r := make([]rune, l) |
| 203 |
for i := range l { |
| 204 |
r[i] = glyphs[src.Intn(len(glyphs))] |
| 205 |
} |
| 206 |
return string(r) |
| 207 |
} |
| 208 |
|
| 209 |
// rtime creates a random time using the rand source. |
| 210 |
func rtime(src *rand.Rand) time.Time { |
| 211 |
a := time.Date(1970, 1, 0, 0, 0, 0, 0, time.UTC).Unix() |
| 212 |
b := time.Date(2070, 1, 0, 0, 0, 0, 0, time.UTC).Unix() |
| 213 |
delta := b - a |
| 214 |
return time.Unix(src.Int63n(delta)+a, 0).UTC() |
| 215 |
} |
| 216 |
|
| 217 |
// rset returns predefined record set values. |
| 218 |
func rset(i int) [][]any { |
| 219 |
return [][]any{ |
| 220 |
{float64(i), "a\tb\tc\td", "x"}, |
| 221 |
{float64(i + 1), "aoeu\ntest\n", nil}, |
| 222 |
{float64(i + 2), "foo\bbar", nil}, |
| 223 |
{float64(i + 3), "袈\t袈\t\t袈", nil}, |
| 224 |
{float64(i + 4), "a\tb\t\r\n\ta", "a\n"}, |
| 225 |
{float64(i + 5), "袈\t袈\t\t袈\n", nil}, |
| 226 |
{float64(i + 6), "javascript", map[string]any{ |
| 227 |
fmt.Sprintf("test%d", i+7): "a value", |
| 228 |
fmt.Sprintf("test%d", i+8): "foo\bbar", |
| 229 |
}}, |
| 230 |
{float64(i + 9), "slice", []string{"a", "b"}}, |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// ResultSet is the shared interface for a result set. |
| 235 |
type ResultSet interface { |
| 236 |
Next() bool |
| 237 |
Scan(...any) error |
| 238 |
Columns() ([]string, error) |
| 239 |
Close() error |
| 240 |
Err() error |
| 241 |
NextResultSet() bool |
| 242 |
} |
| 243 |
|
| 244 |
// PsqlEncodeAll does a values query for each of the values in the result set, |
| 245 |
// writing captured output to the writer. |
| 246 |
func PsqlEncodeAll(w io.Writer, resultSet ResultSet, params map[string]string, dsn string) error { |
| 247 |
if err := PsqlEncode(w, resultSet, params, dsn); err != nil { |
| 248 |
return err |
| 249 |
} |
| 250 |
for resultSet.NextResultSet() { |
| 251 |
if _, err := w.Write(newline); err != nil { |
| 252 |
return err |
| 253 |
} |
| 254 |
if err := PsqlEncode(w, resultSet, params, dsn); err != nil { |
| 255 |
return err |
| 256 |
} |
| 257 |
} |
| 258 |
if _, err := w.Write(newline); err != nil { |
| 259 |
return err |
| 260 |
} |
| 261 |
return nil |
| 262 |
} |
| 263 |
|
| 264 |
// PsqlEncode does a single value query using psql, writing the captured output |
| 265 |
// to the writer. |
| 266 |
func PsqlEncode(w io.Writer, resultSet ResultSet, params map[string]string, dsn string) error { |
| 267 |
// read values |
| 268 |
var vals strings.Builder |
| 269 |
var i int |
| 270 |
for resultSet.Next() { |
| 271 |
var id, name, z any |
| 272 |
if err := resultSet.Scan(&id, &name, &z); err != nil { |
| 273 |
return err |
| 274 |
} |
| 275 |
var extra string |
| 276 |
if i != 0 { |
| 277 |
extra = "," |
| 278 |
} |
| 279 |
n := name.(string) |
| 280 |
vals.WriteString(fmt.Sprintf("%s\n (%v,E'%s', %s)", extra, id, psqlEsc(n), psqlEnc(n, z))) |
| 281 |
i++ |
| 282 |
} |
| 283 |
if err := resultSet.Err(); err != nil { |
| 284 |
return err |
| 285 |
} |
| 286 |
// build pset |
| 287 |
var pset strings.Builder |
| 288 |
for k, v := range params { |
| 289 |
pset.WriteString(fmt.Sprintf("\n\\pset %s '%s'", k, v)) |
| 290 |
} |
| 291 |
// exec |
| 292 |
stdout := new(bytes.Buffer) |
| 293 |
q := fmt.Sprintf(psqlValuesQuery, pset.String(), vals.String()) |
| 294 |
cmd := exec.Command("psql", dsn, "-qX") |
| 295 |
cmd.Stdin, cmd.Stdout = bytes.NewReader([]byte(q)), stdout |
| 296 |
if err := cmd.Run(); err != nil { |
| 297 |
return err |
| 298 |
} |
| 299 |
if _, err := w.Write(bytes.TrimRightFunc(stdout.Bytes(), unicode.IsSpace)); err != nil { |
| 300 |
return err |
| 301 |
} |
| 302 |
_, err := w.Write(newline) |
| 303 |
return err |
| 304 |
} |
| 305 |
|
| 306 |
// psqlEsc escapes a string as a psql string. |
| 307 |
func psqlEsc(s string) string { |
| 308 |
s = strings.ReplaceAll(s, "\n", `\n`) |
| 309 |
s = strings.ReplaceAll(s, "\r", `\r`) |
| 310 |
s = strings.ReplaceAll(s, "\t", `\t`) |
| 311 |
s = strings.ReplaceAll(s, "\b", `\b`) |
| 312 |
s = strings.ReplaceAll(s, "袈", `\u8888`) |
| 313 |
return s |
| 314 |
} |
| 315 |
|
| 316 |
// psqlEnc encodes v based on n. |
| 317 |
func psqlEnc(n string, v any) string { |
| 318 |
if n != "javascript" && n != "slice" { |
| 319 |
return "NULL" |
| 320 |
} |
| 321 |
buf, err := json.MarshalIndent(v, "", " ") |
| 322 |
if err != nil { |
| 323 |
panic(err) |
| 324 |
} |
| 325 |
s := strconv.QuoteToASCII(string(buf)) |
| 326 |
return "E'" + s[1:len(s)-1] + "'" |
| 327 |
} |
| 328 |
|
| 329 |
var glyphs = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _'\"\t\b\n\rゼ一二三四五六七八九十〇") |
| 330 |
|
| 331 |
const psqlValuesQuery = `%s |
| 332 |
SELECT * FROM ( |
| 333 |
VALUES%s |
| 334 |
) AS t (author_id, name, z);` |
| 335 |
|
| 336 |
// newline is the default newline used by the system. |
| 337 |
var newline []byte |