/*
 * This program demonstrates how the mysql_list_fields() call 
 * (as used by myodbc as called from OpenOffice.org)
 * fails to get the fields from the server.
 * It fails with MySQL server versions 4.0.21 through 4.1.7 (current version).
 * It works with all earlier versions of the server.
 *
 * Test case by Stephen Gildea, November 2004.
 */

#include <mysql/mysql.h>
#include <stdio.h>

/* modify these values to suit your database */
static char host[] = "192.168.42.109";
static char user[] = "root";
static char password[] = "";
static char dbname[] = "test";
static char table[] = "t";

int main()
{
  MYSQL *db_conn;
  MYSQL_RES *db_results;
  int num_fields;

  db_conn = mysql_init(NULL);
  mysql_real_connect(db_conn, host, user, password, dbname, 0, NULL, 0);
  if (mysql_errno(db_conn) != 0) {
    printf("mysql_real_connect failed: %s\n", mysql_error(db_conn));
    return 3;
  }

  /* without this query, the following list_fields
     fails even against older servers */
  mysql_query(db_conn, "show tables like '%'");
  db_results = mysql_store_result(db_conn);
  mysql_free_result(db_results);

  /*
   * MySQL server 4.0.21 or newer will erroneously return 0 fields
   * to this mysql_list_fields call.
   */
  db_results = mysql_list_fields(db_conn, table, "%");
  if (db_results == NULL) {
    printf("mysql_list_fields failed: %s\n", mysql_error(db_conn));
    return 1;
  }

  num_fields = mysql_num_fields(db_results);
  if (num_fields == 0) {
    printf("FAILURE!  MySQL server failed to return any fields to mysql_list_fields\n");
    return 2;
  } 

  printf("SUCCESS!  mysql_list_fields found %d fields\n", num_fields);
  return 0;
}

