Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ BEGIN
GET DIAGNOSTICS v_rowcount = ;
v_orders_upserted := v_rowcount;
v_sql := format('SELECT count(*)::int FROM %I.%I WHERE org_id = $1 AND created_at >= $2 AND created_at < $3', 'app_public', 'app_order');
EXECUTE v_sql INTO (unnamed row) USING p_org_id, p_from_ts, p_to_ts;
EXECUTE v_sql INTO v_rowcount USING p_org_id, p_from_ts, p_to_ts;
IF p_debug THEN
RAISE NOTICE 'dynamic count(app_order)=%', v_rowcount;
END IF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ begin
'app_public',
'app_order'
);
execute v_sql into (unnamed row) using p_org_id, p_from_ts, p_to_ts;
execute v_sql into v_rowcount using p_org_id, p_from_ts, p_to_ts;
if p_debug then
raise notice 'dynamic count(app_order)=%', v_rowcount;
end if;
Expand Down Expand Up @@ -343,7 +343,7 @@ BEGIN
'app_public',
'app_order'
);
EXECUTE v_sql INTO (unnamed row) USING p_org_id, p_from_ts, p_to_ts;
EXECUTE v_sql INTO v_rowcount USING p_org_id, p_from_ts, p_to_ts;
IF p_debug THEN
RAISE NOTICE 'dynamic count(app_order)=%', v_rowcount;
END IF;
Expand Down
37 changes: 27 additions & 10 deletions packages/plpgsql-deparser/src/plpgsql-deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ export class PLpgSQLDeparser {
parts.push(`<<${fori.label}>>`);
}

const varName = fori.var ? this.deparseDatumName(fori.var) : 'i';
const varName = fori.var ? this.deparseDatumName(fori.var, context) : 'i';
const lower = fori.lower ? this.deparseExpr(fori.lower) : '1';
const upper = fori.upper ? this.deparseExpr(fori.upper) : '10';

Expand Down Expand Up @@ -810,7 +810,7 @@ export class PLpgSQLDeparser {
parts.push(`<<${fors.label}>>`);
}

const varName = fors.var ? this.deparseDatumName(fors.var) : 'rec';
const varName = fors.var ? this.deparseDatumName(fors.var, context) : 'rec';
const query = fors.query ? this.deparseExpr(fors.query) : '';
parts.push(`${kw('FOR')} ${varName} ${kw('IN')} ${query} ${kw('LOOP')}`);

Expand Down Expand Up @@ -841,7 +841,7 @@ export class PLpgSQLDeparser {
parts.push(`<<${forc.label}>>`);
}

const varName = forc.var ? this.deparseDatumName(forc.var) : 'rec';
const varName = forc.var ? this.deparseDatumName(forc.var, context) : 'rec';
const cursorName = this.getVarName(forc.curvar, context);

let forClause = `${kw('FOR')} ${varName} ${kw('IN')} ${cursorName}`;
Expand Down Expand Up @@ -1057,7 +1057,7 @@ export class PLpgSQLDeparser {
let sql = exec.sqlstmt ? this.deparseExpr(exec.sqlstmt) : '';

if (exec.into && exec.target) {
const targetName = this.deparseDatumName(exec.target);
const targetName = this.deparseDatumName(exec.target, context);
// Check if the SQL already contains INTO
if (!sql.toUpperCase().includes(' INTO ')) {
// Insert INTO clause after SELECT
Expand Down Expand Up @@ -1085,7 +1085,7 @@ export class PLpgSQLDeparser {

if (exec.into && exec.target) {
const strict = exec.strict ? kw('STRICT') + ' ' : '';
parts.push(`${kw('INTO')} ${strict}${this.deparseDatumName(exec.target)}`);
parts.push(`${kw('INTO')} ${strict}${this.deparseDatumName(exec.target, context)}`);
}

if (exec.params && exec.params.length > 0) {
Expand All @@ -1107,7 +1107,7 @@ export class PLpgSQLDeparser {
parts.push(`<<${fors.label}>>`);
}

const varName = fors.var ? this.deparseDatumName(fors.var) : 'rec';
const varName = fors.var ? this.deparseDatumName(fors.var, context) : 'rec';
let forClause = `${kw('FOR')} ${varName} ${kw('IN EXECUTE')} ${fors.query ? this.deparseExpr(fors.query) : ''}`;

if (fors.params && fors.params.length > 0) {
Expand Down Expand Up @@ -1224,7 +1224,7 @@ export class PLpgSQLDeparser {

// INTO target
if (!fetch.is_move && fetch.target) {
parts.push(`${kw('INTO')} ${this.deparseDatumName(fetch.target)}`);
parts.push(`${kw('INTO')} ${this.deparseDatumName(fetch.target, context)}`);
}

return parts.join(' ');
Expand Down Expand Up @@ -1304,21 +1304,38 @@ export class PLpgSQLDeparser {
return `$${varno}`;
}

return this.deparseDatumName(datum);
return this.deparseDatumName(datum, context);
}

/**
* Get the name from a datum
* For PLpgSQL_row with refname "(unnamed row)", expand the fields array
* to get the actual variable names
*/
private deparseDatumName(datum: PLpgSQLDatum): string {
private deparseDatumName(datum: PLpgSQLDatum, context?: PLpgSQLDeparserContext): string {
if ('PLpgSQL_var' in datum) {
return datum.PLpgSQL_var.refname;
}
if ('PLpgSQL_rec' in datum) {
return datum.PLpgSQL_rec.refname;
}
if ('PLpgSQL_row' in datum) {
return datum.PLpgSQL_row.refname;
const row = datum.PLpgSQL_row;
// If this is an "(unnamed row)" with fields, expand the fields to get actual variable names
if (row.refname === '(unnamed row)' && row.fields && row.fields.length > 0 && context?.datums) {
const fieldNames = row.fields.map(field => {
// Try to resolve the varno to get the actual variable name
const fieldDatum = context.datums[field.varno];
if (fieldDatum) {
// Recursively get the name, but without context to avoid infinite loops
return this.deparseDatumName(fieldDatum);
}
// Fall back to the field name if we can't resolve the varno
return field.name;
});
return fieldNames.join(', ');
}
return row.refname;
}
if ('PLpgSQL_recfield' in datum) {
return datum.PLpgSQL_recfield.fieldname;
Expand Down