
/* Result Sets Interface */
#ifndef SQL_CRSR
#  define SQL_CRSR
  struct sql_cursor
  {
    unsigned int curocn;
    void *ptr1;
    void *ptr2;
    unsigned int magic;
  };
  typedef struct sql_cursor sql_cursor;
  typedef struct sql_cursor SQL_CURSOR;
#endif /* SQL_CRSR */

/* Thread Safety */
typedef void * sql_context;
typedef void * SQL_CONTEXT;

/* Object support */
struct sqltvn
{
  unsigned char *tvnvsn; 
  unsigned short tvnvsnl; 
  unsigned char *tvnnm;
  unsigned short tvnnml; 
  unsigned char *tvnsnm;
  unsigned short tvnsnml;
};
typedef struct sqltvn sqltvn;

struct sqladts
{
  unsigned int adtvsn; 
  unsigned short adtmode; 
  unsigned short adtnum;  
  sqltvn adttvn[1];       
};
typedef struct sqladts sqladts;

static struct sqladts sqladt = {
  1,1,0,
};

/* Binding to PL/SQL Records */
struct sqltdss
{
  unsigned int tdsvsn; 
  unsigned short tdsnum; 
  unsigned char *tdsval[1]; 
};
typedef struct sqltdss sqltdss;
static struct sqltdss sqltds =
{
  1,
  0,
};

/* File name & Package Name */
struct sqlcxp
{
  unsigned short fillen;
           char  filnam[13];
};
static struct sqlcxp sqlfpn =
{
    12,
    "lb_oracle.pc"
};


static unsigned int sqlctx = 311075;


static struct sqlexd {
   unsigned long  sqlvsn;
   unsigned int   arrsiz;
   unsigned int   iters;
   unsigned int   offset;
   unsigned short selerr;
   unsigned short sqlety;
   unsigned int   occurs;
            short *cud;
   unsigned char  *sqlest;
            char  *stmt;
   sqladts *sqladtp;
   sqltdss *sqltdsp;
   unsigned char  **sqphsv;
   unsigned long  *sqphsl;
            int   *sqphss;
            short **sqpind;
            int   *sqpins;
   unsigned long  *sqparm;
   unsigned long  **sqparc;
   unsigned short  *sqpadto;
   unsigned short  *sqptdso;
   unsigned int   sqlcmax;
   unsigned int   sqlcmin;
   unsigned int   sqlcincr;
   unsigned int   sqlctimeout;
   unsigned int   sqlcnowait;
            int   sqfoff;
   unsigned int   sqcmod;
   unsigned int   sqfmod;
   unsigned int   sqlpfmem;
   unsigned char  *sqhstv[4];
   unsigned long  sqhstl[4];
            int   sqhsts[4];
            short *sqindv[4];
            int   sqinds[4];
   unsigned long  sqharm[4];
   unsigned long  *sqharc[4];
   unsigned short  sqadto[4];
   unsigned short  sqtdso[4];
} sqlstm = {13,4};

/* SQLLIB Prototypes */
extern sqlcxt (/*_ void **, unsigned int *,
                   struct sqlexd *, struct sqlcxp * _*/);
extern sqlcx2t(/*_ void **, unsigned int *,
                   struct sqlexd *, struct sqlcxp * _*/);
extern sqlbuft(/*_ void **, char * _*/);
extern sqlgs2t(/*_ void **, char * _*/);
extern sqlorat(/*_ void **, unsigned int *, void * _*/);

/* Forms Interface */
static int IAPSUCC = 0;
static int IAPFAIL = 1403;
static int IAPFTL  = 535;
extern void sqliem(/*_ unsigned char *, signed int * _*/);

typedef struct { unsigned short len; unsigned char arr[1]; } VARCHAR;
typedef struct { unsigned short len; unsigned char arr[1]; } varchar;

/* CUD (Compilation Unit Data) Array */
static short sqlcud0[] =
{13,4130,1,0,0,
5,0,0,1,0,0,30,130,0,0,0,0,0,1,0,
20,0,0,0,0,0,27,166,0,0,4,4,0,1,0,1,97,0,0,1,97,0,0,1,97,0,0,1,10,0,0,
51,0,0,0,0,0,27,199,0,0,4,4,0,1,0,1,97,0,0,1,97,0,0,1,97,0,0,1,10,0,0,
};


