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 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_docbook_document_new (NULL); 37 g_object_get (G_OBJECT (doc), "engine", &eng, NULL); 38 gda_report_document_set_template (doc, "customers-report-spec.xml"); 39 g_object_set (G_OBJECT (doc), "fo-stylesheet", "/usr/share/xml/docbook/stylesheet/nwalsh/fo/docbook.xsl", NULL); 40 41 /* GdaConnection */ 42 cnc = open_connection (); 43 gda_report_engine_declare_object (eng, G_OBJECT (cnc), "main_cnc"); 44 45 /* define parameters */ 46 param = gda_holder_new_string ("abstract", "-- This text is from a parameter set in the code, not in the spec. file --"); 47 gda_report_engine_declare_object (eng, G_OBJECT (param), "abstract"); 48 g_object_unref (param); 49 50 /* create queries */ 51 GSList *queries, *list; 52 queries = create_queries (cnc); 53 for (list = queries; list; list = list->next) { 54 gda_report_engine_declare_object (eng, G_OBJECT (list->data), g_object_get_data (G_OBJECT (list->data), "name")); 55 g_object_unref (G_OBJECT (list->data)); 56 } 57 g_slist_free (queries); 58 g_object_unref (eng); 59 60 /* use the doc object */ 61 GError *error = NULL; 62 gchar *outfile = "customers-report-docbook.pdf"; 63 if (! (gda_report_document_run_as_pdf (doc, outfile, &error))) { 64 g_print ("gda_report_document_run_as_pdf error: %s\n", 65 error && error->message ? error->message : "No detail"); 66 exit (1); 67 } 68 else 69 g_print ("%s file generated\n", outfile); 70 71 #ifdef HTML 72 outfile = "customers-report-docbook.html"; 73 if (! (gda_report_document_run_as_html (doc, outfile, &error))) { 74 g_print ("gda_report_document_run_as_html error: %s\n", 75 error && error->message ? error->message : "No detail"); 76 exit (1); 77 } 78 else 79 g_print ("%s file generated\n", outfile); 80 #endif 81 82 g_object_unref (cnc); 83 g_object_unref (doc); 84 85 return 0; 86 } 87 88 GdaConnection * 89 open_connection (void) 90 { 91 GdaConnection *cnc; 92 GError *error = NULL; 93 cnc = gda_connection_open_from_dsn ("SalesTest", NULL, 94 GDA_CONNECTION_OPTIONS_NONE, 95 &error); 96 if (!cnc) { 97 g_print ("Could not open connection to DSN 'SalesTest': %s\n", 98 error && error->message ? error->message : "No detail"); 99 exit (1); 100 } 101 return cnc; 102 } 103 104 GSList * 105 create_queries (GdaConnection *cnc) 106 { 107 GdaSqlParser *parser; 108 GdaStatement *stmt; 109 GSList *list = NULL; 110 111 parser = gda_connection_create_parser (cnc); 112 113 stmt = gda_sql_parser_parse_string (parser, "SELECT * FROM customers", NULL, NULL); 114 g_object_set_data ((GObject*) stmt, "name", "customers"); 115 list = g_slist_prepend (list, stmt); 116 117 118 stmt = gda_sql_parser_parse_string (parser, "SELECT s.* FROM salesrep s " 119 "INNER JOIN customers c ON (s.id=c.default_served_by) " 120 "WHERE c.id=## /* name:\"customers/id\" type:gint */", NULL, NULL); 121 g_object_set_data ((GObject*) stmt, "name", "salesrep_for_customer"); 122 list = g_slist_prepend (list, stmt); 123 124 g_object_unref (parser); 125 126 return list; 127 } 128