#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,"palafour");
	strcat(uid.arr,"/");
	strcat(uid.arr,"1s;ph;;1");
	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)
{
varchar vnom[20];
varchar vprenom[20];
varchar vhopital[20];
char vidhopital[6];
int fini;
int fin=1;
int vnbnom;
int vnbhopit;
int nb=1;
	
	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");
	
	/*----------------------------------lecture----------------------------*/
	EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle error\n");

	/* SELECT p.Nom, p.Prenom, h.Nom FROM PERSONNEL p, HOPITAL h WHERE  h.id_hopital = p.id_hopital and p.id_personnel='PE01'; */
	/* printf("\nUn exemple PE01 \n");*/

	EXEC SQL SELECT p.Nom, p.Prenom, h.Nom INTO :vnom, :vprenom, :vhopital FROM PERSONNEL p, HOPITAL h WHERE  h.id_hopital = p.id_hopital and p.id_personnel='PE01';

	vnom.len=strlen(vnom.arr);
	vprenom.len=strlen(vprenom.arr);
	vhopital.len=strlen(vhopital.arr);
	
	printf("\n  TEST pour PE01 : Nom : %.*s prenom : %.*s hopital : %.*s \n", vnom.len, vnom.arr, vprenom.len,  vprenom.arr, vhopital.len,vhopital.arr);

	printf("\n Affichage les hopitaux \n");
	
	/* SELECT COUNT(h.Nom)  FROM  HOPITAL h */
	EXEC SQL SELECT COUNT(h.Nom) INTO :vnbhopit FROM  HOPITAL h;
	printf("\nNombre d'hopitaux est  %d\n",vnbhopit);

	EXEC SQL DECLARE hopitaux CURSOR FOR SELECT Nom, id_hopital FROM HOPITAL ORDER by Nom;
	EXEC SQL OPEN hopitaux;

	EXEC SQL FETCH hopitaux INTO :vhopital, :vidhopital;
	if (sqlca.sqlcode == 1403) {fin = 0;}
	while  (fin!=0)
        {       
	vhopital.len=strlen(vhopital.arr);
	printf("\n Affichage des personnels de l'hopital %.*s : \n",vhopital.len,vhopital.arr);
	
	/* SELECT COUNT(p.Nom)  FROM PERSONNEL WHERE  h.id_hopital = p.id_hopital; */
	EXEC SQL SELECT COUNT(Nom) INTO :vnbnom FROM PERSONNEL WHERE  id_hopital = :vidhopital;
	printf("\nNombre de personnel de l'hopital %.*s est  %d\n",vhopital.len,vhopital.arr, vnbnom);

	/* SELECT p.Nom, p.Prenom  FROM PERSONNEL p,  WHERE  h.id_hopital = p.id_hopital ORDER by p.prenom ; */
	EXEC SQL DECLARE personnels CURSOR FOR SELECT Nom, Prenom FROM PERSONNEL  WHERE  id_hopital = :vidhopital ORDER by prenom;
	
        EXEC SQL OPEN personnels;
	fini=1;
	EXEC SQL FETCH personnels INTO :vnom, :vprenom;
	if (sqlca.sqlcode == 1403) {fini = 0;}

	while  (fini!=0)
        {       
		vnom.len=strlen(vnom.arr);
		vprenom.len=strlen(vprenom.arr);
	
		printf("%d) Nom : %.*s Prenom : %.*s \n", nb, vnom.len, vnom.arr, vprenom.len,  vprenom.arr);
		nb++;
		EXEC SQL FETCH personnels INTO :vnom, :vprenom;
		if (sqlca.sqlcode == 1403) {fini = 0;}
	}
	EXEC SQL CLOSE personnels;
	
	EXEC SQL FETCH hopitaux INTO :vhopital, :vidhopital;
	if (sqlca.sqlcode == 1403) {fin = 0;}
	}
        printf(" Fini \n");
	EXEC SQL CLOSE hopitaux;

	deconnexion(1);
	return(0);
}


