/* 
 Example Program to Select Instances from Staff table 
 This example script uses the C libraries for SQL     
 The script will prompt the user to enter the sybase userid and the password
 Please beware that the password will not be concealed during entry 
*/

#include <stdio.h>
#include <stdlib.h>
#include <sybfront.h>
#include <sybdb.h>
#include <syberror.h>
#include <stdio.h>
#include <hesiod.h>

DBPROCESS * dbproc;   /* structure for the link with Sybase */
LOGINREC * login;    
char    sybase_username[20];    /* sybase user name */
char    sybase_passwd[20];      /* sybase password */
char    table_name[25];		/* SQL table_name to be read in */
char    column_name[25];	/* SQL column_name to be read in */
char    query[256];		/* SQL query to execute */
char    more[10];		
int return_code;

typedef struct rowena {   /* structure to keep the results */
   char columna[50];
} rowena;

struct rowena row[20];
char attribute[50];
int count=0;


main()
{
  int x=0;			/* a counter variable */

  /* Read the user name and password input from the keyboard */

  system("clear");
  printf("Example C Program with SQL \n\n");
  printf("Warning: The username and password will not be concealed.\n\n");
  printf("Username > ");
  scanf("%s",sybase_username);
  printf("Password > ");
  scanf("%s",sybase_passwd);

  if (dbinit () == FAIL)	/* initialize the database connection */
    exit (ERREXIT);

  /* Login to the database  */
  if ((login = dblogin ()) == NULL)
    {
      printf ("Can't allocate a sybase login record\n");
      exit (0);
    }	       
  
  DBSETLUSER (login, sybase_username); /* set the user name */
  DBSETLPWD (login, sybase_passwd); /* set the password */
  if ((dbproc = dbopen (login, NULL)) == NULL)
    {
      printf ("Sybase login failed\n");
      exit (0);
    }
  
  /* write out the SQL command to dbcmd structure */
  
  dbuse(dbproc, "bkaraca");

  for (;;) 
    {

      printf("Type in a table_name >");
      scanf("%s", table_name);
      printf("I read in the following table_name:\n");
      printf("%s\n", table_name);

      printf("Type in a column_name (of CHAR or VARCHAR data type)>");
      scanf("%s", column_name);
      printf("I read in the following column_name:\n");
      printf("%s\n", column_name);

      sprintf(query, "select %s from %s", column_name, table_name);
      printf("I have the following query:\n");
      printf("%s\n", query);

      dbfcmd (dbproc, "select %s from %s", column_name, table_name);

      /* execute the dbcmd */
      if (dbsqlexec (dbproc) == FAIL)
	{
	  printf ("The SQL select failed\n");
	  exit (0);
	}

      while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS) 
	{ 
          if (return_code == SUCCEED) 
	    { 
/*	      dbprhead(dbproc);
	      dbprrow(dbproc);*/

	      /* Bind results to program variable */
	      dbbind (dbproc, 1, STRINGBIND, (DBINT) 0, attribute);
	      /* results are returned one by one so save until end */
	      while (dbnextrow (dbproc) != NO_MORE_ROWS)
		{ 
		  sprintf (row[count].columna," %s", attribute);
		  count++;
		}

	      printf("\n----------\n  %s\n----------\n", table_name);
	      printf("%s\n-----\n", column_name);
	      for(x=1;x<count;x++){
				    printf("%s\n",row[x].columna);}
	      printf("----------\n");

	      printf("\n\nMore? (Y or N) >\n");
	      scanf("%s", more);
	    }
	  else
	    {
	      printf ("Query failed.\n");
	    }

	}

      if (more[0] != 'Y' && more[0] != 'y')
	{
	  dbexit ();
	  exit (0);
	}
      else
	{
	  dbcancel(dbproc);
	  dbcanquery(dbproc);
	  dbclrbuf(dbproc, (DBINT) 0);
	}
    }
}
