Re: [gnome-db] libgda escape_value() and schema retreival



Something like the attached(hopefully) patch is what I was thinking.

Regards
Alan


Alan Knowles wrote:
I'm toying with an extension of PHP5 (yes I know someone did it before), however, this ones not a straight binding, more a implementation of pear.php.net/DB_DataObject, using libdba as a backend.

I ran into a number of issues though:
a) I could not find any method to escape strings to send via execute_command http://php.net/mysql_real_escape_string (this is kind of critical) b) gda_connection_get_schema (... TABLE ...) does not seem to return the data using GdaFieldAttributes - this would be extremely usefull for the use I had.
(this looks like a kludge may be needed)

I also noticed the arguments hash for connection_execute_command, appear to be ignored by most drivers.. (is this deliberate, or just something nobody has ever got round to implementing?)

Regards
Alan
_______________________________________________
gnome-db-list mailing list
gnome-db-list gnome org
http://mail.gnome.org/mailman/listinfo/gnome-db-list


--
Can you help out?
Need Consulting Services or Know of a Job?
http://www.akbkhome.com
Index: gda-connection.c
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-connection.c,v
retrieving revision 1.58
diff -u -r1.58 gda-connection.c
--- gda-connection.c	1 Aug 2004 10:21:33 -0000	1.58
+++ gda-connection.c	3 Aug 2004 05:58:57 -0000
@@ -980,3 +980,29 @@
 	return gda_server_provider_create_blob (cnc->priv->provider_obj, cnc, blob);
 }
 
+
+/**
+ * gda_connection_escape_string
+ * @cnc: a #GdaConnection object.
+ * @gchar: to
+ * @gchar: from
+ * @unsigned long: length (of from string)
+ *
+ * Natively escapes string with \ slashes etc.
+ *
+ * Returns: %FALSE if the database does not support escaping.?
+ */
+unsigned long
+gda_connection_escape_string (GdaConnection *cnc,
+				 gchar *from,
+				 gchar *to,
+				 unsigned long length)
+{
+	g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);
+	g_return_val_if_fail (from == NULL, NULL);
+	g_return_val_if_fail (to == NULL, NULL);	
+
+	/* execute the command on the provider */
+	return gda_server_provider_escape_string (cnc->priv->provider_obj,
+						    cnc, from, to, length);
+}
Index: gda-connection.h
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-connection.h,v
retrieving revision 1.39
diff -u -r1.39 gda-connection.h
--- gda-connection.h	5 May 2004 09:17:10 -0000	1.39
+++ gda-connection.h	3 Aug 2004 05:58:57 -0000
@@ -113,6 +113,7 @@
 gboolean             gda_connection_rollback_transaction (GdaConnection *cnc, GdaTransaction *xaction);
 
 gboolean             gda_connection_create_blob (GdaConnection *cnc, GdaBlob *blob);
+unsigned long		 gda_connection_escape_string (GdaConnection *cnc, gchar *from, gchar *to, unsigned long length);
 
 const GList         *gda_connection_get_errors (GdaConnection *cnc);
 
Index: gda-server-provider.c
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-server-provider.c,v
retrieving revision 1.28
diff -u -r1.28 gda-server-provider.c
--- gda-server-provider.c	1 Aug 2004 10:21:33 -0000	1.28
+++ gda-server-provider.c	3 Aug 2004 05:58:57 -0000
@@ -76,6 +76,7 @@
 	klass->supports = NULL;
 	klass->get_schema = NULL;
 	klass->create_blob = _gda_server_provider_create_blob;
+	klass->escape_string = NULL;
 }
 
 static void
@@ -563,3 +564,29 @@
 	return CLASS (provider)->create_blob (provider, cnc, blob);
 }
 
+/**
+ * gda_server_provider_escape_string
+ * @provider: a server provider.
+ * @cnc: a #GdaConnection object.
+ * @gchar: to
+ * @gchar: from
+ * @unsigned long: length (of from string)
+ *
+ * Natively escapes string with \ slashes etc.
+ *
+ * Returns: %FALSE if the database does not support escaping.?
+ */
+unsigned long
+gda_server_provider_escape_string (GdaServerProvider *provider,
+				 GdaConnection *cnc,
+				 gchar *from,
+				 gchar *to,
+				 unsigned long length)
+{
+	g_return_val_if_fail (GDA_IS_SERVER_PROVIDER (provider), FALSE);
+	g_return_val_if_fail (GDA_IS_CONNECTION (cnc), FALSE);
+	g_return_val_if_fail (from == NULL, FALSE);
+	g_return_val_if_fail (to == NULL, FALSE);
+
+	return CLASS (provider)->escape_string (provider, cnc, from, to, length);
+}
Index: gda-server-provider.h
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-server-provider.h,v
retrieving revision 1.18
diff -u -r1.18 gda-server-provider.h
--- gda-server-provider.h	1 May 2004 21:01:20 -0000	1.18
+++ gda-server-provider.h	3 Aug 2004 05:58:57 -0000
@@ -113,6 +113,14 @@
 	gboolean (* create_blob) (GdaServerProvider *provider,
 				  GdaConnection *cnc,
 				  GdaBlob *blob);
