|
|
|
@ -14,9 +14,7 @@ const mssql = {
|
|
|
|
|
for (const [i, arg] of Object.entries(args.pt.arguments)) { |
|
|
|
|
if (+i === args.pt.arguments.length - 1) { |
|
|
|
|
query += args.knex |
|
|
|
|
.raw( |
|
|
|
|
`\n\tElse ${(await await args.fn(arg)).builder.builder.toQuery()}` |
|
|
|
|
) |
|
|
|
|
.raw(`\n\tElse ${(await args.fn(arg)).builder.builder.toQuery()}`) |
|
|
|
|
.toQuery(); |
|
|
|
|
} else { |
|
|
|
|
query += args.knex |
|
|
|
@ -37,7 +35,7 @@ const mssql = {
|
|
|
|
|
.toQuery(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return args.knex.raw(`Case ${query}\n End${args.colAlias}`); |
|
|
|
|
return { builder: args.knex.raw(`Case ${query}\n End${args.colAlias}`) }; |
|
|
|
|
}, |
|
|
|
|
MAX: async (args: MapFnArgs) => { |
|
|
|
|
if (args.pt.arguments.length === 1) { |
|
|
|
@ -66,10 +64,11 @@ const mssql = {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return args.knex.raw(`Case ${query}\n End${args.colAlias}`); |
|
|
|
|
return { builder: args.knex.raw(`Case ${query}\n End${args.colAlias}`) }; |
|
|
|
|
}, |
|
|
|
|
LOG: async (args: MapFnArgs) => { |
|
|
|
|
return args.knex.raw( |
|
|
|
|
return { |
|
|
|
|
builder: args.knex.raw( |
|
|
|
|
`LOG(${( |
|
|
|
|
await Promise.all( |
|
|
|
|
args.pt.arguments |
|
|
|
@ -77,7 +76,8 @@ const mssql = {
|
|
|
|
|
.map(async (ar) => (await args.fn(ar)).builder.toQuery()) |
|
|
|
|
) |
|
|
|
|
).join(',')})${args.colAlias}` |
|
|
|
|
); |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
MOD: (pt) => { |
|
|
|
|
Object.assign(pt, { |
|
|
|
@ -96,52 +96,71 @@ const mssql = {
|
|
|
|
|
args.pt.arguments[1] = temp; |
|
|
|
|
}, |
|
|
|
|
INT: async (args: MapFnArgs) => { |
|
|
|
|
return args.knex.raw( |
|
|
|
|
return { |
|
|
|
|
builder: args.knex.raw( |
|
|
|
|
`CASE WHEN ISNUMERIC(${( |
|
|
|
|
await args.fn(args.pt.arguments[0]) |
|
|
|
|
).builder.toQuery()}) = 1 THEN FLOOR(${( |
|
|
|
|
await args.fn(args.pt.arguments[0]) |
|
|
|
|
).builder.toQuery()}) ELSE 0 END${args.colAlias}` |
|
|
|
|
); |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
MID: 'SUBSTR', |
|
|
|
|
FLOAT: (args: MapFnArgs) => { |
|
|
|
|
return args.knex |
|
|
|
|
.raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`) |
|
|
|
|
.wrap('(', ')'); |
|
|
|
|
FLOAT: async (args: MapFnArgs) => { |
|
|
|
|
return { |
|
|
|
|
builder: args.knex |
|
|
|
|
.raw( |
|
|
|
|
`CAST(${(await args.fn(args.pt.arguments[0])).builder} as FLOAT)${ |
|
|
|
|
args.colAlias |
|
|
|
|
}` |
|
|
|
|
) |
|
|
|
|
.wrap('(', ')'), |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
|
const dateIN = (await fn(pt.arguments[1])).builder; |
|
|
|
|
return knex.raw( |
|
|
|
|
return { |
|
|
|
|
builder: knex.raw( |
|
|
|
|
`CASE
|
|
|
|
|
WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN |
|
|
|
|
FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')},
|
|
|
|
|
${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])}, ${fn( |
|
|
|
|
pt.arguments[0] |
|
|
|
|
)}), 'yyyy-MM-dd HH:mm') |
|
|
|
|
WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN |
|
|
|
|
FORMAT(DATEADD(${String((await fn(pt.arguments[2])).builder).replace( |
|
|
|
|
/["']/g, |
|
|
|
|
'' |
|
|
|
|
)},
|
|
|
|
|
${dateIN > 0 ? '+' : ''}${(await fn(pt.arguments[1])).builder}, ${ |
|
|
|
|
(await fn(pt.arguments[0])).builder |
|
|
|
|
}), 'yyyy-MM-dd HH:mm') |
|
|
|
|
ELSE |
|
|
|
|
FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')},
|
|
|
|
|
${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])}, ${fn( |
|
|
|
|
FORMAT(DATEADD(${String((await fn(pt.arguments[2])).builder).replace( |
|
|
|
|
/["']/g, |
|
|
|
|
'' |
|
|
|
|
)},
|
|
|
|
|
${dateIN > 0 ? '+' : ''}${(await fn(pt.arguments[1])).builder}, ${fn( |
|
|
|
|
pt.arguments[0] |
|
|
|
|
)}), 'yyyy-MM-dd') |
|
|
|
|
END${colAlias}` |
|
|
|
|
); |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
|
const datetime_expr1 = fn(pt.arguments[0]); |
|
|
|
|
const datetime_expr2 = fn(pt.arguments[1]); |
|
|
|
|
const datetime_expr1 = (await fn(pt.arguments[0])).builder; |
|
|
|
|
const datetime_expr2 = (await fn(pt.arguments[1])).builder; |
|
|
|
|
const rawUnit = pt.arguments[2] |
|
|
|
|
? (await fn(pt.arguments[2])).builder.bindings[0] |
|
|
|
|
: 'seconds'; |
|
|
|
|
const unit = convertUnits(rawUnit, 'mssql'); |
|
|
|
|
return knex.raw( |
|
|
|
|
return { |
|
|
|
|
builder: knex.raw( |
|
|
|
|
`DATEDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) ${colAlias}` |
|
|
|
|
); |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
WEEKDAY: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
|
// DATEPART(WEEKDAY, DATE): sunday = 1, monday = 2, ..., saturday = 7
|
|
|
|
|
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
|
|
|
|
|
return knex.raw( |
|
|
|
|
return { |
|
|
|
|
builder: knex.raw( |
|
|
|
|
`(DATEPART(WEEKDAY, ${ |
|
|
|
|
pt.arguments[0].type === 'Literal' |
|
|
|
|
? `'${dayjs((await fn(pt.arguments[0])).builder).format( |
|
|
|
@ -151,10 +170,12 @@ const mssql = {
|
|
|
|
|
}) - 2 - ${getWeekdayByText( |
|
|
|
|
pt?.arguments[1]?.value |
|
|
|
|
)} % 7 + 7) % 7 ${colAlias}` |
|
|
|
|
); |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
AND: async (args: MapFnArgs) => { |
|
|
|
|
return args.knex.raw( |
|
|
|
|
return { |
|
|
|
|
builder: args.knex.raw( |
|
|
|
|
`CASE WHEN ${args.knex |
|
|
|
|
.raw( |
|
|
|
|
`${( |
|
|
|
@ -167,10 +188,12 @@ const mssql = {
|
|
|
|
|
) |
|
|
|
|
.wrap('(', ')') |
|
|
|
|
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}` |
|
|
|
|
); |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
OR: async (args: MapFnArgs) => { |
|
|
|
|
return args.knex.raw( |
|
|
|
|
return { |
|
|
|
|
builder: args.knex.raw( |
|
|
|
|
`CASE WHEN ${args.knex |
|
|
|
|
.raw( |
|
|
|
|
`${( |
|
|
|
@ -183,7 +206,8 @@ const mssql = {
|
|
|
|
|
) |
|
|
|
|
.wrap('(', ')') |
|
|
|
|
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}` |
|
|
|
|
); |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|