1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/*
* Copyright (C) 2007 - 2012 Vivien Malerba <malerba@gnome-db.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <libgda/libgda.h>
#include <virtual/libgda-virtual.h>
#include <sql-parser/gda-sql-parser.h>
#include <stdlib.h>
#include <string.h>
static GdaDataModel *run_sql_select (GdaConnection *cnc, const gchar *sql);
static gboolean run_sql_non_select (GdaConnection *cnc, const gchar *sql);
static gboolean test1 (void);
static gboolean test2 (void);
int
main (int argc, char **argv)
{
gint nfailed = 0;
gda_init ();
if (! test1 ())
nfailed++;
if (! test2 ())
nfailed++;
if (nfailed == 0) {
g_print ("Ok, all tests passed\n");
return EXIT_SUCCESS;
}
else {
g_print ("%d failed\n", nfailed);
return EXIT_FAILURE;
}
}
static gboolean
test1 (void)
{
GError *error = NULL;
GdaConnection *cnc;
GdaVirtualProvider *provider;
GdaDataModel *rw_model;
gchar *file;
gboolean retval = FALSE;
g_print ("===== %s() =====\n", __FUNCTION__);
provider = gda_vprovider_data_model_new ();
cnc = gda_virtual_connection_open (provider, NULL);
g_assert (cnc);
/* create RW data model to store results */
rw_model = gda_data_model_array_new_with_g_types (2, G_TYPE_STRING, G_TYPE_INT);
gda_data_model_set_column_name (rw_model, 0, "country");
gda_data_model_set_column_name (rw_model, 1, "population");
/* load CSV data models */
GdaDataModel *country_model, *city_model;
GdaSet *options = gda_set_new_inline (1, "TITLE_AS_FIRST_LINE", G_TYPE_BOOLEAN, TRUE);
file = g_build_filename (CHECK_FILES, "tests", "data-models", "city.csv", NULL);
city_model = gda_data_model_import_new_file (file, TRUE, options);
g_free (file);
file = g_build_filename (CHECK_FILES, "tests", "data-models", "country.csv", NULL);
country_model = gda_data_model_import_new_file (file, TRUE, options);
g_free (file);
file = NULL;
g_object_unref (options);
/* Add data models to connection */
if (!gda_vconnection_data_model_add_model (GDA_VCONNECTION_DATA_MODEL (cnc), rw_model, "results", &error))
g_error ("Add RW model error: %s\n", error && error->message ? error->message : "no detail");
if (!gda_vconnection_data_model_add_model (GDA_VCONNECTION_DATA_MODEL (cnc), city_model, "city", &error))
g_error ("Add city model error: %s\n", error && error->message ? error->message : "no detail");
if (!gda_vconnection_data_model_add_model (GDA_VCONNECTION_DATA_MODEL (cnc), country_model, "country", &error))
g_error ("Add country model error: %s\n", error && error->message ? error->message : "no detail");
/* test */
GdaDataModel *model;
model = run_sql_select (cnc, "SELECT * FROM city");
gda_data_model_dump (model, stdout);
g_object_unref (model);
model = run_sql_select (cnc, "SELECT __gda_row_nb, * FROM city");
gda_data_model_dump (model, stdout);
g_object_unref (model);
/* compute list of countries where population >= 1M */
if (! run_sql_non_select (cnc, "INSERT INTO results SELECT co.name, sum (ci.population) as pop FROM country co INNER JOIN city ci ON (co.code=ci.countrycode) GROUP BY co.name HAVING sum (ci.population) > 10000000 ORDER BY co.name DESC"))
g_error ("Could not run computation query");
/* save resulting data model to CSV */
gchar *export, *expected;
export = gda_data_model_export_to_string (rw_model, GDA_DATA_MODEL_IO_TEXT_SEPARATED,
NULL, 0, NULL, 0, NULL);
file = g_build_filename (CHECK_FILES, "tests", "data-models", "check_virtual.csv", NULL);
if (!g_file_get_contents (file, &expected, NULL, NULL))
goto out;
if (strcmp (export, expected))
goto out;
retval = TRUE;
out:
g_free (file);
g_free (export);
g_free (expected);
g_object_unref (city_model);
g_object_unref (country_model);
g_object_unref (cnc);
g_object_unref (provider);
g_print ("%s() is %s\n", __FUNCTION__, retval ? "Ok" : "NOT Ok");
return retval;
}
static gboolean
test2 (void)
{
GError *error = NULL;
GdaConnection *cnc;
GdaVirtualProvider *provider;
gchar *file;
gboolean retval = FALSE;
g_print ("===== %s () =====\n", __FUNCTION__);
provider = gda_vprovider_data_model_new ();
cnc = gda_virtual_connection_open (provider, NULL);
g_assert (cnc);
/* load CSV data models */
GdaDataModel *country_model, *city_model;
GdaSet *options = gda_set_new_inline (1, "TITLE_AS_FIRST_LINE", G_TYPE_BOOLEAN, TRUE);
file = g_build_filename (CHECK_FILES, "tests", "data-models", "city.csv", NULL);
city_model = gda_data_model_import_new_file (file, TRUE, options);
g_free (file);
file = g_build_filename (CHECK_FILES, "tests", "data-models", "country.csv", NULL);
country_model = gda_data_model_import_new_file (file, TRUE, options);
g_free (file);
g_object_unref (options);
/* Add data models to connection */
if (!gda_vconnection_data_model_add_model (GDA_VCONNECTION_DATA_MODEL (cnc), city_model, "city", &error))
g_error ("Add city model error: %s\n", error && error->message ? error->message : "no detail");
if (!gda_vconnection_data_model_add_model (GDA_VCONNECTION_DATA_MODEL (cnc), country_model, "country", &error))
g_error ("Add country model error: %s\n", error && error->message ? error->message : "no detail");
/* test */
GdaDataModel *model;
model = run_sql_select (cnc, "SELECT c.*, o.name FROM city c INNER JOIN country o ON (c.countrycode = o.code)");
gda_data_model_dump (model, stdout);
g_object_unref (model);
retval = TRUE;
g_object_unref (city_model);
g_object_unref (country_model);
g_object_unref (cnc);
g_object_unref (provider);
g_print ("%s() is %s\n", __FUNCTION__, retval ? "Ok" : "NOT Ok");
return retval;
}
static GdaDataModel *
run_sql_select (GdaConnection *cnc, const gchar *sql)
{
GdaStatement *stmt;
GError *error = NULL;
GdaDataModel *res;
GdaSqlParser *parser;
parser = gda_connection_create_parser (cnc);
stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
g_object_unref (parser);
g_print ("EXECUTING [%s]\n", sql);
res = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
g_object_unref (stmt);
if (!res)
g_error ("Could not execute query: %s\n",
error && error->message ? error->message : "no detail");
return res;
}
static gboolean
run_sql_non_select (GdaConnection *cnc, const gchar *sql)
{
GdaStatement *stmt;
GError *error = NULL;
gint nrows;
GdaSqlParser *parser;
parser = gda_connection_create_parser (cnc);
stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
g_object_unref (parser);
g_print ("EXECUTING [%s]\n", sql);
nrows = gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error);
g_object_unref (stmt);
if (nrows == -1) {
g_print ("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
if (error)
g_error_free (error);
return FALSE;
}
return TRUE;
}
|