uqa-storage-sqlite 0.3.6

SQLite catalog, indexes, compressed storage, graph and key/value providers
Documentation
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Foreign servers, foreign tables, catalog indexes, and path indexes.

use super::{
    params, Catalog, CatalogIndexRow, ForeignTableRow, RelationIdentity, RelationKind, Result,
    SQLiteError, TableAclEntry,
};

impl Catalog {
    // -- Foreign servers ---------------------------------------------------

    pub fn save_foreign_server(
        &self,
        name: &str,
        fdw_type: &str,
        options_json: &str,
    ) -> Result<()> {
        self.conn.with(|c| {
            c.execute(
                "INSERT OR REPLACE INTO _foreign_servers (name, fdw_type, options) \
                 VALUES (?1, ?2, ?3)",
                params![name, fdw_type, options_json],
            )?;
            Ok(())
        })
    }

    pub fn drop_foreign_server(&self, name: &str) -> Result<()> {
        self.conn.with(|c| {
            c.execute(
                "DELETE FROM _foreign_servers WHERE name = ?1",
                params![name],
            )?;
            Ok(())
        })
    }

    pub fn load_foreign_servers(&self) -> Result<Vec<(String, String, String)>> {
        self.conn.with(|c| {
            let mut stmt =
                c.prepare("SELECT name, fdw_type, options FROM _foreign_servers ORDER BY name")?;
            let rows = stmt.query_map([], |r| {
                Ok((
                    r.get::<_, String>(0)?,
                    r.get::<_, String>(1)?,
                    r.get::<_, String>(2)?,
                ))
            })?;
            let mut out = Vec::new();
            for row in rows {
                out.push(row?);
            }
            Ok(out)
        })
    }

    // -- Foreign tables ----------------------------------------------------

    pub fn save_foreign_table(&self, row: &ForeignTableRow) -> Result<()> {
        self.conn.with_mut(|c| {
            let tx = c.savepoint()?;
            Self::claim_relation(&tx, &row.relation, RelationKind::ForeignTable)?;
            let acl_json = row.acl.as_deref().map(serde_json::to_string).transpose()?;
            let column_acls_json = serde_json::to_string(&row.column_acls)?;
            tx.execute(
                "INSERT OR REPLACE INTO _foreign_tables \
                    (schema_name, relation_name, kind, role_owner, acl_json, column_acls_json, server_name, columns_json, options) \
                 VALUES (?1, ?2, 'foreign_table', ?3, ?4, ?5, ?6, ?7, ?8)",
                params![
                    row.relation.schema,
                    row.relation.name,
                    row.role_owner,
                    acl_json,
                    column_acls_json,
                    row.server_name,
                    row.columns_json,
                    row.options_json
                ],
            )?;
            tx.commit()?;
            Ok(())
        })
    }

    pub fn update_foreign_table_security(
        &self,
        relation: &RelationIdentity,
        role_owner: &str,
        acl: Option<&[TableAclEntry]>,
        column_acls: &std::collections::BTreeMap<String, Vec<TableAclEntry>>,
    ) -> Result<bool> {
        self.conn.with_mut(|connection| {
            let acl_json = acl.map(serde_json::to_string).transpose()?;
            let column_acls_json = serde_json::to_string(column_acls)?;
            Ok(connection.execute(
                "UPDATE _foreign_tables
                    SET role_owner = ?3, acl_json = ?4, column_acls_json = ?5
                  WHERE schema_name = ?1 AND relation_name = ?2",
                params![
                    relation.schema,
                    relation.name,
                    role_owner,
                    acl_json,
                    column_acls_json
                ],
            )? != 0)
        })
    }

    pub fn rename_foreign_table(
        &self,
        from: &RelationIdentity,
        to: &RelationIdentity,
    ) -> Result<bool> {
        if from.schema != to.schema {
            return Err(SQLiteError::StorageBackend(
                "moving a foreign table between schemas is not supported by the catalog".into(),
            ));
        }
        self.conn.with_mut(|connection| {
            let source_exists = connection.query_row(
                "SELECT EXISTS(SELECT 1 FROM _foreign_tables WHERE schema_name = ?1 AND relation_name = ?2)",
                params![from.schema, from.name],
                |row| row.get::<_, bool>(0),
            )?;
            if from == to || !source_exists {
                return Ok(source_exists);
            }
            let target_exists = connection.query_row(
                "SELECT EXISTS(SELECT 1 FROM _relations WHERE schema_name = ?1 AND relation_name = ?2)",
                params![to.schema, to.name],
                |row| row.get::<_, bool>(0),
            )?;
            if target_exists {
                return Err(SQLiteError::StorageBackend(format!(
                    "relation `{}` already exists",
                    to.qualified_name()
                )));
            }
            let tx = connection.savepoint()?;
            Self::claim_relation(&tx, to, RelationKind::ForeignTable)?;
            let updated = tx.execute(
                "UPDATE _foreign_tables SET schema_name = ?3, relation_name = ?4 WHERE schema_name = ?1 AND relation_name = ?2",
                params![from.schema, from.name, to.schema, to.name],
            )?;
            if updated != 1 {
                return Err(SQLiteError::StorageBackend(format!(
                    "foreign table `{}` disappeared during rename",
                    from.qualified_name()
                )));
            }
            Self::release_relation(&tx, from, RelationKind::ForeignTable)?;
            tx.commit()?;
            Ok(true)
        })
    }

