[gnome-db] New patch for postgres provider
- From: Gonzalo Paniagua Javier <gonzalo gnome-db org>
- To: Gnome-db list <gnome-db-list gnome org>
- Subject: [gnome-db] New patch for postgres provider
- Date: Wed, 2 Jan 2002 23:52:58 +0100
Here is a new patch for the postgres provider.
I have also made a little change in gda-server-connection.c to
avoid a failling assertion in gda_error_list_free() when there
are no messages in the error list.
gda-test now also tests for procedures and views and shows the
number of procedures, tables, types and views.
Hope you like it!
Index: libgda/ChangeLog
===================================================================
RCS file: /home/gpanjav/gnome-db-2/cvs/libgda/libgda/ChangeLog,v
retrieving revision 1.56
diff -u -r1.56 ChangeLog
--- libgda/ChangeLog 2 Jan 2002 16:27:23 -0000 1.56
+++ libgda/ChangeLog 2 Jan 2002 20:09:58 -0000
@@ -5,6 +5,9 @@
* Makefile.am: s/IDLFILES/IDL_FILES, so that ORBit-generated files
are regenerated when the IDL files change
+ * gda-server-connection.c (gda_server_connection_free_error_list):
+ Test if the error list is NULL to avoid failing assertion in
+ gda_error_list_free().
2001-12-31 Rodrigo Moya <rodrigo gnome-db org>
Index: libgda/gda-server-connection.c
===================================================================
RCS file: /home/gpanjav/gnome-db-2/cvs/libgda/libgda/gda-server-connection.c,v
retrieving revision 1.13
diff -u -r1.13 gda-server-connection.c
--- libgda/gda-server-connection.c 27 Dec 2001 18:04:02 -0000 1.13
+++ libgda/gda-server-connection.c 2 Jan 2002 20:05:09 -0000
@@ -475,6 +475,8 @@
{
g_return_if_fail (GDA_IS_SERVER_CONNECTION (cnc));
- gda_error_list_free (cnc->priv->errors);
- cnc->priv->errors = NULL;
+ if (cnc->priv->errors != NULL){
+ gda_error_list_free (cnc->priv->errors);
+ cnc->priv->errors = NULL;
+ }
}
Index: providers/postgres/ChangeLog
===================================================================
RCS file: /home/gpanjav/gnome-db-2/cvs/libgda/providers/postgres/ChangeLog,v
retrieving revision 1.3
diff -u -r1.3 ChangeLog
--- providers/postgres/ChangeLog 29 Dec 2001 21:01:59 -0000 1.3
+++ providers/postgres/ChangeLog 2 Jan 2002 22:40:21 -0000
@@ -1,3 +1,23 @@
+2002-01-02 Gonzalo Paniagua Javier <gonzalo gnome-db org>
+
+ * gda-postgres-provider.c (process_sql_commands()): don't call
+ PQclear() after storing the resultset. It's done when freeing the
+ resultset.
+ (gda_postgres_provider_get_schema()): added this one and a few helper
+ functions.
+ (gda_postgres_provider_open_connection()): accept REQUIRESSL and
+ HOSTADDR as parameters and changed LOGIN by USER. Now uses
+ PQconnectdb() instead of the old PQsetdbLogin(). Changed default date
+ style to ISO.
+
+ * utils.c (gda_postgres_set_type_value()): GDA_TYPE_DATE now works.
+ Changed the type of the default from GDA_TYPE_UNKNOWN to
+ GDA_TYPE_STRING as a temporary workaround to make TIMESTAMP, TIME and
+ BINARY work.
+
+ * GNOME_Database_Provider_Postgres.server.in: added parameters
+ REQUIRESSL and HOSTADDR. Changed LOGIN by USER.
+
2001-12-26 Gonzalo Paniagua Javier <gonzalo gnome-db org>
* gda-postgres-provider.c: added the call to
Index: providers/postgres/GNOME_Database_Provider_Postgres.server.in
===================================================================
RCS file: /home/gpanjav/gnome-db-2/cvs/libgda/providers/postgres/GNOME_Database_Provider_Postgres.server.in,v
retrieving revision 1.1
diff -u -r1.1 GNOME_Database_Provider_Postgres.server.in
--- providers/postgres/GNOME_Database_Provider_Postgres.server.in 19 Dec 2001 12:54:59 -0000 1.1
+++ providers/postgres/GNOME_Database_Provider_Postgres.server.in 2 Jan 2002 21:13:34 -0000
@@ -19,12 +19,14 @@
_value="GDA Datasource Access for Postgres"/>
<oaf_attribute name="gda_params" type="stringv">
<item value="HOST"/>
+ <item value="HOSTADDR"/>
<item value="DATABASE"/>
<item value="PORT"/>
<item value="OPTIONS"/>
<item value="TTY"/>
- <item value="LOGIN"/>
+ <item value="USER"/>
<item value="PASSWORD"/>
+ <item value="REQUIRESSL"/>
</oaf_attribute>
</oaf_server>
Index: providers/postgres/gda-postgres-provider.c
===================================================================
RCS file: /home/gpanjav/gnome-db-2/cvs/libgda/providers/postgres/gda-postgres-provider.c,v
retrieving revision 1.3
diff -u -r1.3 gda-postgres-provider.c
--- providers/postgres/gda-postgres-provider.c 29 Dec 2001 21:01:59 -0000 1.3
+++ providers/postgres/gda-postgres-provider.c 2 Jan 2002 22:23:00 -0000
@@ -22,6 +22,7 @@
*/
#include "gda-postgres.h"
+#include <libgda/gda-server-recordset-model.h>
#define PARENT_TYPE GDA_TYPE_SERVER_PROVIDER
@@ -65,6 +66,12 @@
static gboolean gda_postgres_provider_supports (GdaServerProvider *provider,
GdaServerConnection *cnc,
GNOME_Database_Feature feature);
+
+static GdaServerRecordset *gda_postgres_provider_get_schema (GdaServerProvider *provider,
+ GdaServerConnection *cnc,
+ GNOME_Database_Connection_Schema schema,
+ GdaParameterList *params);
+
static GObjectClass *parent_class = NULL;
/*
@@ -86,6 +93,7 @@
provider_class->commit_transaction = gda_postgres_provider_commit_transaction;
provider_class->rollback_transaction = gda_postgres_provider_rollback_transaction;
provider_class->supports = gda_postgres_provider_supports;
+ provider_class->get_schema = gda_postgres_provider_get_schema;
}
static void
@@ -141,8 +149,11 @@
const gchar *pq_port;
const gchar *pq_options;
const gchar *pq_tty;
- const gchar *pq_login;
+ const gchar *pq_user;
const gchar *pq_pwd;
+ const gchar *pq_hostaddr;
+ const gchar *pq_requiressl;
+ gchar *conn_string;
PGconn *pconn;
PGresult *pg_res;
GdaError *error;
@@ -154,17 +165,40 @@
/* parse connection string */
pq_host = gda_quark_list_find (params, "HOST");
+ pq_hostaddr = gda_quark_list_find (params, "HOSTADDR");
pq_db = gda_quark_list_find (params, "DATABASE");
pq_port = gda_quark_list_find (params, "PORT");
pq_options = gda_quark_list_find (params, "OPTIONS");
pq_tty = gda_quark_list_find (params, "TTY");
- pq_login = gda_quark_list_find (params, "LOGIN");
+ pq_user = gda_quark_list_find (params, "USER");
pq_pwd = gda_quark_list_find (params, "PASSWORD");
+ pq_requiressl = gda_quark_list_find (params, "REQUIRESSL");
+ conn_string = g_strconcat ( "",
+ pq_host ? "host=" : "",
+ pq_host ? pq_host : "",
+ pq_hostaddr ? " hostaddr=" : "",
+ pq_hostaddr ? pq_hostaddr : "",
+ pq_db ? " dbname=" : "",
+ pq_db ? pq_db : "",
+ pq_port ? " port=" : "",
+ pq_port ? pq_port : "",
+ pq_options ? " options='" : "",
+ pq_options ? pq_options : "",
+ pq_options ? "'" : "",
+ pq_tty ? " tty=" : "",
+ pq_tty ? pq_tty : "",
+ pq_user ? " user=" : "",
+ pq_user ? pq_user : "",
+ pq_pwd ? " password=" : "",
+ pq_pwd ? pq_pwd : "",
+ pq_requiressl ? " requiressl=" : "",
+ pq_requiressl ? pq_requiressl : "",
+ NULL);
/* actually establish the connection */
- pconn = PQsetdbLogin (pq_host, pq_port, pq_options, pq_tty, pq_db,
- pq_login, pq_pwd);
+ pconn = PQconnectdb (conn_string);
+ g_free(conn_string);
if (PQstatus (pconn) != CONNECTION_OK) {
gda_server_connection_add_error (
@@ -174,15 +208,10 @@
}
/*
- * Sets the DATE format for all the current session to DD-MM-YYYY
+ * Sets the DATE format for all the current session to YYYY-MM-DD
*/
- pg_res = PQexec (pconn, "SET DATESTYLE TO 'SQL, US'");
+ pg_res = PQexec (pconn, "SET DATESTYLE TO 'ISO'");
PQclear (pg_res);
- /*
- * the TIMEZONE is left to what is the default, without trying to impose
- * one. Otherwise the command would be:
- * "SET TIME ZONE '???'" or "SET TIME ZONE DEFAULT"
- */
g_object_set_data (G_OBJECT (cnc), OBJECT_DATA_POSTGRES_HANDLE, pconn);
return TRUE;
@@ -203,7 +232,7 @@
if (!pconn)
return FALSE;
- PQfinish (pconn);
+ PQfinish (pconn);
g_object_set_data (G_OBJECT (cnc), OBJECT_DATA_POSTGRES_HANDLE, NULL);
return TRUE;
@@ -224,7 +253,7 @@
/* parse SQL string, which can contain several commands, separated by ';' */
arr = g_strsplit (sql, ";", 0);
if (arr) {
- gint n=0;
+ gint n = 0;
while (arr[n]) {
PGresult *pg_res;
@@ -243,9 +272,6 @@
reclist = g_list_append (reclist, recset);
}
- PQclear(pg_res);
- pg_res = NULL;
-
n++;
}
@@ -342,8 +368,8 @@
result = FALSE;
pg_res = PQexec(pconn, command);
if (pg_res != NULL){
- result = PQresultStatus(pg_res) == PGRES_COMMAND_OK;
- PQclear (pg_res);
+ result = PQresultStatus(pg_res) == PGRES_COMMAND_OK;
+ PQclear (pg_res);
}
if (result == FALSE)
@@ -368,5 +394,149 @@
}
return FALSE;
+}
+
+static GdaServerRecordset *
+get_postgres_procedures (GdaServerConnection *cnc, GdaParameterList *params)
+{
+ GList *reclist;
+ GdaServerRecordset *recset;
+
+ g_return_val_if_fail (GDA_IS_SERVER_CONNECTION (cnc), NULL);
+
+ reclist = process_sql_commands (NULL, cnc,
+ "SELECT proname FROM pg_proc ORDER BY proname");
+
+ if (!reclist)
+ return NULL;
+
+ recset = GDA_SERVER_RECORDSET (reclist->data);
+ g_list_free (reclist);
+
+ return recset;
+}
+
+static GdaServerRecordset *
+get_postgres_tables (GdaServerConnection *cnc, GdaParameterList *params)
+{
+ GList *reclist;
+ GdaServerRecordset *recset;
+
+ g_return_val_if_fail (GDA_IS_SERVER_CONNECTION (cnc), NULL);
+
+ reclist = process_sql_commands (NULL, cnc,
+ "SELECT relname FROM pg_class WHERE relkind = 'r' AND "
+ "relname !~ '^pg_' ORDER BY relname");
+
+ if (!reclist)
+ return NULL;
+
+ recset = GDA_SERVER_RECORDSET (reclist->data);
+ g_list_free (reclist);
+
+ return recset;
+}
+
+static void
+add_string_row (GdaServerRecordsetModel *recset, const gchar *str)
+{
+ GdaValue *value;
+ GList list;
+
+ g_return_if_fail (GDA_IS_SERVER_RECORDSET_MODEL (recset));
+
+ value = gda_value_new_string (str);
+ list.data = value;
+ list.next = NULL;
+ list.prev = NULL;
+
+ gda_server_recordset_model_append_row (recset, &list);
+
+ gda_value_free (value);
+}
+
+static GdaServerRecordset *
+get_postgres_types (GdaServerConnection *cnc, GdaParameterList *params)
+{
+ GdaServerRecordsetModel *recset;
+
+ g_return_val_if_fail (GDA_IS_SERVER_CONNECTION (cnc), NULL);
+
+ /* create the recordset */
+ recset = GDA_SERVER_RECORDSET_MODEL (gda_server_recordset_model_new (cnc, 1));
+ gda_server_recordset_model_set_field_defined_size (recset, 0, 32);
+ gda_server_recordset_model_set_field_name (recset, 0, _("Type"));
+ gda_server_recordset_model_set_field_scale (recset, 0, 0);
+ gda_server_recordset_model_set_field_gdatype (recset, 0, GDA_TYPE_STRING);
+
+ /* fill the recordset */
+ //TODO: Get it from system tables.
+ add_string_row (recset, "abstime");
+ add_string_row (recset, "boolean");
+ add_string_row (recset, "bpchar");
+ add_string_row (recset, "bytea");
+ add_string_row (recset, "char");
+ add_string_row (recset, "date");
+ add_string_row (recset, "datetz");
+ add_string_row (recset, "float4");
+ add_string_row (recset, "float8");
+ add_string_row (recset, "int2");
+ add_string_row (recset, "int4");
+ add_string_row (recset, "int8");
+ add_string_row (recset, "numeric");
+ add_string_row (recset, "reltime");
+ add_string_row (recset, "time");
+ add_string_row (recset, "timetz");
+ add_string_row (recset, "timestamp");
+ add_string_row (recset, "text");
+ add_string_row (recset, "varbit");
+ add_string_row (recset, "varchar");
+
+ return GDA_SERVER_RECORDSET (recset);
+}
+
+static GdaServerRecordset *
+get_postgres_views (GdaServerConnection *cnc, GdaParameterList *params)
+{
+ GList *reclist;
+ GdaServerRecordset *recset;
+
+ g_return_val_if_fail (GDA_IS_SERVER_CONNECTION (cnc), NULL);
+
+ reclist = process_sql_commands (NULL, cnc,
+ "SELECT relname FROM pg_class WHERE relkind = 'v' AND "
+ "relname !~ '^pg_' ORDER BY relname");
+
+ if (!reclist)
+ return NULL;
+
+ recset = GDA_SERVER_RECORDSET (reclist->data);
+ g_list_free (reclist);
+
+ return recset;
+}
+
+/* get_schema handler for the GdaPostgresProvider class */
+static GdaServerRecordset *
+gda_postgres_provider_get_schema (GdaServerProvider *provider,
+ GdaServerConnection *cnc,
+ GNOME_Database_Connection_Schema schema,
+ GdaParameterList *params)
+{
+ g_return_val_if_fail (GDA_IS_SERVER_PROVIDER (provider), NULL);
+ g_return_val_if_fail (GDA_IS_SERVER_CONNECTION (cnc), NULL);
+
+ switch (schema) {
+ case GNOME_Database_Connection_SCHEMA_PROCEDURES :
+ return get_postgres_procedures (cnc, params);
+ case GNOME_Database_Connection_SCHEMA_TABLES :
+ return get_postgres_tables (cnc, params);
+ case GNOME_Database_Connection_SCHEMA_TYPES :
+ return get_postgres_types (cnc, params);
+ case GNOME_Database_Connection_SCHEMA_VIEWS :
+ return get_postgres_views (cnc, params);
+ }
+
+ return NULL;
}
Index: providers/postgres/utils.c
===================================================================
RCS file: /home/gpanjav/gnome-db-2/cvs/libgda/providers/postgres/utils.c,v
retrieving revision 1.3
diff -u -r1.3 utils.c
--- providers/postgres/utils.c 29 Dec 2001 21:01:59 -0000 1.3
+++ providers/postgres/utils.c 2 Jan 2002 22:45:40 -0000
@@ -88,6 +88,8 @@
void
gda_postgres_set_type_value (GdaField *field, GdaType type, const gchar *value)
{
+ GDate *date;
+
switch (type) {
case GDA_TYPE_BOOLEAN :
gda_field_set_gdatype (field, type);
@@ -117,17 +119,26 @@
gda_field_set_gdatype (field, type);
gda_field_set_double_value (field, atof (value));
break;
- case GDA_TYPE_TIMESTAMP : //FIXME
- break;
- case GDA_TYPE_DATE : //FIXME
+ case GDA_TYPE_DATE :
+ gda_field_set_gdatype (field, type);
+ date = g_date_new ();
+ g_date_set_parse (date, value);
+ if (!g_date_valid (date)) {
+ g_warning ("Could not parse '%s' "
+ "Setting date to 01/01/0001!\n", field);
+ g_date_clear (date, 1);
+ g_date_set_dmy (date, 1, 1, 1);
+ }
+ gda_field_set_date_value (field, date);
+ g_date_free (date);
+
break;
+ case GDA_TYPE_TIMESTAMP : //FIXME
case GDA_TYPE_TIME : //FIXME
- break;
case GDA_TYPE_BINARY : //FIXME
- break;
default :
gda_field_set_string_value (field, value);
- gda_field_set_gdatype (field, GDA_TYPE_UNKNOWN);
+ gda_field_set_gdatype (field, GDA_TYPE_STRING);
}
}
Index: testing/ChangeLog
===================================================================
RCS file: /home/gpanjav/gnome-db-2/cvs/libgda/testing/ChangeLog,v
retrieving revision 1.18
diff -u -r1.18 ChangeLog
--- testing/ChangeLog 30 Dec 2001 21:17:13 -0000 1.18
+++ testing/ChangeLog 2 Jan 2002 22:48:14 -0000
@@ -1,3 +1,8 @@
+2002-01-02 Gonzalo Paniagua Javier <gonzalo gnome-db org>
+
+ * client.c: also lists procedures and views (if any). Shows the number
+ of procedures, tables, types and views.
+
2001-12-30 Rodrigo Moya <rodrigo gnome-db org>
* client.c (show_schemas): added types schema
Index: testing/client.c
===================================================================
RCS file: /home/gpanjav/gnome-db-2/cvs/libgda/testing/client.c,v
retrieving revision 1.7
diff -u -r1.7 client.c
--- testing/client.c 30 Dec 2001 21:17:13 -0000 1.7
+++ testing/client.c 2 Jan 2002 22:14:43 -0000
@@ -30,16 +30,17 @@
gint r, c;
gint row_count, col_count;
- g_print ("\t%s\n", label);
-
model = gda_connection_get_schema (cnc, schema, NULL);
if (!GDA_IS_DATA_MODEL (model)) {
+ g_print ("\t%s\n", label);
g_print ("\t\tNONE\n");
return;
}
row_count = gda_data_model_get_n_rows (model);
col_count = gda_data_model_get_n_columns (model);
+ g_print ("\t%s %d\n", label, row_count);
+
for (r = 0; r < row_count; r++) {
g_print ("\t");
for (c = 0; c < col_count; c++) {
@@ -81,8 +82,10 @@
_("Supported") : _("Not supported"));
/* show connection schemas */
+ show_schema (cnc, GDA_CONNECTION_SCHEMA_PROCEDURES, "Stored procedures");
show_schema (cnc, GDA_CONNECTION_SCHEMA_TABLES, "Connection Tables");
show_schema (cnc, GDA_CONNECTION_SCHEMA_TYPES, "Available types");
+ show_schema (cnc, GDA_CONNECTION_SCHEMA_VIEWS, "Views");
/* test transactions */
g_print ("\tStarting transaction...");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]