|
|
@ -33,7 +33,6 @@ async function treatArgAsConditionalExp( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export default { |
|
|
|
export default { |
|
|
|
// todo: handle default case
|
|
|
|
|
|
|
|
SWITCH: async (args: MapFnArgs) => { |
|
|
|
SWITCH: async (args: MapFnArgs) => { |
|
|
|
const count = Math.floor((args.pt.arguments.length - 1) / 2); |
|
|
|
const count = Math.floor((args.pt.arguments.length - 1) / 2); |
|
|
|
let query = ''; |
|
|
|
let query = ''; |
|
|
@ -55,6 +54,9 @@ export default { |
|
|
|
|
|
|
|
|
|
|
|
const switchVal = (await args.fn(args.pt.arguments[0])).builder.toQuery(); |
|
|
|
const switchVal = (await args.fn(args.pt.arguments[0])).builder.toQuery(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// used it for null value check
|
|
|
|
|
|
|
|
let elseValPrefix = ''; |
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < count; i++) { |
|
|
|
for (let i = 0; i < count; i++) { |
|
|
|
let val; |
|
|
|
let val; |
|
|
|
// cast to string if the return value types are different
|
|
|
|
// cast to string if the return value types are different
|
|
|
@ -73,13 +75,29 @@ export default { |
|
|
|
val = (await args.fn(args.pt.arguments[i * 2 + 2])).builder.toQuery(); |
|
|
|
val = (await args.fn(args.pt.arguments[i * 2 + 2])).builder.toQuery(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
query += args.knex |
|
|
|
if ( |
|
|
|
.raw( |
|
|
|
args.pt.arguments[i * 2 + 1].type === 'CallExpression' && |
|
|
|
`\n\tWHEN ${( |
|
|
|
args.pt.arguments[i * 2 + 1].callee?.name === 'BLANK' && |
|
|
|
await args.fn(args.pt.arguments[i * 2 + 1]) |
|
|
|
args.pt.arguments[i * 2 + 1].dataType === FormulaDataTypes.STRING |
|
|
|
).builder.toQuery()} THEN ${val}`,
|
|
|
|
) { |
|
|
|
) |
|
|
|
elseValPrefix += args.knex |
|
|
|
.toQuery(); |
|
|
|
.raw(`\n\tWHEN ${switchVal} IS NULL OR ${switchVal} = '' THEN ${val}`) |
|
|
|
|
|
|
|
.toQuery(); |
|
|
|
|
|
|
|
} else if ( |
|
|
|
|
|
|
|
args.pt.arguments[i * 2 + 1].dataType === FormulaDataTypes.NULL |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
elseValPrefix += args.knex |
|
|
|
|
|
|
|
.raw(`\n\tWHEN ${switchVal} IS NULL THEN ${val}`) |
|
|
|
|
|
|
|
.toQuery(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
query += args.knex |
|
|
|
|
|
|
|
.raw( |
|
|
|
|
|
|
|
`\n\tWHEN ${( |
|
|
|
|
|
|
|
await args.fn(args.pt.arguments[i * 2 + 1]) |
|
|
|
|
|
|
|
).builder.toQuery()} THEN ${val}`,
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.toQuery(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (args.pt.arguments.length % 2 === 0) { |
|
|
|
if (args.pt.arguments.length % 2 === 0) { |
|
|
|
let val; |
|
|
|
let val; |
|
|
@ -100,8 +118,13 @@ export default { |
|
|
|
await args.fn(args.pt.arguments[args.pt.arguments.length - 1]) |
|
|
|
await args.fn(args.pt.arguments[args.pt.arguments.length - 1]) |
|
|
|
).builder.toQuery(); |
|
|
|
).builder.toQuery(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (elseValPrefix) { |
|
|
|
query += `\n\tELSE ${val}`; |
|
|
|
query += `\n\tELSE (CASE ${elseValPrefix} ELSE ${val} END)`; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
query += `\n\tELSE ${val}`; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else if (elseValPrefix) { |
|
|
|
|
|
|
|
query += `\n\tELSE (CASE ${elseValPrefix} END)`; |
|
|
|
} |
|
|
|
} |
|
|
|
return { |
|
|
|
return { |
|
|
|
builder: args.knex.raw( |
|
|
|
builder: args.knex.raw( |
|
|
|