[gnome-db] Patch for postgres provider: improved performance for type mapping
- From: Gonzalo Paniagua Javier <gonzalo gnome-db org>
- To: Gnome-db list <gnome-db-list gnome org>
- Subject: [gnome-db] Patch for postgres provider: improved performance for type mapping
- Date: Wed, 20 Mar 2002 14:53:48 +0100
Based on profiling information, I improved the type mapping.
Please, review this patch. As always, any comments appreciated.
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/libgda/ChangeLog,v
retrieving revision 1.171
diff -u -r1.171 ChangeLog
--- ChangeLog 20 Mar 2002 04:39:18 -0000 1.171
+++ ChangeLog 20 Mar 2002 13:47:57 -0000
@@ -1,5 +1,13 @@
2002-03-20 Gonzalo Paniagua Javier <gonzalo gnome-db org>
+ * providers/postgres/gda-postgres-provider.[ch]:
+ * providers/postgres/gda-postgres-recordset.c:
+ * providers/postgres/gda-postgres.h:
+ * providers/postgres/utils.c: improved performance for type mapping
+ between PostgreSQL OIDs and GdaType. Fixed a couple of memory leaks.
+
+2002-03-20 Gonzalo Paniagua Javier <gonzalo gnome-db org>
+
* libgda/gda-value.c (gda_value_stringify): added support for
GDA_TYPE_NUMERIC.
* providers/postgres/gda-postgres-provider.c: added support for
Index: providers/postgres/gda-postgres-provider.c
===================================================================
RCS file: /cvs/gnome/libgda/providers/postgres/gda-postgres-provider.c,v
retrieving revision 1.18
diff -u -r1.18 gda-postgres-provider.c
--- providers/postgres/gda-postgres-provider.c 20 Mar 2002 04:39:52 -0000 1.18
+++ providers/postgres/gda-postgres-provider.c 20 Mar 2002 13:47:57 -0000
@@ -156,9 +156,43 @@
return type;
}
+static GdaType
+postgres_name_to_gda_type (const gchar *name)
+{
+ if (!strcmp (name, "bool"))
+ return GDA_TYPE_BOOLEAN;
+ else if (!strcmp (name, "int8"))
+ return GDA_TYPE_BIGINT;
+ else if (!strcmp (name, "int4") || !strcmp (name, "abstime") || !strcmp (name, "oid"))
+ return GDA_TYPE_INTEGER;
+ else if (!strcmp (name, "int2"))
+ return GDA_TYPE_SMALLINT;
+ else if (!strcmp (name, "float4"))
+ return GDA_TYPE_SINGLE;
+ else if (!strcmp (name, "float8"))
+ return GDA_TYPE_DOUBLE;
+ else if (!strcmp (name, "numeric"))
+ return GDA_TYPE_NUMERIC;
+ else if (!strncmp (name, "timestamp", 9))
+ return GDA_TYPE_TIMESTAMP;
+ else if (!strcmp (name, "date"))
+ return GDA_TYPE_DATE;
+ else if (!strncmp (name, "time", 4))
+ return GDA_TYPE_TIME;
+ else if (!strcmp (name, "point"))
+ return GDA_TYPE_GEOMETRIC_POINT;
+ /*TODO: by now, this one not supported
+ if (!strncmp (name, "bit", 3))
+ return GDA_TYPE_BINARY;
+ */
+
+ return GDA_TYPE_STRING;
+}
+
static int
get_connection_type_list (GdaPostgresConnectionData *priv_td)
{
+ GHashTable *h_table;
GdaPostgresTypeOid *td;
PGresult *pg_res;
gint nrows, i;
@@ -176,15 +210,19 @@
}
nrows = PQntuples (pg_res);
- td = g_new0 (GdaPostgresTypeOid, nrows);
+ td = g_new (GdaPostgresTypeOid, nrows);
+ h_table = g_hash_table_new (g_str_hash, g_str_equal);
for (i = 0; i < nrows; i++) {
td[i].name = g_strdup (PQgetvalue (pg_res, i, 1));
td[i].oid = atoi (PQgetvalue (pg_res, i, 0));
+ td[i].type = postgres_name_to_gda_type (td[i].name);
+ g_hash_table_insert (h_table, td[i].name, &td[i].type);
}
PQclear (pg_res);
priv_td->ntypes = nrows;
priv_td->type_data = td;
+ priv_td->h_table = h_table;
return 0;
}
@@ -266,7 +304,7 @@
pg_res = PQexec (pconn, "SET DATESTYLE TO 'ISO'");
PQclear (pg_res);
- priv_data = g_new0 (GdaPostgresConnectionData, 1);
+ priv_data = g_new (GdaPostgresConnectionData, 1);
priv_data->pconn = pconn;
if (get_connection_type_list (priv_data) != 0) {
gda_server_connection_add_error (
@@ -301,6 +339,7 @@
for (i = 0; i < priv_data->ntypes; i++)
g_free (priv_data->type_data[i].name);
+ g_hash_table_destroy (priv_data->h_table);
g_free (priv_data->type_data);
g_free (priv_data);
@@ -729,7 +768,7 @@
nidx = PQntuples (pg_idx);
- idx_data = g_new0 (GdaPostgresIdxData, nidx);
+ idx_data = g_new (GdaPostgresIdxData, nidx);
for (i = 0; i < nidx; i++){
gchar **arr, **ptr;
gint ncolumns;
@@ -753,11 +792,12 @@
ncolumns++;
idx_data[i].ncolumns = ncolumns;
- idx_data[i].columns = g_new0 (gint, ncolumns);
+ idx_data[i].columns = g_new (gint, ncolumns);
for (k = 0; k < ncolumns; k++)
idx_data[i].columns[k] = atoi (arr[k]) - 1;
idx_list = g_list_append (idx_list, &idx_data[i]);
+ g_strfreev (arr);
}
PQclear (pg_idx);
@@ -815,7 +855,7 @@
nref = PQntuples (pg_ref);
- ref_data = g_new0 (GdaPostgresRefData, nref);
+ ref_data = g_new (GdaPostgresRefData, nref);
for (i = 0; i < nref; i++){
gchar **arr;
gchar *value;
@@ -827,6 +867,8 @@
ref_data[i].reference = g_strdup_printf ("%s.%s", arr[2], arr[5]);
ref_list = g_list_append (ref_list, &ref_data[i]);
}
+
+ g_strfreev (arr);
}
PQclear (pg_ref);
@@ -905,7 +947,8 @@
/* Data type */
thevalue = PQgetvalue(pg_res, i, 1);
- type = gda_postgres_type_name_to_gda (thevalue);
+ type = gda_postgres_type_name_to_gda (priv_data->h_table,
+ thevalue);
value = gda_value_new_integer (type);
rowlist = g_list_append (rowlist, value);
Index: providers/postgres/gda-postgres-provider.h
===================================================================
RCS file: /cvs/gnome/libgda/providers/postgres/gda-postgres-provider.h,v
retrieving revision 1.3
diff -u -r1.3 gda-postgres-provider.h
--- providers/postgres/gda-postgres-provider.h 17 Mar 2002 23:02:18 -0000 1.3
+++ providers/postgres/gda-postgres-provider.h 20 Mar 2002 13:47:57 -0000
@@ -53,12 +53,14 @@
typedef struct {
gchar *name;
Oid oid;
+ GdaType type;
} GdaPostgresTypeOid;
typedef struct {
PGconn *pconn;
gint ntypes;
GdaPostgresTypeOid *type_data;
+ GHashTable *h_table;
} GdaPostgresConnectionData;
Index: providers/postgres/gda-postgres-recordset.c
===================================================================
RCS file: /cvs/gnome/libgda/providers/postgres/gda-postgres-recordset.c,v
retrieving revision 1.8
diff -u -r1.8 gda-postgres-recordset.c
--- providers/postgres/gda-postgres-recordset.c 17 Mar 2002 23:02:18 -0000 1.8
+++ providers/postgres/gda-postgres-recordset.c 20 Mar 2002 13:47:57 -0000
@@ -33,6 +33,7 @@
PGresult *pg_res;
gint ntypes;
GdaPostgresTypeOid *type_data;
+ GHashTable *h_table;
} GdaPostgresRecordsetPrivate;
/*
@@ -42,10 +43,12 @@
static void
free_postgres_res (gpointer data)
{
- PGresult *pg_res = (PGresult *) data;
+ GdaPostgresRecordsetPrivate *priv_data = (GdaPostgresRecordsetPrivate *) data;
- g_return_if_fail (pg_res != NULL);
- PQclear (pg_res);
+ g_return_if_fail (priv_data != NULL);
+
+ PQclear (priv_data->pg_res);
+ g_free (priv_data);
}
static GdaRow *
@@ -172,10 +175,11 @@
cnc_priv_data = g_object_get_data (G_OBJECT (cnc), OBJECT_DATA_POSTGRES_HANDLE);
- priv_data = g_new0 (GdaPostgresRecordsetPrivate, 1);
+ priv_data = g_new (GdaPostgresRecordsetPrivate, 1);
priv_data->pg_res = pg_res;
priv_data->ntypes = cnc_priv_data->ntypes;
priv_data->type_data = cnc_priv_data->type_data;
+ priv_data->h_table = cnc_priv_data->h_table;
g_object_set_data_full (G_OBJECT (recset), OBJECT_DATA_RECSET_HANDLE,
priv_data, (GDestroyNotify) free_postgres_res);
Index: providers/postgres/gda-postgres.h
===================================================================
RCS file: /cvs/gnome/libgda/providers/postgres/gda-postgres.h,v
retrieving revision 1.6
diff -u -r1.6 gda-postgres.h
--- providers/postgres/gda-postgres.h 17 Mar 2002 23:02:18 -0000 1.6
+++ providers/postgres/gda-postgres.h 20 Mar 2002 13:47:57 -0000
@@ -50,7 +50,7 @@
GdaType gda_postgres_type_oid_to_gda (GdaPostgresTypeOid *type_data,
gint ntypes, Oid postgres_type);
-GdaType gda_postgres_type_name_to_gda (const gchar *name);
+GdaType gda_postgres_type_name_to_gda (GHashTable *h_table, const gchar *name);
G_END_DECLS
Index: providers/postgres/utils.c
===================================================================
RCS file: /cvs/gnome/libgda/providers/postgres/utils.c,v
retrieving revision 1.14
diff -u -r1.14 utils.c
--- providers/postgres/utils.c 20 Mar 2002 04:39:52 -0000 1.14
+++ providers/postgres/utils.c 20 Mar 2002 13:47:57 -0000
@@ -47,34 +47,13 @@
}
GdaType
-gda_postgres_type_name_to_gda (const gchar *name)
+gda_postgres_type_name_to_gda (GHashTable *h_table, const gchar *name)
{
- if (!strcmp (name, "bool"))
- return GDA_TYPE_BOOLEAN;
- if (!strcmp (name, "int8"))
- return GDA_TYPE_BIGINT;
- if (!strcmp (name, "int4") || !strcmp (name, "abstime") || !strcmp (name, "oid"))
- return GDA_TYPE_INTEGER;
- if (!strcmp (name, "int2"))
- return GDA_TYPE_SMALLINT;
- if (!strcmp (name, "float4"))
- return GDA_TYPE_SINGLE;
- if (!strcmp (name, "float8"))
- return GDA_TYPE_DOUBLE;
- if (!strcmp (name, "numeric"))
- return GDA_TYPE_NUMERIC;
- if (!strncmp (name, "timestamp", 9))
- return GDA_TYPE_TIMESTAMP;
- if (!strcmp (name, "date"))
- return GDA_TYPE_DATE;
- if (!strncmp (name, "time", 4))
- return GDA_TYPE_TIME;
- /*TODO: by now, this one not supported
- if (!strncmp (name, "bit", 3))
- return GDA_TYPE_BINARY;
- */
- if (!strcmp (name, "point"))
- return GDA_TYPE_GEOMETRIC_POINT;
+ GdaType *type;
+
+ type = g_hash_table_lookup (h_table, name);
+ if (type)
+ return *type;
return GDA_TYPE_STRING;
}
@@ -91,7 +70,7 @@
if (type_data[i].oid != postgres_type)
return GDA_TYPE_STRING;
- return gda_postgres_type_name_to_gda (type_data[i].name);
+ return type_data[i].type;
}
/* Makes a point from a string like "(3.2,5.6)" */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]