#endif
#include <libxml/xpath.h>
+#include <libxml/xinclude.h>
#include <sql-parser/gda-sql-parser.h>
#include "libdbtransformer.h"
return TRUE;
}
+/**
+ * dbt_set_db_connection:
+ * @dbt:
+ * @gda_conn:
+ *
+ */
void
dbt_set_db_connection (Dbt *dbt, GdaConnection *gda_conn)
{
if (!dbt_get_connection_parser (dbt))
{
priv->gda_conn= NULL;
+
+ g_warning ("Unable to obtain the GdaSqlParser from the connection.");
+
return;
}
}
+/**
+ * dbt_set_db_connection_from_string:
+ * @dbt:
+ * @cnc_string:
+ *
+ */
void
dbt_set_db_connection_from_string (Dbt *dbt, const gchar *cnc_string)
{
&error);
if (!priv->gda_conn)
{
- g_warning ("Could not open connection: %s\n",
- error && error->message ? error->message : "No detail");
+ g_warning ("Could not open connection: %s.\n",
+ error != NULL && error->message != NULL ? error->message : "no detail");
return;
}
}
}
+/**
+ * dbt_set_xml:
+ * @dbt:
+ * @xml_root:
+ *
+ */
void
dbt_set_xml (Dbt *dbt, xmlNodePtr xml_root)
{
/* creation of an xmlDoc */
priv->xml_doc = xmlNewDoc ("1.0");
xmlDocSetRootElement (priv->xml_doc, priv->xml_root);
+
+ /* xmlinclude */
+ if (xmlXIncludeProcess (priv->xml_doc) < 0)
+ {
+ g_warning ("Unable to include xml external files.");
+ }
}
}
-
+/**
+ * dbt_set_xml_from_filename:
+ * @dbt:
+ * @filename:
+ *
+ */
void
dbt_set_xml_from_filename (Dbt *dbt, const gchar *filename)
{
g_return_if_fail (g_strcmp0 (filen, "") != 0);
/* TODO validate against DTD */
- /* TODO allow include */
xdoc = xmlParseFile (filen);
if (xdoc == NULL)
priv = DBT_GET_PRIVATE (dbt);
priv->xml_doc = xdoc;
+
+ /* xmlinclude */
+ if (xmlXIncludeProcess (priv->xml_doc) < 0)
+ {
+ g_warning ("Unable to include xml external files.");
+ }
+
priv->xml_root = xmlDocGetRootElement (priv->xml_doc);
if (priv->xml_root == NULL)
{
op = gda_server_provider_create_operation (provider, priv->gda_conn, GDA_SERVER_OPERATION_CREATE_TABLE, NULL, &error);
if (!op)
{
- g_print ("CREATE TABLE operation is not supported by the provider: %s\n",
- error && error->message ? error->message : "No detail");
+ g_warning ("CREATE TABLE operation is not supported by the provider: %s.",
+ error != NULL && error->message != NULL ? error->message : "no detail");
return ret;
}
/* execute the operation */
if (!gda_server_provider_perform_operation (provider, priv->gda_conn, op, &error))
{
- g_print ("Error executing the operation: %s\n",
- error && error->message ? error->message : "No detail");
+ g_warning ("Error executing the operation: %s.",
+ error != NULL && error->message != NULL ? error->message : "No detail");
return ret;
}
g_object_unref (op);
stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error);
if (!stmt)
{
- g_warning ("Unable to check operation existance: %s\n",
- error && error->message ? error->message : "No detail");
+ g_warning ("Unable to check operation existance: %s.",
+ error != NULL && error->message != NULL ? error->message : "no detail");
}
else
{
stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error);
if (!stmt)
{
- g_warning ("Unable to save operation: %s\n",
- error && error->message ? error->message : "No detail");
+ g_warning ("Unable to save operation: %s.",
+ error != NULL && error->message != NULL ? error->message : "no detail");
}
else
{
guint nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error);
if (nrows == -1)
{
- g_warning ("NON SELECT error: %s\n%s\n", sql,
- error && error->message ? error->message : "No detail");
+ g_warning ("NON SELECT error: %s.\n%s",
+ error != NULL && error->message != NULL ? error->message : "no detail",
+ sql);
}
else
{
stmt = gda_sql_parser_parse_string (priv->gda_parser, g_strstrip (sql), NULL, &error);
if (stmt == NULL)
{
- g_warning ("Unable to create GdaStatement from sql: %s\n%s",
- sql, error && error->message ? error->message : "No detail");
+ g_warning ("Unable to create GdaStatement from sql: %s.\n%s",
+ error != NULL && error->message != NULL ? error->message : "no detail",
+ sql);
ret = FALSE;
}
else
nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error);
if (nrows == -1)
{
- g_warning ("NON SELECT error: %s\n%s\n", sql,
- error && error->message ? error->message : "No detail");
+ g_warning ("NON SELECT error: %s.\n%s",
+ error != NULL && error->message != NULL ? error->message : "no detail",
+ sql);
ret = FALSE;
}
g_object_unref (stmt);
if (xmlStrcmp (cur->name, (const xmlChar *)"serv_op_data") != 0)
{
- g_warning ("Invalid tag \"%s\".\n", cur->name);
+ g_warning ("Invalid tag \"%s\".", cur->name);
ret = FALSE;
cur = cur->next;
continue;
str_operation_type = (gchar *)xmlGetProp(cur, (const xmlChar *)"type");
- /* TODO
- * must find the GdaServerOperationType of the string
- * i asked on gda mailing list
- * for now i implement it
- */
- operation_type = GDA_SERVER_OPERATION_LAST;
- if (g_strcmp0 (str_operation_type, "CREATE_DB") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_DB;
- if (g_strcmp0 (str_operation_type, "DROP_DB") == 0) operation_type = GDA_SERVER_OPERATION_DROP_DB;
- if (g_strcmp0 (str_operation_type, "CREATE_TABLE") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_TABLE;
- if (g_strcmp0 (str_operation_type, "DROP_TABLE") == 0) operation_type = GDA_SERVER_OPERATION_DROP_TABLE;
- if (g_strcmp0 (str_operation_type, "CREATE_INDEX") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_INDEX;
- if (g_strcmp0 (str_operation_type, "DROP_INDEX") == 0) operation_type = GDA_SERVER_OPERATION_DROP_INDEX;
- if (g_strcmp0 (str_operation_type, "RENAME_TABLE") == 0) operation_type = GDA_SERVER_OPERATION_RENAME_TABLE;
- if (g_strcmp0 (str_operation_type, "COMMENT_TABLE") == 0) operation_type = GDA_SERVER_OPERATION_COMMENT_TABLE;
- if (g_strcmp0 (str_operation_type, "ADD_COLUMN") == 0) operation_type = GDA_SERVER_OPERATION_ADD_COLUMN;
- if (g_strcmp0 (str_operation_type, "DROP_COLUMN") == 0) operation_type = GDA_SERVER_OPERATION_DROP_COLUMN;
- if (g_strcmp0 (str_operation_type, "COMMENT_COLUMN") == 0) operation_type = GDA_SERVER_OPERATION_COMMENT_COLUMN;
- if (g_strcmp0 (str_operation_type, "CREATE_VIEW") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_VIEW;
- if (g_strcmp0 (str_operation_type, "DROP_VIEW") == 0) operation_type = GDA_SERVER_OPERATION_DROP_VIEW;
- if (g_strcmp0 (str_operation_type, "CREATE_USER") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_USER;
- if (g_strcmp0 (str_operation_type, "DROP_USER") == 0) operation_type = GDA_SERVER_OPERATION_DROP_USER;
- if (g_strcmp0 (str_operation_type, "ALTER_USER") == 0) operation_type = GDA_SERVER_OPERATION_ALTER_USER;
-
- if (operation_type == GDA_SERVER_OPERATION_LAST)
+ operation_type = gda_server_operation_string_to_op_type (str_operation_type);
+ if (operation_type == G_MAXINT)
{
- g_warning ("Operation type \"%s\" not supported.\n%s\n", str_operation_type,
- error && error->message ? error->message : "No detail");
+ g_warning ("Operation type \"%s\" not supported: %s.", str_operation_type,
+ error != NULL && error->message != NULL ? error->message : "no detail");
return FALSE;
}
op = gda_server_provider_create_operation (provider, priv->gda_conn, operation_type, NULL, &error);
if (!op)
{
- g_warning ("Unable to create operation type \"%s\".\n%s\n", str_operation_type,
- error && error->message ? error->message : "No detail");
+ g_warning ("Unable to create operation type \"%s\": %s.", str_operation_type,
+ error != NULL && error->message != NULL ? error->message : "no detail");
ret = FALSE;
}
else
error = NULL;
if (!gda_server_operation_load_data_from_xml (op, cur, &error))
{
- g_warning ("Unable to load GdaServerOperation from the file.\n%s\n",
- error && error->message ? error->message : "No detail");
+ g_warning ("Unable to load GdaServerOperation from the file: %s.\n",
+ error != NULL && error->message != NULL ? error->message : "no detail");
ret = FALSE;
}
else
{
if (!gda_server_provider_perform_operation (provider, priv->gda_conn, op, &error))
{
- g_warning ("Error on executing GdaServerOperation from the file.\n%s\n",
- error && error->message ? error->message : "No detail");
+ g_warning ("Error on executing GdaServerOperation from the file: %s.\n",
+ error != NULL && error->message != NULL ? error->message : "no detail");
ret = FALSE;
}
}
}
else
{
- g_warning ("Operation type \"%s\" not supported by the provider.\n", str_operation_type);
+ g_warning ("Operation type \"%s\" not supported by the provider.", str_operation_type);
ret = FALSE;
}
return ret;
}
+static gboolean
+dbt_parse_drop_table (Dbt *dbt, xmlNodePtr xnode)
+{
+ DbtPrivate *priv;
+
+ gboolean ret;
+
+ xmlNodePtr cur;
+ gchar *table_name;
+
+ GError *error;
+
+ ret = TRUE;
+
+ priv = DBT_GET_PRIVATE (dbt);
+
+ table_name = NULL;
+ cur = xnode->children;
+ while (cur != NULL && ret)
+ {
+ if (xmlStrcmp (cur->name, (const xmlChar *)"table_name") == 0)
+ {
+ table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur)));
+ }
+
+ cur = cur->next;
+ }
+
+ if (table_name != NULL && g_strcmp0 (table_name, "") != 0)
+ {
+ gchar *sql;
+ GdaStatement *stmt;
+
+ sql = g_strdup_printf ("DROP TABLE %s", table_name);
+
+ error = NULL;
+ stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error);
+ if (stmt == NULL)
+ {
+ g_warning ("Unable to create GdaStatement from sql: %s.\n%s",
+ error != NULL && error->message != NULL ? error->message : "no detail",
+ sql);
+ ret = FALSE;
+ }
+ else
+ {
+ guint nrows;
+
+ nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error);
+ if (nrows == -1)
+ {
+ g_warning ("NON SELECT error: %s.\n%s",
+ error != NULL && error->message != NULL ? error->message : "no detail",
+ sql);
+ ret = FALSE;
+ }
+ g_object_unref (stmt);
+ }
+ }
+ else
+ {
+ g_warning ("You must provide the name of the table.");
+ ret = FALSE;
+ }
+
+ return ret;
+}
+
+/**
+ * dbt_transform:
+ * @dbt:
+ *
+ */
void
dbt_transform (Dbt *dbt)
{
error = NULL;
if (!gda_connection_begin_transaction (priv->gda_conn, NULL, GDA_TRANSACTION_ISOLATION_UNKNOWN, &error))
{
- g_warning ("Unable to start the transaction: %s\n",
- error && error->message ? error->message : "No detail");
+ g_warning ("Unable to start the transaction: %s.\n",
+ error != NULL && error->message != NULL ? error->message : "no detail");
continue;
}
}
{
tosave = dbt_parse_gda_op (dbt, xnode);
}
+ else if (xmlStrcmp (xnode->name, (const xmlChar *)"drop_table") == 0)
+ {
+ tosave = dbt_parse_drop_table (dbt, xnode);
+ }
else
{
- g_warning ("Invalid tag: %s", xnode->name);
+ g_warning ("Invalid tag: %s.", xnode->name);
tosave = FALSE;
}
}