PostgreSQL doesn't accept glib types CREATE TABLE operation
I was working on #157 and found a very strange behavior. Specifically, I had to provide DBMS specific type for PostgreSQL provider CREATE TABLE operation. For SQLite3 it wasn't necessary and common Glib types, e.g. string, gint, gchararray worked just fine. While for postgresql I had to provide varchar or text types. I was using docker image for different versions 10 and 11 to test. This is a code I used for testing:
#include <libgda/libgda.h>
#define GDA_PGSQL_ERROR_HANDLE(e) (g_print("Error: %s: %s\n", G_STRLOC, e && e->message ? e->message : "No default"))
int main()
{
GError *error = NULL;
GdaConnection *cnc = NULL;
GdaServerOperation *so = NULL;
gchar *sql = NULL;
cnc = gda_connection_new_from_string("PostgreSQL",
"HOST=localhost;DB_NAME=test;USERNAME=test;PASSWORD=test1",
NULL,
GDA_CONNECTION_OPTIONS_NONE,
&error);
if (!cnc)
{
GDA_PGSQL_ERROR_HANDLE(error);
goto on_error;
}
if (!gda_connection_open(cnc, &error))
{
GDA_PGSQL_ERROR_HANDLE(error);
goto on_error;
}
else
g_print("connection has been opened successfully\n");
so = gda_connection_create_operation(cnc, GDA_SERVER_OPERATION_CREATE_TABLE, NULL, &error);
if (!so)
{
GDA_PGSQL_ERROR_HANDLE(error);
goto on_error;
}
else
g_print("Server operation has been created successfully\n");
if (!gda_server_operation_set_value_at(so, "Gnome", &error, "/TABLE_DEF_P/TABLE_NAME"))
{
GDA_PGSQL_ERROR_HANDLE(error);
goto on_error;
}
if (!gda_server_operation_set_value_at(so, "gnome_id", &error, "/FIELDS_A/@COLUMN_NAME/%d",0))
{
GDA_PGSQL_ERROR_HANDLE(error);
goto on_error;
}
if (!gda_server_operation_set_value_at(so, "gint", &error, "/FIELDS_A/@COLUMN_TYPE/%d",0))
{
GDA_PGSQL_ERROR_HANDLE(error);
goto on_error;
}
sql = gda_server_operation_render (so, &error);
if (!sql)
{
GDA_PGSQL_ERROR_HANDLE(error);
goto on_error;
}
else
{
g_print("Server Operation was rendered successfully\n");
g_print("SQL SO: %s\n", sql);
}
GdaServerProvider *provider = gda_connection_get_provider(cnc);
if (!gda_server_provider_perform_operation(provider, cnc, so, &error))
{
GDA_PGSQL_ERROR_HANDLE(error);
abort();
}
if (gda_connection_is_opened(cnc))
gda_connection_close(cnc, NULL);
on_error:
if (cnc) g_object_unref (cnc);
if (sql) g_free (sql);
if (so) g_object_unref (so);
return 0;
}
the output was:
Loading providers in <path to providers>/providers
File's name checking for provider: libgda-postgres-6.0.so
Loaded 'PostgreSQL' provider
File's name checking for provider: libgda-ldap-6.0.so
Loaded 'Ldap' provider
File's name checking for provider: libgda-sqlite-6.0.so
Loaded 'SQLite' provider
File's name checking for provider: libgda-sqlcipher-6.0.so
Loaded 'SQLCipher' provider
connection has been opened succesfully
Server operation has been created succesfully
Server Operation was rendered sucessfully
SQL SO: CREATE TABLE Gnome (gnome_id gint) WITH OIDS
Error: test-pgsql.c:78: type "gint" does not exist at character 30
Aborted
As can be seen, the "Error: ..." message comes from the PostgreSQL C API. If I put SQlite provider everything looks ok, and local db generated using Glib types.
Edited by Pavlo Solntsev