blackbeam/mysql_async

Query field TIMESTAMP time class

qqliaoxin opened this issue · 4 comments

When a database query is returned as a TIMESTAMP time type field, the chrono library cannot handle the return value either

Hi. Chrono parser is tested against both textual and binary representations of mysql datetime-like values, so could you please give an example?

let orders = "SELECT * FROM orders"
    .with(())
    .map(
        &mut conn,
        |(order_id, product_id, quantity, amount, shipping, tax, shipping_address,create_at)| {
            Order::new(
                order_id,
                product_id,
                quantity,
                amount,
                shipping,
                tax,
                shipping_address,
                create_at,
            )
        },
    )
    .await?;
drop(conn);

Sorry, but I can't reproduce the issue. For me the TIMESTAMP value is handled correctly.

The code bellow gives the following output:

Reveal the code

The output above was generated by the following code:

let mut conn: Conn = Conn::new(get_opts()).await.unwrap();

  "CREATE TEMPORARY TABLE ts_test (ctime TIMESTAMP NOT NULL)"
      .run(&mut conn)
      .await
      .unwrap();
  "INSERT INTO ts_test VALUES (CURRENT_TIMESTAMP())"
      .run(&mut conn)
      .await
      .unwrap();

  let ctime: Value = "SELECT * from ts_test"
      .first(&mut conn)
      .await
      .unwrap()
      .unwrap();

  if let Value::Bytes(bytes) = &ctime {
      eprintln!("RAW TEXTUAL: {:?}", from_utf8(bytes));
  }

  eprintln!(
      "PARSED TEXTUAL: {:?}",
      from_value::<chrono::NaiveDateTime>(ctime)
  );

  let ctime: Value = "SELECT * from ts_test"
      .with(())
      .first(&mut conn)
      .await
      .unwrap()
      .unwrap();

  eprintln!("RAW BINARY: {ctime:?}");
  let parsed = from_value::<chrono::NaiveDateTime>(ctime);
  eprintln!("PARSED BINARY: {:?}", parsed);

  "UPDATE ts_test SET ctime = ?"
      .with((parsed,))
      .ignore(&mut conn)
      .await
      .unwrap();

  let ctime: Value = "SELECT * from ts_test"
      .first(&mut conn)
      .await
      .unwrap()
      .unwrap();

  if let Value::Bytes(bytes) = &ctime {
      eprintln!("UPDATED RAW TEXTUAL: {:?}", from_utf8(bytes));
  }
RAW TEXTUAL: Ok("2022-12-29 16:51:53")
PARSED TEXTUAL: 2022-12-29T16:51:53
RAW BINARY: Date("'2022-12-29 16:51:53'")
PARSED BINARY: 2022-12-29T16:51:53
UPDATED RAW TEXTUAL: Ok("2022-12-29 16:51:53")
  1. RAW TEXTUAL line shows TIMESTAMP data returned from the server (textual TIMESTAMP representation)
  2. PARSED TEXTUAL line shows that textual TIMESTAMP data was correctly parsed to chrono::NaiveDateTime
  3. RAW BINARY line shows TIMESTAMP data returned from the server (binary TIMESTAMP representation)
  4. PARSED BINARY line shows that binary TIMESTAMP data was correctly parsed to chrono::NaiveDateTime
  5. UPDATED RAW TEXTUAL line shows that chrono::NaiveDateTime was correctly serialized to MySql protocol value and written to the server
xb1520 commented

cargo.toml
mysql_common = {version = "0.30.5",features = ["chrono"]}

//////refer to https://docs.rs/mysql_common/latest/mysql_common/