|
| 1 | +use crate::versioned::ContentType; |
| 2 | + |
| 3 | +/// The name of the sql table. The `eph1` stands for `ephemeral_v1`. |
| 4 | +pub fn table_name(content_type: &ContentType) -> String { |
| 5 | + format!("eph1_{content_type}") |
| 6 | +} |
| 7 | + |
| 8 | +pub fn create_table(content_type: &ContentType) -> String { |
| 9 | + format!( |
| 10 | + " |
| 11 | + CREATE TABLE IF NOT EXISTS {0} ( |
| 12 | + content_id BLOB PRIMARY KEY, |
| 13 | + content_key BLOB NOT NULL, |
| 14 | + content_value BLOB NOT NULL, |
| 15 | + type INTEGER NOT NULL, |
| 16 | + slot INTEGER NOT NULL, |
| 17 | + content_size INTEGER NOT NULL |
| 18 | + ); |
| 19 | + CREATE INDEX IF NOT EXISTS {0}_type_idx ON {0} (type); |
| 20 | + CREATE INDEX IF NOT EXISTS {0}_slot_idx ON {0} (slot); |
| 21 | + CREATE INDEX IF NOT EXISTS {0}_content_size_idx ON {0} (content_size); |
| 22 | + ", |
| 23 | + table_name(content_type) |
| 24 | + ) |
| 25 | +} |
| 26 | + |
| 27 | +pub fn insert(content_type: &ContentType) -> String { |
| 28 | + format!( |
| 29 | + " |
| 30 | + INSERT OR IGNORE INTO {} ( |
| 31 | + content_id, |
| 32 | + content_key, |
| 33 | + content_value, |
| 34 | + type, |
| 35 | + slot, |
| 36 | + content_size |
| 37 | + ) |
| 38 | + VALUES ( |
| 39 | + :content_id, |
| 40 | + :content_key, |
| 41 | + :content_value, |
| 42 | + :type, |
| 43 | + :slot, |
| 44 | + :content_size |
| 45 | + )", |
| 46 | + table_name(content_type) |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +pub fn delete(content_type: &ContentType) -> String { |
| 51 | + format!( |
| 52 | + "DELETE FROM {} |
| 53 | + WHERE content_id = :content_id |
| 54 | + RETURNING content_size", |
| 55 | + table_name(content_type) |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +pub fn lookup_key(content_type: &ContentType) -> String { |
| 60 | + format!( |
| 61 | + "SELECT content_key FROM {} WHERE content_id = :content_id LIMIT 1", |
| 62 | + table_name(content_type) |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +pub fn lookup_value(content_type: &ContentType) -> String { |
| 67 | + format!( |
| 68 | + "SELECT content_value FROM {} WHERE content_id = :content_id LIMIT 1", |
| 69 | + table_name(content_type) |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +pub fn entry_count_and_size(content_type: &ContentType) -> String { |
| 74 | + format!( |
| 75 | + "SELECT COUNT(*) as count, TOTAL(content_size) as used_capacity FROM {}", |
| 76 | + table_name(content_type) |
| 77 | + ) |
| 78 | +} |
0 commit comments