    pub fn drop_foreign_table(&self, relation: &RelationIdentity) -> Result<()> {
        self.conn.with_mut(|c| {
            let tx = c.savepoint()?;
            let removed = tx.execute(
                "DELETE FROM _foreign_tables
                  WHERE schema_name = ?1 AND relation_name = ?2",
                params![relation.schema, relation.name],
            )? != 0;
            if removed {
                Self::release_relation(&tx, relation, RelationKind::ForeignTable)?;
            }
            tx.commit()?;
            Ok(())
        })
    }

    pub fn load_foreign_tables(&self) -> Result<Vec<ForeignTableRow>> {
        self.conn.with(|c| {
            let mut stmt = c.prepare(
                "SELECT schema_name, relation_name, role_owner, acl_json, column_acls_json, server_name, columns_json, options
                   FROM _foreign_tables ORDER BY schema_name, relation_name",
            )?;
            let rows = stmt.query_map([], |r| {
                Ok((
                    r.get::<_, String>(0)?,
                    r.get::<_, String>(1)?,
                    r.get::<_, String>(2)?,
                    r.get::<_, Option<String>>(3)?,
                    r.get::<_, String>(4)?,
                    r.get::<_, String>(5)?,
                    r.get::<_, String>(6)?,
                    r.get::<_, String>(7)?,
                ))
            })?;
            let mut out = Vec::new();
            for row in rows {
                let (schema, name, owner, acl_json, column_acls_json, server, cols, opts) = row?;
                out.push(ForeignTableRow {
                    relation: RelationIdentity::new(schema, name),
                    role_owner: owner,
                    acl: acl_json
                        .as_deref()
                        .map(serde_json::from_str)
                        .transpose()?,
                    column_acls: serde_json::from_str(&column_acls_json)?,
                    server_name: server,
                    columns_json: cols,
                    options_json: opts,
                });
            }
            Ok(out)
        })
    }

    // -- Catalog indexes (CREATE INDEX state) ------------------------------

    pub fn save_catalog_index(
        &self,
        relation: &RelationIdentity,
        index_type: &str,
        table_name: &str,
        columns_json: &str,
        parameters_json: &str,
    ) -> Result<()> {
        self.save_catalog_index_row(&CatalogIndexRow {
            relation: relation.clone(),
            index_type: index_type.to_string(),
            table_name: table_name.to_string(),
            columns_json: columns_json.to_string(),
            parameters_json: parameters_json.to_string(),
            definition_json: None,
        })
    }

    pub fn save_catalog_index_row(&self, index: &CatalogIndexRow) -> Result<()> {
        let CatalogIndexRow {
            relation,
            index_type,
            table_name,
            columns_json,
            parameters_json,
            definition_json,
        } = index;
        let table =
            RelationIdentity::from_legacy_name(table_name).map_err(SQLiteError::StorageBackend)?;
        if relation.schema != table.schema {
            return Err(SQLiteError::StorageBackend(format!(
                "catalog index `{}` cannot belong to a different schema than table `{}`",
                relation.qualified_name(),
                table.qualified_name()
            )));
        }
        self.conn.with_mut(|c| {
            let tx = c.savepoint()?;
            Self::claim_relation(&tx, relation, RelationKind::Index)?;
            tx.execute(
                "INSERT INTO _catalog_indexes
                    (schema_name, relation_name, kind, index_type, table_schema_name,
                     table_relation_name, columns, parameters, definition)
                 VALUES (?1, ?2, 'index', ?3, ?4, ?5, ?6, ?7, ?8)
                 ON CONFLICT(schema_name, relation_name) DO UPDATE SET
                     index_type = excluded.index_type,
                     table_schema_name = excluded.table_schema_name,
                     table_relation_name = excluded.table_relation_name,
                     columns = excluded.columns,
                     parameters = excluded.parameters,
                     definition = excluded.definition",
                params![
                    relation.schema,
                    relation.name,
                    index_type,
                    table.schema,
                    table.name,
                    columns_json,
                    parameters_json,
                    definition_json
                ],
            )?;
            tx.commit()?;
            Ok(())
        })
    }

