|
|
@ -12,95 +12,109 @@ const sqlite3 = { |
|
|
|
...commonFns, |
|
|
|
...commonFns, |
|
|
|
LEN: 'LENGTH', |
|
|
|
LEN: 'LENGTH', |
|
|
|
async CEILING(args) { |
|
|
|
async CEILING(args) { |
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`round(${(await args.fn(args.pt.arguments[0])).builder} + 0.5)${ |
|
|
|
builder: args.knex.raw( |
|
|
|
args.colAlias |
|
|
|
`round(${(await args.fn(args.pt.arguments[0])).builder} + 0.5)${ |
|
|
|
}` |
|
|
|
args.colAlias |
|
|
|
); |
|
|
|
}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
async FLOOR(args) { |
|
|
|
async FLOOR(args) { |
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`round(${(await args.fn(args.pt.arguments[0])).builder} - 0.5)${ |
|
|
|
builder: args.knex.raw( |
|
|
|
args.colAlias |
|
|
|
`round(${(await args.fn(args.pt.arguments[0])).builder} - 0.5)${ |
|
|
|
}` |
|
|
|
args.colAlias |
|
|
|
); |
|
|
|
}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
MOD: async (args: MapFnArgs) => { |
|
|
|
MOD: async (args: MapFnArgs) => { |
|
|
|
return ( |
|
|
|
return args.fn({ |
|
|
|
await args.fn({ |
|
|
|
type: 'BinaryExpression', |
|
|
|
type: 'BinaryExpression', |
|
|
|
operator: '%', |
|
|
|
operator: '%', |
|
|
|
left: args.pt.arguments[0], |
|
|
|
left: args.pt.arguments[0], |
|
|
|
right: args.pt.arguments[1], |
|
|
|
right: args.pt.arguments[1], |
|
|
|
}); |
|
|
|
}) |
|
|
|
|
|
|
|
).builder; |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
async REPEAT(args: MapFnArgs) { |
|
|
|
async REPEAT(args: MapFnArgs) { |
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`replace(printf('%.' || ${ |
|
|
|
builder: args.knex.raw( |
|
|
|
(await args.fn(args.pt.arguments[1])).builder |
|
|
|
`replace(printf('%.' || ${ |
|
|
|
} || 'c', '/'),'/',${(await args.fn(args.pt.arguments[0])).builder})${ |
|
|
|
(await args.fn(args.pt.arguments[1])).builder |
|
|
|
args.colAlias |
|
|
|
} || 'c', '/'),'/',${(await args.fn(args.pt.arguments[0])).builder})${ |
|
|
|
}` |
|
|
|
args.colAlias |
|
|
|
); |
|
|
|
}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
NOW: 'DATE', |
|
|
|
NOW: 'DATE', |
|
|
|
SEARCH: 'INSTR', |
|
|
|
SEARCH: 'INSTR', |
|
|
|
async INT(args: MapFnArgs) { |
|
|
|
async INT(args: MapFnArgs) { |
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`CAST(${(await args.fn(args.pt.arguments[0])).builder} as INTEGER)${ |
|
|
|
builder: args.knex.raw( |
|
|
|
args.colAlias |
|
|
|
`CAST(${(await args.fn(args.pt.arguments[0])).builder} as INTEGER)${ |
|
|
|
}` |
|
|
|
args.colAlias |
|
|
|
); |
|
|
|
}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
LEFT: async (args: MapFnArgs) => { |
|
|
|
LEFT: async (args: MapFnArgs) => { |
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},1,${ |
|
|
|
builder: args.knex.raw( |
|
|
|
(await args.fn(args.pt.arguments[1])).builder |
|
|
|
`SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},1,${ |
|
|
|
})${args.colAlias}` |
|
|
|
(await args.fn(args.pt.arguments[1])).builder |
|
|
|
); |
|
|
|
})${args.colAlias}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
RIGHT: async (args: MapFnArgs) => { |
|
|
|
RIGHT: async (args: MapFnArgs) => { |
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},-(${ |
|
|
|
builder: args.knex.raw( |
|
|
|
(await args.fn(args.pt.arguments[1])).builder |
|
|
|
`SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},-(${ |
|
|
|
}))${args.colAlias}` |
|
|
|
(await args.fn(args.pt.arguments[1])).builder |
|
|
|
); |
|
|
|
}))${args.colAlias}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
MID: 'SUBSTR', |
|
|
|
MID: 'SUBSTR', |
|
|
|
FLOAT: async (args: MapFnArgs) => { |
|
|
|
FLOAT: async (args: MapFnArgs) => { |
|
|
|
return args.knex |
|
|
|
return { |
|
|
|
.raw( |
|
|
|
builder: args.knex |
|
|
|
`CAST(${(await args.fn(args.pt.arguments[0])).builder} as FLOAT)${ |
|
|
|
.raw( |
|
|
|
args.colAlias |
|
|
|
`CAST(${(await args.fn(args.pt.arguments[0])).builder} as FLOAT)${ |
|
|
|
}` |
|
|
|
args.colAlias |
|
|
|
) |
|
|
|
}` |
|
|
|
.wrap('(', ')'); |
|
|
|
) |
|
|
|
|
|
|
|
.wrap('(', ')'), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
const dateIN = (await fn(pt.arguments[1])).builder; |
|
|
|
const dateIN = (await fn(pt.arguments[1])).builder; |
|
|
|
return knex.raw( |
|
|
|
return { |
|
|
|
`CASE
|
|
|
|
builder: knex.raw( |
|
|
|
|
|
|
|
`CASE
|
|
|
|
WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN |
|
|
|
WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN |
|
|
|
STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${fn( |
|
|
|
STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${fn( |
|
|
|
pt.arguments[0] |
|
|
|
pt.arguments[0] |
|
|
|
)}, 'localtime'), |
|
|
|
)}, 'localtime'), |
|
|
|
${dateIN > 0 ? '+' : ''}${ |
|
|
|
${dateIN > 0 ? '+' : ''}${ |
|
|
|
(await fn(pt.arguments[1])).builder |
|
|
|
(await fn(pt.arguments[1])).builder |
|
|
|
} || ' ${String((await fn(pt.arguments[2])).builder).replace( |
|
|
|
} || ' ${String((await fn(pt.arguments[2])).builder).replace( |
|
|
|
/["']/g, |
|
|
|
/["']/g, |
|
|
|
'' |
|
|
|
'' |
|
|
|
)}')) |
|
|
|
)}')) |
|
|
|
ELSE |
|
|
|
ELSE |
|
|
|
DATE(DATETIME(${(await fn(pt.arguments[0])).builder}, 'localtime'), |
|
|
|
DATE(DATETIME(${(await fn(pt.arguments[0])).builder}, 'localtime'), |
|
|
|
${dateIN > 0 ? '+' : ''}${ |
|
|
|
${dateIN > 0 ? '+' : ''}${ |
|
|
|
(await fn(pt.arguments[1])).builder |
|
|
|
(await fn(pt.arguments[1])).builder |
|
|
|
} || ' ${String((await fn(pt.arguments[2])).builder).replace( |
|
|
|
} || ' ${String((await fn(pt.arguments[2])).builder).replace( |
|
|
|
/["']/g, |
|
|
|
/["']/g, |
|
|
|
'' |
|
|
|
'' |
|
|
|
)}') |
|
|
|
)}') |
|
|
|
END${colAlias}` |
|
|
|
END${colAlias}` |
|
|
|
); |
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
let datetime_expr1 = (await fn(pt.arguments[0])).builder; |
|
|
|
let datetime_expr1 = (await fn(pt.arguments[0])).builder; |
|
|
@ -174,54 +188,60 @@ const sqlite3 = { |
|
|
|
default: |
|
|
|
default: |
|
|
|
sql = ''; |
|
|
|
sql = ''; |
|
|
|
} |
|
|
|
} |
|
|
|
return knex.raw(`${sql} ${colAlias}`); |
|
|
|
return { builder: knex.raw(`${sql} ${colAlias}`) }; |
|
|
|
}, |
|
|
|
}, |
|
|
|
WEEKDAY: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
WEEKDAY: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
// strftime('%w', date) - day of week 0 - 6 with Sunday == 0
|
|
|
|
// strftime('%w', date) - day of week 0 - 6 with Sunday == 0
|
|
|
|
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
|
|
|
|
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
|
|
|
|
return knex.raw( |
|
|
|
return { |
|
|
|
`(strftime('%w', ${ |
|
|
|
builder: knex.raw( |
|
|
|
pt.arguments[0].type === 'Literal' |
|
|
|
`(strftime('%w', ${ |
|
|
|
? `'${dayjs((await fn(pt.arguments[0])).builder).format( |
|
|
|
pt.arguments[0].type === 'Literal' |
|
|
|
'YYYY-MM-DD' |
|
|
|
? `'${dayjs((await fn(pt.arguments[0])).builder).format( |
|
|
|
)}'` |
|
|
|
'YYYY-MM-DD' |
|
|
|
: (await fn(pt.arguments[0])).builder |
|
|
|
)}'` |
|
|
|
}) - 1 - ${getWeekdayByText( |
|
|
|
: (await fn(pt.arguments[0])).builder |
|
|
|
pt?.arguments[1]?.value |
|
|
|
}) - 1 - ${getWeekdayByText( |
|
|
|
)} % 7 + 7) % 7 ${colAlias}` |
|
|
|
pt?.arguments[1]?.value |
|
|
|
); |
|
|
|
)} % 7 + 7) % 7 ${colAlias}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
AND: async (args: MapFnArgs) => { |
|
|
|
AND: async (args: MapFnArgs) => { |
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`CASE WHEN ${args.knex |
|
|
|
builder: args.knex.raw( |
|
|
|
.raw( |
|
|
|
`CASE WHEN ${args.knex |
|
|
|
`${( |
|
|
|
.raw( |
|
|
|
await Promise.all( |
|
|
|
`${( |
|
|
|
args.pt.arguments.map(async (ar) => |
|
|
|
await Promise.all( |
|
|
|
(await args.fn(ar, '', 'AND')).builder.toQuery() |
|
|
|
args.pt.arguments.map(async (ar) => |
|
|
|
|
|
|
|
(await args.fn(ar, '', 'AND')).builder.toQuery() |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
).join(' AND ')}` |
|
|
|
).join(' AND ')}` |
|
|
|
) |
|
|
|
) |
|
|
|
.wrap('(', ')') |
|
|
|
.wrap('(', ')') |
|
|
|
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}` |
|
|
|
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}` |
|
|
|
), |
|
|
|
); |
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
OR: async (args: MapFnArgs) => { |
|
|
|
OR: async (args: MapFnArgs) => { |
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`CASE WHEN ${args.knex |
|
|
|
builder: args.knex.raw( |
|
|
|
.raw( |
|
|
|
`CASE WHEN ${args.knex |
|
|
|
`${( |
|
|
|
.raw( |
|
|
|
await Promise.all( |
|
|
|
`${( |
|
|
|
args.pt.arguments.map(async (ar) => |
|
|
|
await Promise.all( |
|
|
|
(await args.fn(ar, '', 'OR')).builder.toQuery() |
|
|
|
args.pt.arguments.map(async (ar) => |
|
|
|
|
|
|
|
(await args.fn(ar, '', 'OR')).builder.toQuery() |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
).join(' OR ')}` |
|
|
|
).join(' OR ')}` |
|
|
|
) |
|
|
|
) |
|
|
|
.wrap('(', ')') |
|
|
|
.wrap('(', ')') |
|
|
|
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}` |
|
|
|
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}` |
|
|
|
), |
|
|
|
); |
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|