You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
7.8 KiB

#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,"palafour2");
strcat(uid.arr,"/");
strcat(uid.arr,"palafour2");
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);
}
void saisie(void)
{
varchar nocl[4];
varchar nom[80];
varchar ville[80];
int cp;
printf("Saisir une nouveau client \n");
printf("Entrer le nom du client : ");
scanf("%s",nom.arr);
nom.len=strlen(nom.arr);
printf("Entrer la ville du client : ");
scanf("%s",ville.arr);
ville.len=strlen(ville.arr);
printf("Entrer le code postal du client : ");
scanf("%d",&cp);
printf("Entrer le code du client : ");
scanf("%s",nocl.arr);
nocl.len=strlen(nocl.arr);
EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle error\n");
EXEC SQL INSERT INTO TClient2017 VALUES (:nocl,:nom,:ville,:cp);
EXEC SQL COMMIT;
}
void modification(void){
varchar noclchange[4];
varchar nomnew[80];
varchar villenew[80];
int cpnew;
varchar nomold[80];
varchar villeold[80];
int cpold;
printf("Modifier un client \n");
printf("Entrer le code du client a modifer : ");
scanf("%s",noclchange.arr);
noclchange.len=strlen(noclchange.arr);
EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle error\n");
EXEC SQL SELECT nom, ville, postal INTO :nomold, :villeold, :cpold FROM Tclient2017 WHERE NOCLIENT = :noclchange;
printf("Nom: %s \n",nomold.arr);
printf("Ville: %s \n",villeold.arr);
printf("CP courant du client: %d \n ",cpold);
printf("Modifier le nom du client : ");
scanf("%s",nomnew.arr);
nomnew.len=strlen(nomnew.arr);
printf("Modifier la ville du client : ");
scanf("%s",villenew.arr);
villenew.len=strlen(villenew.arr);
printf("Modifier le code postal du client : ");
scanf("%d",&cpnew);
EXEC SQL UPDATE TClient2017 SET nom = :nomnew, ville = :villenew, postal = :cpnew WHERE noclient = :noclchange;
EXEC SQL COMMIT;
}
void suppression(void){
varchar noclchange[4];
varchar nomnew[80];
varchar villenew[80];
int kmdeb;
int kmfin;
varchar noveh[4];
varchar datedeb[30];
varchar dateretour[30];
int fini=1;
printf("Supprimer un client \n");
printf("Entrer le code du client a supprimer : ");
scanf("%s",noclchange.arr);
noclchange.len=strlen(noclchange.arr);
EXEC SQL DECLARE locencours CURSOR FOR SELECT noveh, datedeb, kmdeb, dateretprev FROM Tlocation2017 WHERE noclient =:noclchange ;
EXEC SQL DECLARE locretour CURSOR FOR SELECT noveh, datedeb, kmdeb, kmfin, dateretour FROM Tlocatretour2017 WHERE noclient =:noclchange ;
EXEC SQL OPEN locencours;
while (fini=1)
{
EXEC SQL FETCH locencours INTO :noveh, :datedeb, :kmdeb, :dateretour;
printf("noveh: %s datedeb: %s kmdeb : %d date retour: %s \n",noveh.arr,datedeb.arr,kmdeb,dateretour.arr);
if (sqlca.sqlcode == 1403) fini=0;
}
printf("en retour \n");
EXEC SQL CLOSE locencours;
EXEC SQL OPEN locretour;
fini=1;
while (fini=1)
{
EXEC SQL FETCH locretour INTO :noveh, :datedeb, :kmdeb, :kmfin, :dateretour;
printf("noveh: %s datedeb: %s kmdeb : %d kmfin : %d date retour: %s \n",noveh.arr,datedeb.arr,kmdeb,kmfin,dateretour.arr);
if (sqlca.sqlcode == 1403) fini=0;
}
printf("Suppression en cours");
EXEC SQL CLOSE locretour;
EXEC SQL DELETE FROM Tlocation2017 WHERE noclient =:noclchange ;
EXEC SQL DELETE FROM Tlocatretour2017 WHERE noclient =:noclchange ;
EXEC SQL COMMIT;
}
void afficherclient (void){
varchar noclient[4];
varchar nom[80];
varchar ville[80];
int postal;
int fini=1;
printf("Affiche les clients \n");
EXEC SQL DECLARE clients CURSOR FOR SELECT noclient, nom, ville, postal FROM Tclient2017;
EXEC SQL OPEN clients;
while (fini=1)
{
EXEC SQL FETCH clients INTO :noclient, :nom, :ville, :postal;
printf("numero client: %s nom: %s ville : %s CP: %d \n",noclient.arr,nom.arr,ville.arr,postal);
if (sqlca.sqlcode = 1403) fini = 0;
}
printf(" Fini \n");
EXEC SQL CLOSE clients;
}
void afficherlocation (void){
varchar nocl[4];
varchar nom[100][10];
varchar noveh[5];
varchar immat[50][10];
varchar datedeb[50][30];
varchar dateretour[50][30];
int i;
printf("Affiche les locations d'un client \n");
printf("Entrer le numera du client : ");
scanf("%s",nocl.arr);
nocl.len=strlen(nocl.arr);
EXEC SQL SELECT immat, datedeb, dateretour INTO :immat, :datedeb, :dateretour FROM Tvehicule2017, Tlocatretour2017 WHERE Tlocatretour2017.nocl = :nocl and Tlocatretour2017.noveh = Tvehicule2017.noveh ;
printf("Il y a %d lignes selectionnees\n",sqlca.sqlerrd[2]);
i=0;
while (i<sqlca.sqlerrd[2])
{
printf("immat : %s date retour: %s date debut : %s \n",immat[i].arr, datedeb[i].arr,dateretour[i].arr);
i++;
}
}
void afficherlocencours (void){
varchar noclients[4];
varchar nom[80];
varchar immat[10];
varchar modele[30];
varchar couleur[30];
int kilometrage; // Tvehicule2017 noveh
varchar libelle[20]; // Tcategorie2017 nocat
varchar datedeb[20]; // Tlocatretour2017 noclient
varchar dateretprev[20];
int postal;
printf("Affiche les clients ayant une location en cours\n");
EXEC SQL DECLARE c CURSOR FOR SELECT noclient FROM Tlocation2017;
EXEC SQL OPEN c;
EXEC SQL WHENEVER NOT FOUND goto suite;
while (1)
{
EXEC SQL FETCH c INTO :noclients;
printf("numero client: %s nom: %s \n",noclients.arr);
}
suite : printf(" A FINIR \n");
EXEC SQL CLOSE c;
}
int main(int argc, char** argv)
{
char touche=' ';
EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle error\n");
printf("\n Appel de la fonction connexion");
connexion();
while(touche != 'Q')
{
printf("\n MENU \n ");
printf("Faites votre choix \n ");
printf("S pour saisir un client \n ");
printf("M pour modifier un client \n ");
printf("E pour effacer un client \n ");
printf("A pour afficher tous les clients \n ");
printf("L pour afficher toutes les locations d'un client \n ");
printf("C pour afficher toutes les clients ayant une location en cours \n ");
printf("Q pour quitter \n ");
scanf("%s",touche);
if (touche ='S') saisie();
if (touche ='M') modification();
if (touche ='E') suppression();
if (touche ='A') afficherclient();
if (touche ='L') afficherlocation();
if (touche ='C') afficherlocencours();
}
printf("\n Appel de la fonction deconnexion");
deconnexion(1);
return(0);
}