kylefarris / node-querybuilder

Node QueryBuilder Adapter for Node.js (tags: nodejs, node, mysql, active record, activerecord, querybuilder, query builder)
49 stars 19 forks source link

Error when using case when #62

Open cyberrspiritt opened 4 years ago

cyberrspiritt commented 4 years ago

Trying to run this query including case when clause

 this.db.select("ar.id as resolution_id, ar.resolution_no, ar.resolution_heading, ar.resolution_text, ar.resolution_proposer, ar.resolution_seconder, " +
        "case when (art.proxy_vote_allowed=1 and ar.proxy_allowed_to_vote=1) then 1 " +
        "when (art.proxy_vote_allowed=1 and ar.`proxy_allowed_to_vote`=0) then 0 " +
        "when (art.proxy_vote_allowed=0 and ar.`proxy_allowed_to_vote`=0) then 0 " +
        "when (art.proxy_vote_allowed=0 and ar.`proxy_allowed_to_vote`=1) then 1 " +
        "end as proxy_vote_allowed, ard.decision_made_by, ard.decision, art.resolution_type, art.resolution_category, art.objection_feasibility, art.pending_dues_vote_allowed, art.decision_calculation_type, art.decision_at_agm, art.decision_threshold_percentage", false)
        .from('agm_resolutions as ar')
        .join('agm_resolution_decisions as ard', 'ar.id = ard.resolution_id and ard.is_active=1', 'left')
        .join('agm_resolution_types as art', 'art.id = ar.resolution_type_id and art.is_active=1')
        .where('ar.agm_id', agmId)
        .where('ar.is_active', 1);

    try {
        let result = await this.db.get();
        this.db.disconnect();
        return result;
    } catch (e) {
        console.log(this.db.last_query());
    }

but the last_query function prints out something different:

SELECT ar.id as resolution_id, ar.resolution_no, ar.resolution_heading, ar.resolution_text, ar.resolution_proposer, ar.resolution_seconder, case when (art.proxy_vote_allowed=1 and ar.proxy_allowed_to_vote=1) then 1 when (art.proxy_vote_allowed=1 and ar.`proxy_allowed_to_vote`=0) then 0 when (art.proxy_vote_allowed=0 and ar.`proxy_allowed_to_vote`=0) then 0 when (art.proxy_vote_allowed=0 and ar.`proxy_allowed_to_vote`=1) AS `then 1 endproxy_vote_allowed, ard`.`decision_made_by, ard`.`decision, art`.`resolution_type, art`.`resolution_category, art`.`objection_feasibility, art`.`pending_dues_vote_allowed, art`.`decision_calculation_type, art`.`decision_at_agm, art`.`decision_threshold_percentage` FROM `agm_resolutions` as `ar` LEFT JOIN `agm_resolution_decisions` as `ard` ON `ar`.`id` = `ard`.`resolution_id` and `ard`.`is_active` = 1 JOIN `agm_resolution_types` as `art` ON `art`.`id` = `ar`.`resolution_type_id` and `art`.`is_active` = 1 WHERE `ar`.`agm_id` = 1 AND `ar`.`is_active` = 1

It prints out parsing error, obviously because it messed up the case when condition when (art.proxy_vote_allowed=0 and ar.proxy_allowed_to_vote=1) AS `then 1 endproxy_vote_allowed, here.