/*
*file db.c:
*created by M.Vignesh
Arul Valavan
Sowmya Balasubramaniam
*created on 25/04/07
* Copyright (c) 2007 Wipro Technologies
* All right Reserved
*/
/* Includes */
#include<stdio.h>
#include "mysql/mysql.h"
/* The "mysql.h" header file for using the msyql API. */
/* Global variable decleration */
int pmem; // Amount of RAM
int smem; // Amount of hard disk space
int nports; // Number of ports
int proc; // Processor speed
char name[25]; // Name of the blade server
char arch[10]; // System Architecture
/* Function to get input from the user. */
void getdata()
{
printf("AMOUNT OF PRIMARY MEMORY: \n");
if (( scanf("%d",&pmem)!=1)) { printf("Error in line 17");}
printf("AMOUNT OF SECONDARY MEMORY \n");
if ((scanf("%d",&smem)!=1)) {printf("Error in line 19");}
printf("NUMBER OF NETWORK PORTS : \n");
if ((scanf("%d",&nports))!=1) {printf("Error in line 21");}
printf("SYSTEM ARCHITECTURE : \n");
if((scanf("%s",arch))!=1) {printf("Error in line 23");}
printf("PROCESSOR SPEED \n");
if ((scanf("%d",&proc))!=1) {printf("Error in line 25");}
}
/* Start of main program */
int main()
{
int ch; //choice in the menu
char sql[150]; //Query Buffer
int num_fields;
unsigned long *lengths;
int i;
MYSQL *conn; // Mysql Connection information
MYSQL_RES *res; // Result Set
MYSQL_ROW row; //Holds a row from a result set
char *server="localhost"; //mysql server
char *user = "vignesh"; //mysql user
char *passwd = "security"; //user password
char *db = "blade" ; //database name
// Basic Initialization
if((conn = mysql_init(NULL))==NULL) {printf("Error in line in mysql initialization");}
if((mysql_real_connect(conn,server,user,passwd,db,0,NULL,0))==NULL) {printf ("Error in connection");}
//Menu to be displayed when program is executed.
printf("BLADE SERVERS DATABASE");
printf("\n 1.ADD A BLADE SERVER TO DATABASE");
printf("\n 2.SELECT A BLADE SERVER FROM DATABASE");
printf("\n 3.LIST ALL THE BLADE SERVERS ");
printf("\n 4.REMOVE A BLADE SERVER FROM DATABASE");
printf("\n 5.EXIT");
printf("\n Enter an option : ");
if (scanf("%d",&ch)!=1) {printf("Error in line 52");}
switch(ch)
{
/* This section of code is used to add a new server blade to the mysql table */
case 1:
printf("ADDING A BLADE SERVER... \n");
printf("NAME OF THE BLADE SERVER : \n");
if((scanf("%s",name))!=0) {printf ("Error in line 60");}
getdata();
sprintf (sql, "insert into blade(name, pmem, smem, ports, arch, speed) values('%s',%d,%d,%d,'%s',%d)",name, pmem, smem,nports, arch, proc );
if((mysql_query(conn,sql))!=1) {printf("Error in line in query execution");}
printf("BLADE SERVER ADDED SUCCESSFULLY ! \n");
break;
/*Pending: Case 2 and Case 3 not implemented fully (Have to extract data from the result set obtained). */
/* This section of code is used to select a server blade from the mysql table based on the user requirements */
case 2:
printf("ENTER YOUR REQUIREMENTS : \n");
getdata();
sprintf (sql, "select * from blade where name=\"%s\"AND pmem=%d AND smem=%d AND ports=%d AND arch=\"%s\" AND speed=%d",name, pmem, smem,nports, arch, proc );
mysql_free_result();
if((mysql_query(conn,sql))!=0) {printf("Error in line in 71");}
printf("THE FOLLOWING MATCH YOUR REQUIREMENTS : \n");
printf("NAME \t PMEM \t SMEM \t PORTS \t ARCH \t SPEED \n");
mysql_free_result();
while ( (row = mysql_fetch_row(res)) != NULL)
printf("%s \t %s \t %s \t %s \t %s \t %s \n",row[0],row[1],row[2],row[3],row[4],row[5]);
break;
/* This section of code lists all the server blade present in the mysql table */
case 3:
printf("LIST ALL BALDE SERVERS ");
mysql_free_result();
mysql_query(conn,"select count(0) from blade");
res = mysql_store_result (conn);
mysql_free_result();
row = mysql_fetch_row (res);
printf("\nThere are %s blade server(s) in the database.. \n",row[0]);
printf("LISTING BLADE SERVER(S).... \n");
printf("NAME \t PMEM \t SMEM \t PORTS \t ARCH \t SPEED \n");
mysql_free_result();
mysql_query(conn,sql);
res = mysql_store_result (conn);
num_fields = mysql_num_fields(res);
while ( (row = mysql_fetch_row(res)) != NULL)
{
lengths = mysql_fetch_lengths(res);
for(i = 0; i < num_fields; i++)
{
printf("%s \t %s \t %s \t %s \t %s \t %s \n",row[0],row[1],row[2],row[3],row[4],row[5]);
}
printf("\n");
}
break;
/* This section of code is used to remove a server blade from the mysql table */
case 4:
printf("REMOVING A BLADE SERVER... \n");
printf("NAME OF THE BLADE SERVER : \n");
if((scanf("%s",name))!=1) {printf("Error in line 94");}
getdata();
sprintf (sql, "delete from blade where name=\"%s\"AND pmem=%d AND smem=%d AND ports=%d AND arch=\"%s\" AND speed=%d",name, pmem, smem,nports, arch, proc );
mysql_query(conn,sql);
printf("BLADE SERVER REMOVED SUCCESSFULLY ! \n");
break;
case 5:
return 0;
default:
printf("Invalid Option \n");
return 0;
}
/* End of switch case */
return 0;
}
/* End of main */