ironwork-rt 0.1.2

ironwork for COBOL: the runtime a compiled program links, under the runtime exception
Documentation
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Recordings: each call a run made and the answer it got, as text a person can read and edit.
//! [`Replay`] answers from a recording; [`Recorder`] writes one while another backend answers.
//!
//! ```text
//! # ironwork sql recording 1
//! @ 1 PAYROLL:3:9f2a41c0 SELECT
//! > char:"00123"
//! < 0 00000 rows=1
//! = dec:1234.50 | char:"SMITH" | null
//! ```

use super::{Abandoned, Answer, Call, Database, Outcome, Value};
use std::io::Write;
use super::fingerprint;

const HEADER: &str = "# ironwork sql recording 1";

#[derive(Debug)]
struct Entry {
    program: String,
    ordinal: u32,
    hash: u32,
    verb: String,
    cursor: Option<String>,
    inputs: Vec<Value>,
    outcome: Outcome,
}

impl Entry {
    fn answers(&self, call: &Call) -> bool {
        self.program == call.program
            && self.ordinal == call.ordinal
            && self.hash == fingerprint(call.text)
            && self.verb == call.verb
            && self.cursor.as_deref() == call.cursor
            && self.inputs == call.inputs
    }
}

fn describe(program: &str, ordinal: u32, hash: u32, verb: &str, cursor: Option<&str>, inputs: &[Value]) -> String {
    let cursor = cursor.map(|c| format!(" {c}")).unwrap_or_default();
    let inputs = if inputs.is_empty() { String::new() } else { format!(" with {}", values_text(inputs)) };
    format!("{program}:{ordinal}:{hash:08x} {verb}{cursor}{inputs}")
}

/// Answers calls from a recording. Strict replay answers call n from entry n; keyed replay answers
/// each call from the first unused entry with the same statement and inputs. A call the recording
/// does not hold ends the run.
pub struct Replay {
    entries: Vec<Entry>,
    used: Vec<bool>,
    next: usize,
    keyed: bool,
}

impl Replay {
    pub fn parse(text: &str, keyed: bool) -> Result<Self, String> {
        let mut entries: Vec<Entry> = Vec::new();
        let mut header = false;
        let mut awaiting_outcome = false;
        for (n, line) in text.lines().enumerate().map(|(i, l)| (i + 1, l.trim_end())) {
            let fail = |why: String| format!("line {n}: {why}");
            if line.is_empty() {
                continue;
            }
            if line.starts_with('#') {
                header |= line == HEADER;
                continue;
            }
            if !header {
                return Err(fail(format!("a recording starts with \"{HEADER}\"")));
            }
            let (mark, rest) = line.split_at(1);
            let rest = rest.trim_start();
            match mark {
                "@" => {
                    if awaiting_outcome {
                        return Err(fail("the call before this one has no < line".into()));
                    }
                    let words: Vec<&str> = rest.split_whitespace().collect();
                    let [_, id, verb, cursor @ ..] = words.as_slice() else { return Err(fail("@ takes a number, PROGRAM:ORDINAL:HASH and a verb".into())) };
                    let parts: Vec<&str> = id.split(':').collect();
                    let [program, ordinal, hash] = parts.as_slice() else { return Err(fail(format!("{id} is not PROGRAM:ORDINAL:HASH"))) };
                    let ordinal = ordinal.parse().map_err(|_| fail(format!("{ordinal} is not an ordinal")))?;
                    let hash = u32::from_str_radix(hash, 16).map_err(|_| fail(format!("{hash} is not a hexadecimal hash")))?;
                    let cursor = match cursor {
                        [] => None,
                        [c] => Some((*c).to_owned()),
                        _ => return Err(fail("@ takes at most one cursor after the verb".into())),
                    };
                    entries.push(Entry { program: (*program).into(), ordinal, hash, verb: (*verb).into(), cursor, inputs: Vec::new(), outcome: Outcome::ok() });
                    awaiting_outcome = true;
                }
                ">" => match entries.last_mut() {
                    Some(e) if awaiting_outcome && e.inputs.is_empty() => e.inputs = parse_values(rest).map_err(fail)?,
                    _ => return Err(fail("a > line belongs after an @ line, before its < line".into())),
                },
                "<" => match entries.last_mut() {
                    Some(e) if awaiting_outcome => {
                        e.outcome = parse_outcome(rest).map_err(fail)?;
                        awaiting_outcome = false;
                    }
                    _ => return Err(fail("a < line belongs after an @ line".into())),
                },
                "=" => match entries.last_mut() {
                    Some(e) if !awaiting_outcome => e.outcome.rows.push(parse_values(rest).map_err(fail)?),
                    _ => return Err(fail("an = line belongs after a < line".into())),
                },
                _ => return Err(fail(format!("{mark} starts no kind of line"))),
            }
        }
        if awaiting_outcome {
            return Err("the last call has no < line".into());
        }
        if !header {
            return Err(format!("a recording starts with \"{HEADER}\""));
        }
        let used = vec![false; entries.len()];
        Ok(Self { entries, used, next: 0, keyed })
    }

