Skip to content

Instantly share code, notes, and snippets.

@the2pizza
Created January 22, 2025 19:30
Show Gist options
  • Select an option

  • Save the2pizza/43c0326f89be5c68791f508fc54df65d to your computer and use it in GitHub Desktop.

Select an option

Save the2pizza/43c0326f89be5c68791f508fc54df65d to your computer and use it in GitHub Desktop.
worktable!(
name: My,
columns: {
id: u64 primary_key autoincrement,
val: i64,
attribute: String,
},
indexes: {
attribute_idx: attribute,
}
queries: {
update: {
ValByAttr(val) by attribute,
AttrById(attribute) by id
},
delete: {
ByAttr() by attribute,
ById() by id,
}
}
);
let my_table = MyWorkTable::default();
// WT rows (has prefix My because of table name)
let row = MyRow {
val: 1,
attribute: "TEST".to_string(),
id: 0,
};
let row1 = MyRow {
val: 2,
attribute: "TEST2".to_string(),
id: 1,
};
let row2 = MyRow {
val: 1337,
attribute: "TEST2".to_string(),
id: 2,
};
let row3 = MyRow {
val: 555,
attribute: "TEST3".to_string(),
id: 3,
};
// insert
let _ = my_table.insert(row);
let _ = my_table.insert(row1);
let _ = my_table.insert(row2);
let _ = my_table.insert(row3);
let select_all = my_table.select_all().execute();
/* Select Result:
Ok([MyRow { id: 0, val: 1, attribute: "TEST" },
MyRow { id: 1, val: 2, attribute: "TEST2" },
MyRow { id: 2, val: 1337, attribute: "TEST2" },
MyRow { id: 3, val: 555, attribute: "TEST3" }])
*/
let update_attr = my_table.update_attr_by_id(
AttrByIdQuery {
attribute: "TEST3".to_string(),
},
MyPrimaryKey(0),
);
let select_all_after_update = my_table.select_all();
/* Select Result After Attribute Update
Ok([MyRow { id: 0, val: 1, attribute: "TEST3" },
MyRow { id: 1, val: 2, attribute: "TEST2" },
MyRow { id: 2, val: 1337, attribute: "TEST2" },
MyRow { id: 3, val: 555, attribute: "TEST3" }])
*/
// Dlete all records with TEST3 Attribute
let test_delete = my_table.delete_by_attr("TEST3".to_string());
let _ = my_table.select_all().execute();
/* Final select after deleting records TEST3
Ok([MyRow { id: 0, val: 1, attribute: "TEST3" }, < ----- USED old index, is not deleted as i did expect
MyRow { id: 1, val: 2, attribute: "TEST2" },
MyRow { id: 2, val: 1337, attribute: "TEST2" }])
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment