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

#include <libgda/libgda.h>
#include <libgda/sql-parser/gda-sql-parser.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_example_smallbusiness_v2",
    "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;
  }

  gchar* id = gda_connection_quote_sql_identifier(conn, "tablenamewith\"inmiddle");
  gchar* query = g_strdup_printf("CREATE TABLE %s (somefield numeric PRIMARY KEY, someotherfield varchar)", 
    id);
  g_free(id);

  GdaSqlParser* parser = gda_connection_create_parser(conn);
  if(!parser)
  {
    g_warning("gda_connection_create_parser() failed");
    return 0;
  }

  GdaStatement* stmt = gda_sql_parser_parse_string(parser, query, NULL, &error);
  if (error) {
    g_warning("gda_sql_parser_parse_string() failed: %s\n", error->message);
    g_clear_error(&error);
    return 0;
  }

  g_free(query);

  gint rows_affected = gda_connection_statement_execute_non_select(
    conn, stmt, NULL, NULL, &error);
  if (rows_affected == -1) {
    g_warning("gda_connection_statement_execute() failed: %s\n", error->message);
    g_clear_error(&error);
    return 0;
  }

  return 0;
}