    fn answer(&mut self, call: &Call) -> Answer {
        let found = if self.keyed {
            (0..self.entries.len()).find(|&i| !self.used[i] && self.entries[i].answers(call))
        } else {
            (self.next < self.entries.len() && self.entries[self.next].answers(call)).then_some(self.next)
        };
        let actual = describe(call.program, call.ordinal, fingerprint(call.text), call.verb, call.cursor, call.inputs);
        let Some(i) = found else {
            let expected = match self.entries.get(self.next).filter(|_| !self.keyed) {
                Some(e) => format!("the recording's call {} is {}", self.next + 1, describe(&e.program, e.ordinal, e.hash, &e.verb, e.cursor.as_deref(), &e.inputs)),
                None => "the recording holds no such call".into(),
            };
            return Err(Abandoned { code: "SQLR", message: format!("{expected}, and the run made {actual}") });
        };
        self.used[i] = true;
        self.next = i + 1;
        Ok(self.entries[i].outcome.clone())
    }
}

impl Database for Replay {
    fn execute(&mut self, call: &Call) -> Answer {
        self.answer(call)
    }
    fn open(&mut self, call: &Call) -> Answer {
        self.answer(call)
    }
    fn fetch(&mut self, call: &Call) -> Answer {
        self.answer(call)
    }
    fn close(&mut self, call: &Call) -> Answer {
        self.answer(call)
    }
    fn commit(&mut self, call: &Call) -> Answer {
        self.answer(call)
    }
    fn rollback(&mut self, call: &Call) -> Answer {
        self.answer(call)
    }
}

/// Writes every call and its answer while `inner` answers.
pub struct Recorder<'w> {
    inner: Box<dyn Database + 'w>,
    out: Box<dyn Write + 'w>,
    seq: u64,
}

impl<'w> Recorder<'w> {
    /// `source` names what answered, and goes in the recording's header.
    pub fn new(inner: Box<dyn Database + 'w>, mut out: Box<dyn Write + 'w>, source: &str) -> std::io::Result<Self> {
        writeln!(out, "{HEADER}\n# source: {source}")?;
        Ok(Self { inner, out, seq: 0 })
    }

    fn record(&mut self, call: &Call, answer: Answer) -> Answer {
        let outcome = answer?;
        self.seq += 1;
        let text = entry_text(self.seq, call, &outcome);
        // Flushed per call: a served session ends when the server is interrupted, not by returning.
        self.out.write_all(text.as_bytes()).and_then(|()| self.out.flush()).map_err(|e| Abandoned { code: "SQLR", message: format!("the recording could not be written: {e}") })?;
        Ok(outcome)
    }
}

impl Database for Recorder<'_> {
    fn execute(&mut self, call: &Call) -> Answer {
        let a = self.inner.execute(call);
        self.record(call, a)
    }
    fn open(&mut self, call: &Call) -> Answer {
        let a = self.inner.open(call);
        self.record(call, a)
    }
    fn fetch(&mut self, call: &Call) -> Answer {
        let a = self.inner.fetch(call);
        self.record(call, a)
    }
    fn close(&mut self, call: &Call) -> Answer {
        let a = self.inner.close(call);
        self.record(call, a)
    }
    fn commit(&mut self, call: &Call) -> Answer {
        let a = self.inner.commit(call);
        self.record(call, a)
    }
    fn rollback(&mut self, call: &Call) -> Answer {
        let a = self.inner.rollback(call);
        self.record(call, a)
    }
    fn close_all(&mut self) -> Result<(), Abandoned> {
        self.inner.close_all()
    }
}

