From: Andrea Zagli Date: Tue, 29 Jun 2010 19:58:20 +0000 (+0200) Subject: Start coding. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=628803ccb1981d4ca779f1c3bacbbac6b4521370;p=libzakdbt Start coding. --- diff --git a/docs/dbtransformer.dtd b/docs/dbtransformer.dtd new file mode 100644 index 0000000..eef1a95 --- /dev/null +++ b/docs/dbtransformer.dtd @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/src/dbtransformer.c b/src/dbtransformer.c index 2df31fc..c04b0ce 100644 --- a/src/dbtransformer.c +++ b/src/dbtransformer.c @@ -20,6 +20,8 @@ #include #endif +#include + #include "libdbtransformer.h" static void dbt_class_init (DbtClass *class); @@ -39,7 +41,8 @@ static void dbt_get_property (GObject *object, typedef struct _DbtPrivate DbtPrivate; struct _DbtPrivate { - gpointer foo; + GdaConnection *gda_conn; + xmlNodePtr xml_root; }; G_DEFINE_TYPE (Dbt, dbt, G_TYPE_OBJECT) @@ -59,6 +62,9 @@ static void dbt_init (Dbt *dbt) { DbtPrivate *priv = DBT_GET_PRIVATE (dbt); + + priv->gda_conn = NULL; + priv->xml_root = NULL; } /** @@ -69,10 +75,241 @@ dbt_init (Dbt *dbt) Dbt *dbt_new () { - return DBT (g_object_new (dbt_get_type (), NULL)); } +static gboolean +dbt_get_connection_parser (Dbt *dbt) +{ + DbtPrivate *priv; + GdaSqlParser *parser; + + priv = DBT_GET_PRIVATE (dbt); + + /* create an SQL parser */ + parser = gda_connection_create_parser (priv->gda_conn); + if (!parser) /* @cnc doe snot provide its own parser => use default one */ + { + parser = gda_sql_parser_new (); + } + + /* attach the parser object to the connection */ + g_object_set_data_full (G_OBJECT (priv->gda_conn), "parser", parser, g_object_unref); +} + +void +dbt_set_db_connection (Dbt *dbt, GdaConnection *gda_conn) +{ + DbtPrivate *priv; + + g_return_if_fail (IS_DBT (dbt)); + g_return_if_fail (GDA_IS_CONNECTION (gda_conn)); + + priv = DBT_GET_PRIVATE (dbt); + + priv->gda_conn = gda_conn; + + if (!dbt_get_connection_parser (dbt)) + { + priv->gda_conn= NULL; + return; + } +} + +void +dbt_set_db_connection_from_string (Dbt *dbt, const gchar *cnc_string) +{ + DbtPrivate *priv; + + gchar *cnc; + + GError *error; + + g_return_if_fail (IS_DBT (dbt)); + g_return_if_fail (cnc_string != NULL); + + cnc = g_strstrip (g_strdup (cnc_string)); + + g_return_if_fail (g_strcmp0 (cnc, "") != 0); + + priv = DBT_GET_PRIVATE (dbt); + + error = NULL; + priv->gda_conn = gda_connection_open_from_string (NULL, cnc, NULL, + GDA_CONNECTION_OPTIONS_NONE, + &error); + if (!priv->gda_conn) + { + g_warning ("Could not open connection: %s\n", + error && error->message ? error->message : "No detail"); + return; + } + + if (!dbt_get_connection_parser (dbt)) + { + priv->gda_conn= NULL; + return; + } +} + +static gboolean +dbt_check_xml_root (xmlNodePtr xml_root) +{ + if (xmlStrcmp (xml_root->name, (const xmlChar *)"dbtransformer") != 0) + { + g_warning ("Invalid xml root element."); + return FALSE; + } + else + { + return TRUE; + } +} + +void +dbt_set_xml (Dbt *dbt, xmlNodePtr xml_root) +{ + DbtPrivate *priv; + + g_return_if_fail (IS_DBT (dbt)); + g_return_if_fail (xml_root != NULL); + + priv = DBT_GET_PRIVATE (dbt); + + priv->xml_root = xml_root; + if (!dbt_check_xml_root (priv->xml_root)) + { + priv->xml_root = NULL; + } +} + +void +dbt_set_xml_from_filename (Dbt *dbt, const gchar *filename) +{ + DbtPrivate *priv; + + gchar *filen; + + xmlDocPtr xdoc; + + g_return_if_fail (IS_DBT (dbt)); + g_return_if_fail (filename != NULL); + + filen = g_strstrip (g_strdup (filen)); + + g_return_if_fail (g_strcmp0 (filen, "") != 0); + + /* TODO validate against DTD */ + /* TODO allow include */ + + xdoc = xmlParseFile (filen); + if (xdoc == NULL) + { + g_warning ("Unable to parse the file."); + return; + } + + priv->xml_root = xmlDocGetRootElement (xdoc); + if (priv->xml_root == NULL) + { + g_warning ("No root element in xml file."); + return; + } + + if (!dbt_check_xml_root (priv->xml_root)) + { + priv->xml_root = NULL; + } +} + +static gboolean +dbt_check_db (Dbt *dbt) +{ + DbtPrivate *priv; + + GdaSqlParser *parser; + GdaStatement *stmt; + GError *error; + + gchar *sql; + + gboolean ret; + + ret = FALSE; + + priv = DBT_GET_PRIVATE (dbt); + + parser = g_object_get_data (G_OBJECT (priv->gda_conn), "parser"); + if (!parser) + { + g_warning ("Invalid sql parser."); + return ret; + } + + /* dbtransformer tables check */ + sql = "SELECT id FROM dbt_transformations WHERE 0=1"; + error = NULL; + stmt = gda_sql_parser_parse_string (parser, sql, NULL, &error); + if (stmt == NULL) + { + /* table doesn't exist */ + GdaServerProvider *provider; + GdaServerOperation *op; + gint i; + + /* create a new GdaServerOperation object */ + provider = gda_connection_get_provider (priv->gda_conn); + 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"); + return ret; + } + + /* Set parameter's values */ + /* table name */ + gda_server_operation_set_value_at (op, "dbt_transformations", &error, "/TABLE_DEF_P/TABLE_NAME"); + + /* "id' field */ + i = 0; + gda_server_operation_set_value_at (op, "id", &error, "/FIELDS_A/@COLUMN_NAME/%d", i); + gda_server_operation_set_value_at (op, "integer", &error, "/FIELDS_A/@COLUMN_TYPE/%d", i); + gda_server_operation_set_value_at (op, "TRUE", &error, "/FIELDS_A/@COLUMN_PKEY/%d", i); + + /* Actually 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"); + return ret; + } + g_object_unref (op); + } + + ret = TRUE; + + return ret; +} + +void +dbt_exec (Dbt *dbt) +{ + DbtPrivate *priv; + + g_return_if_fail (IS_DBT (dbt)); + + priv = DBT_GET_PRIVATE (dbt); + + g_return_if_fail (priv->gda_conn != NULL); + g_return_if_fail (priv->xml_root != NULL); + + if (!dbt_check_db (dbt)) + { + return; + } +} + /* PRIVATE */ static void dbt_set_property (GObject *object, diff --git a/src/libdbtransformer.h b/src/libdbtransformer.h index 595e267..4bf4f8c 100644 --- a/src/libdbtransformer.h +++ b/src/libdbtransformer.h @@ -53,6 +53,14 @@ GType dbt_get_type (void) G_GNUC_CONST; Dbt *dbt_new (void); +void dbt_set_db_connection (Dbt *dbt, GdaConnection *gda_conn); +void dbt_set_db_connection_from_string (Dbt *dbt, const gchar *cnc_string); + +void dbt_set_xml (Dbt *dbt, xmlNodePtr xml_root); +void dbt_set_xml_from_filename (Dbt *dbt, const gchar *filename); + +void dbt_exec (Dbt *dbt); + G_END_DECLS