#include <libgda/libgda.h>
#include <sql-parser/gda-sql-parser.h>

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

	gchar* quoted;
	if(gda_sql_identifier_needs_quotes(table_name))
		quoted = gda_sql_identifier_add_quotes(table_name);
	else
		quoted = g_strdup(table_name);

	gda_init();

	GdaConnection* conn = gda_connection_open_from_string("SQLite", g_strdup_printf("DB_DIR=.;DB_NAME=%s", table_name), "", GDA_CONNECTION_OPTIONS_NONE, NULL);
	GdaServerProvider* provider = gda_connection_get_provider(conn);

	GdaServerOperation* operation = gda_server_provider_create_operation(provider, conn, GDA_SERVER_OPERATION_CREATE_TABLE, NULL, NULL);

	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, NULL);

	GValue table_name_value = { 0 };
	g_value_init(&table_name_value, G_TYPE_STRING);
	g_value_set_static_string(&table_name_value, quoted);

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

	GdaMetaContext mcontext = { (gchar*)"_tables", 1, column_names, column_values };
	gda_connection_update_meta_store(conn, &mcontext, NULL);

	printf("Quoted:\n");
	GdaDataModel* data = gda_connection_get_meta_store_data(conn, GDA_CONNECTION_META_TABLES, NULL, 0);
	gda_data_model_dump(data, stdout);

	printf("Unquoted:\n");
	g_value_set_static_string(&table_name_value, table_name);
	gda_connection_update_meta_store(conn, &mcontext, NULL);

	data = gda_connection_get_meta_store_data(conn, GDA_CONNECTION_META_TABLES, NULL, 0);
	gda_data_model_dump(data, stdout);

#if 0
	GdaHolder* holder = gda_holder_new_string("name", quoted);
	GList* list = NULL;
	list = g_list_prepend(list, holder);

	GError* error = NULL;
	GdaDataModel* field_model = gda_connection_get_meta_store_data_v(conn, GDA_CONNECTION_META_FIELDS, list, &error);
	if(error)
		printf("%s\n", error->message);
	gda_data_model_dump(field_model, stdout);
#endif
	return 0;
}
