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.

175 lines
5.4 KiB

  1. /*C4*/
  2. /****************************************************************/
  3. /* Author: Jethro Wright, III TS : 3/ 4/1998 9:15 */
  4. /* Date: 02/18/1998 */
  5. /* mytest.c : do some testing of the libmySQL.DLL.... */
  6. /* */
  7. /* History: */
  8. /* 02/18/1998 jw3 also sprach zarathustra.... */
  9. /****************************************************************/
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <mysql.h>
  14. #define DEFALT_SQL_STMT "SELECT * FROM db"
  15. #ifndef offsetof
  16. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  17. #endif
  18. /********************************************************
  19. **
  20. ** main :-
  21. **
  22. ********************************************************/
  23. int
  24. main( int argc, char * argv[] )
  25. {
  26. char szSQL[ 200 ], aszFlds[ 25 ][ 25 ], szDB[ 50 ] ;
  27. const char *pszT;
  28. int i, j, k, l, x ;
  29. MYSQL * myData ;
  30. MYSQL_RES * res ;
  31. MYSQL_FIELD * fd ;
  32. MYSQL_ROW row ;
  33. //....just curious....
  34. printf( "sizeof( MYSQL ) == %d\n", (int) sizeof( MYSQL ) ) ;
  35. if ( argc == 2 )
  36. {
  37. strcpy( szDB, argv[ 1 ] ) ;
  38. strcpy( szSQL, DEFALT_SQL_STMT ) ;
  39. if (!strcmp(szDB,"--debug"))
  40. {
  41. strcpy( szDB, "mysql" ) ;
  42. printf("Some mysql struct information (size and offset):\n");
  43. printf("net:\t%3d %3d\n",(int) sizeof(myData->net),
  44. (int) offsetof(MYSQL,net));
  45. printf("host:\t%3d %3d\n",(int) sizeof(myData->host),
  46. (int) offsetof(MYSQL,host));
  47. printf("port:\t%3d %3d\n", (int) sizeof(myData->port),
  48. (int) offsetof(MYSQL,port));
  49. printf("protocol_version:\t%3d %3d\n",
  50. (int) sizeof(myData->protocol_version),
  51. (int) offsetof(MYSQL,protocol_version));
  52. printf("thread_id:\t%3d %3d\n",(int) sizeof(myData->thread_id),
  53. (int) offsetof(MYSQL,thread_id));
  54. printf("affected_rows:\t%3d %3d\n",(int) sizeof(myData->affected_rows),
  55. (int) offsetof(MYSQL,affected_rows));
  56. printf("packet_length:\t%3d %3d\n",(int) sizeof(myData->packet_length),
  57. (int) offsetof(MYSQL,packet_length));
  58. printf("status:\t%3d %3d\n",(int) sizeof(myData->status),
  59. (int) offsetof(MYSQL,status));
  60. printf("fields:\t%3d %3d\n",(int) sizeof(myData->fields),
  61. (int) offsetof(MYSQL,fields));
  62. printf("field_alloc:\t%3d %3d\n",(int) sizeof(myData->field_alloc),
  63. (int) offsetof(MYSQL,field_alloc));
  64. printf("free_me:\t%3d %3d\n",(int) sizeof(myData->free_me),
  65. (int) offsetof(MYSQL,free_me));
  66. printf("options:\t%3d %3d\n",(int) sizeof(myData->options),
  67. (int) offsetof(MYSQL,options));
  68. puts("");
  69. }
  70. }
  71. else if ( argc > 2 ) {
  72. strcpy( szDB, argv[ 1 ] ) ;
  73. strcpy( szSQL, argv[ 2 ] ) ;
  74. }
  75. else {
  76. strcpy( szDB, "mysql" ) ;
  77. strcpy( szSQL, DEFALT_SQL_STMT ) ;
  78. }
  79. //....
  80. if ( (myData = mysql_init((MYSQL*) 0)) &&
  81. mysql_real_connect( myData, NULL, NULL, NULL, NULL, MYSQL_PORT,
  82. NULL, 0 ) )
  83. {
  84. myData->reconnect= 1;
  85. if ( mysql_select_db( myData, szDB ) < 0 ) {
  86. printf( "Can't select the %s database !\n", szDB ) ;
  87. mysql_close( myData ) ;
  88. return 2 ;
  89. }
  90. }
  91. else {
  92. printf( "Can't connect to the mysql server on port %d !\n",
  93. MYSQL_PORT ) ;
  94. mysql_close( myData ) ;
  95. return 1 ;
  96. }
  97. //....
  98. if ( ! mysql_query( myData, szSQL ) ) {
  99. res = mysql_store_result( myData ) ;
  100. i = (int) mysql_num_rows( res ) ; l = 1 ;
  101. printf( "Query: %s\nNumber of records found: %ld\n", szSQL, i ) ;
  102. //....we can get the field-specific characteristics here....
  103. for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
  104. strcpy( aszFlds[ x ], fd->name ) ;
  105. //....
  106. while ( row = mysql_fetch_row( res ) ) {
  107. j = mysql_num_fields( res ) ;
  108. printf( "Record #%ld:-\n", l++ ) ;
  109. for ( k = 0 ; k < j ; k++ )
  110. printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
  111. (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
  112. puts( "==============================\n" ) ;
  113. }
  114. mysql_free_result( res ) ;
  115. }
  116. else printf( "Couldn't execute %s on the server !\n", szSQL ) ;
  117. //....
  118. puts( "==== Diagnostic info ====" ) ;
  119. pszT = mysql_get_client_info() ;
  120. printf( "Client info: %s\n", pszT ) ;
  121. //....
  122. pszT = mysql_get_host_info( myData ) ;
  123. printf( "Host info: %s\n", pszT ) ;
  124. //....
  125. pszT = mysql_get_server_info( myData ) ;
  126. printf( "Server info: %s\n", pszT ) ;
  127. //....
  128. res = mysql_list_processes( myData ) ; l = 1 ;
  129. if (res)
  130. {
  131. for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
  132. strcpy( aszFlds[ x ], fd->name ) ;
  133. while ( row = mysql_fetch_row( res ) ) {
  134. j = mysql_num_fields( res ) ;
  135. printf( "Process #%ld:-\n", l++ ) ;
  136. for ( k = 0 ; k < j ; k++ )
  137. printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
  138. (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
  139. puts( "==============================\n" ) ;
  140. }
  141. }
  142. else
  143. {
  144. printf("Got error %s when retreiving processlist\n",mysql_error(myData));
  145. }
  146. //....
  147. res = mysql_list_tables( myData, "%" ) ; l = 1 ;
  148. for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
  149. strcpy( aszFlds[ x ], fd->name ) ;
  150. while ( row = mysql_fetch_row( res ) ) {
  151. j = mysql_num_fields( res ) ;
  152. printf( "Table #%ld:-\n", l++ ) ;
  153. for ( k = 0 ; k < j ; k++ )
  154. printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
  155. (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
  156. puts( "==============================\n" ) ;
  157. }
  158. //....
  159. pszT = mysql_stat( myData ) ;
  160. puts( pszT ) ;
  161. //....
  162. mysql_close( myData ) ;
  163. return 0 ;
  164. }