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 stripped_text = g_strndup(text, text_len);
51 md->ifid = g_strconcat(md->ifid, stripped_text, NULL);
52 g_free(stripped_text);
54 else if( !strcmp(md->element_name, "title") ) {
55 //stripped_text = g_strstrip( g_strndup(text, text_len) );
56 stripped_text = g_strndup(text, text_len);
57 md->title = g_strconcat(md->title, stripped_text, NULL);
58 g_free(stripped_text);
60 else if( !strcmp(md->element_name, "author") ) {
61 //stripped_text = g_strstrip( g_strndup(text, text_len) );
62 stripped_text = g_strndup(text, text_len);
63 md->author = g_strconcat(md->author, stripped_text, NULL);
64 g_free(stripped_text);
66 else if( !strcmp(md->element_name, "firstpublished") ) {
67 //stripped_text = g_strstrip( g_strndup(text, text_len) );
68 stripped_text = g_strndup(text, text_len);
69 md->year = g_strconcat(md->year, stripped_text, NULL);
70 g_free(stripped_text);
75 GMarkupParseContext *context,
76 const gchar *element_name,
80 if( !strcmp(element_name, "ifindex") ) {
81 metadata *md = (metadata*) data;
82 printf("IFID: %s\nTitle: %s\nAuthor: %s\nYear: %s\n", md->ifid, md->title, md->author, md->year);
87 * run a non SELECT command and stops if an error occurs
90 run_sql_non_select(GdaConnection *cnc, const gchar *sql)
98 parser = g_object_get_data(G_OBJECT(cnc), "parser");
99 stmt = gda_sql_parser_parse_string(parser, sql, &remain, &error);
101 g_print ("REMAINS: %s\n", remain);
103 nrows = gda_connection_statement_execute_non_select(cnc, stmt, NULL, NULL, &error);
105 g_error("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
106 g_object_unref(stmt);
109 int main(int argc, char **argv) {
111 fprintf(stderr, "Usage: %s <story file>\n", argv[0]);
116 int len = babel_treaty(GET_STORY_FILE_METADATA_EXTENT_SEL, NULL, 0);
118 printf("No metadata found.\n");
123 gchar *buffer = malloc(len * sizeof(gchar));
124 babel_treaty(GET_STORY_FILE_METADATA_SEL, buffer, len);
126 len = strlen(buffer);
129 GMarkupParser xml_parser = {start_element, end_element, text, NULL, NULL};
130 GMarkupParseContext *context = g_markup_parse_context_new(&xml_parser, 0, &data, NULL);
133 if( g_markup_parse_context_parse(context, buffer, len, &err) == FALSE ) {
134 fprintf(stderr, "Metadata parse failed: %s\n", err->message);
138 g_markup_parse_context_free(context);
141 // Open DB connection
143 GdaSqlParser *sql_parser;
146 cnc = gda_connection_open_from_string("SQLite", "DB_DIR=.;DB_NAME=library", NULL, GDA_CONNECTION_OPTIONS_NONE, &err);
148 fprintf(stderr, "Could not open connection to SQLite database in library.db file: %s\n", err && err->message ? err->message : "No details");
152 sql_parser = gda_connection_create_parser(cnc);
153 if(!sql_parser) // cnc does not provide its own parser, use default one
154 sql_parser = gda_sql_parser_new();
156 g_object_set_data_full(G_OBJECT(cnc), "parser", sql_parser, g_object_unref);
158 // Create stories table
159 //run_sql_non_select(cnc, "DROP TABLE IF EXISTS stories");
160 run_sql_non_select(cnc, "CREATE TABLE IF NOT EXISTS stories (ifid text not null primary key, title text, author text, year integer)");
162 // Populate the table
163 GValue *v1, *v2, *v3, *v4;
164 v1 = gda_value_new_from_string(data.ifid, G_TYPE_STRING);
165 v2 = gda_value_new_from_string(data.title, G_TYPE_STRING);
166 v3 = gda_value_new_from_string(data.author, G_TYPE_STRING);
167 v4 = gda_value_new_from_string(data.year, G_TYPE_UINT);
169 if( !gda_insert_row_into_table(cnc, "stories", &err, "ifid", v1, "title", v2, "author", v3, "year", v4, NULL) ) {
170 g_warning("Could not INSERT data into the 'stories' table: %s\n", err && err->message ? err->message : "No details");
178 // Dump the table contents
179 GdaDataModel *data_model;
180 GdaStatement *stmt = gda_sql_parser_parse_string(sql_parser, "SELECT * FROM stories", NULL, NULL);
181 data_model = gda_connection_statement_execute_select(cnc, stmt, NULL, &err);
183 g_error("Could not get the contents of the 'stories' table: %s\n", err && err->message ? err->message : "No details");
184 printf("Dumping library table:\n");
185 gda_data_model_dump(data_model, stdout);
187 g_object_unref(stmt);
188 g_object_unref(data_model);
190 gda_connection_close(cnc);