簡體   English   中英

biqquery 中結構數組的命名參數類型

[英]Named parameter type for Array of Struct in biqquery

我有一個如下代碼

        const changes = dto.data.map(d => `('${d.findingId}', '${d.status}', ${d.comment ? `'${d.comment}'` : null})`).join(',')

        const query = `
            MERGE \`${projectId}.dlp.result-status\`
            USING UNNEST([struct<findingId STRING, status STRING, comment STRING> ${changes} ]) changes
            ON \`${projectId}.dlp.result-status\`.findingId = changes.findingId
            WHEN NOT MATCHED THEN
                INSERT (findingId, status, modifiedAt, comment ) VALUES (changes.findingId, changes.status, '${now}', changes.comment)
            WHEN MATCHED THEN
                UPDATE SET 
                    \`${projectId}.dlp.result-status\`.status = changes.status, 
                    \`${projectId}.dlp.result-status\`.comment = changes.comment
            ;
        `;

        const options = {
            query: query,
            // Location must match that of the dataset(s) referenced in the query.
            location: 'US'
        };

但它容易受到 SQL 注入,所以我將代碼更改為使用參數

        const changes = dto.data.map(d => ({ findingId: d.findingId, status: d.status, comment: d.comment }))

        const query = `
            MERGE \`${projectId}.dlp.result-status\`
            USING UNNEST(@changes) changes
            ON \`${projectId}.dlp.result-status\`.findingId = changes.findingId
            WHEN NOT MATCHED THEN
                INSERT (findingId, status, modifiedAt, comment ) VALUES (changes.findingId, changes.status, @now, changes.comment)
            WHEN MATCHED THEN
                UPDATE SET 
                    \`${projectId}.dlp.result-status\`.status = changes.status, 
                    \`${projectId}.dlp.result-status\`.comment = changes.comment
            ;
        `;

        const options: Query = {
            query: query,
            // Location must match that of the dataset(s) referenced in the query.
            location: 'US',
            params: { now: now, changes: changes },
            types: {
                now: 'STRING',
                // changes: [{ 'findingId': 'STRING', 'status': 'STRING', 'comment': 'STRING' }]
                // changes: ['STRUCT']
            }
        };

如此重要的變化是將changes變量從連接字符串更改為對象數組並將USING UNNEST([struct<findingId STRING, status STRING, comment STRING> ${changes} ]) changesUSING UNNEST(@changes) changes

當我運行這個時,我得到了這個錯誤

Parameter types must be provided for null values via the 'types' field in query options.

所以我嘗試添加types ,現在我被困在如何為結構數組添加類型,我嘗試了這些但沒有鎖定

changes: [{ 'findingId': 'STRING', 'status': 'STRING', 'comment': 'STRING' }]
changes: ['STRUCT']

附言。 這是 Query 上的類型定義在此處輸入圖像描述

似乎這只是打字(ts)問題,我這樣做並工作(添加as any

        const options: Query = {
            query: query,query.
            location: 'US',
            params: { now: now, changes: changes },
            types: {
                now: 'STRING',
                changes: [{ findingId: 'STRING', status: 'STRING', comment: 'STRING' }] as any
            }
        };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM