上一篇
参数化原始SQL查询
使用 DataSource,注意,如果是 PostgreSQL,则参数占位符不能使用问号 ?
,只能使用 $n
,并且在没有表名的情况下需要指定类型:
否则,会出现错误:PostgreSQL - ERROR: could not determine data type of parameter $1
,参见:https://blog.51cto.com/u_10980859/5303844
@Injectable()
export class PermissionsService {
constructor(
private readonly dataSource: DataSource,
) { }
async check(id: number, body: CheckDTO) {
let sql = `SELECT $1::int4`;
let parameters = [id];
try {
let [row1] = await this.dataSource.query(sql, parameters);
console.log(row1);
} catch(e) {
// console.log(e);
}
return false;
}
}
关于绑定占位符的讨论:https://github.com/typeorm/typeorm/issues/881
取决于底层数据库驱动程序:
Oracle: query('SELECT * FROM table WHERE name = :name', [ { val: 'something' } ])
MySQL: query('SELECT * FROM table WHERE name = ?', [ 'something' ])
MSSQL: query('SELECT * FROM table WHERE name = @0', [ 'something' ])
Postgres: query('SELECT * FROM table WHERE name = $1', [ 'something' ])
标签:name,Nest,table,js,something,SQL,query,WHERE,SELECT
From: https://www.cnblogs.com/develon/p/17469237.html