11import { type Database } from 'better-sqlite3'
2- import { StructureType } from '../config'
3- import { parse_json_set } from './helpers'
2+ import { StructureType , TABLES } from '../config'
43
54/**
65 * Type for various types of categorical structures (category, functor, ...)
@@ -14,84 +13,51 @@ export type StructureMeta = {
1413
1514/**
1615 * Returns the list of stored categorical structures of a given type.
16+ * For structures with structure maps (e.g. functors), the associated
17+ * satisfied properties are retrieved as well.
1718 */
1819export function get_structures ( db : Database , type : StructureType ) : StructureMeta [ ] {
19- if ( type === 'category' ) return get_categories ( db )
20- if ( type === 'functor' ) return get_functors ( db )
21- throw new Error ( 'Unsupported type' )
22- }
23-
24- /**
25- * Returns the list of categories saved in the database.
26- */
27- function get_categories ( db : Database ) {
28- return db
29- . prepare <
30- never [ ] ,
31- {
32- id : string
33- name : string
34- dual : string | null
35- }
36- > (
20+ const structures = db
21+ . prepare < [ StructureType ] , StructureMeta > (
3722 `SELECT
38- c .id,
23+ s .id,
3924 s.name,
4025 s.dual_structure_id AS dual
41- FROM categories c
42- INNER JOIN structures s ON s.id = c.id
26+ FROM structures s
27+ WHERE s.type = ?
4328 ORDER BY lower(s.name)` ,
4429 )
45- . all ( )
46- }
30+ . all ( type )
4731
48- /**
49- * Returns the list of functors saved in the database along with
50- * the satisfied properties of their source and target category.
51- */
52- function get_functors ( db : Database ) {
53- const rows = db
54- . prepare <
55- never [ ] ,
56- {
57- id : string
58- name : string
59- source_props : string
60- target_props : string
32+ const structure_maps = db
33+ . prepare < [ StructureType ] , string > ( `SELECT map FROM structure_maps WHERE type = ?` )
34+ . pluck ( )
35+ . all ( type )
36+
37+ if ( ! structure_maps . length ) return structures
38+
39+ const add_associated_properties = db . transaction ( ( ) => {
40+ for ( const map of structure_maps ) {
41+ const prop_query = db
42+ . prepare < [ string ] , string > (
43+ `SELECT property_id FROM property_assignments
44+ INNER JOIN ${ TABLES [ type ] } t ON t.id = ?
45+ WHERE structure_id = t.${ map }
46+ AND is_satisfied = TRUE` ,
47+ )
48+ . pluck ( )
49+
50+ for ( const structure of structures ) {
51+ structure . associated_satisfied_properties ??= { }
52+ const props = prop_query . all ( structure . id )
53+ structure . associated_satisfied_properties [ map ] = new Set ( props )
6154 }
62- > (
63- `SELECT
64- f.id,
65- s.name,
66- (
67- SELECT json_group_array(property_id)
68- FROM property_assignments
69- WHERE
70- structure_id = f.source
71- AND is_satisfied = TRUE
72-
73- ) AS source_props,
74- (
75- SELECT json_group_array(property_id)
76- FROM property_assignments
77- WHERE
78- structure_id = f.target
79- AND is_satisfied = TRUE
80- ) AS target_props
81- FROM functors f
82- INNER JOIN structures s ON s.id = f.id
83- ORDER BY lower(s.name)` ,
84- )
85- . all ( )
55+ }
56+ } )
57+
58+ add_associated_properties ( )
8659
87- return rows . map ( ( functor ) => ( {
88- id : functor . id ,
89- name : functor . name ,
90- associated_satisfied_properties : {
91- source : parse_json_set < string > ( functor . source_props ) ,
92- target : parse_json_set < string > ( functor . target_props ) ,
93- } ,
94- } ) )
60+ return structures
9561}
9662
9763/**
0 commit comments