【sap cap 条件处理】
条件三种写法// 基础多表关联查询 - 添加书籍标题和作者名称条件this.on(getBooksWithAuthorFilter, async(req){const querySELECT .from(Books).columns(b{b.ID, b.title, b.genre, b.price, b.stock, b.author(a{a.ID, a.name, a.country})})// 添加复合条件书籍标题为a且 作者名称为c.where({and:[{title:a}, // 书籍标题条件{author:{name:c}}// 关联作者名称条件]});returnawait cds.run(query);});// 使用链式where条件的另一种写法this.on(getBooksWithAuthorFilterChained, async(req){const querySELECT .from(Books).columns(b{b.ID, b.title, b.genre, b.price, b.stock, b.author(a{a.ID, a.name, a.country})})// 链式条件写法 .where({title:a}).where({author:{name:c}});returnawait cds.run(query);});// 动态条件查询this.on(getBooksWithDynamicFilter, async(req){const{bookTitle, authorName}req.data;const querySELECT .from(Books).columns(b{b.ID, b.title, b.genre, b.price, b.stock, b.author(a{a.ID, a.name, a.country})})// 动态构建条件 .where(b{const conditions[];if(bookTitle)conditions.push({title: bookTitle});if(authorName)conditions.push({author:{name: authorName}});returnconditions.length0?{and: conditions}:true;});returnawait cds.run(query);});