fn entry_text(seq: u64, call: &Call, outcome: &Outcome) -> String {
    let cursor = call.cursor.map(|c| format!(" {c}")).unwrap_or_default();
    let mut text = format!("@ {seq} {}:{}:{:08x} {}{cursor}\n", call.program, call.ordinal, fingerprint(call.text), call.verb);
    if !call.inputs.is_empty() {
        text += &format!("> {}\n", values_text(call.inputs));
    }
    text += &format!("< {} {} rows={}", outcome.sqlcode, outcome.sqlstate, outcome.affected);
    if !outcome.tokens.is_empty() {
        text += &format!(" tokens={}", value_text(&Value::Char(outcome.tokens.clone())));
    }
    text.push('\n');
    for row in &outcome.rows {
        text += &format!("= {}\n", values_text(row));
    }
    text
}

fn values_text(values: &[Value]) -> String {
    values.iter().map(value_text).collect::<Vec<_>>().join(" | ")
}

fn value_text(v: &Value) -> String {
    match v {
        Value::Null => "null".into(),
        Value::Int(i) => format!("int:{i}"),
        Value::Decimal { value, scale } => format!("dec:{}", Value::decimal_text(*value, *scale)),
        Value::Double(f) => format!("double:{f:?}"),
        Value::Char(s) => {
            let mut q = String::from("char:\"");
            for c in s.chars() {
                match c {
                    '"' => q += "\\\"",
                    '\\' => q += "\\\\",
                    c if c.is_control() => q += &format!("\\x{:02X}", c as u32),
                    c => q.push(c),
                }
            }
            q + "\""
        }
        Value::Binary(b) => format!("hex:{}", b.iter().map(|x| format!("{x:02X}")).collect::<String>()),
    }
}

fn parse_outcome(text: &str) -> Result<Outcome, String> {
    let mut words = text.splitn(4, ' ');
    let (Some(code), Some(state), Some(rows)) = (words.next(), words.next(), words.next()) else { return Err("< takes SQLCODE, SQLSTATE and rows=N".into()) };
    let sqlcode = code.parse().map_err(|_| format!("{code} is not an SQLCODE"))?;
    if state.len() != 5 {
        return Err(format!("{state} is not a five-character SQLSTATE"));
    }
    let affected = rows.strip_prefix("rows=").and_then(|n| n.parse().ok()).ok_or_else(|| format!("{rows} is not rows=N"))?;
    let tokens = match words.next().map(str::trim) {
        None | Some("") => String::new(),
        Some(t) => match t.strip_prefix("tokens=").map(parse_value) {
            Some(Ok((Value::Char(s), rest))) if rest.trim().is_empty() => s,
            _ => return Err("the rest of a < line is tokens=char:\"...\"".into()),
        },
    };
    Ok(Outcome { sqlcode, sqlstate: state.into(), affected, rows: Vec::new(), tokens })
}

fn parse_values(text: &str) -> Result<Vec<Value>, String> {
    let (mut out, mut rest) = (Vec::new(), text.trim_start());
    while !rest.is_empty() {
        let (v, after) = parse_value(rest)?;
        out.push(v);
        rest = after.trim_start();
        if let Some(next) = rest.strip_prefix('|') {
            rest = next.trim_start();
            if rest.is_empty() {
                return Err("a value is missing after |".into());
            }
        } else if !rest.is_empty() {
            return Err(format!("values are separated by |, not \"{rest}\""));
        }
    }
    Ok(out)
}