    pub fn drop_catalog_index(&self, relation: &RelationIdentity) -> Result<()> {
        self.conn.with_mut(|c| {
            let tx = c.savepoint()?;
            tx.execute(
                "DELETE FROM _catalog_indexes
                  WHERE schema_name = ?1 AND relation_name = ?2",
                params![relation.schema, relation.name],
            )?;
            Self::release_relation(&tx, relation, RelationKind::Index)?;
            tx.commit()?;
            Ok(())
        })
    }

    pub fn drop_catalog_indexes_for_table(&self, table_name: &str) -> Result<()> {
        let table =
            RelationIdentity::from_legacy_name(table_name).map_err(SQLiteError::StorageBackend)?;
        self.conn.with_mut(|c| {
            let tx = c.savepoint()?;
            Self::drop_catalog_index_rows_for_table(&tx, &table)?;
            tx.commit()?;
            Ok(())
        })
    }

    pub(in crate::catalog) fn drop_catalog_index_rows_for_table(
        conn: &rusqlite::Connection,
        table: &RelationIdentity,
    ) -> Result<()> {
        let indexes = {
            let mut statement = conn.prepare(
                "SELECT schema_name, relation_name
                   FROM _catalog_indexes
                  WHERE table_schema_name = ?1 AND table_relation_name = ?2",
            )?;
            let indexes = statement
                .query_map(params![table.schema, table.name], |row| {
                    Ok(RelationIdentity::new(
                        row.get::<_, String>(0)?,
                        row.get::<_, String>(1)?,
                    ))
                })?
                .collect::<rusqlite::Result<Vec<_>>>()?;
            indexes
        };
        for index in indexes {
            conn.execute(
                "DELETE FROM _catalog_indexes
                  WHERE schema_name = ?1 AND relation_name = ?2",
                params![index.schema, index.name],
            )?;
            Self::release_relation(conn, &index, RelationKind::Index)?;
        }
        Ok(())
    }

    pub fn load_catalog_indexes(&self) -> Result<Vec<CatalogIndexRow>> {
        self.conn.with(|c| {
            let mut stmt = c.prepare(
                "SELECT schema_name, relation_name, index_type,
                        table_schema_name, table_relation_name, columns, parameters, definition
                   FROM _catalog_indexes ORDER BY schema_name, relation_name",
            )?;
            let rows = stmt.query_map([], |r| {
                Ok((
                    r.get::<_, String>(0)?,
                    r.get::<_, String>(1)?,
                    r.get::<_, String>(2)?,
                    r.get::<_, String>(3)?,
                    r.get::<_, String>(4)?,
                    r.get::<_, String>(5)?,
                    r.get::<_, String>(6)?,
                    r.get::<_, Option<String>>(7)?,
                ))
            })?;
            let mut out = Vec::new();
            for row in rows {
                let (schema, name, ty, table_schema, table_name, cols, params_json, definition) =
                    row?;
                out.push(CatalogIndexRow {
                    relation: RelationIdentity::new(schema, name),
                    index_type: ty,
                    table_name: RelationIdentity::new(table_schema, table_name).qualified_name(),
                    columns_json: cols,
                    parameters_json: params_json,
                    definition_json: definition,
                });
            }
            Ok(out)
        })
    }

    // -- Path indexes ------------------------------------------------------

    pub fn save_path_index(&self, graph_name: &str, label_sequences_json: &str) -> Result<()> {
        self.conn.with(|c| {
            c.execute(
                "INSERT OR REPLACE INTO _path_indexes (graph_name, label_sequences) \
                 VALUES (?1, ?2)",
                params![graph_name, label_sequences_json],
            )?;
            Ok(())
        })
    }

    pub fn drop_path_index(&self, graph_name: &str) -> Result<()> {
        self.conn.with(|c| {
            c.execute(
                "DELETE FROM _path_indexes WHERE graph_name = ?1",
                params![graph_name],
            )?;
            Ok(())
        })
    }

    /// `(graph_name, label_sequences_json)` for every persisted path index.
    pub fn load_path_indexes(&self) -> Result<Vec<(String, String)>> {
        self.conn.with(|c| {
            let mut stmt = c.prepare(
                "SELECT graph_name, label_sequences FROM _path_indexes ORDER BY graph_name",
            )?;
            let rows =
                stmt.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?;
            let mut out = Vec::new();
            for row in rows {
                out.push(row?);
            }
            Ok(out)
        })
    }
}