mytest.c 5.66 KB
Newer Older
1
/*C4*/
2

3
/****************************************************************/
4

5
/*	Author:	Jethro Wright, III	TS :  3/ 4/1998  9:15	*/
6

7
/*	Date:	02/18/1998					*/
8

9
/*	mytest.c :  do some testing of the libmySQL.DLL....	*/
10

11
/*								*/
12

13
/*	History:						*/
14

15
/*		02/18/1998  jw3  also sprach zarathustra....	*/
16

17 18 19
/****************************************************************/


20 21 22



23
#include        <windows.h>
24

25
#include	<stdio.h>
26

27 28
#include	<string.h>

29 30


31 32
#include	<mysql.h>

33 34


35
#define		DEFALT_SQL_STMT	"SELECT * FROM db"
36

37
#ifndef offsetof
38

39
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
40

41 42 43
#endif


44 45 46



47
/********************************************************
48

49
**
50

51
**		main  :-
52

53
**
54

55 56
********************************************************/

57 58


59
int
60

61
main( int argc, char * argv[] )
62

63 64
{

65 66


67
  char		szSQL[ 200 ], aszFlds[ 25 ][ 25 ], szDB[ 50 ] ;
68

69 70
  const  char   *pszT;
  int			i, j, k, l, x ;
71

72
  MYSQL		* myData ;
73

74
  MYSQL_RES	* res ;
75

76
  MYSQL_FIELD	* fd ;
77

78 79
  MYSQL_ROW	row ;

80 81


82
  //....just curious....
83

84 85
  printf( "sizeof( MYSQL ) == %d\n", (int) sizeof( MYSQL ) ) ;
  if ( argc == 2 )
86

87
    {
88

89
      strcpy( szDB, argv[ 1 ] ) ;
90

91
      strcpy( szSQL, DEFALT_SQL_STMT ) ;
92

93
      if (!strcmp(szDB,"--debug"))
94

95
      {
96

97
	strcpy( szDB, "mysql" ) ;
98

99
	printf("Some mysql struct information (size and offset):\n");
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
	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));
	puts("");
127

128
      }
129

130
    }		
131

132
  else if ( argc > 2 ) {
133

134
    strcpy( szDB, argv[ 1 ] ) ;
135

136
    strcpy( szSQL, argv[ 2 ] ) ;
137

138
  }
139

140
  else {
141

142
    strcpy( szDB, "mysql" ) ;
143

144
    strcpy( szSQL, DEFALT_SQL_STMT ) ;
145

146
  }
147

148
  //....
149

150
		  
151

152
  if ( (myData = mysql_init((MYSQL*) 0)) && 
153

154
       mysql_real_connect( myData, NULL, NULL, NULL, NULL, MYSQL_PORT,
155

156
			   NULL, 0 ) )
157

158
    {
159

160 161
      myData->reconnect= 1;
      if ( mysql_select_db( myData, szDB ) < 0 ) {
162

163
	printf( "Can't select the %s database !\n", szDB ) ;
164

165
	mysql_close( myData ) ;
166

167
	return 2 ;
168

169
      }
170

171
    }
172

173
  else {
174

175
    printf( "Can't connect to the mysql server on port %d !\n",
176

177
	    MYSQL_PORT ) ;
178

179
    mysql_close( myData ) ;
180

181
    return 1 ;
182

183
  }
184

185
  //....
186

187
  if ( ! mysql_query( myData, szSQL ) ) {
188

189
    res = mysql_store_result( myData ) ;
190

191
    i = (int) mysql_num_rows( res ) ; l = 1 ;
192

193
    printf( "Query:  %s\nNumber of records found:  %ld\n", szSQL, i ) ;
194

195
    //....we can get the field-specific characteristics here....
196

197
    for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
198

199
      strcpy( aszFlds[ x ], fd->name ) ;
200

201
    //....
202

203
    while ( row = mysql_fetch_row( res ) ) {
204

205
      j = mysql_num_fields( res ) ;
206

207
      printf( "Record #%ld:-\n", l++ ) ;
208

209
      for ( k = 0 ; k < j ; k++ )
210

211
	printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
212

213
		(((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
214

215
      puts( "==============================\n" ) ;
216

217
    }
218

219
    mysql_free_result( res ) ;
220

221
  }
222

223
  else printf( "Couldn't execute %s on the server !\n", szSQL ) ;
224

225
  //....
226

227
  puts( "====  Diagnostic info  ====" ) ;
228

229
  pszT = mysql_get_client_info() ;
230

231
  printf( "Client info: %s\n", pszT ) ;
232

233
  //....
234

235
  pszT = mysql_get_host_info( myData ) ;
236

237
  printf( "Host info: %s\n", pszT ) ;
238

239
  //....
240

241
  pszT = mysql_get_server_info( myData ) ;
242

243
  printf( "Server info: %s\n", pszT ) ;
244

245
  //....
246

247
  res = mysql_list_processes( myData ) ; l = 1 ;
248

249
  if (res)
250

251
    {
252

253
      for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
254

255
	strcpy( aszFlds[ x ], fd->name ) ;
256

257
      while ( row = mysql_fetch_row( res ) ) {
258

259
	j = mysql_num_fields( res ) ;
260

261
	printf( "Process #%ld:-\n", l++ ) ;
262

263
	for ( k = 0 ; k < j ; k++ )
264

265
	  printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
266

267
		  (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
268

269
	puts( "==============================\n" ) ;
270

271
      }
272

273
    }
274

275
  else
276

277
    {
278

279
      printf("Got error %s when retreiving processlist\n",mysql_error(myData));
280

281
    }
282

283
  //....
284

285
  res = mysql_list_tables( myData, "%" ) ; l = 1 ;
286

287
  for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
288

289
    strcpy( aszFlds[ x ], fd->name ) ;
290

291
  while ( row = mysql_fetch_row( res ) ) {
292

293
    j = mysql_num_fields( res ) ;
294

295
    printf( "Table #%ld:-\n", l++ ) ;
296

297
    for ( k = 0 ; k < j ; k++ )
298

299
      printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
300

301
	      (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
302

303
    puts( "==============================\n" ) ;
304

305
  }
306

307
  //....
308

309
  pszT = mysql_stat( myData ) ;
310

311
  puts( pszT ) ;
312

313
  //....
314

315
  mysql_close( myData ) ;
316

317 318
  return 0 ;

319 320


321
}
322