n_preptx(), n_exectx() - SQL interface

SYNOPSIS

int n_preptx(SERVER *se,TX *tx,char *queryfmt,...);
int n_exectx(SERVER *p_se,TX *tx);


DESCRIPTION
These functions provide an efficient way to perform the same SQL statement multiple times with varying parameter data.

n_preptx() will perform SQL statement setup. It takes a TX pointer from n_opentx(), a printf style format string, and the arguments to fill in that format string with.

The query will be constructed using the format string and arguments, parsed, and prepared for execution. n_preptx() will return non-zero on success. It will return zero on error.

n_exectx() will begin execution of the SQL statement. It will return non-zero on success and zero on error. n_runtx() or n_gettx() or n_flushtx() would then be called to handle the results of the statement as with n_settx().

Once a SQL statement is prepared with n_preptx() it may be executed multiple times with n_exectx(). Typically the parameter data is changed between executions using the n_paramtx() function.


EXAMPLE

SERVER *se;
TX     *tx;
long    date;
char   *title;
char   *article;
int     tlen, alen, dlen;

   ...
   if(!n_preptx(se,tx,"insert into docs values(counter,?,?,?);"))
      { puts("n_preptx Failed"); return(0); }
   for( each record to insert )
   {
      ...
      date=...
      dlen=sizeof(date);
      title=...
      tlen=strlen(title);
      article=...
      alen=strlen(article);
      if(!n_paramtx(se,tx,1,&date  ,&dlen,SQL_C_LONG,SQL_DATE       ) ||
         !n_paramtx(se,tx,2,title  ,&tlen,SQL_C_CHAR,SQL_LONGVARCHAR) ||
         !n_paramtx(se,tx,3,article,&alen,SQL_C_CHAR,SQL_LONGVARCHAR));
         { puts("n_paramtx Failed"); return(0); }
      if(!n_exectx(se,tx))
         { puts("n_exectx Failed"); return(0); }
      n_flushtx(se,tx);
   }
   ...


EXAMPLE

SERVER *se;
TX     *tx;
char   *query;
int     qlen;
FLDLST *fl;

   ...
   if(!n_preptx(se,tx,
         "select id,Title from docs where Article like ?;"))
      { puts("n_preptx Failed"); return(0); }
   for( each Article query to execute )
   {
      query=...
      qlen=strlen(query);
      if(!n_paramtx(se,tx,1,query,&qlen,SQL_C_CHAR,SQL_LONGVARCHAR))
         { puts("n_paramtx Failed"); return(0); }
      if(!n_exectx(se,tx))
         { puts("n_exectx Failed"); return(0); }
      while((fl=n_gettx(se,tx))!=FLDLSTPN)
      {
         ...
         freefldlst(fl);
      }
   }
   ...


SEE ALSO

n_paramtx(), n_opentx(), n_gettx()


Copyright © Thunderstone Software     Last updated: Apr 15 2024
Copyright © 2024 Thunderstone Software LLC. All rights reserved.