mytest.c 5.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*C4*/
/****************************************************************/
/*	Author:	Jethro Wright, III	TS :  3/ 4/1998  9:15	*/
/*	Date:	02/18/1998					*/
/*	mytest.c :  do some testing of the libmySQL.DLL....	*/
/*								*/
/*	History:						*/
/*		02/18/1998  jw3  also sprach zarathustra....	*/
/****************************************************************/


#include        <windows.h>
#include	<stdio.h>
#include	<string.h>

#include	<mysql.h>

#define		DEFALT_SQL_STMT	"SELECT * FROM db"
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif


/********************************************************
**
**		main  :-
**
********************************************************/

int
main( int argc, char * argv[] )
{

  char		szSQL[ 200 ], aszFlds[ 25 ][ 25 ], szDB[ 50 ] ;
35
  const  char   *pszT;
36 37 38 39 40 41 42
  int			i, j, k, l, x ;
  MYSQL		* myData ;
  MYSQL_RES	* res ;
  MYSQL_FIELD	* fd ;
  MYSQL_ROW	row ;

  //....just curious....
43
  printf( "sizeof( MYSQL ) == %d\n", (int) sizeof( MYSQL ) ) ;
44 45 46 47 48 49 50 51
  if ( argc == 2 )
    {
      strcpy( szDB, argv[ 1 ] ) ;
      strcpy( szSQL, DEFALT_SQL_STMT ) ;
      if (!strcmp(szDB,"--debug"))
      {
	strcpy( szDB, "mysql" ) ;
	printf("Some mysql struct information (size and offset):\n");
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	printf("net:\t%3d %3d\n",(int) sizeof(myData->net),
	       (int) offsetof(MYSQL,net));
	printf("host:\t%3d %3d\n",(int) sizeof(myData->host),
	       (int) offsetof(MYSQL,host));
	printf("port:\t%3d %3d\n", (int) sizeof(myData->port),
	       (int) offsetof(MYSQL,port));
	printf("protocol_version:\t%3d %3d\n",
	       (int) sizeof(myData->protocol_version),
	       (int) offsetof(MYSQL,protocol_version));
	printf("thread_id:\t%3d %3d\n",(int) sizeof(myData->thread_id),
	       (int) offsetof(MYSQL,thread_id));
	printf("affected_rows:\t%3d %3d\n",(int) sizeof(myData->affected_rows),
	       (int) offsetof(MYSQL,affected_rows));
	printf("packet_length:\t%3d %3d\n",(int) sizeof(myData->packet_length),
	       (int) offsetof(MYSQL,packet_length));
	printf("status:\t%3d %3d\n",(int) sizeof(myData->status),
	       (int) offsetof(MYSQL,status));
	printf("fields:\t%3d %3d\n",(int) sizeof(myData->fields),
	       (int) offsetof(MYSQL,fields));
	printf("field_alloc:\t%3d %3d\n",(int) sizeof(myData->field_alloc),
	       (int) offsetof(MYSQL,field_alloc));
	printf("free_me:\t%3d %3d\n",(int) sizeof(myData->free_me),
	       (int) offsetof(MYSQL,free_me));
	printf("options:\t%3d %3d\n",(int) sizeof(myData->options),
	       (int) offsetof(MYSQL,options));
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	puts("");
      }
    }		
  else if ( argc > 2 ) {
    strcpy( szDB, argv[ 1 ] ) ;
    strcpy( szSQL, argv[ 2 ] ) ;
  }
  else {
    strcpy( szDB, "mysql" ) ;
    strcpy( szSQL, DEFALT_SQL_STMT ) ;
  }
  //....
		  
  if ( (myData = mysql_init((MYSQL*) 0)) && 
       mysql_real_connect( myData, NULL, NULL, NULL, NULL, MYSQL_PORT,
			   NULL, 0 ) )
    {
94
      myData->reconnect= 1;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
      if ( mysql_select_db( myData, szDB ) < 0 ) {
	printf( "Can't select the %s database !\n", szDB ) ;
	mysql_close( myData ) ;
	return 2 ;
      }
    }
  else {
    printf( "Can't connect to the mysql server on port %d !\n",
	    MYSQL_PORT ) ;
    mysql_close( myData ) ;
    return 1 ;
  }
  //....
  if ( ! mysql_query( myData, szSQL ) ) {
    res = mysql_store_result( myData ) ;
    i = (int) mysql_num_rows( res ) ; l = 1 ;
    printf( "Query:  %s\nNumber of records found:  %ld\n", szSQL, i ) ;
    //....we can get the field-specific characteristics here....
    for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
      strcpy( aszFlds[ x ], fd->name ) ;
    //....
    while ( row = mysql_fetch_row( res ) ) {
      j = mysql_num_fields( res ) ;
      printf( "Record #%ld:-\n", l++ ) ;
      for ( k = 0 ; k < j ; k++ )
	printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
		(((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
      puts( "==============================\n" ) ;
    }
    mysql_free_result( res ) ;
  }
  else printf( "Couldn't execute %s on the server !\n", szSQL ) ;
  //....
  puts( "====  Diagnostic info  ====" ) ;
  pszT = mysql_get_client_info() ;
  printf( "Client info: %s\n", pszT ) ;
  //....
  pszT = mysql_get_host_info( myData ) ;
  printf( "Host info: %s\n", pszT ) ;
  //....
  pszT = mysql_get_server_info( myData ) ;
  printf( "Server info: %s\n", pszT ) ;
  //....
  res = mysql_list_processes( myData ) ; l = 1 ;
  if (res)
    {
      for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
	strcpy( aszFlds[ x ], fd->name ) ;
      while ( row = mysql_fetch_row( res ) ) {
	j = mysql_num_fields( res ) ;
	printf( "Process #%ld:-\n", l++ ) ;
	for ( k = 0 ; k < j ; k++ )
	  printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
		  (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
	puts( "==============================\n" ) ;
      }
    }
  else
    {
      printf("Got error %s when retreiving processlist\n",mysql_error(myData));
    }
  //....
  res = mysql_list_tables( myData, "%" ) ; l = 1 ;
  for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
    strcpy( aszFlds[ x ], fd->name ) ;
  while ( row = mysql_fetch_row( res ) ) {
    j = mysql_num_fields( res ) ;
    printf( "Table #%ld:-\n", l++ ) ;
    for ( k = 0 ; k < j ; k++ )
      printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
	      (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
    puts( "==============================\n" ) ;
  }
  //....
  pszT = mysql_stat( myData ) ;
  puts( pszT ) ;
  //....
  mysql_close( myData ) ;
  return 0 ;

}