1 /*
2  * Copyright (C) 2007 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 #include <libgda/libgda.h>
19 #include <libgda-report/libgda-report.h>
20 #include <sql-parser/gda-sql-parser.h>
21 
22 GdaConnection *open_connection (void);
23 GSList        *create_queries (GdaConnection *cnc);
24 
25 int
main(int argc,char ** argv)26 main (int argc, char **argv)
27 {
28 	GdaReportEngine *eng;
29 	GdaConnection *cnc;
30 	GdaHolder *param;
31 	GdaReportDocument *doc;
32 
33 	gda_init ();
34 
35 	/* Doc object */
36 	doc = gda_report_rml_document_new (NULL);
37 	g_object_get (G_OBJECT (doc), "engine", &eng, NULL);
38 	gda_report_document_set_template (doc, "customers-report-rml.rml");
39 
40 	/* GdaConnection */
41 	cnc = open_connection ();
42 	gda_report_engine_declare_object (eng, G_OBJECT (cnc), "main_cnc");
43 
44 	/* define parameters */
45 	param = gda_holder_new_string ("abstract", "-- This text is from a parameter set in the code, not in the spec. file --");
46 	gda_report_engine_declare_object (eng, G_OBJECT (param), "abstract");
47 	g_object_unref (param);
48 
49 	/* create queries */
50 	GSList *queries, *list;
51 	queries = create_queries (cnc);
52 	for (list = queries; list; list = list->next) {
53 		gda_report_engine_declare_object (eng, G_OBJECT (list->data), g_object_get_data (G_OBJECT (list->data), "name"));
54 		g_object_unref (G_OBJECT (list->data));
55 	}
56 	g_slist_free (queries);
57 	g_object_unref (eng);
58 
59 	/* use the doc object */
60 	GError *error = NULL;
61 	gchar *outfile = "customers-report-rml.pdf";
62 	if (! (gda_report_document_run_as_pdf (doc, outfile, &error))) {
63 		g_print ("gda_report_document_run_as_pdf error: %s\n",
64 			 error && error->message ? error->message : "No detail");
65 		exit (1);
66 	}
67 	else
68 		g_print ("%s file generated\n", outfile);
69 
70 #ifdef HTML
71 	outfile = "customers-report-rml.html";
72 	if (! (gda_report_document_run_as_html (doc, outfile, &error))) {
73 		g_print ("gda_report_document_run_as_html error: %s\n",
74 			 error && error->message ? error->message : "No detail");
75 		exit (1);
76 	}
77 	else
78 		g_print ("%s file generated\n", outfile);
79 #endif
80 
81 	g_object_unref (cnc);
82 	g_object_unref (doc);
83 
84 	return 0;
85 }
86 
87 GdaConnection *
open_connection(void)88 open_connection (void)
89 {
90         GdaConnection *cnc;
91         GError *error = NULL;
92         cnc = gda_connection_open_from_dsn ("SalesTest", NULL,
93 					    GDA_CONNECTION_OPTIONS_NONE,
94 					    &error);
95         if (!cnc) {
96                 g_print ("Could not open connection to DSN 'SalesTest': %s\n",
97                          error && error->message ? error->message : "No detail");
98                 exit (1);
99         }
100         return cnc;
101 }
102 
103 GSList *
create_queries(GdaConnection * cnc)104 create_queries (GdaConnection *cnc)
105 {
106 	GdaSqlParser *parser;
107 	GdaStatement *stmt;
108 	GSList *list = NULL;
109 
110 	parser = gda_connection_create_parser (cnc);
111 
112 	stmt = gda_sql_parser_parse_string (parser, "SELECT * FROM customers", NULL, NULL);
113 	g_object_set_data ((GObject*) stmt, "name", "customers");
114 	list = g_slist_prepend (list, stmt);
115 
116 
117 	stmt = gda_sql_parser_parse_string (parser, "SELECT s.* FROM salesrep s "
118 					"INNER JOIN customers c ON (s.id=c.default_served_by) "
119 					    "WHERE c.id=## /* name:\"customers/id\" type:gint */", NULL, NULL);
120 	g_object_set_data ((GObject*) stmt, "name", "salesrep_for_customer");
121 	list = g_slist_prepend (list, stmt);
122 
123 	g_object_unref (parser);
124 
125 	return list;
126 }
127