#include <libgda/libgda.h>

int main()
{
	char table_name[100];
	printf("Table Name: ");
	fflush(stdout);
	scanf("%s", table_name); 

	gda_init();

  GError* error = NULL;
  
	GdaConnection* conn = gda_connection_open_from_string("SQLite", g_strdup_printf("DB_DIR=.;DB_NAME=%s", table_name), "", GDA_CONNECTION_OPTIONS_SQL_IDENTIFIERS_CASE_SENSITIVE, &error);
	/*
	GdaConnection* conn = gda_connection_open_from_string("PostgreSQL",
    "HOST=localhost;PORT=5433;DB_NAME=glom_musiccollection21",
    "USERNAME=glom_default_developer_user;PASSWORD=glom_default_developer_password",
    GDA_CONNECTION_OPTIONS_SQL_IDENTIFIERS_CASE_SENSITIVE,
    &error);
  */
  if(error)
	{
	  g_warning("gda_connection_open_from_string() failed: %s\n", error->message);
	  g_clear_error(&error);
	  return 0;
	}
	
	
	GdaServerProvider* provider = gda_connection_get_provider(conn);

	GdaServerOperation* operation = gda_server_provider_create_operation(provider, conn, GDA_SERVER_OPERATION_CREATE_TABLE, NULL, &error);
  if(error)
	{
	  g_warning("create_operation(CREATE_TABLE) failed: %s\n", error->message);
	  g_clear_error(&error);
	}

	gda_server_operation_set_value_at(operation, table_name, NULL, "/TABLE_DEF_P/TABLE_NAME");
	gda_server_operation_set_value_at(operation, "id", NULL, "/FIELDS_A/@COLUMN_NAME/0");
	gda_server_operation_set_value_at(operation, "int", NULL, "/FIELDS_A/@COLUMN_TYPE/0");
	gda_server_operation_set_value_at(operation, "TRUE", NULL, "/FIELDS_A/@COLUMN_PKEY/0");

	gda_server_provider_perform_operation(provider, conn, operation, &error);
	if(error)
	{
	  g_warning("perform_operation(CREATE_TABLE) failed: %s\n", error->message);
	  g_clear_error(&error);
	  return 0;
	}

	gda_connection_update_meta_store(conn, NULL, NULL);
	

	GdaDataModel* data = gda_connection_get_meta_store_data(conn, GDA_CONNECTION_META_TABLES, NULL, 0);
	gda_data_model_dump(data, stdout);
  
  /* List the fields for this table: */
  GList* list = NULL;
  list = g_list_prepend(list, gda_holder_new_string("name", table_name));

  GdaDataModel* datamodel_fields = gda_connection_get_meta_store_data_v(conn, 
    GDA_CONNECTION_META_FIELDS, list,
    &error);
  if(error)
	{
	  g_warning("get_meta_store_data() failed: %s\n", error->message);
	  g_clear_error(&error);
	  return 0;
	}
	
  gda_data_model_dump(datamodel_fields, stdout);
  
	return 0;
}
