dbui-client 0.0.64

WASM web client sources for dbui
use crate::templates::card::card;

use anyhow::Result;
use dbui_core::database::results::ResultSet;
use dbui_core::field_type::FieldType;
use maud::{html, Markup};

pub(crate) fn results(ctx: &crate::ctx::ClientContext, result: &ResultSet) -> Result<Markup> {
  Ok(card(ctx, html! {
    div.resultset {
      table.uk-table.uk-table-divider.uk-table-small.uk-table-justify.uk-text-left {
        thead {
          tr {
            @for c in result.columns() {
              th title=(format!("{:?}", c.t())) { (c.name()) }
            }
          }
        }
        tbody {
          @if result.data().is_empty() {
            tr {
              td colspan=(if result.columns().is_empty() { 1 } else { result.columns().len() }) { "No rows returned" }
            }
          }
          @for row in result.data() {
            tr {
              @for cell in result.columns().iter().zip(row) {
                td { (write_field(cell.0.t(), cell.1)) }
              }
            }
          }
        }
      }
      div.result-footer {
        @if let Some(sql) = result.sql() {
          div {
            "SQL:"
            pre { (sql) }
          }
        }
        div {
          (format!("Zero rows returned a few seconds ago in {}ms", result.duration_ms()))
        }
      }
    }
  }))
}

fn write_field(ft: &FieldType, v: &Option<String>) -> Markup {
  match *ft {
    FieldType::Json => html! { pre { (v.as_ref().unwrap_or(&"".to_string())) } },
    _ => html! { (v.as_ref().unwrap_or(&"".to_string())) }
  }
}