File: models.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 (242 lines) | stat: -rw-r--r-- 6,283 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* 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 "models.h"

static const char *
command_type_to_string (GdaCommandType cmd_type)
{
	const gchar *type_name;

	switch (cmd_type) {
		case GDA_COMMAND_TYPE_SQL :
			type_name = "SQL";
			break;
		case GDA_COMMAND_TYPE_XML :
			type_name = "XML";
			break;
		case GDA_COMMAND_TYPE_PROCEDURE :
			type_name = "PROCEDURE";
			break;
		case GDA_COMMAND_TYPE_TABLE :
			type_name = "TABLE";
			break;
		case GDA_COMMAND_TYPE_INVALID :
			type_name = "INVALID!";
			break;
		default:
			type_name = "Unknown type.";
	}

	return type_name;
}

/*
 * display_recordset_data
 * @model: a recordset with the data to display.
 *
 * For each row in the recordset displays the name of each column, type,
 * value...
 *
 * Returns: nothing
 */
void
display_recordset_data (GdaDataModel *model)
{
	gint cols, rows;
	gint c, r;
	GdaCommandType cmd_type;
	const gchar *cmd_text, *type_text;

	g_return_if_fail (GDA_IS_DATA_MODEL (model));

	cols = gda_data_model_get_n_columns (model);
	rows = gda_data_model_get_n_rows (model);

	g_print ("Displaying recordset %p, with %d columns and %d rows:\n",
		 model, cols, rows);

	cmd_type = gda_data_model_get_command_type (model);
	type_text = command_type_to_string (cmd_type);
	cmd_text = gda_data_model_get_command_text (model);
	g_print ("The query (type '%s') leading up to this result was:\n\t'%s'\n\n",
			type_text, cmd_text ? cmd_text : "<null>");

	for (r = 0; r < rows; r++) {
		g_print ("\tRow %02d -------\n", r);
		for (c = 0; c < cols; c++) {
			const GdaValue *value;
			gchar *strvalue;
			const gchar *colname;

			colname = gda_data_model_get_column_title (model, c);
			value = gda_data_model_get_value_at (model, c, r);
			strvalue = gda_value_stringify (value);
			g_print ("\t\tColumn %02d name: %s\n", c, colname);
			g_print ("\t\t         value: ('%s')\n", strvalue);
			g_free (strvalue);
		}
	}

	/* Do it again. Just to measure the speed */
	for (r = 0; r < rows; r++) {
		for (c = 0; c < cols; c++) {
			gda_data_model_get_column_title (model, c);
		}
	}
}

void
test_speed_random (GdaDataModel *model)
{
	gint cols, rows;
	gint i, c, r;
	GRand *generator;
	GTimer *timer;

	cols = gda_data_model_get_n_columns (model);
	rows = gda_data_model_get_n_rows (model);

	g_print (" Testing access to values of data model %p, with %d columns and %d rows\n",
		 model, cols, rows);

	generator = g_rand_new ();
	timer = g_timer_new ();
	for (i = 0; i < 10000; i++) {
		c = g_rand_int_range (generator, 0, cols);
		r = g_rand_int_range (generator, 0, rows);
		(void) gda_data_model_get_value_at (model, c, r);
	}
	g_timer_stop (timer);
	g_print (" Elapsed time for fetching 10000 random values: %f\n",
		 g_timer_elapsed (timer, NULL));
	g_timer_destroy (timer);
	g_rand_free (generator);
}

void
display_data_model (GdaDataModel *model)
{
	gint cols, rows;
	gint c, r;

	cols = gda_data_model_get_n_columns (model);
	rows = gda_data_model_get_n_rows (model);

	g_print (" Displaying data model %p, with %d columns and %d rows\n",
		 model, cols, rows);

	for (r = 0; r < rows; r++) {
		for (c = 0; c < cols; c++) {
			const GdaValue *value;
			gchar *str;

			value = gda_data_model_get_value_at (model, c, r);
			str = gda_value_stringify (value);
			g_print ("\tColumn %d - Row %d: %s\n", c, r, str);
			g_free (str);
		}
	}
}

static void
model_changed_cb (GdaDataModel *model, gpointer user_data)
{
	g_print ("\tModel %p has changed\n", model);
	display_data_model (model);
}

/* Test a data model object */
static void
setup_data_model (GdaDataModel *model)
{
	g_return_if_fail (GDA_IS_DATA_MODEL (model));

	display_data_model (model);
	g_signal_connect (G_OBJECT (model), "changed",
			  G_CALLBACK (model_changed_cb), NULL);
}

/* Test the list data model */
static void
test_list_model (void)
{
	GdaDataModelList *model;
	GList *sections;

	g_print (" Testing list model...\n");

	/* retrieve a string list */
	sections = gda_config_list_keys ("/desktop/gnome/interface");
	model = (GdaDataModelList *) gda_data_model_list_new_from_string_list (sections);
	gda_config_free_list (sections);

	if (!GDA_IS_DATA_MODEL (model)) {
		g_print ("** ERROR: Could not create GdaDataModelList\n");
		gda_main_quit ();
	}

	setup_data_model (GDA_DATA_MODEL (model));
}

/* Tests the array data model */
static void
test_array_model (void)
{
	gint i;
	GdaDataModelArray *model;

	g_print (" Testing array model...\n");

	model = (GdaDataModelArray *) gda_data_model_array_new (6);
	if (!GDA_IS_DATA_MODEL_ARRAY (model)) {
		g_print ("** ERROR: Could not create GdaDataModelArray\n");
		gda_main_quit ();
	}

	for (i = 0; i < 50; i++) {
		GList *values = NULL;

		values = g_list_append (values, gda_value_new_string ("This is a string"));
		values = g_list_append (values, gda_value_new_integer (200));
		values = g_list_append (values, gda_value_new_boolean (FALSE));
		values = g_list_append (values, gda_value_new_single (3.1416));
		values = g_list_append (values, gda_value_new_string ("Another string"));
		values = g_list_append (values, gda_value_new_double (4560.45672113323093));

		gda_data_model_append_row (GDA_DATA_MODEL (model), values);

		g_list_foreach (values, (GFunc) gda_value_free, NULL);
		g_list_free (values);
	}

	setup_data_model (GDA_DATA_MODEL (model));
}

/* Main entry point for the data models test suite */
void
test_models (void)
{
	DISPLAY_MESSAGE ("Testing data models");
	test_list_model ();
	test_array_model ();
}