Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions databases/catdat/migrations/014_order_assignments.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
DROP TABLE category_property_assignments;

CREATE TABLE category_property_assignments (
id INTEGER PRIMARY KEY,
category_id TEXT NOT NULL,
property_id TEXT NOT NULL,
is_satisfied INTEGER NOT NULL
CHECK (is_satisfied in (TRUE, FALSE)),
reason TEXT NOT NULL CHECK (length(reason) > 0),
is_deduced INTEGER NOT NULL DEFAULT FALSE
CHECK (is_deduced in (TRUE, FALSE)),
UNIQUE (category_id, property_id),
FOREIGN KEY (category_id) REFERENCES categories (id) ON DELETE CASCADE,
FOREIGN KEY (property_id) REFERENCES category_properties (id) ON DELETE CASCADE
);

CREATE INDEX idx_property_category ON category_property_assignments (property_id);

DROP TABLE functor_property_assignments;

CREATE TABLE functor_property_assignments (
id INTEGER PRIMARY KEY,
functor_id TEXT NOT NULL,
property_id TEXT NOT NULL,
is_satisfied INTEGER NOT NULL
CHECK (is_satisfied IN (TRUE, FALSE)),
reason TEXT NOT NULL CHECK (length(reason) > 0),
is_deduced INTEGER NOT NULL DEFAULT FALSE
CHECK (is_deduced in (TRUE, FALSE)),
UNIQUE (functor_id, property_id),
FOREIGN KEY (functor_id) REFERENCES functors (id) ON DELETE CASCADE,
FOREIGN KEY (property_id) REFERENCES functor_properties (id) ON DELETE CASCADE
);

CREATE INDEX idx_property_functor ON functor_property_assignments (property_id);
50 changes: 26 additions & 24 deletions databases/catdat/scripts/deduce-category-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,21 @@ async function deduce_satisfied_category_properties(
const value_fragments: string[] = []
const values: (string | number)[] = []

for (let i = 0; i < deduced_satisfied_props.length; i++) {
const id = deduced_satisfied_props[i]
value_fragments.push(`(?, ?, TRUE, ?, ?, TRUE)`)
values.push(category_id, id, reasons[id], i + 1)
for (const id of deduced_satisfied_props) {
value_fragments.push(`(?, ?, TRUE, ?, TRUE)`)
values.push(category_id, id, reasons[id])
}

const insert_sql = options.check_conflicts
? `INSERT INTO category_property_assignments
(category_id, property_id, is_satisfied, reason, position, is_deduced)
VALUES ${value_fragments.join(',\n')}`
: `INSERT INTO category_property_assignments
(category_id, property_id, is_satisfied, reason, position, is_deduced)
VALUES ${value_fragments.join(',\n')}
ON CONFLICT (category_id, property_id) DO NOTHING`
const conflict_clause = options.check_conflicts
? ''
: 'ON CONFLICT (category_id, property_id) DO NOTHING'

const insert_sql = `
INSERT INTO category_property_assignments
(category_id, property_id, is_satisfied, reason, is_deduced)
VALUES ${value_fragments.join(',\n')}
${conflict_clause}
`

try {
await tx.execute({ sql: insert_sql, args: values })
Expand Down Expand Up @@ -390,20 +391,21 @@ async function deduce_unsatisfied_category_properties(
const value_fragments: string[] = []
const values: (string | number)[] = []

for (let i = 0; i < deduced_unsatisfied_props.length; i++) {
const id = deduced_unsatisfied_props[i]
value_fragments.push('(?, ?, FALSE, ?, ?, TRUE)')
values.push(category_id, id, reasons[id], i + 1)
for (const id of deduced_unsatisfied_props) {
value_fragments.push('(?, ?, FALSE, ?, TRUE)')
values.push(category_id, id, reasons[id])
}

const insert_query = options.check_conflicts
? `INSERT INTO category_property_assignments
(category_id, property_id, is_satisfied, reason, position, is_deduced)
VALUES ${value_fragments.join(',\n')}`
: `INSERT INTO category_property_assignments
(category_id, property_id, is_satisfied, reason, position, is_deduced)
VALUES ${value_fragments.join(',\n')}
ON CONFLICT (category_id, property_id) DO NOTHING`
const conflict_clause = options.check_conflicts
? ''
: 'ON CONFLICT (category_id, property_id) DO NOTHING'

const insert_query = `
INSERT INTO category_property_assignments
(category_id, property_id, is_satisfied, reason, is_deduced)
VALUES ${value_fragments.join(',\n')}
${conflict_clause}
`

try {
await tx.execute({ sql: insert_query, args: values })
Expand Down
15 changes: 7 additions & 8 deletions databases/catdat/scripts/deduce-functor-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,14 @@ async function deduce_satisfied_functor_properties(
const value_fragments: string[] = []
const values: (string | number)[] = []

for (let i = 0; i < deduced_satisfied_props.length; i++) {
const id = deduced_satisfied_props[i]
value_fragments.push(`(?, ?, TRUE, ?, ?, TRUE)`)
values.push(functor.id, id, reasons[id], i + 1)
for (const id of deduced_satisfied_props) {
value_fragments.push(`(?, ?, TRUE, ?, TRUE)`)
values.push(functor.id, id, reasons[id])
}

const insert_sql = `
INSERT INTO functor_property_assignments (
functor_id, property_id, is_satisfied, reason, position, is_deduced
functor_id, property_id, is_satisfied, reason, is_deduced
)
VALUES
${value_fragments.join(',\n')}
Expand Down Expand Up @@ -361,13 +360,13 @@ async function deduce_unsatisfied_functor_properties(

for (let i = 0; i < deduced_unsatisfied_props.length; i++) {
const id = deduced_unsatisfied_props[i]
value_fragments.push('(?, ?, FALSE, ?, ?, TRUE)')
values.push(functor.id, id, reasons[id], i + 1)
value_fragments.push('(?, ?, FALSE, ?, TRUE)')
values.push(functor.id, id, reasons[id])
}

const insert_query = `
INSERT INTO functor_property_assignments (
functor_id, property_id, is_satisfied, reason, position, is_deduced
functor_id, property_id, is_satisfied, reason, is_deduced
)
VALUES
${value_fragments.join(',\n')}`
Expand Down
2 changes: 1 addition & 1 deletion src/routes/category/[id]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const load = async (event) => {
INNER JOIN category_properties p ON p.id = cp.property_id
INNER JOIN relations r ON r.relation = p.relation
WHERE cp.category_id = ${id}
ORDER BY cp.position, lower(cp.property_id)
ORDER BY cp.id
`,
// unknown properties
sql`
Expand Down
2 changes: 1 addition & 1 deletion src/routes/functor/[id]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const load = async (event) => {
INNER JOIN functor_properties p ON p.id = fp.property_id
INNER JOIN relations r ON r.relation = p.relation
WHERE fp.functor_id = ${id}
ORDER BY fp.position, lower(fp.property_id)
ORDER BY fp.id
`,
// unknown properties
sql`
Expand Down