From: Andrea Zagli Date: Sat, 5 Mar 2011 15:04:14 +0000 (+0100) Subject: Added function GdaExQueryEditor::get_sql_as_xml. X-Git-Tag: 0.5.0~54 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=c9cd8d5b1ac8d0a04b65d59ac9694d1cf98eae8a;p=libgdaex Added function GdaExQueryEditor::get_sql_as_xml. --- diff --git a/src/queryeditor.c b/src/queryeditor.c index a00b525..d5ba5d3 100644 --- a/src/queryeditor.c +++ b/src/queryeditor.c @@ -722,6 +722,151 @@ const gchar return ret; } +xmlNode +*gdaex_query_editor_get_sql_as_xml (GdaExQueryEditor *qe) +{ + GdaExQueryEditorPrivate *priv; + + xmlNode *ret; + + GtkTreeIter iter; + + gchar *table_name; + gchar *field_name; + + xmlNode *node; + + ret = NULL; + + g_return_val_if_fail (GDAEX_IS_QUERY_EDITOR (qe), NULL); + + priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (qe); + + ret = xmlNewNode (NULL, "gdaex_query_editor_choices"); + + if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->lstore_show), &iter)) + { + xmlNode *node_show; + + node_show = xmlNewNode (NULL, "show"); + xmlAddChild (ret, node_show); + + do + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_show), &iter, + COL_SHOW_TABLE_NAME, &table_name, + COL_SHOW_NAME, &field_name, + -1); + + node = xmlNewNode (NULL, "field"); + xmlAddChild (node_show, node); + xmlNewProp (node, "table", table_name); + xmlNewProp (node, "field", field_name); + } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->lstore_show), &iter)); + } + + if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->tstore_where), &iter)) + { + xmlNode *node_where; + + gboolean not; + guint where_type; + gchar *from_str; + gchar *to_str; + gchar *str_op; + + node_where = xmlNewNode (NULL, "where"); + xmlAddChild (ret, node_where); + + do + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_where), &iter, + COL_WHERE_TABLE_NAME, &table_name, + COL_WHERE_NAME, &field_name, + COL_WHERE_CONDITION_NOT, ¬, + COL_WHERE_CONDITION_TYPE, &where_type, + COL_WHERE_CONDITION_FROM, &from_str, + COL_WHERE_CONDITION_TO, &to_str, + -1); + + node = xmlNewNode (NULL, "field"); + xmlAddChild (node_where, node); + xmlNewProp (node, "table", table_name); + xmlNewProp (node, "field", field_name); + xmlNewProp (node, "not", (not ? "y" : "n")); + + switch (where_type) + { + case GDAEX_QE_WHERE_TYPE_EQUAL: + str_op = g_strdup ("EQUAL"); + break; + + case GDAEX_QE_WHERE_TYPE_LIKE: + str_op = g_strdup ("LIKE"); + break; + + case GDAEX_QE_WHERE_TYPE_ILIKE: + str_op = g_strdup ("ILIKE"); + break; + + case GDAEX_QE_WHERE_TYPE_GREAT: + str_op = g_strdup ("GREAT"); + break; + + case GDAEX_QE_WHERE_TYPE_GREAT_EQUAL: + str_op = g_strdup ("GREAT_EQUAL"); + break; + + case GDAEX_QE_WHERE_TYPE_LESS: + str_op = g_strdup ("LESS"); + break; + + case GDAEX_QE_WHERE_TYPE_LESS_EQUAL: + str_op = g_strdup ("LESS_EQUAL"); + break; + + case GDAEX_QE_WHERE_TYPE_BETWEEN: + str_op = g_strdup ("BETWEEN"); + break; + + default: + g_warning ("Where type «%d» not valid.", where_type); + continue; + } + xmlNewProp (node, "where_type", str_op); + + xmlNewProp (node, "from", from_str); + xmlNewProp (node, "to", to_str); + } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->tstore_where), &iter)); + } + + if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->lstore_order), &iter)) + { + gchar *asc_desc; + xmlNode *node_order; + + node_order = xmlNewNode (NULL, "order"); + xmlAddChild (ret, node_order); + + do + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_order), &iter, + COL_ORDER_TABLE_NAME, &table_name, + COL_ORDER_NAME, &field_name, + COL_ORDER_ORDER, &asc_desc, + -1); + + node = xmlNewNode (NULL, "field"); + xmlAddChild (node_order, node); + xmlNewProp (node, "table", table_name); + xmlNewProp (node, "field", field_name); + xmlNewProp (node, "asc_desc", asc_desc); + } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->lstore_order), &iter)); + } + + return ret; +} + /* PRIVATE */ static void gdaex_query_editor_set_property (GObject *object, diff --git a/src/queryeditor.h b/src/queryeditor.h index 74eeb4c..9193ce1 100644 --- a/src/queryeditor.h +++ b/src/queryeditor.h @@ -87,16 +87,10 @@ typedef struct gchar *name_visible; gchar *description; gchar *alias; - /* - tipo di campo (string, integer, double, date, datetime) */ GdaExQueryEditorFieldType type; - /* - sceglibile per la parte show (es. le chiavi esterne non ha senno che lo siano) */ gboolean for_show; - /* - sempre presente nelle query, quindi non sceglibile per la parte show */ gboolean always_showed; - /* - sceglibile per la parte where */ gboolean for_where; - /* - condizioni where che è possibile applicare (es. i campi id_* non ha senso che abbiano un between, */ - /* ma ci deve essere la possibilità di fare ricerche anche sulle decodifiche) */ guint available_where_type; gchar *decode_table2; /* TODO @@ -122,6 +116,8 @@ gboolean gdaex_query_editor_add_relation (GdaExQueryEditor *qe, const gchar *gdaex_query_editor_get_sql (GdaExQueryEditor *qe); +xmlNode *gdaex_query_editor_get_sql_as_xml (GdaExQueryEditor *qe); + G_END_DECLS diff --git a/tests/query_editor.c b/tests/query_editor.c index 0a3dd0e..c709bab 100644 --- a/tests/query_editor.c +++ b/tests/query_editor.c @@ -129,5 +129,16 @@ main (int argc, char *argv[]) g_message (gdaex_query_editor_get_sql (qe)); + xmlDoc *doc; + xmlNode *node; + xmlChar *buf; + gint size; + + doc = xmlNewDoc ("1.0"); + node = gdaex_query_editor_get_sql_as_xml (qe); + xmlDocSetRootElement (doc, node); + xmlDocDumpMemory (doc, &buf, &size); + g_message (buf); + return 0; }