Thread: Trigger (C) question

Trigger (C) question

From
"Mitch Vincent"
Date:
This is a bit off topic but email to the author of the trigger bounced..

In the fti.c (full text index) trigger I see..

sprintf(query, "INSERT INTO %s (string, id) VALUES ($1, $2)", indexname);

below.. I need to add something else here.. Basically I'm eliminating the
need for the text table, I'm going to integrate the fti indexed table into
an existing one I have but I need to insert another ID into the resumes_fti
(my indexed fti table)..

The above makes the query string, as I can see but what in the world is the
$1 $2 and where did they come from? Since it looks like that's how the
values of string and id are put into the query, I need to know so I can add
one more in there..

Below is the whole function I'm referring to in fti.c

Thanks!!!


if (isinsert)
 {
  char    *substring,
       *column;
  void    *pplan;
  Oid     *argtypes;
  Datum  values[2];
  int   colnum;
  struct varlena *data;
  EPlan    *plan;

  sprintf(query, "I%s$%s", args[0], args[1]);
  plan = find_plan(query, &InsertPlans, &nInsertPlans);

  /* no plan yet, so allocate mem for argtypes */
  if (plan->nplans <= 0)
  {
   argtypes = (Oid *) palloc(2 * sizeof(Oid));

   argtypes[0] = VARCHAROID; /* create table t_name (string
           * varchar, */
   argtypes[1] = OIDOID;  /* id   oid);    */

   /* prepare plan to gain speed */
   sprintf(query, "INSERT INTO %s (string, id) VALUES ($1, $2)",
     indexname);
   pplan = SPI_prepare(query, 2, argtypes);
   if (!pplan)
    elog(ERROR, "Full Text Indexing: SPI_prepare returned NULL "
      "in insert");

   pplan = SPI_saveplan(pplan);
   if (pplan == NULL)
    elog(ERROR, "Full Text Indexing: SPI_saveplan returned NULL"
      " in insert");

   plan->splan = (void **) malloc(sizeof(void *));
   *(plan->splan) = pplan;
   plan->nplans = 1;
  }





Re: Trigger (C) question

From
Tom Lane
Date:
"Mitch Vincent" <mitch@venux.net> writes:
> sprintf(query, "INSERT INTO %s (string, id) VALUES ($1, $2)", indexname);

> The above makes the query string, as I can see but what in the world is the
> $1 $2 and where did they come from?

Parameters.  The chunk of code you quote shows the plan being set up
with two parameters, the first of type VARCHAR and the second of type
OID (this allows the parser to know what datatype conversions it might
need to apply).  The actual values of the parameters will get passed to
the executor when the plan is executed, later on.  Too lazy to go
looking for the details right now...

            regards, tom lane