سؤال

لنفترض أنني إنشاء جدول في كيو مع تعليق على عمود:

create table t1 (
   c1 varchar(10)
);
comment on column t1.c1 is 'foo';

وبعض الوقت في وقت لاحق، وأنا قررت إضافة عمود آخر:

alter table t1 add column c2 varchar(20);

وأريد للبحث عن محتويات تعليق العمود الأول، وأشرك العمود الجديد:

select comment_text from (what?) where table_name = 't1' and column_name = 'c1'

وو(ماذا؟) سيكون جدول نظام، ولكن بعد أن نظرت حولي في pgAdmin والبحث على شبكة الإنترنت أنا لم يتعلموا اسمها.

ومن الناحية المثالية أود أن يكون قادرا على:

comment on column t1.c1 is (select ...);

ولكن لدي شعور بأن انها تمتد أشياء بعيدة بعض الشيء. شكرا لأية أفكار.

تحديث: استنادا إلى اقتراحات وصلتني هنا، أنا أنهى كتابة برنامج لأتمتة مهمة تعليقات نقل، وذلك كجزء من عملية أوسع لتغيير نوع البيانات من عمود كيو. يمكنك أن تقرأ عن ذلك على بلدي بلوق .

هل كانت مفيدة؟

المحلول

ووالشيء التالي هو لمعرفة كيفية الحصول على إمكانية احتواء الجدول. أعتقد أن استخدام هذا كجزء من التعليق على لن تنجح، كما كنت أظن.

    postgres=# create table comtest1 (id int, val varchar);
    CREATE TABLE
    postgres=# insert into comtest1 values (1,'a');
    INSERT 0 1
    postgres=# select distinct tableoid from comtest1;
     tableoid
    ----------
        32792
    (1 row)

    postgres=# comment on column comtest1.id is 'Identifier Number One';
    COMMENT
    postgres=# select col_description(32792,1);
        col_description
    -----------------------
     Identifier Number One
    (1 row)

وعلى أية حال، أنا جلد حتى وظيفة plpgsql سريعة لنسخ تعليقات من جدول واحد الزوج / عمود إلى آخر. لديك لcreatelang plpgsql على قاعدة البيانات واستخدامها مثل هذا:

    Copy the comment on the first column of table comtest1 to the id 
    column of the table comtest2. Yes, it should be improved but 
    that's left as work for the reader.

    postgres=# select copy_comment('comtest1',1,'comtest2','id');
     copy_comment
    --------------
                1
    (1 row)
CREATE OR REPLACE FUNCTION copy_comment(varchar,int,varchar,varchar) RETURNS int AS $PROC$
DECLARE
        src_tbl ALIAS FOR $1;
        src_col ALIAS FOR $2;
        dst_tbl ALIAS FOR $3;
        dst_col ALIAS FOR $4;
        row RECORD;
        oid INT;
        comment VARCHAR;
BEGIN
        FOR row IN EXECUTE 'SELECT DISTINCT tableoid FROM ' || quote_ident(src_tbl) LOOP
                oid := row.tableoid;
        END LOOP;

        FOR row IN EXECUTE 'SELECT col_description(' || quote_literal(oid) || ',' || quote_literal(src_col) || ')' LOOP
                comment := row.col_description;
        END LOOP;

        EXECUTE 'COMMENT ON COLUMN ' || quote_ident(dst_tbl) || '.' || quote_ident(dst_col) || ' IS ' || quote_literal(comment);

        RETURN 1;
END;
$PROC$ LANGUAGE plpgsql;

نصائح أخرى

ويمكنك استرداد التعليقات على الأعمدة باستخدام col_description وظيفة النظام (table_oid، العمود). انظر هذه الصفحة ل مزيد من التفاصيل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top