File: blobs_example.html

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 (259 lines) | stat: -rw-r--r-- 8,510 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Binary large objects (BLOBs) example: GNOME Data Access 5 manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="GNOME Data Access 5 manual">
<link rel="up" href="getting_started.html" title="Code examples">
<link rel="prev" href="ddl_example.html" title="DDL example">
<link rel="next" href="other_examples.html" title="Other examples">
<meta name="generator" content="GTK-Doc V1.32 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="getting_started.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="ddl_example.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="other_examples.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="blobs_example"></a>Binary large objects (BLOBs) example</h2></div></div></div>
<p>
       The following example is available in the <code class="filename">samples/Blobs</code> source directory
       and illustrate how to manipulate BLOBs. This test program offers 2 operations which are to
       store the contents of a file to the database (which returns the ID of the stored data), and to
       fetch a stored data from the database from its ID (which creates a fetched_&lt;ID&gt; file).
    </p>
<p>
      The code is:
</p>
<pre class="programlisting">
#include &lt;libgda/libgda.h&gt;
#include &lt;libgda/sql-parser/gda-sql-parser.h&gt;
#include &lt;libgda/gda-blob-op.h&gt;

GdaConnection *open_connection (void);
static gboolean do_store (GdaConnection *cnc, const gchar *filename, GError **error);
static gboolean do_fetch (GdaConnection *cnc, gint id, GError **error);

int
main (int argc, char *argv[])
{
        GdaConnection *cnc;
	const gchar *filename = NULL;
	gint id = 0;
	gboolean store;
	GError *error = NULL;
	gboolean result;

	/* parse arguments */
	if (argc != 3)
		goto help;
	if (! g_ascii_strcasecmp (argv[1], "store")) {
		filename = argv[2];
		store = TRUE;
	}
	else if (! g_ascii_strcasecmp (argv[1], "fetch")) {
		id = atoi (argv[2]);
		store = FALSE;
	}
	else
		goto help;

	/* do the job */
        gda_init ();
	cnc = open_connection ();
	if (store)
		result = do_store (cnc, filename, &amp;error);
	else
		result = do_fetch (cnc, id, &amp;error);

	if (gda_connection_get_transaction_status (cnc))
		g_print ("Still in a transaction, all modifications will be lost when connection is closed\n");
        gda_connection_close (cnc);

	if (!result) {
		g_print ("ERROR: %s\n", error &amp;&amp; error-&gt;message ? error-&gt;message : "No detail");
		g_clear_error (&amp;error);
	}
	else
		g_print ("Ok.\n");

        return result ? 0 : 1;

 help:
	g_print ("%s [store &lt;filename&gt; | fetch &lt;ID&gt;]\n", argv[0]);
	return 0;
}

/*
 * Open a connection to the example.db file
 */
GdaConnection *
open_connection (void)
{
        GdaConnection *cnc;
        GError *error = NULL;
        cnc = gda_connection_open_from_string ("SQLite", "DB_DIR=.;DB_NAME=testblob", NULL,
					       GDA_CONNECTION_OPTIONS_NONE,
					       &amp;error);
        if (!cnc) {
                g_print ("Could not open connection to SQLite database in testblob.db file: %s\n",
                         error &amp;&amp; error-&gt;message ? error-&gt;message : "No detail");
                exit (1);
        }
        return cnc;
}

