#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size_t long

EXEC SQL INCLUDE SQLCA.H;
EXEC SQL INCLUDE SQLDA.H;
EXEC SQL INCLUDE ORACA.H;

void connexion()
{ 	VARCHAR uid[50];
	char login[20];
	char passwd[20];
	printf("\n");
	strcpy(uid.arr,"mfservaj");
	strcat(uid.arr,"/");
	strcat(uid.arr,"marif044");
	strcat(uid.arr,"@kirov");
	uid.len=strlen(uid.arr);
	EXEC SQL CONNECT :uid;
	if (sqlca.sqlcode==0)
		printf(" Connexion réussie avec succès.\n\n");
	else
		{
		printf ("Problème à la connexion.\n\n");
		exit(1);
		}
}
void deconnexion(int validation)
{
	if (validation == 1)
		{
		EXEC SQL COMMIT WORK RELEASE;
		} 
	else 
		{
		EXEC SQL ROLLBACK WORK RELEASE;
		}
	printf("\nDéconnexion sans problème.\n");
}


void sql_error(char *msg)
{
	char err_msg[128];
	long buf_len, msg_len;
	EXEC SQL WHENEVER SQLERROR CONTINUE;
	printf("%s\n", msg);
	buf_len = sizeof (err_msg);
	sqlglm(err_msg, &buf_len, &msg_len);
	if (msg_len > buf_len)
		msg_len = buf_len;
	printf("%.*s\n", msg_len, err_msg);
	deconnexion(0);
	exit(1);
}


void traitErreur(char *msg)
{	
	EXEC SQL WHENEVER SQLERROR CONTINUE;
	printf("%s\n", msg);
	deconnexion(0);
	exit(1);
}


int main(int argc, char** argv)
{
	
	EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle error\n");
	printf("\n Appel de la fonction connexion");
	connexion();
	printf("\n Appel de la fonction deconnexion");
	
	EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle error\n");
	/*EXEC SQL INSERT INTO tessai1 VALUES ('T01', 100,'C','Essai', to_date('01/03/2015', 'dd/mm/yyyy'));*/
	/*EXEC SQL INSERT INTO tessai1 VALUES ('T02', 200,'C','Essai', to_date('01/03/2015', 'dd/mm/yyyy'));*/
	/*EXEC SQL INSERT INTO tessai1 VALUES ('T03', 100,'D','Essai', to_date('01/03/2015', 'dd/mm/yyyy'));*/
	/*EXEC SQL INSERT INTO tessai1 VALUES ('T04', 100,'C','Adam', to_date('01/03/2015', 'dd/mm/yyyy'));*/
	/*EXEC SQL INSERT INTO tessai1 VALUES ('T05', 100,'C','Essai', to_date('01/03/2014', 'dd/mm/yyyy'));*/
	/*EXEC SQL INSERT INTO tessai1 VALUES ('T06', 180,'A','EveEtAdamEtLesEnfants', to_date('01/02/2015', 'dd/mm/yyyy'));*/
	EXEC SQL INSERT INTO tessai1 VALUES ('T07', 180,'A','EveEtAdam', to_date('01/02/2015', 'dd/mm/yyyy'));
	EXEC SQL COMMIT;
	
	deconnexion(1);
	return(0);
}