/// One value literal from the start of `text`, and what follows it.
fn parse_value(text: &str) -> Result<(Value, &str), String> {
    if let Some(quoted) = text.strip_prefix("char:\"") {
        let mut s = String::new();
        let mut chars = quoted.char_indices();
        while let Some((i, c)) = chars.next() {
            match c {
                '"' => return Ok((Value::Char(s), &quoted[i + 1..])),
                '\\' => match chars.next() {
                    Some((_, '"')) => s.push('"'),
                    Some((_, '\\')) => s.push('\\'),
                    Some((j, 'x')) => {
                        let hex = quoted.get(j + 1..j + 3).ok_or("\\x takes two hexadecimal digits")?;
                        let code = u32::from_str_radix(hex, 16).map_err(|_| format!("\\x{hex} is not hexadecimal"))?;
                        s.push(char::from_u32(code).ok_or("\\x names no character")?);
                        chars.next();
                        chars.next();
                    }
                    _ => return Err("a backslash in char:\"...\" escapes \", \\ or xNN".into()),
                },
                c => s.push(c),
            }
        }
        return Err("char:\" is not closed".into());
    }
    let end = text.find(|c: char| c.is_whitespace() || c == '|').unwrap_or(text.len());
    let (word, rest) = text.split_at(end);
    let value = match word.split_once(':') {
        None if word == "null" => Value::Null,
        Some(("int", n)) => Value::Int(n.parse().map_err(|_| format!("{word} is not an integer"))?),
        Some(("dec", n)) => Value::parse_decimal(n).ok_or_else(|| format!("{word} is not a decimal"))?,
        Some(("double", n)) => Value::Double(n.parse().map_err(|_| format!("{word} is not a double"))?),
        Some(("hex", h)) if h.len() % 2 == 0 => {
            let bytes: Result<Vec<u8>, _> = (0..h.len()).step_by(2).map(|i| u8::from_str_radix(&h[i..i + 2], 16)).collect();
            Value::Binary(bytes.map_err(|_| format!("{word} is not hexadecimal"))?)
        }
        _ => return Err(format!("{word} is not a value: null, int:, dec:, double:, char:\"...\" or hex:")),
    };
    Ok((value, rest))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn call<'a>(verb: &'a str, text: &'a str, inputs: &'a [Value]) -> Call<'a> {
        Call { program: "P", ordinal: 2, verb, cursor: None, text, inputs }
    }

    #[test]
    fn values_round_trip() {
        let values = vec![
            Value::Null,
            Value::Int(-42),
            Value::Decimal { value: -123_450, scale: 2 },
            Value::Decimal { value: 5, scale: 2 },
            Value::Decimal { value: 7, scale: 0 },
            Value::Double(0.1),
            Value::Double(6.02e23),
            Value::Char("say \"hi\" | x \\ é\n".into()),
            Value::Binary(vec![0xC1, 0x00]),
        ];
        let text = values_text(&values);
        assert!(text.contains("dec:-1234.50") && text.contains("dec:0.05") && text.contains("dec:7"), "{text}");
        assert_eq!(parse_values(&text), Ok(values));
    }

    #[test]
    fn a_recording_answers_the_calls_it_holds_in_order() {
        let inputs = [Value::Int(7)];
        let first = entry_text(1, &call("SELECT", "SELECT A FROM T WHERE K = ?", &inputs), &Outcome::rows(vec![vec![Value::Char("X".into())]]));
        let second = entry_text(2, &call("COMMIT", "COMMIT", &[]), &Outcome::ok());
        let mut replay = Replay::parse(&format!("{HEADER}\n{first}{second}"), false).expect("parses");
        assert_eq!(replay.execute(&call("SELECT", "SELECT A FROM T WHERE K = ?", &inputs)).unwrap().rows, [[Value::Char("X".into())]]);
        assert_eq!(replay.commit(&call("COMMIT", "COMMIT", &[])), Ok(Outcome::ok()));
        let beyond = replay.commit(&call("COMMIT", "COMMIT", &[])).unwrap_err();
        assert_eq!((beyond.code, beyond.message.starts_with("the recording holds no such call")), ("SQLR", true));
    }

    #[test]
    fn strict_replay_refuses_a_different_call_and_names_both() {
        let text = format!("{HEADER}\n{}", entry_text(1, &call("SELECT", "SELECT A FROM T WHERE K = ?", &[Value::Int(7)]), &Outcome::ok()));
        let mut replay = Replay::parse(&text, false).unwrap();
        let err = replay.execute(&call("SELECT", "SELECT A FROM T WHERE K = ?", &[Value::Int(8)])).unwrap_err();
        assert_eq!(err.code, "SQLR");
        assert!(err.message.contains("with int:7") && err.message.contains("with int:8"), "{}", err.message);
    }

    #[test]
    fn keyed_replay_takes_calls_in_any_order() {
        let (a, b) = ([Value::Int(1)], [Value::Int(2)]);
        let text = format!(
            "{HEADER}\n{}{}",
            entry_text(1, &call("SELECT", "Q", &a), &Outcome::rows(vec![vec![Value::Int(10)]])),
            entry_text(2, &call("SELECT", "Q", &b), &Outcome::rows(vec![vec![Value::Int(20)]]))
        );
        let mut replay = Replay::parse(&text, true).unwrap();
        assert_eq!(replay.execute(&call("SELECT", "Q", &b)).unwrap().rows, [[Value::Int(20)]]);
        assert_eq!(replay.execute(&call("SELECT", "Q", &a)).unwrap().rows, [[Value::Int(10)]]);
        assert!(replay.execute(&call("SELECT", "Q", &a)).is_err());
    }

    #[test]
    fn a_recorder_writes_what_replay_reads() {
        struct Fixed;
        impl Database for Fixed {
            fn execute(&mut self, _: &Call) -> Answer {
                Ok(Outcome { tokens: "T1".into(), ..Outcome::rows(vec![vec![Value::Decimal { value: 150, scale: 2 }, Value::Null]]) })
            }
            fn open(&mut self, _: &Call) -> Answer {
                Ok(Outcome::ok())
            }
            fn fetch(&mut self, _: &Call) -> Answer {
                Ok(Outcome::error(100, "02000"))
            }
            fn close(&mut self, _: &Call) -> Answer {
                Ok(Outcome::ok())
            }
            fn commit(&mut self, _: &Call) -> Answer {
                Ok(Outcome::ok())
            }
            fn rollback(&mut self, _: &Call) -> Answer {
                Ok(Outcome::ok())
            }
        }
        let written = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
        struct Sink(std::rc::Rc<std::cell::RefCell<Vec<u8>>>);
        impl Write for Sink {
            fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
                self.0.borrow_mut().extend_from_slice(b);
                Ok(b.len())
            }
            fn flush(&mut self) -> std::io::Result<()> {
                Ok(())
            }
        }
        let inputs = [Value::Char("A|B".into())];
        let mut recorder = Recorder::new(Box::new(Fixed), Box::new(Sink(written.clone())), "a test double").unwrap();
        let live = recorder.execute(&call("SELECT", "SELECT X, Y FROM T WHERE Z = ?", &inputs)).unwrap();
        let text = String::from_utf8(written.borrow().clone()).unwrap();
        assert!(text.starts_with(&format!("{HEADER}\n# source: a test double\n")), "{text}");
        let mut replay = Replay::parse(&text, false).unwrap();
        assert_eq!(replay.execute(&call("SELECT", "SELECT X, Y FROM T WHERE Z = ?", &inputs)), Ok(live));
    }

    #[test]
    fn malformed_recordings_name_the_line() {
        assert_eq!(Replay::parse("@ 1 P:1:0 SELECT\n< 0 00000 rows=0\n", false).err().unwrap(), format!("line 1: a recording starts with \"{HEADER}\""));
        let err = Replay::parse(&format!("{HEADER}\n@ 1 P:1:0 SELECT\n= int:1\n"), false).err().unwrap();
        assert_eq!(err, "line 3: an = line belongs after a < line");
        assert!(Replay::parse(&format!("{HEADER}\n@ 1 P:1:0 SELECT\n< 0 00000 rows=0\n= int:x\n"), false).err().unwrap().starts_with("line 4: "));
        assert_eq!(Replay::parse(&format!("{HEADER}\n@ 1 P:1:0 SELECT\n"), false).err().unwrap(), "the last call has no < line");
    }
}