File: client.c

package info (click to toggle)
libgda2 1.2.4-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 13,724 kB
  • ctags: 6,979
  • sloc: ansic: 47,166; xml: 19,896; sh: 8,797; makefile: 863; yacc: 202; lex: 129; cpp: 73
file content (213 lines) | stat: -rw-r--r-- 7,205 bytes parent folder | download | duplicates (3)
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
/* GDA - Test suite
 * Copyright (C) 1998-2002 The GNOME Foundation
 *
 * AUTHORS:
 *	Rodrigo Moya <rodrigo@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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; see the file COPYING.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <string.h>
#include <libgda/gda-intl.h>
#include "gda-test.h"
#include "postgres-test.h"
#include "freetds-test.h"
#include "sybase-test.h"
#include "ibmdb2-test.h"

/* Shows the object schemas */
static void
show_schema (GdaConnection *cnc, GdaConnectionSchema schema, const gchar *label)
{
	GdaDataModel *model;
	gint r, c;
	gint row_count, col_count;

	model = gda_connection_get_schema (cnc, schema, NULL);
	if (!GDA_IS_DATA_MODEL (model)) {
		g_print ("\t%s\n", label);
		g_print (_("\t\tNONE\n"));
		return;
	}

	row_count = gda_data_model_get_n_rows (model);
	col_count = gda_data_model_get_n_columns (model);
	g_print ("\t%s %d\n", label, row_count);

	for (r = 0; r < row_count; r++) {
		g_print ("\t");
		for (c = 0; c < col_count; c++) {
			const GdaValue *value;
			gchar *str;

			value = gda_data_model_get_value_at (model, c, r);
			str = gda_value_stringify ((GdaValue *) value);
			if (str) {
				g_print ("\t%s", str);
				g_free (str);
			} else {
				g_print("\t??NULL??");
			}
		}
		g_print ("\n");
	}
	
	g_object_unref ((GObject *) model);
}

/* Opens a connection and test basic operations on it */
static void
open_connection (GdaClient *client,
		 const gchar *name,
		 const gchar *username,
		 const gchar *password)
{
	GdaConnection *cnc;
	gboolean res;
	GdaDataSourceInfo *dsn_info;
	GdaTransaction *xaction;

	g_return_if_fail (GDA_IS_CLIENT (client));

	cnc = gda_client_open_connection (client, name, username, password, 0);
	if (!GDA_IS_CONNECTION (cnc)) {
		g_print (_("** ERROR: could not open connection to %s\n"), name);
		return;
	}

	/* show provider features */
	g_print (_("\tProvider capabilities...\n"));
	g_print (_("\t\tAggregates: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_AGGREGATES) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tIndexes: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_INDEXES) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tNamespaces: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_NAMESPACES) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tProcedures: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_PROCEDURES) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tSequences: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_SEQUENCES) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tSQL: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_SQL) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tTransactions: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_TRANSACTIONS) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tTriggers: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_TRIGGERS) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tUsers: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_USERS) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tViews: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_VIEWS) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tXML queries: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_XML_QUERIES) ?
		 _("Supported") : _("Not supported"));
	g_print (_("\t\tBLOBs: %s\n"),
		 gda_connection_supports (cnc, GDA_CONNECTION_FEATURE_BLOBS) ?
		 _("Supported") : _("Not supported"));

	/* show connection schemas */
	g_print (_("\n\tProvider reports version: %s\n\n"),
			gda_connection_get_server_version (cnc));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_NAMESPACES, _("Namespaces"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_DATABASES, _("Databases"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_PROCEDURES, _("Stored procedures"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_TABLES, _("Connection Tables"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_TYPES, _("Available types"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_VIEWS, _("Views"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_INDEXES, _("Indexes"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_AGGREGATES, _("Aggregates"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_USERS, _("Users"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_SEQUENCES, _("Sequences"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_TRIGGERS, _("Triggers"));
	show_schema (cnc, GDA_CONNECTION_SCHEMA_LANGUAGES, _("Languages"));

	/* test transactions */
	xaction = gda_transaction_new (NULL);

	g_print (_("\tStarting transaction..."));
	res = gda_connection_begin_transaction (cnc, xaction);
	g_print ("%s\n", res ? "OK" : _("Error"));

	g_print (_("\tFinishing transaction..."));
	res = gda_connection_commit_transaction (cnc, xaction);
	g_print ("%s\n", res ? "OK" : _("Error"));

	g_print (_("\tStarting transaction..."));
	res = gda_connection_begin_transaction (cnc, xaction);
	g_print ("%s\n", res ? "OK" : _("Error"));

	g_print (_("\tRolling back transaction..."));
	res = gda_connection_rollback_transaction (cnc, xaction);
	g_print ("%s\n", res ? "OK" : _("Error"));

	g_object_unref (G_OBJECT (xaction));

	dsn_info = gda_config_find_data_source (name);
	/* Postgres own tests */
	if (!strcmp (dsn_info->provider, POSTGRES_PROVIDER_NAME))
		do_postgres_test (cnc);
	if (!strcmp (dsn_info->provider, FREETDS_PROVIDER_NAME))
		do_freetds_test (cnc);
	if (!strcmp (dsn_info->provider, SYBASE_PROVIDER_NAME))
		do_sybase_test (cnc);
	if (!strcmp (dsn_info->provider, IBMDB2_PROVIDER_NAME))
		do_ibmdb2_test (cnc);
		
	gda_config_free_data_source_info (dsn_info);

	/* close the connection */
	gda_connection_close (cnc);
}

/* Main entry point for the basic client API test */
void
test_client (void)
{
	GdaClient *client;
	GList *dsnlist;
	GList *l;

	DISPLAY_MESSAGE (_("Testing GDA client API"));

	client = gda_client_new ();

	dsnlist = gda_config_get_data_source_list ();
	for (l = dsnlist; l != NULL; l = l->next) {
		GdaDataSourceInfo *info = (GdaDataSourceInfo *) l->data;

		if (!info) {
			g_print (_("** ERROR: gda_config_get_data_source_list returned a NULL item\n"));
			gda_main_quit ();
		}

		g_print (_(" Data source = %s, User = %s\n"), info->name, info->username);
		open_connection (client, info->name, info->username,
		                 (info->password) ? info->password : "");
	}

	/* free memory */
	gda_config_free_data_source_list (dsnlist);
	g_object_unref (G_OBJECT (client));
}