static gboolean
do_store (GdaConnection *cnc, const gchar *filename, GError **error)
{
	if (! g_file_test (filename, G_FILE_TEST_EXISTS) ||
	    g_file_test (filename, G_FILE_TEST_IS_DIR)) {
		g_set_error (error, 0, 0,
			     "%s", "File does not exist or is a directory");
		return FALSE;
	}

	GdaSqlParser *parser;
	GdaStatement *stmt;
	GdaSet *params, *newrow;
	GdaHolder *holder;
	GValue *value;
	GdaBlob *blob;
	gint res;

	parser = gda_sql_parser_new ();
	stmt = gda_sql_parser_parse_string (parser,
					    "INSERT INTO blobstable (data) VALUES (##blob::blob)",
					    NULL, error);
	g_object_unref (parser);
	if (!stmt)
		return FALSE;

	if (! gda_statement_get_parameters (stmt, &amp;params, error)) {
		g_object_unref (stmt);
		return FALSE;
	}

	holder = gda_set_get_holder (params, "blob");
	value = gda_value_new_blob_from_file (filename);
	blob = (GdaBlob*) gda_value_get_blob (value);
	g_assert (gda_holder_take_value (holder, value, NULL));

	g_print ("Before writing BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
		 "Transaction started" : "No transaction started");

	g_print ("STORING file '%s' to database BLOB\n", filename);
	res = gda_connection_statement_execute_non_select (cnc, stmt, params, &amp;newrow, error);
	g_object_unref (params);
	g_object_unref (stmt);

	g_print ("After writing BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
		 "Transaction started" : "No transaction started");

	if (newrow) {
		GSList *list;
		g_print ("Inserted row is (for each numbered column in the table):\n");
		for (list = newrow-&gt;holders; list; list = list-&gt;next) {
			const GValue *value;
			gchar *tmp;
			value = gda_holder_get_value (GDA_HOLDER (list-&gt;data));
			tmp = gda_value_stringify (value);
			g_print ("  [%s] = [%s]\n", gda_holder_get_id (GDA_HOLDER (list-&gt;data)), tmp);
			g_free (tmp);
		}
		g_object_unref (newrow);
	}
	else
		g_print ("Provider did not return the inserted row\n");

	gda_connection_rollback_transaction (cnc, NULL, NULL);
	g_print ("After rolling back blob READ transaction: %s\n", gda_connection_get_transaction_status (cnc) ?
		 "Transaction started" : "No transaction started");

	return (res == -1) ? FALSE : TRUE;
}

static gboolean
do_fetch (GdaConnection *cnc, gint id, GError **error)
{
	GdaSqlParser *parser;
	GdaStatement *stmt;
	GdaSet *params;
	GdaDataModel *model;
	const GValue *value;
	GdaBlob *blob;
	gboolean result = TRUE;

	g_print ("Before reading BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
		 "Transaction started" : "No transaction started");

	gchar *filename;
	filename = g_strdup_printf ("fetched_%d", id);
	g_print ("FETCHING BLOB with ID %d to file '%s'\n", id, filename);

	parser = gda_sql_parser_new ();
	stmt = gda_sql_parser_parse_string (parser,
					    "SELECT data FROM blobstable WHERE id=##id::int",
					    NULL, error);
	g_object_unref (parser);
	if (!stmt)
		return FALSE;

	if (! gda_statement_get_parameters (stmt, &amp;params, error)) {
		g_object_unref (stmt);
		return FALSE;
	}
	g_assert (gda_set_set_holder_value (params, NULL, "id", id));
	model = gda_connection_statement_execute_select (cnc, stmt, params, error);
	g_object_unref (params);
	g_object_unref (stmt);
	if (! model)
		return FALSE;

	g_print ("After reading BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
		 "Transaction started" : "No transaction started");

	value = gda_data_model_get_value_at (model, 0, 0, error);
	if (!value) {
		g_object_unref (model);
		return FALSE;
	}
	g_assert (G_VALUE_TYPE (value) == GDA_TYPE_BLOB);
	
	blob = (GdaBlob*) gda_value_get_blob (value);
	if (blob-&gt;op) {
		GValue *dest_value;
		GdaBlob *dest_blob;
		
		dest_value = gda_value_new_blob_from_file (filename);
		dest_blob = (GdaBlob*) gda_value_get_blob (dest_value);
		result = gda_blob_op_write_all (dest_blob-&gt;op, (GdaBlob*) blob);
		gda_value_free (dest_value);
	}
	else
		result = g_file_set_contents (filename, (gchar *) ((GdaBinary*)blob)-&gt;data,
					     ((GdaBinary*)blob)-&gt;binary_length, error);
	g_free (filename);
	g_object_unref (model);

	gda_connection_rollback_transaction (cnc, NULL, NULL);
	g_print ("After rolling back blob READ transaction: %s\n", gda_connection_get_transaction_status (cnc) ?
		 "Transaction started" : "No transaction started");

	return result;
}

</pre>
<p>
    </p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.32</div>
</body>
</html>