java - 在 PostgreSQL 中保存文件路径,删除反斜杠
问题描述
问题是标题。我们有一个 postgreSQL 数据库,我们想在其中保存一些路径,并且数据库\
从路径中删除反斜杠()。
功能:
CREATE OR REPLACE FUNCTION public.add_filenotification_array(IN fninput public.filenotification_input [])
RETURNS Table(TYPE public."filenotification") AS
$$
DECLARE
fn public.filenotification_input;
filenotification public.filenotification;
filenotificationlist public.filenotification [];
BEGIN
FOREACH fn IN ARRAY fninput LOOP
INSERT INTO public."FileNotification"("FileLocation", "FileTargetLocation", "DocumentLanguage", "LastUpdate", "idStatus", "FileSize")
VALUES (fn.filelocation, fn.filetargetlocation, fn.documentlanguage, CURRENT_TIMESTAMP, 0, 0)
RETURNING "id", "FileLocation", "FileTargetLocation", "DocumentLanguage", "LastUpdate", "idStatus"
INTO filenotification.id, filenotification.filelocation, filenotification.filetargetlocation, filenotification.documentlanguage, filenotification.lastupdate, filenotification.idstatus;
filenotificationlist := array_append(filenotificationlist, filenotification);
END LOOP;
RETURN QUERY
SELECT * FROM unnest(filenotificationlist::public.filenotification []);
END;
$$
LANGUAGE plpgsql;
文件类型:
TYPE filenotification AS (
"id" integer,
"filelocation" character varying,
"filetargetlocation" character varying,
"documentlanguage" character varying,
"lastupdate" timestamp,
"idstatus" integer
);
TYPE filenotification_input AS (
"filelocation" character varying,
"filetargetlocation" character varying,
"documentlanguage" character varying
);
从应用程序中,我们发送 a java.sql.Array
of ,在和参数filenotification
处具有正确的路径,结果完全没有反冲。我们的问题是:发生了什么事?为什么它会删除反斜杠?filelocation
filetargetlocation
编辑:如果我们将 4 个反斜杠放入函数参数中,那么它会输出 1 个反斜杠。如果我们在函数参数中加入 8 个反斜杠,那么它会输出 2 个反斜杠
解决方案
好的,基于 dbfiddle 我可以看到问题所在。(顺便说一句,它不喜欢在那里引用美元,这就是为什么你不能运行它。你只需要替换$$
为'
将它作为字符串引用,它就会运行。)
您的输入是'{"(c:\\\\\\\rs\\me, Path, lang)"}'
. 它是一个类型的数组。
让我们采用一个简单的类型:CREATE TYPE public.t AS (txt TEXT)
. 当您选择一个类型作为行时,而不是扩展字段时,任何“特殊”字符都将被转义。
所以:SELECT ROW('C:\temp')::public.t
返回,并通过返回("C:\\temp")
扩展它。SELECT (ROW('C:\temp')::public.t).*
C:\temp
您的输入是一行(它使用(data1,data2,etc)
符号,它是一个行文字,并且未扩展),因此所有反斜杠都被转义。SELECT ('(c:\\\\\\\rs\\me, Path, lang)'::public.filenotification_input).*
扩展行 ( )的路径部分将是c:\\\rs\me
.
然而,还有一层转义:数据在数组中这一事实。与未扩展的行相同,特殊字符将在数组中转义。运行SELECT ARRAY['C:\temp']
返回["C:\\temp"]
。
把它们放在一起,你的行中有需要转义的反斜杠,然后每个反斜杠都需要在数组中转义。因此,要在“普通”文本中获得一个反斜杠,您必须在行 ( \\
) 中对其进行转义,然后对数组中的每个反斜杠 () 进行转义\\\\
。
因此,考虑到您提供输入的方式,您需要 4 个反斜杠才能将单个反斜杠插入表中。
运行它并查看各种输出:https ://www.db-fiddle.com/f/83wBZsztETriNtZGDVXdcN/0
推荐阅读
- javascript - 在Javascript中单击同一div中的按钮时输出数据
- mysql - 将 Merge 语句从 teradata 迁移到 MYSQL
- java - 没有找到 Spring Boot Thin Launcher exec jar
- mysql - 当需要几种连接类型时,是否可以抑制 UNION?
- sass - 如何将 '%' 添加到值中?
- opencv - 关于在 Debian 10 arm64 上安装 libgtk-3-dev 的问题
- javascript - 包装一个函数来延迟该函数的调用?(在 React 中删除任务)
- sql - 检索最新日期预言机
- android - 如何通过下拉刷新 Expo Webview 中的页面?
- google-cloud-platform - 是否可以在 Cloud Run 服务中公开 5001 (HTTP) 和 5672 (TCP) 端口?