Typeorm getManager().query() 仅在传递硬编码 SQL 查询字符串而非变量时有效
Typeorm getManager().query() works only if pass hardcoded SQL query string not variables
我尝试使用 Typeform 和 getManager().query() 从服务器发出 SQL 请求,并使用 SQL.
传递给查询字符串
- 来自 MySqlWorkbench 屏幕截图的工作示例 https://prnt.sc/uad3tu 和代码片段。如果我将它复制并粘贴到代码中,它就可以正常工作
SELECT `Event`.*, `Repeats`.`start_event_at`, `Repeats`.`end_event_at`
FROM `events` `Event`
LEFT JOIN `event_repeats` `Repeats` on `Repeats`.`eventId` = `Event`.`id`
WHERE `Event`.`category` = 'fitness'
AND `Repeats`.`end_event_at` >= '2020-09-02T14:59:00.000Z'
- 但是如果我在内插字符串中使用变量,它会抛出错误,屏幕截图 https://prnt.sc/uad5zy 和
SELECT `Event`.*, `Repeats`.`start_event_at`, `Repeats`.`end_event_at`
FROM `events` `Event`
LEFT JOIN `event_repeats` `Repeats` on `Repeats`.`eventId` = `Event`.`id`
WHERE `Event`.`category` = ${category}
AND `Repeats`.`end_event_at` >= '2020-09-02T14:59:00.000Z'
我做错了什么以及如何处理?
使用查询方法时使用基础driver escaping mechanism。对于 mysql:
await getEntityManager().query('SELECT * FROM tbl_1 WHERE category = ?', [ 'fitness' ])
最终代码如下所示:
async getAllEvents(search_options: any, end_at, offFilters): Promise<Event[]> {
const { isOnline, isMobility, impairment, category } = search_options;
let events = null;
if (offFilters) {
events = await getManager().query(
' SELECT Event.*, Repeats.start_event_at, Repeats.end_event_at ' +
' FROM events Event ' +
' LEFT JOIN event_repeats Repeats ON Repeats.eventId = Event.id ' +
' WHERE Event.category = ? ' +
' AND Repeats.end_event_at >= ?', [category, end_at])
} else {
const online = isOnline ? 1 : 0
const mobility = isMobility ? 1 : 0
events = await getManager().query(
' SELECT Event.*, Repeats.start_event_at, Repeats.end_event_at ' +
' FROM events Event ' +
' LEFT JOIN event_repeats Repeats ON Repeats.eventId = Event.id ' +
' WHERE Event.category = ? ' +
' AND Event.isOnline = ? ' +
' AND Event.isMobility = ? ' +
' AND Event.impairment = ? ' +
' AND Repeats.end_event_at >= ?', [category, online, mobility, impairment, end_at])
}
return events
}
我尝试使用 Typeform 和 getManager().query() 从服务器发出 SQL 请求,并使用 SQL.
传递给查询字符串- 来自 MySqlWorkbench 屏幕截图的工作示例 https://prnt.sc/uad3tu 和代码片段。如果我将它复制并粘贴到代码中,它就可以正常工作
SELECT `Event`.*, `Repeats`.`start_event_at`, `Repeats`.`end_event_at`
FROM `events` `Event`
LEFT JOIN `event_repeats` `Repeats` on `Repeats`.`eventId` = `Event`.`id`
WHERE `Event`.`category` = 'fitness'
AND `Repeats`.`end_event_at` >= '2020-09-02T14:59:00.000Z'
- 但是如果我在内插字符串中使用变量,它会抛出错误,屏幕截图 https://prnt.sc/uad5zy 和
SELECT `Event`.*, `Repeats`.`start_event_at`, `Repeats`.`end_event_at`
FROM `events` `Event`
LEFT JOIN `event_repeats` `Repeats` on `Repeats`.`eventId` = `Event`.`id`
WHERE `Event`.`category` = ${category}
AND `Repeats`.`end_event_at` >= '2020-09-02T14:59:00.000Z'
我做错了什么以及如何处理?
使用查询方法时使用基础driver escaping mechanism。对于 mysql:
await getEntityManager().query('SELECT * FROM tbl_1 WHERE category = ?', [ 'fitness' ])
最终代码如下所示:
async getAllEvents(search_options: any, end_at, offFilters): Promise<Event[]> {
const { isOnline, isMobility, impairment, category } = search_options;
let events = null;
if (offFilters) {
events = await getManager().query(
' SELECT Event.*, Repeats.start_event_at, Repeats.end_event_at ' +
' FROM events Event ' +
' LEFT JOIN event_repeats Repeats ON Repeats.eventId = Event.id ' +
' WHERE Event.category = ? ' +
' AND Repeats.end_event_at >= ?', [category, end_at])
} else {
const online = isOnline ? 1 : 0
const mobility = isMobility ? 1 : 0
events = await getManager().query(
' SELECT Event.*, Repeats.start_event_at, Repeats.end_event_at ' +
' FROM events Event ' +
' LEFT JOIN event_repeats Repeats ON Repeats.eventId = Event.id ' +
' WHERE Event.category = ? ' +
' AND Event.isOnline = ? ' +
' AND Event.isMobility = ? ' +
' AND Event.impairment = ? ' +
' AND Repeats.end_event_at >= ?', [category, online, mobility, impairment, end_at])
}
return events
}