1use super::{Abandoned, Answer, Call, Database, Outcome, Value};
13use std::io::Write;
14use super::fingerprint;
15
16const HEADER: &str = "# ironwork sql recording 1";
17
18#[derive(Debug)]
19struct Entry {
20 program: String,
21 ordinal: u32,
22 hash: u32,
23 verb: String,
24 cursor: Option<String>,
25 inputs: Vec<Value>,
26 outcome: Outcome,
27}
28
29impl Entry {
30 fn answers(&self, call: &Call) -> bool {
31 self.program == call.program
32 && self.ordinal == call.ordinal
33 && self.hash == fingerprint(call.text)
34 && self.verb == call.verb
35 && self.cursor.as_deref() == call.cursor
36 && self.inputs == call.inputs
37 }
38}
39
40fn describe(program: &str, ordinal: u32, hash: u32, verb: &str, cursor: Option<&str>, inputs: &[Value]) -> String {
41 let cursor = cursor.map(|c| format!(" {c}")).unwrap_or_default();
42 let inputs = if inputs.is_empty() { String::new() } else { format!(" with {}", values_text(inputs)) };
43 format!("{program}:{ordinal}:{hash:08x} {verb}{cursor}{inputs}")
44}
45
46pub struct Replay {
50 entries: Vec<Entry>,
51 used: Vec<bool>,
52 next: usize,
53 keyed: bool,
54}
55
56impl Replay {
57 pub fn parse(text: &str, keyed: bool) -> Result<Self, String> {
58 let mut entries: Vec<Entry> = Vec::new();
59 let mut header = false;
60 let mut awaiting_outcome = false;
61 for (n, line) in text.lines().enumerate().map(|(i, l)| (i + 1, l.trim_end())) {
62 let fail = |why: String| format!("line {n}: {why}");
63 if line.is_empty() {
64 continue;
65 }
66 if line.starts_with('#') {
67 header |= line == HEADER;
68 continue;
69 }
70 if !header {
71 return Err(fail(format!("a recording starts with \"{HEADER}\"")));
72 }
73 let (mark, rest) = line.split_at(1);
74 let rest = rest.trim_start();
75 match mark {
76 "@" => {
77 if awaiting_outcome {
78 return Err(fail("the call before this one has no < line".into()));
79 }
80 let words: Vec<&str> = rest.split_whitespace().collect();
81 let [_, id, verb, cursor @ ..] = words.as_slice() else { return Err(fail("@ takes a number, PROGRAM:ORDINAL:HASH and a verb".into())) };
82 let parts: Vec<&str> = id.split(':').collect();
83 let [program, ordinal, hash] = parts.as_slice() else { return Err(fail(format!("{id} is not PROGRAM:ORDINAL:HASH"))) };
84 let ordinal = ordinal.parse().map_err(|_| fail(format!("{ordinal} is not an ordinal")))?;
85 let hash = u32::from_str_radix(hash, 16).map_err(|_| fail(format!("{hash} is not a hexadecimal hash")))?;
86 let cursor = match cursor {
87 [] => None,
88 [c] => Some((*c).to_owned()),
89 _ => return Err(fail("@ takes at most one cursor after the verb".into())),
90 };
91 entries.push(Entry { program: (*program).into(), ordinal, hash, verb: (*verb).into(), cursor, inputs: Vec::new(), outcome: Outcome::ok() });
92 awaiting_outcome = true;
93 }
94 ">" => match entries.last_mut() {
95 Some(e) if awaiting_outcome && e.inputs.is_empty() => e.inputs = parse_values(rest).map_err(fail)?,
96 _ => return Err(fail("a > line belongs after an @ line, before its < line".into())),
97 },
98 "<" => match entries.last_mut() {
99 Some(e) if awaiting_outcome => {
100 e.outcome = parse_outcome(rest).map_err(fail)?;
101 awaiting_outcome = false;
102 }
103 _ => return Err(fail("a < line belongs after an @ line".into())),
104 },
105 "=" => match entries.last_mut() {
106 Some(e) if !awaiting_outcome => e.outcome.rows.push(parse_values(rest).map_err(fail)?),
107 _ => return Err(fail("an = line belongs after a < line".into())),
108 },
109 _ => return Err(fail(format!("{mark} starts no kind of line"))),
110 }
111 }
112 if awaiting_outcome {
113 return Err("the last call has no < line".into());
114 }
115 if !header {
116 return Err(format!("a recording starts with \"{HEADER}\""));
117 }
118 let used = vec![false; entries.len()];
119 Ok(Self { entries, used, next: 0, keyed })
120 }
121
122 fn answer(&mut self, call: &Call) -> Answer {
123 let found = if self.keyed {
124 (0..self.entries.len()).find(|&i| !self.used[i] && self.entries[i].answers(call))
125 } else {
126 (self.next < self.entries.len() && self.entries[self.next].answers(call)).then_some(self.next)
127 };
128 let actual = describe(call.program, call.ordinal, fingerprint(call.text), call.verb, call.cursor, call.inputs);
129 let Some(i) = found else {
130 let expected = match self.entries.get(self.next).filter(|_| !self.keyed) {
131 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)),
132 None => "the recording holds no such call".into(),
133 };
134 return Err(Abandoned { code: "SQLR", message: format!("{expected}, and the run made {actual}") });
135 };
136 self.used[i] = true;
137 self.next = i + 1;
138 Ok(self.entries[i].outcome.clone())
139 }
140}
141
142impl Database for Replay {
143 fn execute(&mut self, call: &Call) -> Answer {
144 self.answer(call)
145 }
146 fn open(&mut self, call: &Call) -> Answer {
147 self.answer(call)
148 }
149 fn fetch(&mut self, call: &Call) -> Answer {
150 self.answer(call)
151 }
152 fn close(&mut self, call: &Call) -> Answer {
153 self.answer(call)
154 }
155 fn commit(&mut self, call: &Call) -> Answer {
156 self.answer(call)
157 }
158 fn rollback(&mut self, call: &Call) -> Answer {
159 self.answer(call)
160 }
161}
162
163pub struct Recorder<'w> {
165 inner: Box<dyn Database + 'w>,
166 out: Box<dyn Write + 'w>,
167 seq: u64,
168}
169
170impl<'w> Recorder<'w> {
171 pub fn new(inner: Box<dyn Database + 'w>, mut out: Box<dyn Write + 'w>, source: &str) -> std::io::Result<Self> {
173 writeln!(out, "{HEADER}\n# source: {source}")?;
174 Ok(Self { inner, out, seq: 0 })
175 }
176
177 fn record(&mut self, call: &Call, answer: Answer) -> Answer {
178 let outcome = answer?;
179 self.seq += 1;
180 let text = entry_text(self.seq, call, &outcome);
181 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}") })?;
183 Ok(outcome)
184 }
185}
186
187impl Database for Recorder<'_> {
188 fn execute(&mut self, call: &Call) -> Answer {
189 let a = self.inner.execute(call);
190 self.record(call, a)
191 }
192 fn open(&mut self, call: &Call) -> Answer {
193 let a = self.inner.open(call);
194 self.record(call, a)
195 }
196 fn fetch(&mut self, call: &Call) -> Answer {
197 let a = self.inner.fetch(call);
198 self.record(call, a)
199 }
200 fn close(&mut self, call: &Call) -> Answer {
201 let a = self.inner.close(call);
202 self.record(call, a)
203 }
204 fn commit(&mut self, call: &Call) -> Answer {
205 let a = self.inner.commit(call);
206 self.record(call, a)
207 }
208 fn rollback(&mut self, call: &Call) -> Answer {
209 let a = self.inner.rollback(call);
210 self.record(call, a)
211 }
212 fn close_all(&mut self) -> Result<(), Abandoned> {
213 self.inner.close_all()
214 }
215}
216
217fn entry_text(seq: u64, call: &Call, outcome: &Outcome) -> String {
218 let cursor = call.cursor.map(|c| format!(" {c}")).unwrap_or_default();
219 let mut text = format!("@ {seq} {}:{}:{:08x} {}{cursor}\n", call.program, call.ordinal, fingerprint(call.text), call.verb);
220 if !call.inputs.is_empty() {
221 text += &format!("> {}\n", values_text(call.inputs));
222 }
223 text += &format!("< {} {} rows={}", outcome.sqlcode, outcome.sqlstate, outcome.affected);
224 if !outcome.tokens.is_empty() {
225 text += &format!(" tokens={}", value_text(&Value::Char(outcome.tokens.clone())));
226 }
227 text.push('\n');
228 for row in &outcome.rows {
229 text += &format!("= {}\n", values_text(row));
230 }
231 text
232}
233
234fn values_text(values: &[Value]) -> String {
235 values.iter().map(value_text).collect::<Vec<_>>().join(" | ")
236}
237
238fn value_text(v: &Value) -> String {
239 match v {
240 Value::Null => "null".into(),
241 Value::Int(i) => format!("int:{i}"),
242 Value::Decimal { value, scale } => format!("dec:{}", Value::decimal_text(*value, *scale)),
243 Value::Double(f) => format!("double:{f:?}"),
244 Value::Char(s) => {
245 let mut q = String::from("char:\"");
246 for c in s.chars() {
247 match c {
248 '"' => q += "\\\"",
249 '\\' => q += "\\\\",
250 c if c.is_control() => q += &format!("\\x{:02X}", c as u32),
251 c => q.push(c),
252 }
253 }
254 q + "\""
255 }
256 Value::Binary(b) => format!("hex:{}", b.iter().map(|x| format!("{x:02X}")).collect::<String>()),
257 }
258}
259
260fn parse_outcome(text: &str) -> Result<Outcome, String> {
261 let mut words = text.splitn(4, ' ');
262 let (Some(code), Some(state), Some(rows)) = (words.next(), words.next(), words.next()) else { return Err("< takes SQLCODE, SQLSTATE and rows=N".into()) };
263 let sqlcode = code.parse().map_err(|_| format!("{code} is not an SQLCODE"))?;
264 if state.len() != 5 {
265 return Err(format!("{state} is not a five-character SQLSTATE"));
266 }
267 let affected = rows.strip_prefix("rows=").and_then(|n| n.parse().ok()).ok_or_else(|| format!("{rows} is not rows=N"))?;
268 let tokens = match words.next().map(str::trim) {
269 None | Some("") => String::new(),
270 Some(t) => match t.strip_prefix("tokens=").map(parse_value) {
271 Some(Ok((Value::Char(s), rest))) if rest.trim().is_empty() => s,
272 _ => return Err("the rest of a < line is tokens=char:\"...\"".into()),
273 },
274 };
275 Ok(Outcome { sqlcode, sqlstate: state.into(), affected, rows: Vec::new(), tokens })
276}
277
278fn parse_values(text: &str) -> Result<Vec<Value>, String> {
279 let (mut out, mut rest) = (Vec::new(), text.trim_start());
280 while !rest.is_empty() {
281 let (v, after) = parse_value(rest)?;
282 out.push(v);
283 rest = after.trim_start();
284 if let Some(next) = rest.strip_prefix('|') {
285 rest = next.trim_start();
286 if rest.is_empty() {
287 return Err("a value is missing after |".into());
288 }
289 } else if !rest.is_empty() {
290 return Err(format!("values are separated by |, not \"{rest}\""));
291 }
292 }
293 Ok(out)
294}
295
296fn parse_value(text: &str) -> Result<(Value, &str), String> {
298 if let Some(quoted) = text.strip_prefix("char:\"") {
299 let mut s = String::new();
300 let mut chars = quoted.char_indices();
301 while let Some((i, c)) = chars.next() {
302 match c {
303 '"' => return Ok((Value::Char(s), "ed[i + 1..])),
304 '\\' => match chars.next() {
305 Some((_, '"')) => s.push('"'),
306 Some((_, '\\')) => s.push('\\'),
307 Some((j, 'x')) => {
308 let hex = quoted.get(j + 1..j + 3).ok_or("\\x takes two hexadecimal digits")?;
309 let code = u32::from_str_radix(hex, 16).map_err(|_| format!("\\x{hex} is not hexadecimal"))?;
310 s.push(char::from_u32(code).ok_or("\\x names no character")?);
311 chars.next();
312 chars.next();
313 }
314 _ => return Err("a backslash in char:\"...\" escapes \", \\ or xNN".into()),
315 },
316 c => s.push(c),
317 }
318 }
319 return Err("char:\" is not closed".into());
320 }
321 let end = text.find(|c: char| c.is_whitespace() || c == '|').unwrap_or(text.len());
322 let (word, rest) = text.split_at(end);
323 let value = match word.split_once(':') {
324 None if word == "null" => Value::Null,
325 Some(("int", n)) => Value::Int(n.parse().map_err(|_| format!("{word} is not an integer"))?),
326 Some(("dec", n)) => Value::parse_decimal(n).ok_or_else(|| format!("{word} is not a decimal"))?,
327 Some(("double", n)) => Value::Double(n.parse().map_err(|_| format!("{word} is not a double"))?),
328 Some(("hex", h)) if h.len() % 2 == 0 => {
329 let bytes: Result<Vec<u8>, _> = (0..h.len()).step_by(2).map(|i| u8::from_str_radix(&h[i..i + 2], 16)).collect();
330 Value::Binary(bytes.map_err(|_| format!("{word} is not hexadecimal"))?)
331 }
332 _ => return Err(format!("{word} is not a value: null, int:, dec:, double:, char:\"...\" or hex:")),
333 };
334 Ok((value, rest))
335}
336
337#[cfg(test)]
338mod tests {
339 use super::*;
340
341 fn call<'a>(verb: &'a str, text: &'a str, inputs: &'a [Value]) -> Call<'a> {
342 Call { program: "P", ordinal: 2, verb, cursor: None, text, inputs }
343 }
344
345 #[test]
346 fn values_round_trip() {
347 let values = vec![
348 Value::Null,
349 Value::Int(-42),
350 Value::Decimal { value: -123_450, scale: 2 },
351 Value::Decimal { value: 5, scale: 2 },
352 Value::Decimal { value: 7, scale: 0 },
353 Value::Double(0.1),
354 Value::Double(6.02e23),
355 Value::Char("say \"hi\" | x \\ é\n".into()),
356 Value::Binary(vec![0xC1, 0x00]),
357 ];
358 let text = values_text(&values);
359 assert!(text.contains("dec:-1234.50") && text.contains("dec:0.05") && text.contains("dec:7"), "{text}");
360 assert_eq!(parse_values(&text), Ok(values));
361 }
362
363 #[test]
364 fn a_recording_answers_the_calls_it_holds_in_order() {
365 let inputs = [Value::Int(7)];
366 let first = entry_text(1, &call("SELECT", "SELECT A FROM T WHERE K = ?", &inputs), &Outcome::rows(vec![vec![Value::Char("X".into())]]));
367 let second = entry_text(2, &call("COMMIT", "COMMIT", &[]), &Outcome::ok());
368 let mut replay = Replay::parse(&format!("{HEADER}\n{first}{second}"), false).expect("parses");
369 assert_eq!(replay.execute(&call("SELECT", "SELECT A FROM T WHERE K = ?", &inputs)).unwrap().rows, [[Value::Char("X".into())]]);
370 assert_eq!(replay.commit(&call("COMMIT", "COMMIT", &[])), Ok(Outcome::ok()));
371 let beyond = replay.commit(&call("COMMIT", "COMMIT", &[])).unwrap_err();
372 assert_eq!((beyond.code, beyond.message.starts_with("the recording holds no such call")), ("SQLR", true));
373 }
374
375 #[test]
376 fn strict_replay_refuses_a_different_call_and_names_both() {
377 let text = format!("{HEADER}\n{}", entry_text(1, &call("SELECT", "SELECT A FROM T WHERE K = ?", &[Value::Int(7)]), &Outcome::ok()));
378 let mut replay = Replay::parse(&text, false).unwrap();
379 let err = replay.execute(&call("SELECT", "SELECT A FROM T WHERE K = ?", &[Value::Int(8)])).unwrap_err();
380 assert_eq!(err.code, "SQLR");
381 assert!(err.message.contains("with int:7") && err.message.contains("with int:8"), "{}", err.message);
382 }
383
384 #[test]
385 fn keyed_replay_takes_calls_in_any_order() {
386 let (a, b) = ([Value::Int(1)], [Value::Int(2)]);
387 let text = format!(
388 "{HEADER}\n{}{}",
389 entry_text(1, &call("SELECT", "Q", &a), &Outcome::rows(vec![vec![Value::Int(10)]])),
390 entry_text(2, &call("SELECT", "Q", &b), &Outcome::rows(vec![vec![Value::Int(20)]]))
391 );
392 let mut replay = Replay::parse(&text, true).unwrap();
393 assert_eq!(replay.execute(&call("SELECT", "Q", &b)).unwrap().rows, [[Value::Int(20)]]);
394 assert_eq!(replay.execute(&call("SELECT", "Q", &a)).unwrap().rows, [[Value::Int(10)]]);
395 assert!(replay.execute(&call("SELECT", "Q", &a)).is_err());
396 }
397
398 #[test]
399 fn a_recorder_writes_what_replay_reads() {
400 struct Fixed;
401 impl Database for Fixed {
402 fn execute(&mut self, _: &Call) -> Answer {
403 Ok(Outcome { tokens: "T1".into(), ..Outcome::rows(vec![vec![Value::Decimal { value: 150, scale: 2 }, Value::Null]]) })
404 }
405 fn open(&mut self, _: &Call) -> Answer {
406 Ok(Outcome::ok())
407 }
408 fn fetch(&mut self, _: &Call) -> Answer {
409 Ok(Outcome::error(100, "02000"))
410 }
411 fn close(&mut self, _: &Call) -> Answer {
412 Ok(Outcome::ok())
413 }
414 fn commit(&mut self, _: &Call) -> Answer {
415 Ok(Outcome::ok())
416 }
417 fn rollback(&mut self, _: &Call) -> Answer {
418 Ok(Outcome::ok())
419 }
420 }
421 let written = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
422 struct Sink(std::rc::Rc<std::cell::RefCell<Vec<u8>>>);
423 impl Write for Sink {
424 fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
425 self.0.borrow_mut().extend_from_slice(b);
426 Ok(b.len())
427 }
428 fn flush(&mut self) -> std::io::Result<()> {
429 Ok(())
430 }
431 }
432 let inputs = [Value::Char("A|B".into())];
433 let mut recorder = Recorder::new(Box::new(Fixed), Box::new(Sink(written.clone())), "a test double").unwrap();
434 let live = recorder.execute(&call("SELECT", "SELECT X, Y FROM T WHERE Z = ?", &inputs)).unwrap();
435 let text = String::from_utf8(written.borrow().clone()).unwrap();
436 assert!(text.starts_with(&format!("{HEADER}\n# source: a test double\n")), "{text}");
437 let mut replay = Replay::parse(&text, false).unwrap();
438 assert_eq!(replay.execute(&call("SELECT", "SELECT X, Y FROM T WHERE Z = ?", &inputs)), Ok(live));
439 }
440
441 #[test]
442 fn malformed_recordings_name_the_line() {
443 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}\""));
444 let err = Replay::parse(&format!("{HEADER}\n@ 1 P:1:0 SELECT\n= int:1\n"), false).err().unwrap();
445 assert_eq!(err, "line 3: an = line belongs after a < line");
446 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: "));
447 assert_eq!(Replay::parse(&format!("{HEADER}\n@ 1 P:1:0 SELECT\n"), false).err().unwrap(), "the last call has no < line");
448 }
449}