/*
*========================================================================
*
*   SYSTEM   : AZN_PLAZA_LANE LIBRARY
*   FILE     : lb_oracle.sc
*   Progrmer : JBY (baarong@neighbor21.co.kr)
*   Date     : 2018.09.05
*   Modified :
*   Remark   :
 *              
 *========================================================================
*/
#if 0
#include <sql.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sqlca.h>
#include <oraca.h>
#include "string.h"
#if 0
#include <ulpLibInterface.h>
#endif
#include "path_def.h"
#include "def_set.h"
//#include "lb_func_list.h"

#define  USERID  "NAMSAN1"
#define  PASSWD  "namsan1"
#define  TNSNAME "NAMSAN1"
#define  USERIDC  "NAMSAN"
#define  PASSWDC  "namsan"
#define  TNSNAMEC "NAMSAN"
#if 0
EXEC SQL INCLUDE oraca;
EXEC SQL INCLUDE sqlca;

EXEC ORACLE OPTION (ORACA=YES);
EXEC ORACLE OPTION (RELEASE_CURSOR=YES);
#endif

/*
 *========================================================================
 *
 *   Function : sqlcode()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.03
 *   Modified :
 *   Remark   : Oracle Error Message Display.
 *
 *========================================================================
*/

int sqlcode (query)
char *query;
{
  sqlca.sqlerrm.sqlerrmc [sqlca.sqlerrm.sqlerrml] = '\0';
#if 0
  oraca.orastxt.orastxtc [oraca.orastxt.orastxtl] = '\0';
  oraca.orasfnm.orasfnmc [oraca.orasfnm.orasfnml] = '\0';
#endif
  fprintf (stderr, "%s:\n%s\n", query, sqlca.sqlerrm.sqlerrmc);

  return (sqlca.sqlcode);
}
#if 0
/*
 *========================================================================
 *
 *   Function : connect_db()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   :
 *
 *========================================================================
*/

int connect_db ()
{
    VARCHAR userid[64];
    VARCHAR passwd[64];
    VARCHAR dbconn[64];
    VARCHAR tnsname[64];

    memset (&dbconn, 0x00, sizeof (dbconn));
    memset (&userid, 0x00, sizeof (userid));
    memset (&passwd, 0x00, sizeof (passwd));
    memset (&tnsname, 0x00, sizeof (tnsname));

//    userid.len = sprintf(userid.arr, "%s", lb_get_inifile(INI_FILE,"ORACLE","UID"));
  userid.len = sprintf ((char *)userid.arr, "%s", USERID);

//    passwd.len = sprintf(passwd.arr, "%s", lb_get_inifile(INI_FILE,"ORACLE","PWD"));
  passwd.len = sprintf ((char *)passwd.arr, "%s", PASSWD);

//    tnsname.len = sprintf(tnsname.arr, "%s", lb_get_inifile(INI_FILE,"ORACLE","HOST"));
  tnsname.len = sprintf ((char *)tnsname.arr, "%s", TNSNAME);
/*  dbconn.len = sprintf ((char *)dbconn.arr, "%s/%s", USERID, PASSWD); */
  dbconn.len = sprintf ((char *)dbconn.arr, "%s/%s@%s", USERID, PASSWD, TNSNAME);

    EXEC SQL CONNECT :userid IDENTIFIED BY :passwd USING :tnsname;

    if (sqlca.sqlcode < 0) 
    {
        printf ("UserID[%s] PassWd[%s] TNS[%s]", userid, passwd, tnsname);
        printf ("oracle connect error connect_db(%d)\n", sqlca.sqlcode);
        sprintf ((char *)dbconn.arr, "oerr sql %d", abs (sqlca.sqlcode));
        system (dbconn.arr);
        return (-1);
    }
    
    return (0);
}
#endif
/*
 *========================================================================
 *
 *   Function : disconnect_db()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   :
 *
 *========================================================================
*/

