1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// DbRecord is the only trait the application needs to implement to use the
// library. Everything else (table creation, SQL building, row mapping) is
// handled automatically by Repository<T>.
//
// ValueSet is the opaque row handle passed to from_values().
// The app accesses column values by name — no positional indices, no rusqlite.
use crate;
use crateDbError;
use crateSqlValue;
// ── ValueSet ──────────────────────────────────────────────────────────────────
/// An opaque row handle passed to DbRecord::from_values().
///
/// Access values by column name. The `id` column is always available via
/// `get("id")`. All other column names must appear in `DbRecord::columns()`.
///
/// # Example
/// ```rust
/// fn from_values(v: &ValueSet) -> Result<Self, DbError> {
/// Ok(Task {
/// id: v.get("id")?.as_i64()?,
/// title: v.get("title")?.as_text()?,
/// done: v.get("done")?.as_bool()?,
/// })
/// }
/// ```
// ── DbRecord trait ────────────────────────────────────────────────────────────
/// Trait that makes a struct storable in a SQLite table via Repository<T>.
///
/// # What you implement
/// - `table_name` — the SQLite table name (e.g., `"tasks"`).
/// - `columns` — all columns except `id` (which is auto-managed).
/// - `indexes` — optional composite indexes (default: none).
/// - `from_values` — deserialize one row into Self.
/// - `to_params` — serialize Self into column-value pairs for INSERT/UPDATE.
/// - `id` / `set_id` — let the library manage the primary key.
///
/// # What the library handles automatically
/// - `CREATE TABLE IF NOT EXISTS` with all columns and the `id` PK.
/// - `CREATE INDEX IF NOT EXISTS` for every IndexDef.
/// - Building parameterized INSERT, UPDATE, DELETE, SELECT SQL.
/// - Mapping query results back to Vec<T> via from_values.
///
/// # Security
/// Column names and table names are double-quoted in all generated SQL.
/// All values go through `?` parameterized placeholders — no interpolation.
///
/// # Example implementation
/// ```rust
/// impl DbRecord for Task {
/// fn table_name() -> &'static str { "tasks" }
///
/// fn columns() -> Vec<Column> {
/// vec![
/// Column::new("user_id", ColType::Integer).not_null(),
/// Column::new("title", ColType::Text).not_null(),
/// Column::new("done", ColType::Integer).not_null().default("0"),
/// Column::new("deleted", ColType::Integer).not_null().default("0"),
/// Column::new("updated_at", ColType::Integer).not_null().default("(unixepoch())"),
/// ]
/// }
///
/// fn indexes() -> Vec<IndexDef> {
/// vec![ IndexDef::new(&["user_id", "deleted"]) ]
/// }
///
/// fn from_values(v: &ValueSet) -> Result<Self, DbError> {
/// Ok(Task {
/// id: v.get("id")?.as_i64()?,
/// user_id: v.get("user_id")?.as_i64()?,
/// title: v.get("title")?.as_text()?,
/// done: v.get("done")?.as_bool()?,
/// deleted: v.get("deleted")?.as_bool()?,
/// updated_at: v.get("updated_at")?.as_i64()?,
/// })
/// }
///
/// fn to_params(&self) -> Vec<(&'static str, SqlValue)> {
/// vec![
/// ("user_id", self.user_id.into()),
/// ("title", self.title.clone().into()),
/// ("done", self.done.into()),
/// ("deleted", self.deleted.into()),
/// ("updated_at", db_lib::now().into()),
/// ]
/// }
///
/// fn id(&self) -> Option<i64> { if self.id > 0 { Some(self.id) } else { None } }
/// fn set_id(&mut self, id: i64) { self.id = id; }
/// }
/// ```
/// Size -> avoid usin `dyn DbRecord` insted use `fun t<T: DbRecord>(record: &T) {}`