获取存储在postgres数组中的QUERY行的值

时间:2016-04-29 09:42:03

标签: arrays postgresql function postgresql-9.4

DECLARE 
  resources varchar[];
  default_active development.switch_state;
  defaultsp varchar[];
  x varchar[];
  columnname varchar;
  looper integer;

BEGIN
  -- setting resources
  resources := array(SELECT column_name::text AS resource FROM information_schema.columns WHERE table_name='usr_defprivilegies');
  resources := array_remove(resources, 'id_defprvg');
  resources := array_remove(resources, 'id_category');
  resources := array_remove(resources, 'id_level');
  -- setting privilegies switch
  SELECT ap.default_active AS switcher INTO default_active FROM development.usr_activeprivilegies AS ap 
INNER JOIN development.usr_managelayer AS um ON ap.id_activeprvg = um.id_activeprvg WHERE um.id_user = $1;

  IF default_active = '1' THEN
    looper := 1;
    FOREACH x SLICE 1 IN ARRAY resources
    LOOP
      columnname := resources[looper];
      defaultsp := array(SELECT columnname FROM development.usr_defprivilegies AS dp WHERE dp.id_category = $4 AND dp.id_level = $3);
      EXECUTE 'UPDATE development.usr_activeprivilegies SET default_active = ''0'', '
    || resources[looper]
    || ' = '
    || quote_literal(defaultsp[looper])
    || ' WHERE id_activeprvg = '
    || $2;
      looper := looper + 1;
    END LOOP;
  END IF;

RETURN 1;
END;

我需要访问存储在数组中的列的值,但我找不到办法。这段代码工作正常,但这一行:

quote_literal(defaultsp[looper])

返回类似这样的内容:'column_name'但我需要'column_value';

感谢您的回答。

1 个答案:

答案 0 :(得分:0)

登录后更改权限的解决方案,两个表默认为X活动,默认是一组基于用户类别和级别的规则,权限是可适应的,我们从postgres中的information_schema获取资源来源,这是参数循环。

DECLARE 
  resources varchar[];
  resource varchar;
  res_count integer;
  default_active development.switch_state;
  defaultsp development.switch_state;
  looper integer;

BEGIN
  -- setting resources
  resources := array(SELECT column_name::text AS resource FROM information_schema.columns WHERE table_name='usr_defprivilegies');
  resources := array_remove(resources, 'id_defprvg');
  resources := array_remove(resources, 'id_category');
  resources := array_remove(resources, 'id_level');
  -- setting privilegies switch
  SELECT ap.default_active AS switcher INTO default_active FROM development.usr_activeprivilegies AS ap 
    INNER JOIN development.usr_managelayer AS um ON ap.id_activeprvg = um.id_activeprvg WHERE um.id_user = $1;
  -- setting privilegies
  IF default_active = '1' THEN
    looper := 1;
    res_count := array_length(resources, 1);

    LOOP
      resource := 'dp.' || resources[looper];
      EXECUTE 'SELECT ' 
        || resource 
        || ' FROM development.usr_defprivilegies AS dp WHERE dp.id_category = ' 
        || quote_literal($4) 
        || ' AND dp.id_level = ' 
        || quote_literal($3) INTO defaultsp;

      EXECUTE 'UPDATE development.usr_activeprivilegies SET '
        || resources[looper]
        || ' = '
        || quote_literal(defaultsp)
        || ' WHERE id_activeprvg = '
        || $2;

      looper := looper + 1;
      EXIT WHEN looper > res_count;
    END LOOP;

    EXECUTE 'UPDATE development.usr_activeprivilegies SET updateby = ' 
      || quote_literal($1) 
      || ', updatedate = CURRENT_TIMESTAMP, default_active = ' 
      || quote_literal(0);
  END IF;

RETURN 1;
END;

这很好用;