int disconnect_db()
{
  /* EXEC SQL COMMIT WORK RELEASE; */ 

{
  struct sqlexd sqlstm;
  sqlorat((void **)0, &sqlctx, &oraca);
  sqlstm.sqlvsn = 13;
  sqlstm.arrsiz = 0;
  sqlstm.sqladtp = &sqladt;
  sqlstm.sqltdsp = &sqltds;
  sqlstm.iters = (unsigned int  )1;
  sqlstm.offset = (unsigned int  )5;
  sqlstm.cud = sqlcud0;
  sqlstm.sqlest = (unsigned char  *)&sqlca;
  sqlstm.sqlety = (unsigned short)4352;
  sqlstm.occurs = (unsigned int  )0;
  sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}



  if (sqlca.sqlcode < 0) 
  {
      printf ("oracle connect error disconnect_db(%d)\n", sqlca.sqlcode);
  }

  return (0);
}

/*
 *========================================================================
 *
 *   Function : c_DbConnect()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.13
 *   Modified :
 *   Remark   :
 *
 *========================================================================
*/
#if 1
int c_DbConnect()
{
   char  userid[10+1];
   char  passwd[10+1];
   char  tnsname[100+1];
/*
   strcpy(userid, "etcs");
   strcpy(passwd, "etcs");
   strcpy(tnsname, "Server=211.189.132.239;CONNTYPE=1;PORT_NO=20300;NLS_USE=UTF8");
*/
   strcpy(userid, USERID);
   strcpy(passwd, PASSWD);
   strcpy(tnsname, TNSNAME);

   /* EXEC SQL CONNECT :userid IDENTIFIED BY :passwd USING :tnsname; */ 

{
   struct sqlexd sqlstm;
   sqlorat((void **)0, &sqlctx, &oraca);
   sqlstm.sqlvsn = 13;
   sqlstm.arrsiz = 4;
   sqlstm.sqladtp = &sqladt;
   sqlstm.sqltdsp = &sqltds;
   sqlstm.iters = (unsigned int  )10;
   sqlstm.offset = (unsigned int  )20;
   sqlstm.cud = sqlcud0;
   sqlstm.sqlest = (unsigned char  *)&sqlca;
   sqlstm.sqlety = (unsigned short)4352;
   sqlstm.occurs = (unsigned int  )0;
   sqlstm.sqhstv[0] = (unsigned char  *)userid;
   sqlstm.sqhstl[0] = (unsigned long )11;
   sqlstm.sqhsts[0] = (         int  )11;
   sqlstm.sqindv[0] = (         short *)0;
   sqlstm.sqinds[0] = (         int  )0;
   sqlstm.sqharm[0] = (unsigned long )0;
   sqlstm.sqadto[0] = (unsigned short )0;
   sqlstm.sqtdso[0] = (unsigned short )0;
   sqlstm.sqhstv[1] = (unsigned char  *)passwd;
   sqlstm.sqhstl[1] = (unsigned long )11;
   sqlstm.sqhsts[1] = (         int  )11;
   sqlstm.sqindv[1] = (         short *)0;
   sqlstm.sqinds[1] = (         int  )0;
   sqlstm.sqharm[1] = (unsigned long )0;
   sqlstm.sqadto[1] = (unsigned short )0;
   sqlstm.sqtdso[1] = (unsigned short )0;
   sqlstm.sqhstv[2] = (unsigned char  *)tnsname;
   sqlstm.sqhstl[2] = (unsigned long )101;
   sqlstm.sqhsts[2] = (         int  )101;
   sqlstm.sqindv[2] = (         short *)0;
   sqlstm.sqinds[2] = (         int  )0;
   sqlstm.sqharm[2] = (unsigned long )0;
   sqlstm.sqadto[2] = (unsigned short )0;
   sqlstm.sqtdso[2] = (unsigned short )0;
   sqlstm.sqphsv = sqlstm.sqhstv;
   sqlstm.sqphsl = sqlstm.sqhstl;
   sqlstm.sqphss = sqlstm.sqhsts;
   sqlstm.sqpind = sqlstm.sqindv;
   sqlstm.sqpins = sqlstm.sqinds;
   sqlstm.sqparm = sqlstm.sqharm;
   sqlstm.sqparc = sqlstm.sqharc;
   sqlstm.sqpadto = sqlstm.sqadto;
   sqlstm.sqptdso = sqlstm.sqtdso;
   sqlstm.sqlcmax = (unsigned int )100;
   sqlstm.sqlcmin = (unsigned int )2;
   sqlstm.sqlcincr = (unsigned int )1;
   sqlstm.sqlctimeout = (unsigned int )0;
   sqlstm.sqlcnowait = (unsigned int )0;
   sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}



   if (sqlca.sqlcode < 0)
   {
       return(FAIL);
   }

   return(SUCCESS);
}
#endif