+	
+	unsigned long (* escape_string) (GdaServerProvider *provider,
+				      GdaConnection *cnc,
+				      gchar *to,
+				      const gchar *from
+				      unsigned long length);
+				  
+				  
 };
 
 GType    gda_server_provider_get_type (void);
@@ -177,6 +185,13 @@
 					  GdaConnection *cnc,
 					  GdaBlob *blob);
 
+unsigned long gda_server_provider_escape_string (GdaServerProvider *provider,
+				      GdaConnection *cnc,
+				      gchar *to,
+				      const gchar *from
+				      unsigned long length);			  
+	
+
 G_END_DECLS
 
 #endif
Index: mysql/gda-mysql-provider.c
===================================================================
RCS file: /cvs/gnome/libgda/providers/mysql/gda-mysql-provider.c,v
retrieving revision 1.48
diff -u -r1.48 gda-mysql-provider.c
--- mysql/gda-mysql-provider.c	30 May 2004 23:41:31 -0000	1.48
+++ mysql/gda-mysql-provider.c	3 Aug 2004 05:59:06 -0000
@@ -83,7 +83,12 @@
 						    GdaConnection *cnc,
 						    GdaConnectionSchema schema,
 						    GdaParameterList *params);
-
+static unsigned long gda_mysql_escape_string (GdaServerProvider *provider,
+					     GdaConnection *cnc,
+					 	 gchar *from,
+				 		 gchar *to,
+				 		 unsigned long length);
+				 		 
 static GObjectClass *parent_class = NULL;
 
 /*
@@ -114,6 +119,7 @@
 	provider_class->rollback_transaction = gda_mysql_provider_rollback_transaction;
 	provider_class->supports = gda_mysql_provider_supports;
 	provider_class->get_schema = gda_mysql_provider_get_schema;
+	provider_class->escape_string = gda_mysql_provider_escape_string;
 }
 
 static void
@@ -1216,3 +1222,30 @@
 
 	return NULL;
 }
+
+
+unsigned long
+gda_mysql_provider_escape_string (GdaServerProvider *provider,
+				 GdaConnection *cnc,
+				 gchar *from,
+				 gchar *to,
+				 unsigned long length)
+{
+	MYSQL *mysql;
+	
+	GdaMysqlProvider *myprv = (GdaMysqlProvider *) provider;
+	g_return_val_if_fail (GDA_IS_MYSQL_PROVIDER (myprv), FALSE);
+	g_return_val_if_fail (GDA_IS_CONNECTION (cnc), FALSE);
+	g_return_val_if_fail (from == NULL, FALSE);
+	g_return_val_if_fail (to == NULL, FALSE);
+
+	mysql = g_object_get_data (G_OBJECT (cnc), OBJECT_DATA_MYSQL_HANDLE);
+	if (!mysql) {
+		gda_connection_add_error_string (cnc, _("Invalid MYSQL handle"));
+		return 0;
+	}
+	return mysql_real_escape_string(mysql, to, from, length);
+
+
+
+}
Index: postgres/gda-postgres-provider.c
===================================================================
RCS file: /cvs/gnome/libgda/providers/postgres/gda-postgres-provider.c,v
retrieving revision 1.62
diff -u -r1.62 gda-postgres-provider.c
--- postgres/gda-postgres-provider.c	1 May 2004 20:34:51 -0000	1.62
+++ postgres/gda-postgres-provider.c	3 Aug 2004 05:59:07 -0000
@@ -88,7 +88,12 @@
 static gboolean gda_postgres_provider_create_blob (GdaServerProvider *provider,
 						   GdaConnection *cnc,
 						   GdaBlob *blob);
-
+static unsigned long gda_postgres_escape_string (GdaServerProvider *provider,
+					     GdaConnection *cnc,
+					 	 gchar *from,
+				 		 gchar *to,
+				 		 unsigned long length);
+				 		 
 typedef struct {
 	gchar *col_name;
 	GdaValueType data_type;
@@ -141,6 +146,7 @@
 	provider_class->supports = gda_postgres_provider_supports;
 	provider_class->get_schema = gda_postgres_provider_get_schema;
 	provider_class->create_blob = gda_postgres_provider_create_blob;
+	provider_class->escape_string = gda_postgres_provider_escape_string;
 }
 
 static void
@@ -2090,3 +2096,24 @@
 	return gda_postgres_blob_create (blob, cnc);
 }
 
+
+
+unsigned long
+gda_postgres_provider_escape_string (GdaServerProvider *provider,
+				 GdaConnection *cnc,
+				 gchar *from,
+				 gchar *to,
+				 unsigned long length)
+{
+	MYSQL *mysql;
+	
+	 
+	g_return_val_if_fail (GDA_IS_MYSQL_PROVIDER (myprv), FALSE);
+	g_return_val_if_fail (GDA_IS_CONNECTION (cnc), FALSE);
+	g_return_val_if_fail (from == NULL, FALSE);
+	g_return_val_if_fail (to == NULL, FALSE);
+	return (unsigned long)  PQescapeString (to, (const char *)from, (size_t) length);
+
+
+
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]