|
| 1 | +use crate::load::DIST_PER_WARE; |
| 2 | +use crate::{TpccArgs, TpccError, TpccTest, TpccTransaction}; |
| 3 | +use chrono::Utc; |
| 4 | +use fnck_sql::db::DBTransaction; |
| 5 | +use fnck_sql::storage::Storage; |
| 6 | +use rand::prelude::ThreadRng; |
| 7 | +use rand::Rng; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +pub(crate) struct DeliveryArgs { |
| 11 | + w_id: usize, |
| 12 | + o_carrier_id: usize, |
| 13 | +} |
| 14 | + |
| 15 | +impl DeliveryArgs { |
| 16 | + pub(crate) fn new(w_id: usize, o_carrier_id: usize) -> Self { |
| 17 | + Self { w_id, o_carrier_id } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +pub(crate) struct Delivery; |
| 22 | +pub(crate) struct DeliveryTest; |
| 23 | + |
| 24 | +impl<S: Storage> TpccTransaction<S> for Delivery { |
| 25 | + type Args = DeliveryArgs; |
| 26 | + |
| 27 | + fn run(tx: &mut DBTransaction<S>, args: &Self::Args) -> Result<(), TpccError> { |
| 28 | + let now = Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(); |
| 29 | + |
| 30 | + for d_id in 1..DIST_PER_WARE + 1 { |
| 31 | + // "SELECT COALESCE(MIN(no_o_id),0) FROM new_orders WHERE no_d_id = ? AND no_w_id = ?" |
| 32 | + let (_, tuple) = tx.run(format!("SELECT COALESCE(MIN(no_o_id),0) FROM new_orders WHERE no_d_id = {} AND no_w_id = {}", d_id, args.w_id))?; |
| 33 | + let no_o_id = tuple[0].values[0].i32().unwrap(); |
| 34 | + |
| 35 | + if no_o_id == 0 { |
| 36 | + continue; |
| 37 | + } |
| 38 | + // "DELETE FROM new_orders WHERE no_o_id = ? AND no_d_id = ? AND no_w_id = ?" |
| 39 | + let _ = tx.run(format!( |
| 40 | + "DELETE FROM new_orders WHERE no_o_id = {} AND no_d_id = {} AND no_w_id = {}", |
| 41 | + no_o_id, d_id, args.w_id |
| 42 | + ))?; |
| 43 | + // "SELECT o_c_id FROM orders WHERE o_id = ? AND o_d_id = ? AND o_w_id = ?" |
| 44 | + let (_, tuple) = tx.run(format!( |
| 45 | + "SELECT o_c_id FROM orders WHERE o_id = {} AND o_d_id = {} AND o_w_id = {}", |
| 46 | + no_o_id, d_id, args.w_id |
| 47 | + ))?; |
| 48 | + let c_id = tuple[0].values[0].i32().unwrap(); |
| 49 | + // "UPDATE orders SET o_carrier_id = ? WHERE o_id = ? AND o_d_id = ? AND o_w_id = ?" |
| 50 | + let _ = tx.run(format!("UPDATE orders SET o_carrier_id = {} WHERE o_id = {} AND o_d_id = {} AND o_w_id = {}", args.o_carrier_id, no_o_id, d_id, args.w_id))?; |
| 51 | + // "UPDATE order_line SET ol_delivery_d = ? WHERE ol_o_id = ? AND ol_d_id = ? AND ol_w_id = ?" |
| 52 | + let _ = tx.run(format!("UPDATE order_line SET ol_delivery_d = '{}' WHERE ol_o_id = {} AND ol_d_id = {} AND ol_w_id = {}", now, no_o_id, d_id, args.w_id))?; |
| 53 | + // "SELECT SUM(ol_amount) FROM order_line WHERE ol_o_id = ? AND ol_d_id = ? AND ol_w_id = ?" |
| 54 | + let (_, tuple) = tx.run(format!("SELECT SUM(ol_amount) FROM order_line WHERE ol_o_id = {} AND ol_d_id = {} AND ol_w_id = {}", no_o_id, d_id, args.w_id))?; |
| 55 | + let ol_total = tuple[0].values[0].decimal().unwrap(); |
| 56 | + // "UPDATE customer SET c_balance = c_balance + ? , c_delivery_cnt = c_delivery_cnt + 1 WHERE c_id = ? AND c_d_id = ? AND c_w_id = ?" |
| 57 | + let _ = tx.run(format!("UPDATE customer SET c_balance = c_balance + {} , c_delivery_cnt = c_delivery_cnt + 1 WHERE c_id = {} AND c_d_id = {} AND c_w_id = {}", ol_total, c_id, d_id, args.w_id))?; |
| 58 | + } |
| 59 | + |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<S: Storage> TpccTest<S> for DeliveryTest { |
| 65 | + fn name(&self) -> &'static str { |
| 66 | + "Delivery" |
| 67 | + } |
| 68 | + |
| 69 | + fn do_transaction( |
| 70 | + &self, |
| 71 | + rng: &mut ThreadRng, |
| 72 | + tx: &mut DBTransaction<S>, |
| 73 | + num_ware: usize, |
| 74 | + _: &TpccArgs, |
| 75 | + ) -> Result<(), TpccError> { |
| 76 | + let w_id = rng.gen_range(0..num_ware) + 1; |
| 77 | + let o_carrier_id = rng.gen_range(1..10); |
| 78 | + |
| 79 | + let args = DeliveryArgs::new(w_id, o_carrier_id); |
| 80 | + Delivery::run(tx, &args)?; |
| 81 | + |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | +} |
0 commit comments