/*
 *========================================================================
 *
 *   Function : cen_DbConnect()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.13
 *   Modified :
 *   Remark   :
 *
 *========================================================================
*/
#if 1
int cen_DbConnect()
{
   char  userid[10+1];
   char  passwd[10+1];
   char  tnsname[100+1];

   strcpy(userid, USERID);
   strcpy(passwd, PASSWD);
   strcpy(tnsname, TNSNAME);

   /* EXEC SQL CONNECT :userid IDENTIFIED BY :passwd USING :tnsname; */ 

{
   struct sqlexd sqlstm;
   sqlorat((void **)0, &sqlctx, &oraca);
   sqlstm.sqlvsn = 13;
   sqlstm.arrsiz = 4;
   sqlstm.sqladtp = &sqladt;
   sqlstm.sqltdsp = &sqltds;
   sqlstm.iters = (unsigned int  )10;
   sqlstm.offset = (unsigned int  )51;
   sqlstm.cud = sqlcud0;
   sqlstm.sqlest = (unsigned char  *)&sqlca;
   sqlstm.sqlety = (unsigned short)4352;
   sqlstm.occurs = (unsigned int  )0;
   sqlstm.sqhstv[0] = (unsigned char  *)userid;
   sqlstm.sqhstl[0] = (unsigned long )11;
   sqlstm.sqhsts[0] = (         int  )11;
   sqlstm.sqindv[0] = (         short *)0;
   sqlstm.sqinds[0] = (         int  )0;
   sqlstm.sqharm[0] = (unsigned long )0;
   sqlstm.sqadto[0] = (unsigned short )0;
   sqlstm.sqtdso[0] = (unsigned short )0;
   sqlstm.sqhstv[1] = (unsigned char  *)passwd;
   sqlstm.sqhstl[1] = (unsigned long )11;
   sqlstm.sqhsts[1] = (         int  )11;
   sqlstm.sqindv[1] = (         short *)0;
   sqlstm.sqinds[1] = (         int  )0;
   sqlstm.sqharm[1] = (unsigned long )0;
   sqlstm.sqadto[1] = (unsigned short )0;
   sqlstm.sqtdso[1] = (unsigned short )0;
   sqlstm.sqhstv[2] = (unsigned char  *)tnsname;
   sqlstm.sqhstl[2] = (unsigned long )101;
   sqlstm.sqhsts[2] = (         int  )101;
   sqlstm.sqindv[2] = (         short *)0;
   sqlstm.sqinds[2] = (         int  )0;
   sqlstm.sqharm[2] = (unsigned long )0;
   sqlstm.sqadto[2] = (unsigned short )0;
   sqlstm.sqtdso[2] = (unsigned short )0;
   sqlstm.sqphsv = sqlstm.sqhstv;
   sqlstm.sqphsl = sqlstm.sqhstl;
   sqlstm.sqphss = sqlstm.sqhsts;
   sqlstm.sqpind = sqlstm.sqindv;
   sqlstm.sqpins = sqlstm.sqinds;
   sqlstm.sqparm = sqlstm.sqharm;
   sqlstm.sqparc = sqlstm.sqharc;
   sqlstm.sqpadto = sqlstm.sqadto;
   sqlstm.sqptdso = sqlstm.sqtdso;
   sqlstm.sqlcmax = (unsigned int )100;
   sqlstm.sqlcmin = (unsigned int )2;
   sqlstm.sqlcincr = (unsigned int )1;
   sqlstm.sqlctimeout = (unsigned int )0;
   sqlstm.sqlcnowait = (unsigned int )0;
   sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}



   if (sqlca.sqlcode < 0)
   {
       return(FAIL);
   }

   return(SUCCESS);
}
#endif
