简体   繁体   English

复杂 postgresql sql 查询的语法错误

[英]Syntax error with complicated postgresql sql query

I need to insert multiple related tables simultaneously into postgres.我需要将多个相关表同时插入到 postgres 中。

My query looks like我的查询看起来像

          WITH report AS (
            INSERT INTO reports(
                source_type, 
                target, 
                db_date,
                grype_version, 
                distro_name, 
                distro_version, 
                project_id)
            VALUES(
                 'directory',
                 '.',
                 '2022-01-16T00:15:11.000-08:00',
                 '0.28.0',
                 '',
                 '',
                 'f11d9a9c-0398-40aa-a1fa-1be563e5f7bd'
            ) RETURNING id
    
          ) 
          -- The below part repeats many hundreds of times, but I have limited to just one for now
          WITH vuln AS (
            SELECT id FROM vulnerabilities WHERE slug = 'CVE-2008-0732:nvd' RETURNING id
          ), pkg AS (        
            SELECT id FROM vulnerability_packages WHERE slug = 'CVE-2008-0732:nvd:geronimo-j2ee-management_1.1_spec' RETURNING id
          ), version AS (
            SELECT id FROM package_versions WHERE slug = 'CVE-2008-0732:nvd:geronimo-j2ee-management_1.1_spec:none (unknown)' RETURNING id
          )
          INSERT INTO findings(
            vulnerability_id,
            vulnerability_package_id,
            package_version_id,
            report_id,
            package_name,
            version,
            version_matcher,
            type,
            locations,
            language,
            purl,
            virtual_path,
            matcher
          ) VALUES (
            vuln.id,
            pkg.id,
            version.id,
            report.id,
            'geronimo-j2ee-management_1.1_spec',
            '1.0.1',
            'none (unknown)',
            'java-archive',
            array['tools/log4shell/test/vulnerable-services/zipkin-server-2.23.15-exec.jar'],
            'java',
            'pkg:maven/org.apache.geronimo.specs/geronimo-j2ee-management_1.1_spec@1.0.1',
            'tools/log4shell/test/vulnerable-services/zipkin-server-2.23.15-exec.jar:BOOT-INF/lib/geronimo-j2ee-management_1.1_spec-1.0.1.jar',
            'java-matcher'
          );

As you can see, I want to first insert a 'report', then take it's ID and and use it in the subsequent insertions of 'finding'.如您所见,我想先插入一个“报告”,然后获取它的 ID,并在随后的“查找”插入中使用它。 When I insert a finding, I also need to fetch a few more IDs to create relations from other tables that already exist.当我插入一个结果时,我还需要获取更多的 ID 来从其他已经存在的表中创建关系。 You can see that with the subqueries named vuln , pkg , and version .您可以通过名为vulnpkgversion的子查询看到这一点。

I am getting a syntax error error: syntax error at or near "WITH" and I'm not sure exactly where the issue is.我收到语法错误error: syntax error at or near "WITH"的语法错误,我不确定问题出在哪里。 Does this query look right to you?你觉得这个查询合适吗? Is this the right way to go about writing an efficient query?这是 go 关于编写高效查询的正确方法吗? Note that because there are many findings I need to report, and this action happens often, it is probabably too slow to seperate out each finding insertion into a separate set of queries surrounded by a transaction.请注意,由于我需要报告许多发现,并且此操作经常发生,因此将每个发现插入分离到由事务包围的单独查询集可能太慢了。 Doing it atomically will likely be much faster.以原子方式执行它可能会快得多。

I was misunderstanding how those temporary "tables" work.我误解了那些临时“桌子”是如何工作的。 You still need to select from them as you would a normal table.你仍然需要从他们那里得到 select ,就像普通的桌子一样。 I also rolled the three named subqueries from the 'findings' insert into anonymous subqueries inside the values, much cleaner.我还将“发现”插入中的三个命名子查询滚动到值内的匿名子查询中,更清晰。

          WITH this_report AS (
            INSERT INTO public.reports(
                source_type, 
                target, 
                db_date,
                grype_version, 
                distro_name, 
                distro_version, 
                project_id)
            VALUES(
                 'directory',
                 '.',
                 '2022-01-16T00:15:11.000-08:00',
                 '0.28.0',
                 '',
                 '',
                 '53aca794-0e55-4d99-974c-f522bab414e1'
            ) RETURNING id
          )
          INSERT INTO findings(
            vulnerability_id,
            vulnerability_package_id,
            package_version_id,
            report_id,
            package_name,
            version,
            version_matcher,
            type,
            locations,
            language,
            purl,
            virtual_path,
            matcher
          ) VALUES (
            ( SELECT id FROM public.vulnerabilities WHERE slug = 'CVE-2008-0732:nvd' ),
            ( SELECT id FROM public.vulnerability_packages WHERE slug = 'CVE-2008-0732:nvd:geronimo-j2ee-management_1.1_spec'),
            ( SELECT id FROM public.package_versions WHERE slug = 'CVE-2008-0732:nvd:geronimo-j2ee-management_1.1_spec:none (unknown)'),
            ( SELECT id FROM this_report ),
            'geronimo-j2ee-management_1.1_spec',
            '1.0.1',
            'none (unknown)',
            'java-archive',
            array['tools/log4shell/test/vulnerable-services/zipkin-server-2.23.15-exec.jar'],
            'java',
            'pkg:maven/org.apache.geronimo.specs/geronimo-j2ee-management_1.1_spec@1.0.1',
            'tools/log4shell/test/vulnerable-services/zipkin-server-2.23.15-exec.jar:BOOT-INF/lib/geronimo-j2ee-management_1.1_spec-1.0.1.jar',
            'java-matcher'
          );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM