Completed babeltest. It now opens the library.bdf in the current directory (or create...
[projects/chimara/chimara.git] / tests / babeltest.c
1 #include "babel/babel_handler.h"
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <glib.h>
7 #include <glib/gprintf.h>
8 #include <libgda/libgda.h>
9 #include <sql-parser/gda-sql-parser.h>
10
11 typedef struct _metadata {
12         const gchar *element_name;
13         gchar *ifid;
14         gchar *title;
15         gchar *author;
16         gchar *year;
17 } metadata;
18
19 void start_element(
20         GMarkupParseContext *context,
21         const gchar *element_name,
22         const gchar **attribute_names,
23         const gchar **attribute_values,
24         gpointer data,
25         GError **error)
26 {
27         metadata *md = (metadata*) data;
28         md->element_name = element_name;
29
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("");
35         }
36 }
37
38 void text(
39         GMarkupParseContext *context,
40         const gchar *text,
41         gsize text_len,
42         gpointer data,
43         GError **error)
44 {
45         metadata *md = (metadata*) data;
46         gchar *stripped_text;
47
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);
53         }
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);
59         }
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);
65         }
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);
71         }
72 }
73
74 void end_element(
75         GMarkupParseContext *context,
76         const gchar *element_name,
77         gpointer data,
78         GError **error)
79 {
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);
83         }
84 }
85
86 /*
87  * run a non SELECT command and stops if an error occurs
88  */
89 void
90 run_sql_non_select(GdaConnection *cnc, const gchar *sql)
91 {
92         GdaStatement *stmt;
93         GError *error = NULL;
94         gint nrows;
95         const gchar *remain;
96         GdaSqlParser *parser;
97
98         parser = g_object_get_data(G_OBJECT(cnc), "parser");
99         stmt = gda_sql_parser_parse_string(parser, sql, &remain, &error);
100         if(remain) 
101                 g_print ("REMAINS: %s\n", remain);
102
103         nrows = gda_connection_statement_execute_non_select(cnc, stmt, NULL, NULL, &error);
104         if(nrows == -1)
105                 g_error("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
106         g_object_unref(stmt);
107 }
108
109 int main(int argc, char **argv) {
110         if(argc < 2) {
111                 fprintf(stderr, "Usage: %s <story file>\n", argv[0]);
112                 return 1;
113         }
114
115         babel_init(argv[1]);
116         int len = babel_treaty(GET_STORY_FILE_METADATA_EXTENT_SEL, NULL, 0);
117         if(len == 0) {
118                 printf("No metadata found.\n");
119                 babel_release();
120                 return 0;
121         }
122
123         gchar *buffer = malloc(len * sizeof(gchar));
124         babel_treaty(GET_STORY_FILE_METADATA_SEL, buffer, len);
125         g_strchomp(buffer);
126         len = strlen(buffer);
127
128         metadata data;
129         GMarkupParser xml_parser = {start_element, end_element, text, NULL, NULL};
130         GMarkupParseContext *context = g_markup_parse_context_new(&xml_parser, 0, &data, NULL);
131
132         GError *err = NULL;
133         if( g_markup_parse_context_parse(context, buffer, len, &err) == FALSE ) {
134                 fprintf(stderr, "Metadata parse failed: %s\n", err->message);
135         }
136
137         free(buffer);
138         g_markup_parse_context_free(context);
139         babel_release();
140
141         // Open DB connection
142         GdaConnection *cnc;
143         GdaSqlParser *sql_parser;
144
145         gda_init();
146         cnc = gda_connection_open_from_string("SQLite", "DB_DIR=.;DB_NAME=library", NULL, GDA_CONNECTION_OPTIONS_NONE, &err);
147         if(!cnc) {
148                 fprintf(stderr, "Could not open connection to SQLite database in library.db file: %s\n", err && err->message ? err->message : "No details");
149                 return 1;
150         }
151
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();
155
156         g_object_set_data_full(G_OBJECT(cnc), "parser", sql_parser, g_object_unref);
157         
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)");
161
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);
168
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");
171         }
172
173         gda_value_free(v1);
174         gda_value_free(v2);
175         gda_value_free(v3);
176         gda_value_free(v4);
177
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);
182         if(!data_model)
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);
186
187         g_object_unref(stmt);
188         g_object_unref(data_model);
189
190         gda_connection_close(cnc);
191         return 0;
192 }