Skip to content

Commit 9b2d7e5

Browse files
committed
[ui] show details of example databases
1 parent b681105 commit 9b2d7e5

File tree

7 files changed

+130
-32
lines changed

7 files changed

+130
-32
lines changed

ui/assets/sqlite_logo.png

13.1 KB
Loading

ui/assets/tailwind.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/src/included_db.rs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/// Preloaded examples of databases to start UI with somethinh
2+
3+
use std::include_bytes;
4+
5+
pub const SIMPLE_DB: &str = "Simple";
6+
pub const BIG_PAGE_DB: &str = "Max page size";
7+
pub const TABLE_INDEX_LEAF_DB: &str = "Leaf nodes";
8+
pub const OVERFLOW_PAGE_DB: &str = "Overflow pages";
9+
pub const TABLE_INDEX_INTERIOR_DB: &str = "Interior nodes";
10+
11+
pub static INCLUDED_DB: &'static [(&'static str, (&'static [u8], &'static [&'static str]))] = &[
12+
(
13+
SIMPLE_DB,
14+
(
15+
include_bytes!("../included/simple"),
16+
&[
17+
"CREATE TABLE simple(int)",
18+
"INSERT INTO simple VALUES(1), (2), (3), (4)",
19+
],
20+
),
21+
),
22+
(
23+
BIG_PAGE_DB,
24+
(
25+
include_bytes!("../included/big_page"),
26+
&[
27+
"PRAGMA page_size=65536",
28+
"CREATE TABLE big_page(int)",
29+
"INSERT INTO simple VALUES(1), (2), (3), (4)",
30+
],
31+
),
32+
),
33+
(
34+
TABLE_INDEX_LEAF_DB,
35+
(
36+
include_bytes!("../included/table_index_leaf"),
37+
&[
38+
"CREATE TABLE stars(id INTEGER PRIMARY KEY, name TEXT, distance REAL, brightness REAL)",
39+
"INSERT INTO stars VALUES(100, 'Sirius', 8.6, -1.46), ... ",
40+
"CREATE INDEX idx_stars_name ON stars (name)",
41+
"CREATE TABLE spaceships(launched, name, operator)",
42+
"INSERT INTO spaceships VALUES(1977, 'Voyager 1', 'NASA'), ... ",
43+
"CREATE INDEX idx_spaceships_name ON spaceships(name)",
44+
],
45+
),
46+
),
47+
(
48+
OVERFLOW_PAGE_DB,
49+
(
50+
include_bytes!("../included/overflow_page"),
51+
&[
52+
"PRAGMA page_size=1024",
53+
"CREATE TABLE mixed_overflow(text, longint, int, blob)",
54+
"CREATE TABLE blob_overflow(blob)",
55+
"INSERT INTO blob_overflow VALUES(fileio_read('dev/overflow.txt'))",
56+
"INSERT INTO mixed_overflow SELECT CAST(blob as TEXT), 234234235, 0, blob FROM blob_overflow",
57+
"INSERT INTO mixed_overflow SELECT CAST(blob as TEXT), 94542343, 1, blob FROM blob_overflow",
58+
],
59+
),
60+
),
61+
(
62+
TABLE_INDEX_INTERIOR_DB,
63+
(
64+
include_bytes!("../included/table_index_interior"),
65+
&[
66+
"PRAGMA page_size=512",
67+
"CREATE TABLE macro_story(line)",
68+
"INSERT INTO macro_story SELECT VALUE FROM fileio_scan('dev/lines.txt')",
69+
"CREATE INDEX idx_macro_story_line ON macro_story(line)",
70+
],
71+
),
72+
),
73+
];
74+

ui/src/index.rs

+47-7
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,21 @@ pub fn Header() -> Element {
110110
div {
111111
class: "h-12 flex items-center bg-slate-200",
112112
div {
113-
class: "text-xl font-bold tracking-tighter pl-4",
113+
class: "pl-4",
114+
a {
115+
href: "https://www.sqlite.org/fileformat2.html",
116+
img {
117+
class: "h-10 object-scale-down",
118+
src: "./sqlite_logo.png"
119+
}
120+
}
121+
}
122+
div {
123+
class: "text-xl font-bold tracking-tighter",
114124
"SQLite File Format",
115125
}
116126
div {
117-
class: "pl-4 tooltip tooltip-right",
127+
class: "tooltip tooltip-bottom pl-4",
118128
"data-tip": "Like the project? Give us a star ☆",
119129
a {
120130
href: "https://github.com/torymur/sqlite-repr",
@@ -127,10 +137,7 @@ pub fn Header() -> Element {
127137
div { class: "flex-grow" }
128138
div {
129139
class: "join",
130-
div {
131-
class: "join-item btn btn-secondary tracking-tighter font-bold",
132-
"Example database"
133-
}
140+
ExampleDetails { }
134141
select {
135142
class: "join-item select select-secondary select-bordered font-bold tracking-tighter focus:outline-none",
136143
oninput: move |e| {
@@ -161,6 +168,39 @@ pub fn Header() -> Element {
161168
}
162169
}
163170

171+
pub fn ExampleDetails() -> Element {
172+
let current_db = use_context::<AppState>().current_db;
173+
let viewer = use_context::<AppState>().viewer;
174+
let rviewer = viewer.read();
175+
let details = rviewer.included_db.get(current_db().as_str());
176+
match details {
177+
None => rsx! { div { } },
178+
Some((_, desc)) => {
179+
rsx! {
180+
div {
181+
class: "dropdown dropdown-hover",
182+
div {
183+
class: "join-item btn bg-secondary border border-secondary tracking-tighter font-bold hover:border-secondary hover:bg-secondary",
184+
tabindex: 0,
185+
role: "button",
186+
"Database Example"
187+
}
188+
ul {
189+
class: "text-xs dropdown-content z-[1] menu bg-secondary shadow w-max tracking-tighter",
190+
for line in desc {
191+
li {
192+
a {
193+
"{line}"
194+
}
195+
}
196+
}
197+
}
198+
}
199+
}
200+
}
201+
}
202+
}
203+
164204
pub fn Body() -> Element {
165205
rsx! {
166206
div {
@@ -197,7 +237,7 @@ pub fn SideBar() -> Element {
197237
class: "rounded-box p-4 h-[calc(100vh-48px)] w-fit overflow-y-auto",
198238
div {
199239
class: "text-lg font-bold truncate pb-4",
200-
"🗐 Pages",
240+
"Pages",
201241
}
202242
div {
203243
for (n, page) in pages.into_iter().enumerate() {

ui/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! rendering of parsed structures.
33
44
pub mod header;
5+
pub mod included_db;
56
pub mod index;
67
pub mod overflow_pages;
78
pub mod pages;

ui/src/state.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::rc::Rc;
44

55
use dioxus::prelude::*;
66

7-
use crate::viewer::{Viewer, SIMPLE_DB};
7+
use crate::included_db::SIMPLE_DB;
8+
use crate::viewer::Viewer;
89
use crate::{Field, PageView, Part};
910

1011
#[derive(Clone, Debug)]

ui/src/viewer.rs

+5-23
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,28 @@
11
//! Database UI Viewer.
22
33
use std::collections::BTreeMap;
4-
use std::include_bytes;
54
use std::rc::Rc;
65

76
use parser::{Cell, OverflowPage, Reader, StdError};
87

8+
use crate::included_db::INCLUDED_DB;
99
use crate::overflow_pages::OverflowPageElementBuilder;
1010
use crate::pages::BtreePageElementBuilder;
1111
use crate::PageView;
1212

1313
#[derive(Debug)]
1414
pub struct Viewer {
15-
pub included_db: BTreeMap<&'static str, &'static [u8]>,
15+
pub included_db: BTreeMap<&'static str, (&'static [u8], &'static [&'static str])>,
1616
pub pages: Vec<Rc<dyn PageView>>,
1717
}
1818

1919
pub type Result<T, E = StdError> = std::result::Result<T, E>;
2020

21-
// Preloaded examples of databases to start UI with something
22-
pub const SIMPLE_DB_BYTES: &[u8] = include_bytes!("../included/simple");
23-
pub const BIG_PAGE_DB_BYTES: &[u8] = include_bytes!("../included/big_page");
24-
pub const TABLE_INDEX_LEAF_DB_BYTES: &[u8] = include_bytes!("../included/table_index_leaf");
25-
pub const OVERFLOW_PAGE_DB_BYTES: &[u8] = include_bytes!("../included/overflow_page");
26-
pub const TABLE_INDEX_INTERIOR_DB_BYTES: &[u8] = include_bytes!("../included/table_index_interior");
27-
pub const SIMPLE_DB: &str = "Simple";
28-
pub const BIG_PAGE_DB: &str = "Max page size";
29-
pub const TABLE_INDEX_LEAF_DB: &str = "Leaf nodes";
30-
pub const OVERFLOW_PAGE_DB: &str = "Overflow pages";
31-
pub const TABLE_INDEX_INTERIOR_DB: &str = "Interior nodes";
32-
3321
impl Viewer {
3422
pub fn new_from_included(name: &str) -> Result<Self, StdError> {
35-
let included_db = BTreeMap::from([
36-
(SIMPLE_DB, SIMPLE_DB_BYTES),
37-
(BIG_PAGE_DB, BIG_PAGE_DB_BYTES),
38-
(TABLE_INDEX_LEAF_DB, TABLE_INDEX_LEAF_DB_BYTES),
39-
(OVERFLOW_PAGE_DB, OVERFLOW_PAGE_DB_BYTES),
40-
(TABLE_INDEX_INTERIOR_DB, TABLE_INDEX_INTERIOR_DB_BYTES),
41-
]);
42-
43-
let bytes = included_db.get(name).ok_or("This db is not included.")?;
23+
let included_db: BTreeMap<&'static str, (&'static [u8], &'static [&'static str])> =
24+
BTreeMap::from_iter(INCLUDED_DB.iter().map(|v| *v));
25+
let (bytes, _) = included_db.get(name).ok_or("This db is not included.")?;
4426
let reader = Reader::new(bytes)?;
4527
let size = reader.db_header.page_size as usize;
4628
let mut pages_map: BTreeMap<usize, Rc<dyn PageView>> = BTreeMap::new();

0 commit comments

Comments
 (0)