From 0382f4b331a9dffff0a2b8479045805b84012027 Mon Sep 17 00:00:00 2001 From: Brent Moran Date: Thu, 12 Dec 2024 23:25:26 +0800 Subject: [PATCH] extract default describing function, add execution logic --- db/sql/00_msar.sql | 55 ++++++++++++++++++++++++++++++----------- db/sql/test_00_msar.sql | 2 +- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/db/sql/00_msar.sql b/db/sql/00_msar.sql index 64563da1ba..b217227b6a 100644 --- a/db/sql/00_msar.sql +++ b/db/sql/00_msar.sql @@ -934,6 +934,46 @@ WHERE has_privilege; $$ LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT; +CREATE OR REPLACE FUNCTION +msar.describe_column_default(tab_id regclass, col_id smallint) RETURNS jsonb AS $$/* +Return a JSONB object describing the default (if any) of the given column in the given table. + +The returned JSON will have the form: + { + "value": , + "is_dynamic": , + } + +If the default is possibly dynamic, i.e., if "is_dynamic" is true, then "value" will be a text SQL +expression that generates the default value if evaluated. If it is not dynamic, then "value" is the +actual default value. +*/ +DECLARE + def_expr text; + def_json jsonb; +BEGIN +def_expr = CASE + WHEN attidentity='' THEN pg_catalog.pg_get_expr(adbin, tab_id) + ELSE 'identity' +END +FROM pg_catalog.pg_attribute LEFT JOIN pg_catalog.pg_attrdef ON attrelid=adrelid AND attnum=adnum +WHERE attrelid=tab_id AND attnum=col_id; +IF def_expr IS NULL THEN + RETURN NULL; +ELSIF msar.is_default_possibly_dynamic(tab_id, col_id) THEN + EXECUTE format( + 'SELECT jsonb_build_object(''value'', %L, ''is_dynamic'', true)', def_expr + ) INTO def_json; +ELSE + EXECUTE format( + 'SELECT jsonb_build_object(''value'', msar.format_data(%s), ''is_dynamic'', false)', def_expr + ) INTO def_json; +END IF; +RETURN def_json; +END; +$$ LANGUAGE plpgsql RETURNS NULL ON NULL INPUT; + + CREATE OR REPLACE FUNCTION msar.get_column_info(tab_id regclass) RETURNS jsonb AS $$/* Given a table identifier, return an array of objects describing the columns of the table. @@ -965,20 +1005,7 @@ SELECT jsonb_agg( 'type_options', msar.get_type_options(atttypid, atttypmod, attndims), 'nullable', NOT attnotnull, 'primary_key', COALESCE(pgi.indisprimary, false), - 'default', - nullif( - jsonb_strip_nulls( - jsonb_build_object( - 'value', - CASE - WHEN attidentity='' THEN pg_get_expr(adbin, tab_id) - ELSE 'identity' - END, - 'is_dynamic', msar.is_default_possibly_dynamic(tab_id, attnum) - ) - ), - jsonb_build_object() - ), + 'default', msar.describe_column_default(tab_id, attnum), 'has_dependents', msar.has_dependents(tab_id, attnum), 'description', msar.col_description(tab_id, attnum), 'current_role_priv', msar.list_column_privileges_for_current_role(tab_id, attnum), diff --git a/db/sql/test_00_msar.sql b/db/sql/test_00_msar.sql index 820e4a4545..924eb9c30c 100644 --- a/db/sql/test_00_msar.sql +++ b/db/sql/test_00_msar.sql @@ -2662,7 +2662,7 @@ BEGIN "name": "txt", "type": "text", "default": { - "value": "'abc'::text", + "value": "abc", "is_dynamic": false }, "nullable": true,