/* Build with:
 * gcc ./test_value_count.c `pkg-config libgda-5.0 --libs --cflags`
 */

#include <libgda/libgda.h>

GdaConnection* conn = NULL;


int main()
{
	gda_init();

	GError* error = NULL;
	conn = gda_connection_open_from_string("PostgreSQL",
		"HOST=localhost;PORT=5434;DB_NAME=glom_repositoryanalyzer3121",
		"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;
	}
	
	error = 0;
	GdaDataModel* data_model = 
	  gda_connection_execute_select_command(
	    conn,
	    "SELECT COUNT (*) FROM (SELECT scans.scan_id FROM scans) AS glomarbitraryalias",
	    &error);
	if(error) {
	  g_warning("Error executing SQL: %s\n", error->message);
	  g_clear_error(&error);
	}
	
	g_print("Rows count: %d\n", gda_data_model_get_n_rows(data_model));
  g_print("Columns count: %d\n", gda_data_model_get_n_columns(data_model));
  
  const GValue* count_value = gda_data_model_get_value_at (data_model, 0, 0, &error);
  if(error) {
	  g_warning("Error getting value: %s\n", error->message);
	  g_clear_error(&error);
	}
	
	//This usually holds a gint64.
	g_print("Result gtype=%s\n", G_VALUE_TYPE_NAME(count_value));
	
  gint count = g_value_get_int64 (count_value);
  g_print("Result: %d\n", count);
  
	
	gda_connection_close(conn);

	return 0;
}
