[libgda] Added COMMENT_TABLE and COMMENT_COLUMN server operations



commit 57127268456514ae5e15bef37e0249400a44b9fc
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sun Jul 5 18:41:28 2009 +0200

    Added COMMENT_TABLE and COMMENT_COLUMN server operations
    
    * Added generic COMMENT_TABLE and COMMENT_COLUMN operations to modify
      the comments on a table or a column
    * Implementation for the MySQL provider
    
    All thanks to Carlos Savoretti

 libgda/gda-server-operation.c                     |    4 +
 libgda/gda-server-operation.h                     |    3 +
 libgda/gda-server-provider.c                      |   18 ++++
 providers/mysql/Makefile.am                       |    2 +
 providers/mysql/gda-mysql-ddl.c                   |  109 +++++++++++++++++++++
 providers/mysql/gda-mysql-ddl.h                   |    4 +
 providers/mysql/gda-mysql-provider.c              |   10 ++-
 providers/mysql/gda-mysql-recordset.c             |    2 +-
 providers/mysql/mysql_specs_comment_column.xml.in |    8 ++
 providers/mysql/mysql_specs_comment_table.xml.in  |    7 ++
 10 files changed, 165 insertions(+), 2 deletions(-)
---
diff --git a/libgda/gda-server-operation.c b/libgda/gda-server-operation.c
index 70d013c..77f1b36 100644
--- a/libgda/gda-server-operation.c
+++ b/libgda/gda-server-operation.c
@@ -1210,10 +1210,14 @@ gda_server_operation_op_type_to_string (GdaServerOperationType type)
 		return "DROP_INDEX";
         case GDA_SERVER_OPERATION_RENAME_TABLE:
 		return "RENAME_TABLE";
+        case GDA_SERVER_OPERATION_COMMENT_TABLE:
+		return "COMMENT_TABLE";
         case GDA_SERVER_OPERATION_ADD_COLUMN:
 		return "ADD_COLUMN";
         case GDA_SERVER_OPERATION_DROP_COLUMN:
 		return "DROP_COLUMN";
+        case GDA_SERVER_OPERATION_COMMENT_COLUMN:
+		return "COMMENT_COLUMN";
 	case GDA_SERVER_OPERATION_CREATE_VIEW:
 		return "CREATE_VIEW";
 	case GDA_SERVER_OPERATION_DROP_VIEW:
diff --git a/libgda/gda-server-operation.h b/libgda/gda-server-operation.h
index df7ebe0..bf6944b 100644
--- a/libgda/gda-server-operation.h
+++ b/libgda/gda-server-operation.h
@@ -56,6 +56,9 @@ typedef enum {
 	GDA_SERVER_OPERATION_CREATE_VIEW,
 	GDA_SERVER_OPERATION_DROP_VIEW,
 
+	GDA_SERVER_OPERATION_COMMENT_TABLE,
+	GDA_SERVER_OPERATION_COMMENT_COLUMN,
+
 	GDA_SERVER_OPERATION_LAST
 } GdaServerOperationType;
 
diff --git a/libgda/gda-server-provider.c b/libgda/gda-server-provider.c
index 424ab4b..ed283e0 100644
--- a/libgda/gda-server-provider.c
+++ b/libgda/gda-server-provider.c
@@ -375,6 +375,13 @@ static OpReq op_req_RENAME_TABLE [] = {
 	{NULL}
 };
 
