/*
 * 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.
 */

/*

DROP TABLE IF EXISTS `foo`;
CREATE TABLE `foo` (
  `RecId` int(10) unsigned NOT NULL auto_increment,
  `Results` longblob NOT NULL,
  `vcField` varchar(255) default NULL,
  PRIMARY KEY  (`RecId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO Foo VALUES (NULL, COMPRESS(repeat("MySQL12345", 10)), COMPRESS(repeat("MySQL12345", 10)));

*/


#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#include <windows.h>
#endif

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

/* modify these values to suit your database */
static char host[] = "localhost";
static char user[] = "root";
static char password[] = "";
static char dbname[] = "thoga";
static char table[] = "Foo";

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

  MYSQL_FIELD *field;

  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 */


  /*

  LOOK HERE: Mysql function UNCOMPRESS return MYSQL_TYPE_VAR_STRING for all types of fields either BLOB or VARCHAR...

  */
  mysql_query(db_conn, "SELECT RecId, Results, Uncompress(Results) as unpacked_blob,  Uncompress(vcField) as unpacked_varchar FROM Foo");
  db_results = mysql_store_result(db_conn);
  while((field = mysql_fetch_field(db_results)))
  {
	  printf("field name %s\n", field->name);
	  printf("field size %d\n", field->length);
	  printf("field max size %d\n", field->max_length);
	  printf("field type ");
	  switch(field->type)
	  {
		  case MYSQL_TYPE_BLOB: printf ("MYSQL_TYPE_BLOB");break;
		  case MYSQL_TYPE_VAR_STRING: printf ("MYSQL_TYPE_VAR_STRING");break;
		  case MYSQL_TYPE_VARCHAR: printf ("MYSQL_TYPE_VARCHAR");break;
		  case MYSQL_TYPE_LONG: printf ("MYSQL_TYPE_LONG");break;
		  default:
			  printf("OTHER TYPE");

	  }
	  printf("\n\n");
  }
  mysql_free_result(db_results);

  /*
   * MySQL server 4.0.21 or newer will erroneously return 0 fields
   * to this mysql_list_fields call.
   */
  return 0;
}

