/* Build like so:
 * gcc test_libgda_unquoted_as.c `pkg-config libgda-5.0 --cflags --libs`
 */

#include <libgda/libgda.h>
#include <string.h>

GdaConnection* conn = NULL;


int main()
{
  gda_init();

  GError* error = NULL;
  conn = gda_connection_open_from_string("PostgreSQL",
    "HOST=localhost;PORT=5433;DB_NAME=glom_musiccollection211",
    "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;
    }

/* Generate SQL like this:	
  SELECT "songs"."song_id", "songs"."name" FROM "songs" WHERE ("songs"."name" = "something")
*/

  if (error) {
    g_warning("ggda_connection_update_meta_store() failed: %s\n", error->message);
    g_clear_error(&error);
    return 0;
  }

  GdaSqlBuilder* builder = gda_sql_builder_new(GDA_SQL_STATEMENT_SELECT);
  const guint target_songs = gda_sql_builder_select_add_target(builder, "songs", NULL);
  
  GValue value;
  memset( &value, 0, sizeof(GValue) );
  g_value_init(&value, G_TYPE_STRING);
  g_value_set_string(&value, "something");

  gda_sql_builder_select_add_field(builder, "song_id", "songs", NULL);
  gda_sql_builder_select_add_field(builder, "name", "songs", NULL);

  gda_sql_builder_set_where(builder, 
    gda_sql_builder_add_cond(
      builder,
      GDA_SQL_OPERATOR_TYPE_EQ,
      gda_sql_builder_add_field_id(builder, "name", "songs"),
      gda_sql_builder_add_expr(builder, NULL, G_TYPE_STRING, "something"),
      0));
  
  GdaStatement* stmt = gda_sql_builder_get_statement(builder, &error);
  if (error) {
		g_warning("gda_sql_builder_get_statement() failed: %s\n", error->message);
		g_clear_error(&error);
		return 0;
	}

  GdaDataModel* model = gda_connection_statement_execute_select(
    conn, stmt, NULL, &error);
  if (error) {
    g_warning("gda_connection_statement_execute_select() failed: %s\n", error->message);
    g_clear_error(&error);
    return 0;
  }

  g_object_unref(model);
  
  return 0;
}
