コード例 #1
0
ファイル: reorg.c プロジェクト: dvarrazzo/pg_reorg
/**
 * @fn      Datum reorg_trigger(PG_FUNCTION_ARGS)
 * @brief   Insert a operation log into log-table.
 *
 * reorg_trigger(sql)
 *
 * @param	sql	SQL to insert a operation log into log-table.
 */
Datum
reorg_trigger(PG_FUNCTION_ARGS)
{
    TriggerData	   *trigdata = (TriggerData *) fcinfo->context;
    TupleDesc		desc;
    HeapTuple		tuple;
    Datum			values[2];
    bool			nulls[2] = { 0, 0 };
    Oid				argtypes[2];
    const char	   *sql;

    /* authority check */
    must_be_superuser("reorg_trigger");

    /* make sure it's called as a trigger at all */
    if (!CALLED_AS_TRIGGER(fcinfo) ||
            !TRIGGER_FIRED_BEFORE(trigdata->tg_event) ||
            !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event) ||
            trigdata->tg_trigger->tgnargs != 1)
        elog(ERROR, "reorg_trigger: invalid trigger call");

    /* retrieve parameters */
    sql = trigdata->tg_trigger->tgargs[0];
    desc = RelationGetDescr(trigdata->tg_relation);
    argtypes[0] = argtypes[1] = trigdata->tg_relation->rd_rel->reltype;

    /* connect to SPI manager */
    reorg_init();

    if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
    {
        /* INSERT: (NULL, newtup) */
        tuple = trigdata->tg_trigtuple;
        nulls[0] = true;
        values[1] = copy_tuple(tuple, desc);
    }
    else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
    {
        /* DELETE: (oldtup, NULL) */
        tuple = trigdata->tg_trigtuple;
        values[0] = copy_tuple(tuple, desc);
        nulls[1] = true;
    }
    else
    {
        /* UPDATE: (oldtup, newtup) */
        tuple = trigdata->tg_newtuple;
        values[0] = copy_tuple(trigdata->tg_trigtuple, desc);
        values[1] = copy_tuple(tuple, desc);
    }

    /* INSERT INTO reorg.log VALUES ($1, $2) */
    execute_with_args(SPI_OK_INSERT, sql, 2, argtypes, values, nulls);

    SPI_finish();

    PG_RETURN_POINTER(tuple);
}
コード例 #2
0
ファイル: exprtree.c プロジェクト: WoodMath/mathmap
exprtree*
make_cast (scanner_ident_t *tagname, exprtree *tuple)
{
    exprtree *tree = alloc_exprtree();
    int tagnum = tag_number_for_name(tagname->str);

    if (tuple->type == EXPR_TUPLE_CONST)
    {
	tree->type = EXPR_TUPLE_CONST;
	tree->val.tuple_const = copy_tuple(tuple->val.tuple_const);
	tree->val.tuple_const->number = tagnum;
    }
    else
    {
	tree->type = EXPR_CAST;
	tree->val.cast.tagnum = tagnum;
	tree->val.cast.tuple = tuple;
    }
    tree->region = scanner_region_merge(tagname->region, tuple->region);
    tree->result = make_tuple_info(tagnum, tuple->result.length);

    return tree;
}
コード例 #3
0
ファイル: glpmpl2.c プロジェクト: ChrisMoreton/Test3
void tabbing_format
(     MPL *mpl,
      SYMBOL *altval          /* not changed */
)
{     SET *set = NULL;
      PARAMETER *par;
      SLICE *list, *col;
      TUPLE *tuple;
      int next_token, j, dim = 0;
      char *last_name = NULL;
      /* read the optional <prefix> */
      if (is_symbol(mpl))
      {  get_token(mpl /* <symbol> */);
         next_token = mpl->token;
         unget_token(mpl /* <symbol> */);
         if (next_token == T_COLON)
         {  /* select the set to saturate it with data */
            set = select_set(mpl, mpl->image);
            /* the set must be simple (i.e. not set of sets) */
            if (set->dim != 0)
               error(mpl, "%s must be a simple set", set->name);
            /* and must not be defined yet */
            if (set->array->head != NULL)
               error(mpl, "%s already defined", set->name);
            /* add new (the only) member to the set and assign it empty
               elemental set */
            add_member(mpl, set->array, NULL)->value.set =
               create_elemset(mpl, set->dimen);
            last_name = set->name, dim = set->dimen;
            get_token(mpl /* <symbol> */);
            insist(mpl->token == T_COLON);
            get_token(mpl /* : */);
         }
      }
      /* read the table heading that contains parameter names */
      list = create_slice(mpl);
      while (mpl->token != T_ASSIGN)
      {  /* there must be symbolic name of parameter */
         if (!is_symbol(mpl))
            error(mpl, "parameter name or := missing where expected");
         /* select the parameter to saturate it with data */
         par = select_parameter(mpl, mpl->image);
         /* the parameter must be subscripted */
         if (par->dim == 0)
            error(mpl, "%s not a subscripted parameter", mpl->image);
         /* the set (if specified) and all the parameters in the data
            block must have identical dimension */
         if (dim != 0 && par->dim != dim)
         {  insist(last_name != NULL);
            error(mpl, "%s has dimension %d while %s has dimension %d",
               last_name, dim, par->name, par->dim);
         }
         /* set default value for the parameter (if specified) */
         if (altval != NULL)
            set_default(mpl, par, copy_symbol(mpl, altval));
         /* append the parameter to the column list */
         list = expand_slice(mpl, list, (SYMBOL *)par);
         last_name = par->name, dim = par->dim;
         get_token(mpl /* <symbol> */);
         /* skip optional comma */
         if (mpl->token == T_COMMA) get_token(mpl /* , */);
      }
      if (slice_dimen(mpl, list) == 0)
         error(mpl, "at least one parameter name required");
      get_token(mpl /* := */);
      /* skip optional comma */
      if (mpl->token == T_COMMA) get_token(mpl /* , */);
      /* read rows that contain tabbing data */
      while (is_symbol(mpl))
      {  /* read subscript list */
         tuple = create_tuple(mpl);
         for (j = 1; j <= dim; j++)
         {  /* read j-th subscript */
            if (!is_symbol(mpl))
            {  int lack = slice_dimen(mpl, list) + dim - j + 1;
               insist(tuple != NULL);
               insist(lack > 1);
               error(mpl, "%d items missing in data group beginning wit"
                  "h %s", lack, format_symbol(mpl, tuple->sym));
            }
            /* read and append j-th subscript to the n-tuple */
            tuple = expand_tuple(mpl, tuple, read_symbol(mpl));
            /* skip optional comma *between* <symbols> */
            if (j < dim && mpl->token == T_COMMA)
               get_token(mpl /* , */);
         }
         /* if the set is specified, add to it new n-tuple, which is a
            copy of the subscript list just read */
         if (set != NULL)
            check_then_add(mpl, set->array->head->value.set,
               copy_tuple(mpl, tuple));
         /* skip optional comma between <symbol> and <value> */
         if (mpl->token == T_COMMA) get_token(mpl /* , */);
         /* read values accordingly to the column list */
         for (col = list; col != NULL; col = col->next)
         {  /* if the token is single point, no value is provided */
            if (is_literal(mpl, "."))
            {  get_token(mpl /* . */);
               continue;
            }
            /* read value and assign it to new parameter member */
            if (!is_symbol(mpl))
            {  int lack = slice_dimen(mpl, col);
               insist(tuple != NULL);
               if (lack == 1)
                  error(mpl, "one item missing in data group beginning "
                     "with %s", format_symbol(mpl, tuple->sym));
               else
                  error(mpl, "%d items missing in data group beginning "
                     "with %s", lack, format_symbol(mpl, tuple->sym));
            }
            read_value(mpl, (PARAMETER *)col->sym, copy_tuple(mpl,
               tuple));
            /* skip optional comma preceding the next value */
            if (col->next != NULL && mpl->token == T_COMMA)
               get_token(mpl /* , */);
         }
         /* delete the original subscript list */
         delete_tuple(mpl, tuple);
         /* skip optional comma (only if there is next data group) */
         if (mpl->token == T_COMMA)
         {  get_token(mpl /* , */);
            if (!is_symbol(mpl)) unget_token(mpl /* , */);
         }
      }
      /* delete the column list (it contains parameters, not symbols,
         so nullify it before) */
      for (col = list; col != NULL; col = col->next) col->sym = NULL;
      delete_slice(mpl, list);
      return;
}