#include <libgda/libgda.h>

GdaConnection* conn = NULL;

void update_meta_store_for_table(const gchar* table_name)
{
	/* Update the meta store for just this table: */
        gchar *table_name_quoted = gda_meta_store_sql_identifier_quote (table_name, conn);

        /* Make it faster: */
        const gchar* schema_name = "public"; /* "public" for postgresql. "main" for sqlite. */ 
  
	GValue table_name_value = { 0 };
	g_value_init(&table_name_value, G_TYPE_STRING);
	g_value_set_static_string(&table_name_value, table_name_quoted);

	GValue table_schema_value = { 0 };
	g_value_init(&table_schema_value, G_TYPE_STRING);
	g_value_set_static_string(&table_schema_value, schema_name);

	gchar* column_names[] = { (gchar*)"table_name" , (gchar*)"table_schema"};
	GValue* column_values[] = { &table_name_value, &table_schema_value };

	GdaMetaContext mcontext = {(gchar*)"_tables", 2, column_names, column_values };
	GError* error = 0;

        /* This fails: */
	gda_connection_update_meta_store(conn, &mcontext, &error);

	if(error) {
		g_warning("update_meta_store(table) failed: %s\n", error->message);
		g_clear_error(&error);
	}
	
	g_value_unset(&table_name_value);
	g_free(table_name_quoted);
}

int main()
{
	char table_name[100];
	printf("Table Name to create and use: ");
	fflush(stdout);
	scanf("%s", table_name);
	g_print ("Table name: %s\n", table_name);

	gda_init();

	GError* error = NULL;
  	/*
	conn = gda_connection_open_from_string("SQLite", 
		g_strdup_printf("DB_DIR=.;DB_NAME=%s", table_name), NULL,
		GDA_CONNECTION_OPTIONS_SQL_IDENTIFIERS_CASE_SENSITIVE, &error);
	*/
	conn = gda_connection_open_from_string("PostgreSQL",
		"HOST=localhost;PORT=5432;DB_NAME=testdb",
		"USERNAME=murrayc",
		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;
	}
	
	GdaMetaStore *mstore = gda_meta_store_new_with_file ("store.db");
	g_object_set (G_OBJECT (conn), "meta-store", mstore, NULL);
	g_object_unref (mstore);
	
	/* create table */
	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, "TheId", 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;
	}

	/* update meta store */
        /* This works, other than this warning:
         * Implementation missing: _gda_postgres_meta__enums() in gda-postgres-meta.c line 543
         *
	g_print ("========= Updating the whole meta store:\n");
	gda_connection_update_meta_store (conn, NULL, NULL);
        */
	
	/* display all tables */
        /*
	g_print ("========= ALL tables:\n");
	GdaDataModel* data = gda_connection_get_meta_store_data (conn, GDA_CONNECTION_META_TABLES, NULL, 0);
	gda_data_model_dump(data, stdout);
        */

	update_meta_store_for_table(table_name);

	return 0;
}
