· 8 years ago · Jan 26, 2018, 09:24 AM
1/*
2 * Copyright (C) 2007 - 2008 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 "common.h"
20#include <sql-parser/gda-sql-parser.h>
21
22GdaConnection *
23open_source_connection (void)
24{
25 GdaConnection *cnc;
26 GError *error = NULL;
27 cnc = gda_connection_open_from_dsn ("SalesTest", NULL,
28 GDA_CONNECTION_OPTIONS_NONE,
29 &error);
30 if (!cnc) {
31 g_print ("Could not open connection to DSN 'SalesTest': %s\n",
32 error && error->message ? error->message : "No detail");
33 exit (1);
34 }
35 return cnc;
36}
37
38GdaConnection *
39open_destination_connection (void)
40{
41 /* create connection */
42 GdaConnection *cnc;
43 GError *error = NULL;
44 cnc = gda_connection_open_from_string ("SQLite", "DB_DIR=.;DB_NAME=copy",
45 NULL,
46 GDA_CONNECTION_OPTIONS_NONE,
47 &error);
48 if (!cnc) {
49 g_print ("Could not open connection to local SQLite database: %s\n",
50 error && error->message ? error->message : "No detail");
51 exit (1);
52 }
53
54 /* table "products_copied1" */
55 run_sql_non_select (cnc, "DROP table IF EXISTS products_copied1");
56 run_sql_non_select (cnc, "CREATE table products_copied1 (ref string not null primary key, "
57 "name string not null, price real, wh_stored integer)");
58
59 /* table "products_copied2" */
60 run_sql_non_select (cnc, "DROP table IF EXISTS products_copied2");
61 run_sql_non_select (cnc, "CREATE table products_copied2 (ref string not null primary key, "
62 "name string not null, price real, wh_stored integer)");
63
64 /* table "products_copied3" */
65 run_sql_non_select (cnc, "DROP table IF EXISTS products_copied3");
66 run_sql_non_select (cnc, "CREATE table products_copied3 (ref string not null primary key, "
67 "name string not null, price real, wh_stored integer)");
68
69 return cnc;
70}
71
72void
73run_sql_non_select (GdaConnection *cnc, const gchar *sql)
74{
75 GdaStatement *stmt;
76 GError *error = NULL;
77 gint nrows;
78 GdaSqlParser *parser;
79
80 parser = gda_connection_create_parser (cnc);
81 stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
82 g_object_unref (parser);
83
84 nrows = gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error);
85 g_object_unref (stmt);
86 if (nrows == -1)
87 g_error ("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
88}