1 /*
2 * Copyright (C) 2009 - 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 <sql-parser/gda-sql-parser.h>
20
21 GdaSqlParser *parser;
22
23 GdaConnection *open_connection (void);
24 void display_customers (GdaConnection *cnc);
25 void run_sql_non_select (GdaConnection *cnc, const gchar *sql);
26
27 int
main(int argc,char * argv[])28 main (int argc, char *argv[])
29 {
30 gda_init ();
31
32 GdaConnection *cnc;
33 GError *error = NULL;
34 GdaStatement *stmt;
35 GdaDataModel *model;
36 gchar *str;
37 GValue *name;
38
39 /* open connection */
40 cnc = open_connection ();
41
42 /* begin transaction */
43 if (! gda_connection_begin_transaction (cnc, NULL, GDA_TRANSACTION_ISOLATION_UNKNOWN,
44 &error)) {
45 g_print ("Could not begin transaction: %s\n",
46 error && error->message ? error->message : "No detail");
47 exit (1);
48 }
49
50 /* execute SELECT */
51 stmt = gda_sql_parser_parse_string (parser, "SELECT id, name FROM customers ORDER BY id", NULL, NULL);
52 g_assert (stmt);
53 model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
54 g_object_unref (stmt);
55 if (!model) {
56 g_print ("Could not execute SELECT statement: %s\n",
57 error && error->message ? error->message : "No detail");
58 exit (1);
59 }
60
61 g_print ("** Data model is:\n");
62 gda_data_model_dump (model, stdout);
63
64 /*
65 * make sure the mete data is up to date
66 */
67 g_print ("Computing meta data, this may take a while; in real applications, this should be cached to avoid waiting\n");
68 if (! gda_connection_update_meta_store (cnc, NULL, &error)) {
69 g_print ("Could not fetch meta data: %s\n",
70 error && error->message ? error->message : "No detail");
71 exit (1);
72 }
73
74 /*
75 * Make the data model compute the modification statements which
76 * will actually be executed when the data model is modified
77 */
78 if (! gda_data_select_compute_modification_statements (GDA_DATA_SELECT (model), &error)) {
79 g_print ("Could not compute modification statements: %s\n",
80 error && error->message ? error->message : "No detail");
81 exit (1);
82 }
83
84 g_object_get (G_OBJECT (model), "update-stmt", &stmt, NULL);
85 str = gda_statement_to_sql (stmt, NULL, NULL);
86 g_print ("Computed UPDATE: %s\n", str);
87 g_free (str);
88 g_object_unref (stmt);
89 g_object_get (G_OBJECT (model), "delete-stmt", &stmt, NULL);
90 str = gda_statement_to_sql (stmt, NULL, NULL);
91 g_print ("Computed DELETE: %s\n", str);
92 g_free (str);
93 g_object_unref (stmt);
94 g_object_get (G_OBJECT (model), "insert-stmt", &stmt, NULL);
95 str = gda_statement_to_sql (stmt, NULL, NULL);
96 g_print ("Computed INSERT: %s\n", str);
97 g_free (str);
98 g_object_unref (stmt);
99
100 /*
101 * remove row 0 (1st row)
102 */
103 g_print ("\n\n** Removing row 0\n");
104 if (! gda_data_model_remove_row (model, 0, &error)) {
105 g_print ("Could not remove row 0: %s\n",
106 error && error->message ? error->message : "No detail");
107 exit (1);
108 }
109 g_print ("** Data model is now:\n");
110 gda_data_model_dump (model, stdout);
111 g_print ("** Table's contents is now:\n");
112 display_customers (cnc);
113
114 /*
115 * add a row: the row's values is a list of GValue pointers
116 * (or NULL pointers where the default value should be inserted
117 */
118 GList *list;
119 g_print ("\n\n** Adding a row\n");
120 list = g_list_append (NULL, NULL);
121 g_value_set_string ((name = gda_value_new (G_TYPE_STRING)), "Hiro");
122 list = g_list_append (list, name);
123 if (gda_data_model_append_values (model, list, &error) == -1) {
124 g_print ("Could not add a row: %s\n",
125 error && error->message ? error->message : "No detail");
126 exit (1);
127 }
128 gda_value_free (name);
129 g_list_free (list);
130 g_print ("** Data model is now:\n");
131 gda_data_model_dump (model, stdout);
132 g_print ("** Table's contents is now:\n");
133 display_customers (cnc);
134
135 /*
136 * alter row 2
137 */
138 g_print ("\n\n** Modifying row 2\n");
139 g_value_set_string ((name = gda_value_new (G_TYPE_STRING)), "Tom");
140 if (! gda_data_model_set_value_at (model, 1, 2, name, &error)) {
141 g_print ("Could not modify row 2: %s\n",
142 error && error->message ? error->message : "No detail");
143 exit (1);
144 }
145 gda_value_free (name);
146 g_print ("** Data model is now:\n");
147 gda_data_model_dump (model, stdout);
148 g_print ("** Table's contents is now:\n");
149 display_customers (cnc);
150
151 /* rollback transaction */
152 gda_connection_rollback_transaction (cnc, NULL, NULL);
153
154 gda_connection_close (cnc);
155
156 return 0;
157 }
158
159 /*
160 * Open a connection to the example.db file
161 */
162 GdaConnection *
open_connection()163 open_connection ()
164 {
165 GdaConnection *cnc;
166 GError *error = NULL;
167
168 /* open connection */
169 cnc = gda_connection_open_from_dsn ("SalesTest", NULL,
170 GDA_CONNECTION_OPTIONS_NONE,
171 &error);
172 if (!cnc) {
173 g_print ("Could not open connection: %s\n",
174 error && error->message ? error->message : "No detail");
175 exit (1);
176 }
177
178 /* create an SQL parser */
179 parser = gda_connection_create_parser (cnc);
180 if (!parser) /* @cnc does not provide its own parser => use default one */
181 parser = gda_sql_parser_new ();
182 /* attach the parser object to the connection */
183 g_object_set_data_full (G_OBJECT (cnc), "parser", parser, g_object_unref);
184
185 return cnc;
186 }
187
188 /*
189 * display the contents of the 'products' table
190 */
191 void
display_customers(GdaConnection * cnc)192 display_customers (GdaConnection *cnc)
193 {
194 GdaDataModel *data_model;
195 GdaSqlParser *parser;
196 GdaStatement *stmt;
197 gchar *sql = "SELECT id, name FROM customers ORDER BY id";
198 GError *error = NULL;
199
200 parser = g_object_get_data (G_OBJECT (cnc), "parser");
201 stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
202 data_model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
203 g_object_unref (stmt);
204 if (!data_model)
205 g_error ("Could not get the contents of the 'products' table: %s\n",
206 error && error->message ? error->message : "No detail");
207 gda_data_model_dump (data_model, stdout);
208 g_object_unref (data_model);
209 }
210