+static OpReq op_req_COMMENT_TABLE [] = {
+	{"/TABLE_DESC_P",               GDA_SERVER_OPERATION_NODE_PARAMLIST, 0},
+	{"/TABLE_DESC_P/TABLE_NAME",    GDA_SERVER_OPERATION_NODE_PARAM, G_TYPE_STRING},
+	{"/TABLE_DESC_P/TABLE_COMMENT", GDA_SERVER_OPERATION_NODE_PARAM, G_TYPE_STRING},
+	{NULL}
+};
+
 static OpReq op_req_ADD_COLUMN [] = {
 	{"/COLUMN_DEF_P",               GDA_SERVER_OPERATION_NODE_PARAMLIST, 0},
 	{"/COLUMN_DEF_P/TABLE_NAME",    GDA_SERVER_OPERATION_NODE_PARAM, G_TYPE_STRING},
@@ -390,6 +397,14 @@ static OpReq op_req_DROP_COLUMN [] = {
 	{NULL}
 };
 
+static OpReq op_req_COMMENT_COLUMN [] = {
+	{"/COLUMN_DESC_P",                GDA_SERVER_OPERATION_NODE_PARAMLIST, 0},
+	{"/COLUMN_DESC_P/TABLE_NAME",     GDA_SERVER_OPERATION_NODE_PARAM, G_TYPE_STRING},
+	{"/COLUMN_DESC_P/COLUMN_NAME",    GDA_SERVER_OPERATION_NODE_PARAM, G_TYPE_STRING},
+	{"/COLUMN_DESC_P/COLUMN_COMMENT", GDA_SERVER_OPERATION_NODE_PARAM, G_TYPE_STRING},
+	{NULL}
+};
+
 static OpReq op_req_CREATE_INDEX [] = {
 	{"/INDEX_DEF_P/INDEX_NAME",       GDA_SERVER_OPERATION_NODE_PARAM, G_TYPE_STRING},
 	{"/INDEX_DEF_P/INDEX_ON_TABLE",   GDA_SERVER_OPERATION_NODE_PARAM, G_TYPE_STRING},
@@ -459,6 +474,9 @@ gda_server_provider_create_operation (GdaServerProvider *provider, GdaConnection
 
 		op_req_table [GDA_SERVER_OPERATION_CREATE_VIEW] = op_req_CREATE_VIEW;
 		op_req_table [GDA_SERVER_OPERATION_DROP_VIEW] = op_req_DROP_VIEW;
+
+		op_req_table [GDA_SERVER_OPERATION_COMMENT_TABLE] = op_req_COMMENT_TABLE;
+		op_req_table [GDA_SERVER_OPERATION_COMMENT_COLUMN] = op_req_COMMENT_COLUMN;
 	}
 	g_static_mutex_unlock (&init_mutex);
 
diff --git a/providers/mysql/Makefile.am b/providers/mysql/Makefile.am
index 380de40..b366e2e 100644
--- a/providers/mysql/Makefile.am
+++ b/providers/mysql/Makefile.am
@@ -57,11 +57,13 @@ xml_in_files = \
 	mysql_specs_dsn.xml.in \
 	mysql_specs_create_table.xml.in \
         mysql_specs_drop_table.xml.in \
+        mysql_specs_comment_table.xml.in \
         mysql_specs_create_index.xml.in \
         mysql_specs_drop_index.xml.in \
 	mysql_specs_rename_table.xml.in \
 	mysql_specs_add_column.xml.in \
 	mysql_specs_drop_column.xml.in \
+	mysql_specs_comment_column.xml.in \
 	mysql_specs_create_view.xml.in \
 	mysql_specs_drop_view.xml.in
 
diff --git a/providers/mysql/gda-mysql-ddl.c b/providers/mysql/gda-mysql-ddl.c
index 0291565..cf5617c 100644
--- a/providers/mysql/gda-mysql-ddl.c
+++ b/providers/mysql/gda-mysql-ddl.c
@@ -518,6 +518,36 @@ gda_mysql_render_RENAME_TABLE (GdaServerProvider *provider, GdaConnection *cnc,
 	return sql;
 }
 
+
+gchar *
+gda_mysql_render_COMMENT_TABLE (GdaServerProvider *provider, GdaConnection *cnc, 
+				 GdaServerOperation *op, GError **error)
+{
+	GString *string;
+	const GValue *value;
+	gchar *sql = NULL;
+
+	string = g_string_new ("ALTER TABLE ");
+
+	value = gda_server_operation_get_value_at (op, "/TABLE_DESC_P/TABLE_NAME");
+	g_assert (value && G_VALUE_HOLDS (value, G_TYPE_STRING));
+	g_string_append (string, g_value_get_string (value));
+
+	gchar *table_name = g_value_dup_string (value);
+
+	value = gda_server_operation_get_value_at (op, "/TABLE_DESC_P/TABLE_COMMENT");
+	g_assert (value && G_VALUE_HOLDS (value, G_TYPE_STRING));
+	g_string_append (string, " COMMENT '");
+	g_string_append (string, g_value_get_string (value));
+	g_string_append (string, "'");
+
+	sql = string->str;
+	g_string_free (string, FALSE);
+
+	return sql;
+}
+
+
 gchar *
 gda_mysql_render_ADD_COLUMN (GdaServerProvider *provider, GdaConnection *cnc, 
 			     GdaServerOperation *op, GError **error)
@@ -652,6 +682,85 @@ gda_mysql_render_DROP_COLUMN (GdaServerProvider *provider, GdaConnection *cnc,
 
 
 gchar *
+gda_mysql_render_COMMENT_COLUMN (GdaServerProvider *provider, GdaConnection *cnc, 
+				 GdaServerOperation *op, GError **error)
+{
+	GString *string;
+	const GValue *value;
+	gchar *sql = NULL;
+
+	string = g_string_new ("ALTER TABLE ");
+
+	value = gda_server_operation_get_value_at (op, "/COLUMN_DESC_P/TABLE_NAME");
+	g_assert (value && G_VALUE_HOLDS (value, G_TYPE_STRING));
+	g_string_append (string, g_value_get_string (value));
+
+	gchar *table_name = g_value_dup_string (value);
+
+	value = gda_server_operation_get_value_at (op, "/COLUMN_DESC_P/COLUMN_NAME");
+	g_assert (value && G_VALUE_HOLDS (value, G_TYPE_STRING));
+	g_string_append (string, " CHANGE COLUMN ");
+	g_string_append (string, g_value_get_string (value));
+	g_string_append (string, " ");
+	g_string_append (string, g_value_get_string (value));
+	g_string_append (string, " ");
+
+	gchar *column_name = g_value_dup_string (value);
+
+	GString *tmp_string = g_string_new ("SELECT column_type FROM "
+					    "information_schema.columns "
+					    "WHERE table_name = '");
+	g_string_append (tmp_string, table_name);
+	g_string_append (tmp_string, "' AND column_name = '");
+	g_string_append (tmp_string, column_name);
+	g_string_append (tmp_string, "'");
+
+	g_free (table_name);
+	g_free (column_name);
+
+	GdaSqlParser *parser = gda_connection_create_parser (cnc);
+	if (parser == NULL)        /* @cnc does not provide its own parser. Use default one */
+		parser = gda_sql_parser_new ();
+	GdaStatement *statement = gda_sql_parser_parse_string (parser,
+							       tmp_string->str,
+							       NULL, NULL);
+	g_string_free (tmp_string, FALSE);
+
+	GdaDataModel *model;
+	GError *gerror = NULL;
+	model = gda_connection_statement_execute_select (cnc, statement,
+							 NULL, &gerror);
+	g_object_unref (G_OBJECT(statement));
+
+	g_assert (model != NULL && gda_data_model_get_n_rows (model) == 1);
+
+	GValue *tmp_value;
+	tmp_value = gda_data_model_get_value_at (model, 0, 0, error);
+
+	gchar *str;
+	g_assert (tmp_value && (str = gda_value_stringify (tmp_value)));
+
+	g_string_append (string, str);
+	g_free (str);
+
+	g_object_unref (G_OBJECT(model));
+
+	g_string_append (string, " COMMENT");
+	g_string_append (string, " '");
+
+	value = gda_server_operation_get_value_at (op, "/COLUMN_DESC_P/COLUMN_COMMENT");
+	g_assert (value && G_VALUE_HOLDS (value, G_TYPE_STRING));
+	g_string_append (string, g_value_get_string (value));
+	g_string_append (string, "'");
+
+	sql = string->str;
+	g_string_free (string, FALSE);
+
+	return sql;
+}
+
+
+gchar *
 gda_mysql_render_CREATE_INDEX (GdaServerProvider *provider, GdaConnection *cnc, 
 			       GdaServerOperation *op, GError **error)
 {
diff --git a/providers/mysql/gda-mysql-ddl.h b/providers/mysql/gda-mysql-ddl.h
index 5172fb2..8c0b10b 100644
--- a/providers/mysql/gda-mysql-ddl.h
+++ b/providers/mysql/gda-mysql-ddl.h
@@ -36,10 +36,14 @@ gchar *gda_mysql_render_DROP_TABLE   (GdaServerProvider *provider, GdaConnection
 				      GdaServerOperation *op, GError **error);
 gchar *gda_mysql_render_RENAME_TABLE (GdaServerProvider *provider, GdaConnection *cnc, 
 				      GdaServerOperation *op, GError **error);
+gchar *gda_mysql_render_COMMENT_TABLE (GdaServerProvider *provider, GdaConnection *cnc, 
+				      GdaServerOperation *op, GError **error);
 gchar *gda_mysql_render_ADD_COLUMN   (GdaServerProvider *provider, GdaConnection *cnc, 
 				      GdaServerOperation *op, GError **error);
 gchar *gda_mysql_render_DROP_COLUMN  (GdaServerProvider *provider, GdaConnection *cnc, 
 				      GdaServerOperation *op, GError **error);
+gchar *gda_mysql_render_COMMENT_COLUMN(GdaServerProvider *provider, GdaConnection *cnc, 
+				      GdaServerOperation *op, GError **error);
 gchar *gda_mysql_render_CREATE_INDEX (GdaServerProvider *provider, GdaConnection *cnc, 
 				      GdaServerOperation *op, GError **error);
 gchar *gda_mysql_render_DROP_INDEX   (GdaServerProvider *provider, GdaConnection *cnc, 
diff --git a/providers/mysql/gda-mysql-provider.c b/providers/mysql/gda-mysql-provider.c
index f31ebc4..c54e287 100644
--- a/providers/mysql/gda-mysql-provider.c
+++ b/providers/mysql/gda-mysql-provider.c
@@ -707,8 +707,10 @@ gda_mysql_provider_supports_operation (GdaServerProvider       *provider,
         case GDA_SERVER_OPERATION_CREATE_TABLE:
         case GDA_SERVER_OPERATION_DROP_TABLE:
         case GDA_SERVER_OPERATION_RENAME_TABLE:
+        case GDA_SERVER_OPERATION_COMMENT_TABLE:
         case GDA_SERVER_OPERATION_ADD_COLUMN:
 	case GDA_SERVER_OPERATION_DROP_COLUMN:
+	case GDA_SERVER_OPERATION_COMMENT_COLUMN:
         case GDA_SERVER_OPERATION_CREATE_INDEX:
         case GDA_SERVER_OPERATION_DROP_INDEX:
         case GDA_SERVER_OPERATION_CREATE_VIEW:
@@ -820,12 +822,18 @@ gda_mysql_provider_render_operation (GdaServerProvider   *provider,
         case GDA_SERVER_OPERATION_RENAME_TABLE:
 		sql = gda_mysql_render_RENAME_TABLE (provider, cnc, op, error);
 		break;
+        case GDA_SERVER_OPERATION_COMMENT_TABLE:
+		sql = gda_mysql_render_COMMENT_TABLE (provider, cnc, op, error);
+		break;
         case GDA_SERVER_OPERATION_ADD_COLUMN:
 		sql = gda_mysql_render_ADD_COLUMN (provider, cnc, op, error);
 		break;
 	case GDA_SERVER_OPERATION_DROP_COLUMN:
 		sql = gda_mysql_render_DROP_COLUMN (provider, cnc, op, error);
 		break;
+	case GDA_SERVER_OPERATION_COMMENT_COLUMN:
+		sql = gda_mysql_render_COMMENT_COLUMN (provider, cnc, op, error);
+		break;
         case GDA_SERVER_OPERATION_CREATE_INDEX:
 		sql = gda_mysql_render_CREATE_INDEX (provider, cnc, op, error);
 		break;
@@ -1175,7 +1183,7 @@ mysql_render_select_target (GdaSqlSelectTarget *target, GdaSqlRenderingContext *
 	/* can't have: target->expr == NULL */
 	if (!gda_sql_any_part_check_structure (GDA_SQL_ANY_PART (target), error)) return NULL;
 
-	if (! target->expr->value || (G_VALUE_TYPE (target->expr->value) != G_TYPE_STRING)) {
+	if (!target->expr->value || (G_VALUE_TYPE (target->expr->value) != G_TYPE_STRING)) {
 		str = context->render_expr (target->expr, context, NULL, NULL, error);
 		if (!str)
 			return NULL;
diff --git a/providers/mysql/gda-mysql-recordset.c b/providers/mysql/gda-mysql-recordset.c
index 7ece9f5..2a2ca77 100644
--- a/providers/mysql/gda-mysql-recordset.c
+++ b/providers/mysql/gda-mysql-recordset.c
@@ -562,7 +562,7 @@ gda_mysql_recordset_fetch_nb_rows (GdaDataSelect *model)
 }
 
 static GdaRow *
-new_row_from_mysql_stmt (GdaMysqlRecordset  *imodel, gint rownum, GError **error)
+new_row_from_mysql_stmt (GdaMysqlRecordset  *imodel, gint  rownum, GError **error)
 {
 	//g_print ("%s(): NCOLS=%d  ROWNUM=%d\n", __func__, ((GdaDataSelect *) imodel)->prep_stmt->ncols, rownum);
 
diff --git a/providers/mysql/mysql_specs_comment_column.xml.in b/providers/mysql/mysql_specs_comment_column.xml.in
new file mode 100644
index 0000000..7e74765
--- /dev/null
+++ b/providers/mysql/mysql_specs_comment_column.xml.in
@@ -0,0 +1,8 @@
+<?xml version="1.0"?>
+<serv_op>
+  <parameters id="COLUMN_DESC_P" _name="Column's description">
+    <parameter id="TABLE_NAME" _name="Table" _descr="Table's name" gdatype="gchararray" nullok="FALSE"/>
+    <parameter id="COLUMN_NAME" _name="Field name" gdatype="gchararray" nullok="FALSE"/>
+    <parameter id="COLUMN_COMMENT" _name="Field comment" gdatype="gchararray" nullok="FALSE"/>
+  </parameters>
+</serv_op>
diff --git a/providers/mysql/mysql_specs_comment_table.xml.in b/providers/mysql/mysql_specs_comment_table.xml.in
new file mode 100644
index 0000000..2ad5591
--- /dev/null
+++ b/providers/mysql/mysql_specs_comment_table.xml.in
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<serv_op>
+  <parameters id="TABLE_DESC_P" _name="Table's description">
+    <parameter id="TABLE_NAME" _name="Table" _descr="Table's name" gdatype="gchararray" nullok="FALSE"/>
+    <parameter id="TABLE_COMMENT" _name="Table comment" gdatype="gchararray" nullok="FALSE"/>
+  </parameters>
+</serv_op>



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