using subselect to determine leafs
This commit is contained in:
@@ -1,32 +1,33 @@
|
|||||||
const cds = require('@sap/cds/lib')
|
const cds = require('@sap/cds/lib')
|
||||||
if (cds.requires.db?.kind === 'sqlite') {
|
if (cds.requires.db?.kind === 'sqlite') {
|
||||||
|
cds.on ('serving:AdminService', srv => srv.prepend(() => {
|
||||||
// Add a column to the Genres table, to efficiently determine if a node
|
const {Genres} = srv.entities
|
||||||
// is a leaf, and use it for DrillState in hierarchy queries.
|
|
||||||
cds.on('loaded', m => Object.assign (
|
|
||||||
m.definitions['sap.capire.bookshop.Genres'].elements,
|
|
||||||
cds.parse (`entity Genres {
|
|
||||||
DrillState: String = coalesce(leaf,'collapsed');
|
|
||||||
leaf: String;
|
|
||||||
}`).definitions.Genres.elements
|
|
||||||
))
|
|
||||||
|
|
||||||
// Fill in `leaf` helper column for initial data entries
|
|
||||||
cds.on('served', ()=> cds.run(`
|
|
||||||
UPDATE sap_capire_bookshop_Genres as g SET leaf='leaf' WHERE not exists (
|
|
||||||
SELECT 1 from sap_capire_bookshop_Genres WHERE parent_ID = g.ID
|
|
||||||
)`
|
|
||||||
))
|
|
||||||
|
|
||||||
// Register a simplistic handler for hierarchical queries
|
// Register a simplistic handler for hierarchical queries
|
||||||
cds.on('serving:AdminService', srv => srv.prepend(() => srv.on('READ', 'Genres', (req,next) => {
|
srv.on('READ', Genres, (req,next) => {
|
||||||
const q = req.query, parent = {ref:['parent','ID']}
|
const q = req.query
|
||||||
if (q.SELECT.recurse?.where?.[0].ref[0] === 'Distance') q.SELECT.where[0] = parent
|
// Expand query on a single row
|
||||||
else if (!q.SELECT.search) q.SELECT.where ??= [ parent, 'is null' ]
|
if (q.SELECT.recurse?.where?.[0].ref[0] === 'Distance') {
|
||||||
// Suppress error message: Feature "recurse" queries not supported.
|
q.SELECT.from.as = 'g'
|
||||||
delete q.SELECT.__proto__.recurse
|
q.SELECT.columns = q.SELECT.columns.map (c => {
|
||||||
delete q.SELECT.recurse
|
if (c.ref?.[0] === 'DrillState') return { xpr:[`
|
||||||
return next()
|
CASE WHEN exists (
|
||||||
})))
|
SELECT 1 from ${Genres} where parent_ID = g.ID
|
||||||
|
) THEN 'collapsed' ELSE 'leaf' END`
|
||||||
|
], as: 'DrillState' }
|
||||||
|
else return c
|
||||||
|
})
|
||||||
|
q.SELECT.where[0] = 'parent_ID'
|
||||||
|
// Initial query
|
||||||
|
} else if (!q.SELECT.search && !is_count(q)) {
|
||||||
|
q.SELECT.where ??= [ 'parent_ID is null' ]
|
||||||
|
}
|
||||||
|
// Suppress error message: Feature "recurse" queries not supported.
|
||||||
|
delete q.SELECT.__proto__.recurse
|
||||||
|
delete q.SELECT.recurse
|
||||||
|
return next()
|
||||||
|
})
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const is_count = q => q.SELECT.columns.length === 1 && q.SELECT.columns[0].func === 'count'
|
||||||
Reference in New Issue
Block a user