File: access-raw.c

package info (click to toggle)
libgda5 5.2.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,168 kB
  • sloc: ansic: 495,319; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,095; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (139 lines) | stat: -rw-r--r-- 3,953 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2007 - 2011 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 <libgda/gda-data-model-bdb.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"

int
main (int argc, char *argv [])
{
	GdaDataModel *model;
	gint i, nrows;
	GError *error = NULL;

	gda_init ();
	if (! g_file_test (DATABASE, G_FILE_TEST_EXISTS)) {
		g_print ("File '%s' does not exist\n", DATABASE);
		exit (1);
	}
	model = gda_data_model_bdb_new (DATABASE, NULL);
	gda_data_model_dump (model, stdout);

	nrows = gda_data_model_get_n_rows (model);
	for (i = 0; i < nrows; i++) {
		Key *key;
		Value *value;
		const GValue *c_value;
		const GdaBinary *bin;
		
		g_print ("=============== ROW %d\n", i);

		c_value= gda_data_model_get_value_at (model, 0, i, &error);
		if (!c_value) {
			g_print ("Could not get value from data model: %s\n",
				 error && error->message ? error->message : "No detail");
			exit (1);
		}
		bin = gda_value_get_binary (c_value);
		key = (Key *)bin->data;
		g_print ("color/type = %s/%d\n", key->color, key->type);

		c_value= gda_data_model_get_value_at (model, 1, i, &error);
		if (!c_value) {
			g_print ("Could not get value from data model: %s\n",
				 error && error->message ? error->message : "No detail");
			exit (1);
		}
		bin = gda_value_get_binary (c_value);
		value = (Value *)bin->data;
		g_print ("size/name = %f/%s\n", value->size, value->name);
		
	}

	i = gda_data_model_append_row (model, &error);
	if (i < 0) {
		g_print ("Could not append row: %s\n", 
			 error && error->message ? error->message : "no detail");
		exit (1);
	}
	else {
		{
			/* EXTRA tests */
			gda_data_model_dump (model, stdout);
			if (!gda_data_model_remove_row (model, i, &error)) {
				g_print ("Could not remove row: %s\n", 
					 error && error->message ? error->message : "no detail");
				exit (1);
			}
			gda_data_model_dump (model, stdout);

			gchar *str = "AAA";
			GValue *value = gda_value_new_binary ((guchar*) str, 4);
			if (!gda_data_model_set_value_at (model, 1, 2, value, &error)) {
				g_print ("Could not set value: %s\n", 
					 error && error->message ? error->message : "no detail");
				exit (1);
			}
			gda_data_model_dump (model, stdout);

			exit (0);
		}

		GValue *value;
		GdaBinary bin;
		Key m_key;
		Value m_value;
		
		strncpy (m_key.color, "black", COLORSIZE);
		m_key.type = 100;
		
		bin.data = (gpointer) &m_key;
		bin.binary_length = sizeof (Key);
		value = gda_value_new (GDA_TYPE_BINARY);
		gda_value_set_binary (value, &bin);

		if (!gda_data_model_set_value_at (model, 0, i, value, &error)) {
			g_print ("Could not set key: %s\n", 
				 error && error->message ? error->message : "no detail");
			exit (1);
		}
		gda_value_free (value);

		m_value.size = 100.1;
		strncpy (m_value.name, "blackhole", NAMESIZE);
		
		bin.data = (gpointer) &m_value;
		bin.binary_length = sizeof (Value);
		value = gda_value_new (GDA_TYPE_BINARY);
		gda_value_set_binary (value, &bin);

		if (!gda_data_model_set_value_at (model, 1, i, value, &error)) {
			g_print ("Could not set key: %s\n", 
				 error && error->message ? error->message : "no detail");
			exit (1);
		}
		gda_value_free (value);
	}
	g_object_unref (model);
	return 0;
}