将自定义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文字 ,而不是ARRAYROW构造函数(这些函数需要在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: