|
|
@ -13,43 +13,53 @@ const pg = { |
|
|
|
POWER: 'pow', |
|
|
|
POWER: 'pow', |
|
|
|
SQRT: 'sqrt', |
|
|
|
SQRT: 'sqrt', |
|
|
|
SEARCH: async (args: MapFnArgs) => { |
|
|
|
SEARCH: async (args: MapFnArgs) => { |
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`POSITION(${args.knex.raw( |
|
|
|
builder: args.knex.raw( |
|
|
|
(await args.fn(args.pt.arguments[1])).builder.toQuery() |
|
|
|
`POSITION(${args.knex.raw( |
|
|
|
)} in ${args.knex |
|
|
|
(await args.fn(args.pt.arguments[1])).builder.toQuery() |
|
|
|
.raw((await args.fn(args.pt.arguments[0])).builder) |
|
|
|
)} in ${args.knex |
|
|
|
.toQuery()})${args.colAlias}` |
|
|
|
.raw((await args.fn(args.pt.arguments[0])).builder) |
|
|
|
); |
|
|
|
.toQuery()})${args.colAlias}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
INT(args: MapFnArgs) { |
|
|
|
INT(args: MapFnArgs) { |
|
|
|
// todo: correction
|
|
|
|
// todo: correction
|
|
|
|
return args.knex.raw( |
|
|
|
return { |
|
|
|
`REGEXP_REPLACE(COALESCE(${args.fn( |
|
|
|
builder: args.knex.raw( |
|
|
|
args.pt.arguments[0] |
|
|
|
`REGEXP_REPLACE(COALESCE(${args.fn( |
|
|
|
)}::character varying, '0'), '[^0-9]+|\\.[0-9]+' ,'')${args.colAlias}` |
|
|
|
args.pt.arguments[0] |
|
|
|
); |
|
|
|
)}::character varying, '0'), '[^0-9]+|\\.[0-9]+' ,'')${args.colAlias}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
MID: 'SUBSTR', |
|
|
|
MID: 'SUBSTR', |
|
|
|
FLOAT: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
FLOAT: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
return knex |
|
|
|
return { |
|
|
|
.raw(`CAST(${fn(pt.arguments[0])} as DOUBLE PRECISION)${colAlias}`) |
|
|
|
builder: knex |
|
|
|
.wrap('(', ')'); |
|
|
|
.raw(`CAST(${fn(pt.arguments[0])} as DOUBLE PRECISION)${colAlias}`) |
|
|
|
|
|
|
|
.wrap('(', ')'), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
ROUND: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
ROUND: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
return knex.raw( |
|
|
|
return { |
|
|
|
`ROUND((${fn(pt.arguments[0])})::numeric, ${ |
|
|
|
builder: knex.raw( |
|
|
|
pt?.arguments[1] ? fn(pt.arguments[1]) : 0 |
|
|
|
`ROUND((${fn(pt.arguments[0])})::numeric, ${ |
|
|
|
}) ${colAlias}` |
|
|
|
pt?.arguments[1] ? fn(pt.arguments[1]) : 0 |
|
|
|
); |
|
|
|
}) ${colAlias}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
return knex.raw( |
|
|
|
return { |
|
|
|
`${fn(pt.arguments[0])} + (${fn(pt.arguments[1])} ||
|
|
|
|
builder: knex.raw( |
|
|
|
|
|
|
|
`${fn(pt.arguments[0])} + (${fn(pt.arguments[1])} ||
|
|
|
|
'${String(fn(pt.arguments[2])).replace( |
|
|
|
'${String(fn(pt.arguments[2])).replace( |
|
|
|
/["']/g, |
|
|
|
/["']/g, |
|
|
|
'' |
|
|
|
'' |
|
|
|
)}')::interval${colAlias}` |
|
|
|
)}')::interval${colAlias}` |
|
|
|
); |
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
const datetime_expr1 = fn(pt.arguments[0]); |
|
|
|
const datetime_expr1 = fn(pt.arguments[0]); |
|
|
@ -99,61 +109,75 @@ const pg = { |
|
|
|
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) => { |
|
|
|
// isodow: the day of the week as Monday (1) to Sunday (7)
|
|
|
|
// isodow: the day of the week as Monday (1) to Sunday (7)
|
|
|
|
// 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 { |
|
|
|
`(EXTRACT(ISODOW FROM ${ |
|
|
|
builder: knex.raw( |
|
|
|
pt.arguments[0].type === 'Literal' |
|
|
|
`(EXTRACT(ISODOW FROM ${ |
|
|
|
? `date '${dayjs((await fn(pt.arguments[0])).builder).format( |
|
|
|
pt.arguments[0].type === 'Literal' |
|
|
|
'YYYY-MM-DD' |
|
|
|
? `date '${dayjs((await fn(pt.arguments[0])).builder).format( |
|
|
|
)}'` |
|
|
|
'YYYY-MM-DD' |
|
|
|
: fn(pt.arguments[0]) |
|
|
|
)}'` |
|
|
|
}) - 1 - ${getWeekdayByText( |
|
|
|
: fn(pt.arguments[0]) |
|
|
|
pt?.arguments[1]?.value |
|
|
|
}) - 1 - ${getWeekdayByText( |
|
|
|
)} % 7 + 7) ::INTEGER % 7 ${colAlias}` |
|
|
|
pt?.arguments[1]?.value |
|
|
|
); |
|
|
|
)} % 7 + 7) ::INTEGER % 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 |
|
|
|
`${args.pt.arguments |
|
|
|
.raw( |
|
|
|
.map(async (ar) => (await args.fn(ar, '', 'AND')).builder.toQuery()) |
|
|
|
`${args.pt.arguments |
|
|
|
.join(' AND ')}` |
|
|
|
.map(async (ar) => |
|
|
|
) |
|
|
|
(await args.fn(ar, '', 'AND')).builder.toQuery() |
|
|
|
.wrap('(', ')') |
|
|
|
) |
|
|
|
.toQuery()} THEN TRUE ELSE FALSE END ${args.colAlias}` |
|
|
|
.join(' AND ')}` |
|
|
|
); |
|
|
|
) |
|
|
|
|
|
|
|
.wrap('(', ')') |
|
|
|
|
|
|
|
.toQuery()} THEN TRUE ELSE FALSE 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 |
|
|
|
`${args.pt.arguments |
|
|
|
.raw( |
|
|
|
.map(async (ar) => (await args.fn(ar, '', 'OR')).builder.toQuery()) |
|
|
|
`${args.pt.arguments |
|
|
|
.join(' OR ')}` |
|
|
|
.map(async (ar) => |
|
|
|
) |
|
|
|
(await args.fn(ar, '', 'OR')).builder.toQuery() |
|
|
|
.wrap('(', ')') |
|
|
|
) |
|
|
|
.toQuery()} THEN TRUE ELSE FALSE END ${args.colAlias}` |
|
|
|
.join(' OR ')}` |
|
|
|
); |
|
|
|
) |
|
|
|
|
|
|
|
.wrap('(', ')') |
|
|
|
|
|
|
|
.toQuery()} THEN TRUE ELSE FALSE END ${args.colAlias}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
SUBSTR: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
SUBSTR: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
const str = fn(pt.arguments[0]); |
|
|
|
const str = fn(pt.arguments[0]); |
|
|
|
const positionFrom = fn(pt.arguments[1] ?? 1); |
|
|
|
const positionFrom = fn(pt.arguments[1] ?? 1); |
|
|
|
const numberOfCharacters = fn(pt.arguments[2] ?? ''); |
|
|
|
const numberOfCharacters = fn(pt.arguments[2] ?? ''); |
|
|
|
return knex.raw( |
|
|
|
return { |
|
|
|
`SUBSTR(${str}::TEXT, ${positionFrom}${ |
|
|
|
builder: knex.raw( |
|
|
|
numberOfCharacters ? ', ' + numberOfCharacters : '' |
|
|
|
`SUBSTR(${str}::TEXT, ${positionFrom}${ |
|
|
|
})${colAlias}` |
|
|
|
numberOfCharacters ? ', ' + numberOfCharacters : '' |
|
|
|
); |
|
|
|
})${colAlias}` |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
MOD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
MOD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
const x = fn(pt.arguments[0]); |
|
|
|
const x = fn(pt.arguments[0]); |
|
|
|
const y = fn(pt.arguments[1]); |
|
|
|
const y = fn(pt.arguments[1]); |
|
|
|
return knex.raw(`MOD((${x})::NUMERIC, (${y})::NUMERIC) ${colAlias}`); |
|
|
|
return { |
|
|
|
|
|
|
|
builder: knex.raw(`MOD((${x})::NUMERIC, (${y})::NUMERIC) ${colAlias}`), |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|