将自定义types的数组传递给来自node-pg和SQL注入的postgres函数
CREATE TYPE phototable AS ( photoid integer, parentid integer, fileextension character varying(20), description text, tag character varying(100) ); CREATE FUNCTION addphotos( p_placeid integer , p_permissiontypeid integer , p_description text DEFAULT NULL::text , p_photos phototable[] DEFAULT NULL::phototable[]) BEGIN ........ END
我通过生成一个SQL查询从node.js(使用node-postres)调用这个函数。 我想避免它,并希望使用参数化查询,因为我认为这是不安全的SQL注入攻击。 我无法find一种方法将自定义types数组传递给node-postgres的查询方法。 有没有办法将自定义types数组传递给node-postgres的查询function?
查询:
select * from addphotos(p_placeid:=2210, p_permissiontypeid:=2, p_description:='Party', p_photos:=array[row(null, null,'.JPG','smart','6e8f74b2-4c14-4f40-ae19-8abae026a539'), row(null, null,'.JPG',null,'c4e9f75f-25fa-4893-82f1-44c4791d58e5')]::phototable[]);
我不熟悉node.js,但是您可以为列p_photos
提供一个string文字 ,而不是ARRAY
和ROW
构造函数(这些函数需要在Postgres一侧执行!)。 您的查询将如下所示:
SELECT * FROM addphotos( p_placeid := 2210 , p_permissiontypeid := 2 , p_description := 'Party' , p_photos:='{"(,,.JPG,smart,6e8f74b2-4c14-4f40-ae19-8abae026a539)" ,"(,,.JPG,,c4e9f75f-25fa-4893-82f1-44c4791d58e5)"}'::phototable[] );
甚至应该没有明确的演员工作:
SELECT * FROM addphotos( p_placeid := 2210 , p_permissiontypeid := 2 , p_description := 'Party' , p_photos:='{"(,,.JPG,smart,6e8f74b2-4c14-4f40-ae19-8abae026a539)" ,"(,,.JPG,,c4e9f75f-25fa-4893-82f1-44c4791d58e5)"}' );
将语法“重写”为string文字的快速而简单的方法是让Postgres执行此操作:
SELECT array[row(null, null,'.JPG','smart','6e8f74b2-4c14-4f40-ae19-8abae026a539'), row(null, null,'.JPG',null,'c4e9f75f-25fa-4893-82f1-44c4791d58e5')]::phototable[]
返回上面使用的string表示forms:
- 从打字稿模块自动生成index.d.ts,types定义
- 将string转换为数字node.js
- 解决 – 如何使用内容types:应用程序/ JSON,与节点JS上的请求+ restfulapi
- Typescript和Node js得到“不是函数错误”
- 区分节点的本地function和匿名function
- 500错误:对于使用ResourceJS的Mongoose模型,对于path\“_ id \”中的值转换为ObjectId失败
- browser.d.ts依赖Edge Web扩展API browser.runtime。* in typescript
- 如何使用node.js amqp模块将AQMP消息缓冲区转换为JSON对象?
- streamtypes和合并json对象