
#if defined(POSTGRES_IN) || defined(MYPG_IN)

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<netinet/in_systm.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<time.h>
#include<errno.h>

#include"zither.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"

#include<libpq-fe.h>
// extern  PGconn * conn;
// extern  PGresult * res;

extern  global_data_t gd;

// V6 対応


int psql_connectset(private_data_t * pd, char * set)
{
int pret = 0;
char * swap = (char *)calloc(2, sizeof(char));

swap = safe_sprintf(swap, BUF_MAX, "select * from connection where cname='%s';",
   pd->rec.user);
safe_pthread_mutex_lock(&pg_mutex);
pret = psqlexec_select(swap);
pthread_mutex_unlock(&pg_mutex);
#ifdef POSTGRES_DEBUG
printf("POSTGRES_DEBUG: psql_connectset(1): SELECT STAT=%d >> %s\n",pret, swap);
#endif


if(pret <= 0){
   char * pgconnection_table = "(uct, ye, mo, da, ha, mi, se, cname, cip, server, mode, stat, ver)";
   swap = safe_sprintf(swap, BUF_MAX, "%s%s%s('%ld','%ld','%ld','%ld','%ld','%ld','%ld','%s','%s','%s','%s','%s','%s');",
     "INSERT INTO connection ",
     pgconnection_table,
     " VALUES ",
     (long int)time(NULL),
     ctime_getr('y'),
     ctime_getr('m'),
     ctime_getr('d'),
     ctime_getr('h'),
     ctime_getr('i'),
     ctime_getr('s'),
     pd->rec.user,
     gd.ms.inip_addr[pd->rec.fdno],
     gd.pg.pg_webserver,
     "START",
     set,
     pd->rec.ver);

   safe_pthread_mutex_lock(&pg_mutex);
   psqlexec_datain(pd, swap);
   pthread_mutex_unlock(&pg_mutex);
   #ifdef POSTGRES_DEBUG 
   printf("POSTGRES_DEBUG: psql_connectset(2): >>%s\n", swap);
   #endif
   }

if(pret > 0){
   swap = safe_sprintf(swap, BUF_MAX, "%s uct='%ld', ye='%ld', mo='%ld', da='%ld', ha='%ld', mi='%ld', se='%ld', cname='%s', cip='%s', server='%s', stat='%s', ver='%s' WHERE cname='%s';",
     "UPDATE connection SET",
     (long int)time(NULL),
     ctime_getr('y'),
     ctime_getr('m'),
     ctime_getr('d'),
     ctime_getr('h'),
     ctime_getr('i'),
     ctime_getr('s'),
     pd->rec.user,
     gd.ms.inip_addr[pd->rec.fdno],
     gd.pg.pg_webserver,
     set,
     pd->rec.ver,
     pd->rec.user);

   safe_pthread_mutex_lock(&pg_mutex);
   psqlexec_datain(pd, swap);
   pthread_mutex_unlock(&pg_mutex);
   #ifdef POSTGRES_DEBUG 
   printf("POSTGRES_DEBUG: psql_connectset(3): >>%s\n", swap);
   #endif
   }

free(swap);
return 0;
}


#endif






