static char sqla_program_id[40] = 
{65,111,66,65,71,65,68,65,76,73,74,32,32,32,32,32,67,79,65,76,
32,32,32,32,99,66,49,90,87,73,77,78,48,49,49,49,49,32,32,32};


#include "sqladef.h"

static struct sqla_runtime_info sqla_rtinfo = 
{{'S','Q','L','A','R','T','I','N'}, sizeof(wchar_t), 0, {' ',' ',' ',' '}};


#line 1 "coal.sqc"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlenv.h>


/*
EXEC SQL INCLUDE SQLCA;
*/

/* SQL Communication Area - SQLCA - structures and constants */
#include "sqlca.h"
struct sqlca sqlca;


#line 6 "coal.sqc"


int compareDate(char* d1, char* d2, int interval);
int getTime(char* time1, char* time2);

int main(int argc, char** argv) {
  
/*
EXEC SQL BEGIN DECLARE SECTION;
*/

#line 12 "coal.sqc"

    long count, i= 2;
    char eid[12], mid[12], seid[12], smid[12];
    double salary, ssalary;
    char startDate[11], sStartDate[11];
    char endDate[11], sEndDate[11], pEndDate[11];
    char lasttime[9], curtime[9];
  
/*
EXEC SQL END DECLARE SECTION;
*/

#line 19 "coal.sqc"


  
/*
EXEC SQL CONNECT TO cs560;
*/

{
#line 21 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 21 "coal.sqc"
  sqlaaloc(2,1,1,0L);
    {
      struct sqla_setd_list sql_setdlist[1];
#line 21 "coal.sqc"
      sql_setdlist[0].sqltype = 460; sql_setdlist[0].sqllen = 6;
#line 21 "coal.sqc"
      sql_setdlist[0].sqldata = (void*)"cs560";
#line 21 "coal.sqc"
      sql_setdlist[0].sqlind = 0L;
#line 21 "coal.sqc"
      sqlasetd(2,0,1,sql_setdlist,0L);
    }
#line 21 "coal.sqc"
  sqlacall((unsigned short)29,4,2,0,0L);
#line 21 "coal.sqc"
  sqlastop(0L);
}

#line 21 "coal.sqc"


  /* table need to be created before the execution of this program 
     because it is not allowed to insert 
  EXEC SQL CREATE TABLE emp10res (EID char(11),  MID char(11), 
     Salary Decimal(7,2), StartDate date, EndDate date); */

  
/*
EXEC SQL VALUES (CURRENT TIME) INTO :lasttime;
*/

{
#line 28 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 28 "coal.sqc"
  sqlaaloc(3,1,2,0L);
    {
      struct sqla_setd_list sql_setdlist[1];
#line 28 "coal.sqc"
      sql_setdlist[0].sqltype = 460; sql_setdlist[0].sqllen = 9;
#line 28 "coal.sqc"
      sql_setdlist[0].sqldata = (void*)lasttime;
#line 28 "coal.sqc"
      sql_setdlist[0].sqlind = 0L;
#line 28 "coal.sqc"
      sqlasetd(3,0,1,sql_setdlist,0L);
    }
#line 28 "coal.sqc"
  sqlacall((unsigned short)24,1,0,3,0L);
#line 28 "coal.sqc"
  sqlastop(0L);
}

#line 28 "coal.sqc"


  
/*
EXEC SQL SELECT COUNT(*) INTO :count FROM emp10;
*/

{
#line 30 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 30 "coal.sqc"
  sqlaaloc(3,1,3,0L);
    {
      struct sqla_setd_list sql_setdlist[1];
#line 30 "coal.sqc"
      sql_setdlist[0].sqltype = 496; sql_setdlist[0].sqllen = 4;
#line 30 "coal.sqc"
      sql_setdlist[0].sqldata = (void*)&count;
#line 30 "coal.sqc"
      sql_setdlist[0].sqlind = 0L;
#line 30 "coal.sqc"
      sqlasetd(3,0,1,sql_setdlist,0L);
    }
#line 30 "coal.sqc"
  sqlacall((unsigned short)24,2,0,3,0L);
#line 30 "coal.sqc"
  sqlastop(0L);
}

#line 30 "coal.sqc"

  
/*
EXEC SQL DECLARE ccur CURSOR FOR                 
    select EID, MID, Salary, StartDate, EndDate    
    from emp10                                       
    order by EID, MID, Salary, StartDate, EndDate  
    for read only;
*/

#line 35 "coal.sqc"
                                 

  
/*
EXEC SQL OPEN ccur;
*/

{
#line 37 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 37 "coal.sqc"
  sqlacall((unsigned short)26,3,0,0,0L);
#line 37 "coal.sqc"
  sqlastop(0L);
}

#line 37 "coal.sqc"


  /*  EXEC SQL VALUES (CURRENT TIME) INTO :curtime;
      printf("The sorting time is %d seconds\n", 
      getTime(lasttime, curtime)); */

  /* assuming at least two tuples */
  
/*
EXEC SQL FETCH ccur
      into :seid, :smid, :ssalary, :sStartDate, :sEndDate;
*/

{
#line 45 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 45 "coal.sqc"
  sqlaaloc(3,5,4,0L);
    {
      struct sqla_setd_list sql_setdlist[5];
#line 45 "coal.sqc"
      sql_setdlist[0].sqltype = 460; sql_setdlist[0].sqllen = 12;
#line 45 "coal.sqc"
      sql_setdlist[0].sqldata = (void*)seid;
#line 45 "coal.sqc"
      sql_setdlist[0].sqlind = 0L;
#line 45 "coal.sqc"
      sql_setdlist[1].sqltype = 460; sql_setdlist[1].sqllen = 12;
#line 45 "coal.sqc"
      sql_setdlist[1].sqldata = (void*)smid;
#line 45 "coal.sqc"
      sql_setdlist[1].sqlind = 0L;
#line 45 "coal.sqc"
      sql_setdlist[2].sqltype = 480; sql_setdlist[2].sqllen = 8;
#line 45 "coal.sqc"
      sql_setdlist[2].sqldata = (void*)&ssalary;
#line 45 "coal.sqc"
      sql_setdlist[2].sqlind = 0L;
#line 45 "coal.sqc"
      sql_setdlist[3].sqltype = 460; sql_setdlist[3].sqllen = 11;
#line 45 "coal.sqc"
      sql_setdlist[3].sqldata = (void*)sStartDate;
#line 45 "coal.sqc"
      sql_setdlist[3].sqlind = 0L;
#line 45 "coal.sqc"
      sql_setdlist[4].sqltype = 460; sql_setdlist[4].sqllen = 11;
#line 45 "coal.sqc"
      sql_setdlist[4].sqldata = (void*)sEndDate;
#line 45 "coal.sqc"
      sql_setdlist[4].sqlind = 0L;
#line 45 "coal.sqc"
      sqlasetd(3,0,5,sql_setdlist,0L);
    }
#line 45 "coal.sqc"
  sqlacall((unsigned short)25,3,0,3,0L);
#line 45 "coal.sqc"
  sqlastop(0L);
}

#line 45 "coal.sqc"

  
/*
EXEC SQL FETCH ccur
      into :eid, :mid, :salary, :startDate, :endDate;
*/

{
#line 47 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 47 "coal.sqc"
  sqlaaloc(3,5,5,0L);
    {
      struct sqla_setd_list sql_setdlist[5];
#line 47 "coal.sqc"
      sql_setdlist[0].sqltype = 460; sql_setdlist[0].sqllen = 12;
#line 47 "coal.sqc"
      sql_setdlist[0].sqldata = (void*)eid;
#line 47 "coal.sqc"
      sql_setdlist[0].sqlind = 0L;
#line 47 "coal.sqc"
      sql_setdlist[1].sqltype = 460; sql_setdlist[1].sqllen = 12;
#line 47 "coal.sqc"
      sql_setdlist[1].sqldata = (void*)mid;
#line 47 "coal.sqc"
      sql_setdlist[1].sqlind = 0L;
#line 47 "coal.sqc"
      sql_setdlist[2].sqltype = 480; sql_setdlist[2].sqllen = 8;
#line 47 "coal.sqc"
      sql_setdlist[2].sqldata = (void*)&salary;
#line 47 "coal.sqc"
      sql_setdlist[2].sqlind = 0L;
#line 47 "coal.sqc"
      sql_setdlist[3].sqltype = 460; sql_setdlist[3].sqllen = 11;
#line 47 "coal.sqc"
      sql_setdlist[3].sqldata = (void*)startDate;
#line 47 "coal.sqc"
      sql_setdlist[3].sqlind = 0L;
#line 47 "coal.sqc"
      sql_setdlist[4].sqltype = 460; sql_setdlist[4].sqllen = 11;
#line 47 "coal.sqc"
      sql_setdlist[4].sqldata = (void*)endDate;
#line 47 "coal.sqc"
      sql_setdlist[4].sqlind = 0L;
#line 47 "coal.sqc"
      sqlasetd(3,0,5,sql_setdlist,0L);
    }
#line 47 "coal.sqc"
  sqlacall((unsigned short)25,3,0,3,0L);
#line 47 "coal.sqc"
  sqlastop(0L);
}

#line 47 "coal.sqc"




  strcpy(pEndDate, sEndDate);
  while (1) {
    if ( strcmp(seid, eid) != 0 || strcmp(smid, mid) != 0 ||
	 ssalary != salary || compareDate(pEndDate, startDate, 1) < 0) {
      
/*
EXEC SQL INSERT INTO empRes values (:seid, :smid,
	  :ssalary, :sStartDate, :pEndDate);
*/

{
#line 56 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 56 "coal.sqc"
  sqlaaloc(2,5,6,0L);
    {
      struct sqla_setd_list sql_setdlist[5];
#line 56 "coal.sqc"
      sql_setdlist[0].sqltype = 460; sql_setdlist[0].sqllen = 12;
#line 56 "coal.sqc"
      sql_setdlist[0].sqldata = (void*)seid;
#line 56 "coal.sqc"
      sql_setdlist[0].sqlind = 0L;
#line 56 "coal.sqc"
      sql_setdlist[1].sqltype = 460; sql_setdlist[1].sqllen = 12;
#line 56 "coal.sqc"
      sql_setdlist[1].sqldata = (void*)smid;
#line 56 "coal.sqc"
      sql_setdlist[1].sqlind = 0L;
#line 56 "coal.sqc"
      sql_setdlist[2].sqltype = 480; sql_setdlist[2].sqllen = 8;
#line 56 "coal.sqc"
      sql_setdlist[2].sqldata = (void*)&ssalary;
#line 56 "coal.sqc"
      sql_setdlist[2].sqlind = 0L;
#line 56 "coal.sqc"
      sql_setdlist[3].sqltype = 460; sql_setdlist[3].sqllen = 11;
#line 56 "coal.sqc"
      sql_setdlist[3].sqldata = (void*)sStartDate;
#line 56 "coal.sqc"
      sql_setdlist[3].sqlind = 0L;
#line 56 "coal.sqc"
      sql_setdlist[4].sqltype = 460; sql_setdlist[4].sqllen = 11;
#line 56 "coal.sqc"
      sql_setdlist[4].sqldata = (void*)pEndDate;
#line 56 "coal.sqc"
      sql_setdlist[4].sqlind = 0L;
#line 56 "coal.sqc"
      sqlasetd(2,0,5,sql_setdlist,0L);
    }
#line 56 "coal.sqc"
  sqlacall((unsigned short)24,4,2,0,0L);
#line 56 "coal.sqc"
  sqlastop(0L);
}

#line 56 "coal.sqc"
 
      strcpy(seid, eid);
      strcpy(smid, mid);
      ssalary = salary;
      strcpy(sStartDate, startDate);
      strcpy(sEndDate, endDate);
      strcpy(pEndDate, endDate);
    }      
    if ( compareDate(pEndDate, endDate, 0) < 0)
       strcpy(pEndDate, endDate); 

    if ( ++i <= count) 
      
/*
EXEC SQL FETCH ccur
	into :eid, :mid, :salary, :startDate, :endDate;
*/

{
#line 69 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 69 "coal.sqc"
  sqlaaloc(3,5,7,0L);
    {
      struct sqla_setd_list sql_setdlist[5];
#line 69 "coal.sqc"
      sql_setdlist[0].sqltype = 460; sql_setdlist[0].sqllen = 12;
#line 69 "coal.sqc"
      sql_setdlist[0].sqldata = (void*)eid;
#line 69 "coal.sqc"
      sql_setdlist[0].sqlind = 0L;
#line 69 "coal.sqc"
      sql_setdlist[1].sqltype = 460; sql_setdlist[1].sqllen = 12;
#line 69 "coal.sqc"
      sql_setdlist[1].sqldata = (void*)mid;
#line 69 "coal.sqc"
      sql_setdlist[1].sqlind = 0L;
#line 69 "coal.sqc"
      sql_setdlist[2].sqltype = 480; sql_setdlist[2].sqllen = 8;
#line 69 "coal.sqc"
      sql_setdlist[2].sqldata = (void*)&salary;
#line 69 "coal.sqc"
      sql_setdlist[2].sqlind = 0L;
#line 69 "coal.sqc"
      sql_setdlist[3].sqltype = 460; sql_setdlist[3].sqllen = 11;
#line 69 "coal.sqc"
      sql_setdlist[3].sqldata = (void*)startDate;
#line 69 "coal.sqc"
      sql_setdlist[3].sqlind = 0L;
#line 69 "coal.sqc"
      sql_setdlist[4].sqltype = 460; sql_setdlist[4].sqllen = 11;
#line 69 "coal.sqc"
      sql_setdlist[4].sqldata = (void*)endDate;
#line 69 "coal.sqc"
      sql_setdlist[4].sqlind = 0L;
#line 69 "coal.sqc"
      sqlasetd(3,0,5,sql_setdlist,0L);
    }
#line 69 "coal.sqc"
  sqlacall((unsigned short)25,3,0,3,0L);
#line 69 "coal.sqc"
  sqlastop(0L);
}

#line 69 "coal.sqc"

    else
      break;
  }
  
/*
EXEC SQL INSERT INTO empRes values (:seid, :smid,
     :ssalary, :sStartDate, :pEndDate);
*/

{
#line 74 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 74 "coal.sqc"
  sqlaaloc(2,5,8,0L);
    {
      struct sqla_setd_list sql_setdlist[5];
#line 74 "coal.sqc"
      sql_setdlist[0].sqltype = 460; sql_setdlist[0].sqllen = 12;
#line 74 "coal.sqc"
      sql_setdlist[0].sqldata = (void*)seid;
#line 74 "coal.sqc"
      sql_setdlist[0].sqlind = 0L;
#line 74 "coal.sqc"
      sql_setdlist[1].sqltype = 460; sql_setdlist[1].sqllen = 12;
#line 74 "coal.sqc"
      sql_setdlist[1].sqldata = (void*)smid;
#line 74 "coal.sqc"
      sql_setdlist[1].sqlind = 0L;
#line 74 "coal.sqc"
      sql_setdlist[2].sqltype = 480; sql_setdlist[2].sqllen = 8;
#line 74 "coal.sqc"
      sql_setdlist[2].sqldata = (void*)&ssalary;
#line 74 "coal.sqc"
      sql_setdlist[2].sqlind = 0L;
#line 74 "coal.sqc"
      sql_setdlist[3].sqltype = 460; sql_setdlist[3].sqllen = 11;
#line 74 "coal.sqc"
      sql_setdlist[3].sqldata = (void*)sStartDate;
#line 74 "coal.sqc"
      sql_setdlist[3].sqlind = 0L;
#line 74 "coal.sqc"
      sql_setdlist[4].sqltype = 460; sql_setdlist[4].sqllen = 11;
#line 74 "coal.sqc"
      sql_setdlist[4].sqldata = (void*)pEndDate;
#line 74 "coal.sqc"
      sql_setdlist[4].sqlind = 0L;
#line 74 "coal.sqc"
      sqlasetd(2,0,5,sql_setdlist,0L);
    }
#line 74 "coal.sqc"
  sqlacall((unsigned short)24,5,2,0,0L);
#line 74 "coal.sqc"
  sqlastop(0L);
}

#line 74 "coal.sqc"
 
      
  
/*
EXEC SQL CLOSE ccur;
*/

{
#line 76 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 76 "coal.sqc"
  sqlacall((unsigned short)20,3,0,0,0L);
#line 76 "coal.sqc"
  sqlastop(0L);
}

#line 76 "coal.sqc"

  
/*
EXEC SQL COMMIT;
*/

{
#line 77 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 77 "coal.sqc"
  sqlacall((unsigned short)21,0,0,0,0L);
#line 77 "coal.sqc"
  sqlastop(0L);
}

#line 77 "coal.sqc"

  
/*
EXEC SQL VALUES (CURRENT TIME) INTO :curtime;
*/

{
#line 78 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 78 "coal.sqc"
  sqlaaloc(3,1,9,0L);
    {
      struct sqla_setd_list sql_setdlist[1];
#line 78 "coal.sqc"
      sql_setdlist[0].sqltype = 460; sql_setdlist[0].sqllen = 9;
#line 78 "coal.sqc"
      sql_setdlist[0].sqldata = (void*)curtime;
#line 78 "coal.sqc"
      sql_setdlist[0].sqlind = 0L;
#line 78 "coal.sqc"
      sqlasetd(3,0,1,sql_setdlist,0L);
    }
#line 78 "coal.sqc"
  sqlacall((unsigned short)24,6,0,3,0L);
#line 78 "coal.sqc"
  sqlastop(0L);
}

#line 78 "coal.sqc"

  printf("The coalescing time is %d seconds\n", 
	 getTime(lasttime, curtime));
  
/*
EXEC SQL CONNECT RESET;
*/

{
#line 81 "coal.sqc"
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
#line 81 "coal.sqc"
  sqlacall((unsigned short)29,3,0,0,0L);
#line 81 "coal.sqc"
  sqlastop(0L);
}

#line 81 "coal.sqc"

  return 0;
}

int compareDate(char* d1, char* d2, int interval) {
  int month1, date1, year1;
  int month2, date2, year2;

  sscanf(d1, "%d/%d/%d", &month1, &date1, &year1);
  sscanf(d2, "%d/%d/%d", &month2, &date2, &year2);

  date1 += interval;
  if ( year1 < year2)
    return -1;
  if ( year1 > year2)
    return 1;
  if ( month1 < month2)
    return -1;
  if ( month1 > month2)
    return 1;
  if ( date1 < date2)
    return -1;
  if ( date1 > date2)
    return 1;
  return 0;
}

int getTime(char* time1, char* time2) {
  int hour1, min1, sec1;
  int hour2, min2, sec2;

  sscanf(time1, "%d:%d:%d", &hour1, &min1, &sec1);
  sscanf(time2, "%d:%d:%d", &hour2, &min2, &sec2);
  
  return (hour2*3600+min2*60+sec2 - hour1*3600-min1*60-sec1);
}
