1 #include "babel/babel_handler.h"
7 #include <glib/gprintf.h>
8 #include <libgda/libgda.h>
9 #include <sql-parser/gda-sql-parser.h>
11 typedef struct _metadata {
12 const gchar *element_name;
20 GMarkupParseContext *context,
21 const gchar *element_name,
22 const gchar **attribute_names,
23 const gchar **attribute_values,
27 metadata *md = (metadata*) data;
28 md->element_name = element_name;
30 if( !strcmp(element_name, "ifindex") ) {
31 md->ifid = g_strdup("");
32 md->title = g_strdup("");
33 md->author = g_strdup("");
34 md->year = g_strdup("");
39 GMarkupParseContext *context,
45 metadata *md = (metadata*) data;
48 if( !strcmp(md->element_name, "ifid") ) {
49 stripped_text = g_strstrip( g_strndup(text, text_len) );
50 md->ifid = g_strconcat(md->ifid, stripped_text, NULL);
51 g_free(stripped_text);
53 else if( !strcmp(md->element_name, "title") ) {
54 stripped_text = g_strstrip( g_strndup(text, text_len) );
55 md->title = g_strconcat(md->title, stripped_text, NULL);
56 g_free(stripped_text);
58 else if( !strcmp(md->element_name, "author") ) {
59 stripped_text = g_strstrip( g_strndup(text, text_len) );
60 md->author = g_strconcat(md->author, stripped_text, NULL);
61 g_free(stripped_text);
63 else if( !strcmp(md->element_name, "firstpublished") ) {
64 stripped_text = g_strstrip( g_strndup(text, text_len) );
65 md->year = g_strconcat(md->year, stripped_text, NULL);
66 g_free(stripped_text);
71 GMarkupParseContext *context,
72 const gchar *element_name,
76 if( !strcmp(element_name, "ifindex") ) {
77 metadata *md = (metadata*) data;
78 printf("IFID: %s\nTitle: %s\nAuthor: %s\nYear: %s\n", md->ifid, md->title, md->author, md->year);
83 * run a non SELECT command and stops if an error occurs
86 run_sql_non_select(GdaConnection *cnc, const gchar *sql)
94 parser = g_object_get_data(G_OBJECT(cnc), "parser");
95 stmt = gda_sql_parser_parse_string(parser, sql, &remain, &error);
97 g_print ("REMAINS: %s\n", remain);
99 nrows = gda_connection_statement_execute_non_select(cnc, stmt, NULL, NULL, &error);
101 g_error("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
102 g_object_unref(stmt);
105 int main(int argc, char **argv) {
107 fprintf(stderr, "Usage: %s <story file>\n", argv[0]);
112 int len = babel_treaty(GET_STORY_FILE_METADATA_EXTENT_SEL, NULL, 0);
114 printf("No metadata found.\n");
119 gchar *buffer = malloc(len * sizeof(gchar));
120 babel_treaty(GET_STORY_FILE_METADATA_SEL, buffer, len);
122 len = strlen(buffer);
125 GMarkupParser xml_parser = {start_element, end_element, text, NULL, NULL};
126 GMarkupParseContext *context = g_markup_parse_context_new(&xml_parser, 0, &data, NULL);
129 if( g_markup_parse_context_parse(context, buffer, len, &err) == FALSE ) {
130 fprintf(stderr, "Metadata parse failed: %s\n", err->message);
134 g_markup_parse_context_free(context);
137 // Open DB connection
139 GdaSqlParser *sql_parser;
142 cnc = gda_connection_open_from_string("SQLite", "DB_DIR=.;DB_NAME=library", NULL, GDA_CONNECTION_OPTIONS_NONE, &err);
144 fprintf(stderr, "Could not open connection to SQLite database in library.db file: %s\n", err && err->message ? err->message : "No details");
148 sql_parser = gda_connection_create_parser(cnc);
149 if(!sql_parser) // cnc does not provide its own parser, use default one
150 sql_parser = gda_sql_parser_new();
152 g_object_set_data_full(G_OBJECT(cnc), "parser", sql_parser, g_object_unref);
154 // Create stories table
155 run_sql_non_select(cnc, "DROP TABLE IF EXISTS stories");
156 run_sql_non_select(cnc, "CREATE TABLE stories (ifid text not null primary key, title text, author text, year integer)");
158 // Populate the table
159 GValue *v1, *v2, *v3, *v4;
160 v1 = gda_value_new_from_string(data.ifid, G_TYPE_STRING);
161 v2 = gda_value_new_from_string(data.title, G_TYPE_STRING);
162 v3 = gda_value_new_from_string(data.author, G_TYPE_STRING);
163 v4 = gda_value_new_from_string(data.year, G_TYPE_UINT);
165 if( !gda_insert_row_into_table(cnc, "stories", &err, "ifid", v1, "title", v2, "author", v3, "year", v4, NULL) ) {
166 g_error("Could not INSERT data into the 'stories' table: %s\n", err && err->message ? err->message : "No details");
175 gda_connection_close(cnc);