1 /* 2 * Copyright (C) 2012 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 "common.h" 19 #include <sql-parser/gda-sql-parser.h> 20 21 GdaConnection * 22 ui_tests_common_open_connection (void) 23 { 24 GdaConnection *cnc; 25 gchar *cnc_string, *fname; 26 GError *error = NULL; 27 fname = g_build_filename (ROOT_DIR, "data", NULL); 28 cnc_string = g_strdup_printf ("DB_DIR=%s;DB_NAME=sales_test", fname); 29 g_free (fname); 30 cnc = gda_connection_open_from_string ("SQLite", cnc_string, NULL, 31 GDA_CONNECTION_OPTIONS_READ_ONLY, &error); 32 if (!cnc) { 33 g_print ("Failed to open connection, cnc_string = [%s]: %s\n", cnc_string, 34 error && error->message ? error->message : "No detail"); 35 exit (1); 36 } 37 g_free (cnc_string); 38 return cnc; 39 } 40 41 static GdaStatement * 42 stmt_from_string (const gchar *sql) 43 { 44 GdaStatement *stmt; 45 GError *error = NULL; 46 47 static GdaSqlParser *parser = NULL; 48 if (!parser) 49 parser = gda_sql_parser_new (); 50 51 stmt = gda_sql_parser_parse_string (parser, sql, NULL, &error); 52 if (!stmt) { 53 g_print ("Cound not parse SQL: %s\nSQL was: %s\n", 54 error && error->message ? error->message : "No detail", 55 sql); 56 exit (EXIT_FAILURE); 57 } 58 return stmt; 59 } 60 61 GdaDataModel * 62 ui_tests_common_get_products (GdaConnection *cnc, gboolean part_only) 63 { 64 GError *error = NULL; 65 GdaDataModel *model; 66 GdaStatement *stmt; 67 68 if (part_only) 69 stmt = stmt_from_string ("SELECT * FROM products WHERE price > 20.2 ORDER BY ref, category LIMIT 10"); 70 else 71 stmt = stmt_from_string ("SELECT * FROM products ORDER BY ref, category LIMIT 15"); 72 model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error); 73 if (!model) { 74 g_print ("Failed to get list of products: %s\n", 75 error && error->message ? error->message : "No detail"); 76 exit (1); 77 } 78 79 return model; 80 } 81 82 GdaDataModel * 83 ui_tests_common_get_products_2cols (GdaConnection *cnc, gboolean part_only) 84 { 85 GError *error = NULL; 86 GdaDataModel *model; 87 GdaStatement *stmt; 88 89 if (part_only) 90 stmt = stmt_from_string ("SELECT ref, category FROM products WHERE price > 20.2 ORDER BY ref, category LIMIT 10"); 91 else 92 stmt = stmt_from_string ("SELECT ref, category FROM products ORDER BY ref, category LIMIT 15"); 93 model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error); 94 if (!model) { 95 g_print ("Failed to get list of products: %s\n", 96 error && error->message ? error->message : "No detail"); 97 exit (1); 98 } 99 100 return model; 101 } 102