Parameterized/Prepared 语句使用 pg-promise
Parameterized/Prepared Statements usage pg-promise
我正在使用 koa v2
和 pg-promise
。我尝试在 Parameterized/Prepared 语句中做一个简单的 SELECT 2 + 2;
来测试我的设置:
// http://127.0.0.1:3000/sql/2
router.get('/sql/:id', async (ctx) => {
await db.any({
name: 'addition',
text: 'SELECT 2 + 2;',
})
.then((data) => {
console.log('DATA:', data);
ctx.state = { title: data }; // => I want to return num 4 instead of [Object Object]
})
.catch((error) => {
console.log('ERROR:', error);
ctx.body = '::DATABASE CONNECTION ERROR::';
})
.finally(pgp.end);
await ctx.render('index');
});
在模板中呈现 [Object Object]
并将其从 pg-monitor
返回到控制台:
17:30:54 connect(postgres@postgres)
17:30:54 name="addition", text="SELECT 2 + 2;"
17:30:54 disconnect(postgres@postgres)
DATA: [ anonymous { '?column?': 4 } ]
我的问题:
我想将结果 4
存储在 ctx.state
中。我不知道如何在 [ anonymous { '?column?': 4 } ]
内访问它?
感谢您的帮助!
编辑:
我在官方 wiki 中找到了另一个 recommended(1) ways(2) 来处理命名参数。
// http://127.0.0.1:3000/sql/2
router.get('/sql/:id', async (ctx) => {
const obj = {
id: parseInt(ctx.params.id, 10),
};
await db.result('SELECT ${id} + ${id}', obj)
.then((data) => {
console.log('DATA:', data.rows[0]['?column?']);
ctx.state = { title: data.rows[0]['?column?'] }; // => 4
})
.catch((error) => {
console.log('ERROR:', error);
ctx.body = '::DATABASE CONNECTION ERROR::';
})
.finally(pgp.end);
await ctx.render('index');
});
我将 any
对象更改为 result
,它返回原始文本。比起我访问数字 4
就像访问 javascript 对象一样。难道我做错了什么?还有其他方法可以访问此值吗?
推荐的、更快、更安全的使用方式是什么?
由于您只请求一个值,因此您应该使用方法 one:
const {value} = await db.one({
name: 'addition',
text: 'SELECT 2 + 2 as value',
}); // value = 4
对于这样的例子,你不能使用类型 PreparedStatement or ParameterizedQuery,因为它们在服务器端格式化查询,而 PostgreSQL 不支持像 +
.
这样的语法
真正的问题是 - 你真的需要那些类型吗?
我正在使用 koa v2
和 pg-promise
。我尝试在 Parameterized/Prepared 语句中做一个简单的 SELECT 2 + 2;
来测试我的设置:
// http://127.0.0.1:3000/sql/2
router.get('/sql/:id', async (ctx) => {
await db.any({
name: 'addition',
text: 'SELECT 2 + 2;',
})
.then((data) => {
console.log('DATA:', data);
ctx.state = { title: data }; // => I want to return num 4 instead of [Object Object]
})
.catch((error) => {
console.log('ERROR:', error);
ctx.body = '::DATABASE CONNECTION ERROR::';
})
.finally(pgp.end);
await ctx.render('index');
});
在模板中呈现 [Object Object]
并将其从 pg-monitor
返回到控制台:
17:30:54 connect(postgres@postgres)
17:30:54 name="addition", text="SELECT 2 + 2;"
17:30:54 disconnect(postgres@postgres)
DATA: [ anonymous { '?column?': 4 } ]
我的问题:
我想将结果 4
存储在 ctx.state
中。我不知道如何在 [ anonymous { '?column?': 4 } ]
内访问它?
感谢您的帮助!
编辑:
我在官方 wiki 中找到了另一个 recommended(1) ways(2) 来处理命名参数。
// http://127.0.0.1:3000/sql/2
router.get('/sql/:id', async (ctx) => {
const obj = {
id: parseInt(ctx.params.id, 10),
};
await db.result('SELECT ${id} + ${id}', obj)
.then((data) => {
console.log('DATA:', data.rows[0]['?column?']);
ctx.state = { title: data.rows[0]['?column?'] }; // => 4
})
.catch((error) => {
console.log('ERROR:', error);
ctx.body = '::DATABASE CONNECTION ERROR::';
})
.finally(pgp.end);
await ctx.render('index');
});
我将 any
对象更改为 result
,它返回原始文本。比起我访问数字 4
就像访问 javascript 对象一样。难道我做错了什么?还有其他方法可以访问此值吗?
推荐的、更快、更安全的使用方式是什么?
由于您只请求一个值,因此您应该使用方法 one:
const {value} = await db.one({
name: 'addition',
text: 'SELECT 2 + 2 as value',
}); // value = 4
对于这样的例子,你不能使用类型 PreparedStatement or ParameterizedQuery,因为它们在服务器端格式化查询,而 PostgreSQL 不支持像 +
.
真正的问题是 - 你真的需要那些类型吗?