
#if defined(MYSQL_IN) || defined(MYPG_IN)

#include <stdlib.h>
#include <stdio.h>
#include<arpa/inet.h>
#include<pthread.h>


#include<mysql.h>

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

// extern  MYSQL mysql_buf;
// extern  MYSQL * my_conn;
// extern  MYSQL_RES * my_result;


// V6 対応

extern  global_data_t gd;


int mysql_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(&mysql_mutex);
pret = mysqlexec_select(swap);
pthread_mutex_unlock(&mysql_mutex);

#ifdef MYSQL_DEBUG
printf("MYSQL_DEBUG: mysql_connectset(1): mysqlexec_select STAT=%d pg.stat=%d >> %s\n",pret, gd.mysql.stat, swap);
#endif


if(pret <= 0){
   char * connection_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 ",
     connection_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(&mysql_mutex);
   mysqlexec_datain(swap);
   pthread_mutex_unlock(&mysql_mutex);
   #ifdef MYSQL_DEBUG 
   printf("MYSQL_DEBUG: mysql_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(&mysql_mutex);
   mysqlexec_datain(swap);
   pthread_mutex_unlock(&mysql_mutex);
   #ifdef MYSQL_DEBUG 
   printf("MYSQL_DEBUG: mysql_connectset(3): >>%s\n", swap);
   #endif
   }

free(swap);
return 0;
}


#endif




