summaryrefslogtreecommitdiffstats
path: root/relblobif/test/blobdump.pgc
blob: 69b35b7d1aab602467eb435a67bc54c3de8ab5da (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* This file is part of rasdaman community.
*
* Rasdaman community 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 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community 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 rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
#include "mymalloc/mymalloc.h"
/*************************************************************
 *
 *
 * PURPOSE:
 * dump RAS_TILES blob table.
 * Depending on parameters, either a particular tuple is printed
 * or the whole table. Optionally also the blob is dumped in hex.
 * Parameters: see usage().
 *
 *
 * COMMENTS:
 * - assumes RAS_TILES 
 *
 ************************************************************/

using namespace std;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>             /* getopt */

#include <sqlca.h>

// SQL error codes:
#include "externs.h"

// PG stuff
#include "libpq-fe.h"	// C interface to PgSQL
#include "libpq/libpq-fs.h"     /* large object (lo) api */
// libq-style connection ptr taken from ECPG connect (needed for libq functions):
// (currently used by relblobif/blobtile.pgc)
PGconn *pgConn = NULL;
PGresult *pgResult = NULL;

#define RC_OK 0
#define RC_ERROR -1

#define QUERYSIZE 2000
static char pgQuery[QUERYSIZE];

void exitNicely()
{
	PQclear( pgResult );
	(void) PQexec( pgConn, "ROLLBACK WORK" );
	PQfinish( pgConn );
	exit( RC_ERROR );
}


void
disconnect()
{
	pgResult = PQexec( pgConn, "ROLLBACK WORK" );
	if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
	{
		cerr << "error during disconnect: " << PQerrorMessage(pgConn) << endl << flush;
		PQclear( pgResult );
		exit( RC_ERROR );
	}
	PQclear( pgResult );

	PQfinish( pgConn );
	pgConn = NULL;
}

void
connect( const char * db )
{
	char pgConnInfo[200];

	(void) snprintf( pgConnInfo, (size_t) sizeof(pgConnInfo), "dbname = %s", db );
	pgConn = PQconnectdb( pgConnInfo );
	if (PQstatus(pgConn) != CONNECTION_OK)
	{
		cerr << "error during connect: " << PQerrorMessage(pgConn) << endl << flush;
		exitNicely();
	}

	pgResult = PQexec( pgConn, "BEGIN TRANSACTION" );
	if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
	{
		cerr << "error during being ta: " << PQerrorMessage(pgConn) << endl << flush;
		PQclear( pgResult );
		exit( RC_ERROR );
	}
	PQclear( pgResult );

}

// read one tuple from ras_tiles, identified by myOId, and display it (with contents if withDump==true)
void
readFromDb( long myOId, bool withDump )
{
	unsigned int blobOid = 0;
	short dataFormat = 0;
	unsigned long size = 0;
	char *cells = NULL;

	// (1) --- access tuple
	int result = snprintf( pgQuery, (size_t) sizeof(pgQuery), "SELECT Tile, DataFormat FROM RAS_TILES WHERE BlobId = %d", myOId );

	pgResult = PQexec( pgConn, pgQuery );
	if (PQresultStatus(pgResult) != PGRES_TUPLES_OK)
	{
		cerr << "error during 'select': " << PQerrorMessage(pgConn) << endl << flush;
		exitNicely();
	}
	else if (PQntuples(pgResult) < 1)
	{
		cerr << "no tuple with id " << myOId << endl << flush;
		exitNicely();
	}

	blobOid    = atoi( PQgetvalue( pgResult, 0, 0 ) );
	dataFormat = atoi( PQgetvalue( pgResult, 0, 1 ) );
	PQclear( pgResult );

	// (2) --- open, read, close blob
	int fd = lo_open( pgConn, blobOid, INV_READ );		// open; manual tells no error indication
	size = lo_lseek( pgConn, fd, 0, SEEK_END );		// determine blob size; FIXME: more efficient method??
	(void) lo_lseek( pgConn, fd, 0, SEEK_SET );		// rewind for reading
	cells = (char*) mymalloc( size * sizeof(char) );	// allocate buffer for blob
	if (cells == NULL)
	{
		cerr << "error during malloc()" << endl << flush;
		exitNicely();
	}
	int loResult = lo_read( pgConn, fd, cells, size );	
	if (loResult < 0)
	{
		cerr << "error during lo_read(): " << PQerrorMessage(pgConn) << endl << flush;
		exitNicely();
	}
	else if (loResult != size)				// did not get all
	{
		cerr << "error (did not get all bytes): " << PQerrorMessage(pgConn) << endl << flush;
		exitNicely();
	}
	int ignoredPgResult = lo_close( pgConn, fd );		// close blob

	if (withDump)
	{
		cout << "   id = " << myOId << ", dataFormat = " << dataFormat << ", bloboid = " << blobOid << ", blob length = " << size << ", contents = ";
		for (int a = 0; a < size; a++)
		 	cout << " " << hex << (0xff & (unsigned int)(cells[a])) << dec;
		cout << dec << endl;
	}
	else
	{
		cout << "   id = " << myOId << ", dataFormat = " << dataFormat << ", bloboid = " << blobOid << ", blob length = " << size << endl;
	}

	free( cells );
}

// read all tuples from ras_tiles and display them (with contents if withDump==true)
void
readAllFromDb( bool withDump )
{
	unsigned int blobOid;
	long myId;
	short dataFormat;
	unsigned long size = 0;
	char *cells = NULL;
	int rowNumber = 0;

	// (1) --- declare cursor
	pgResult = PQexec( pgConn, "SELECT BlobId, Tile, DataFormat FROM RAS_TILES" );
	if (PQresultStatus(pgResult) != PGRES_TUPLES_OK && PQresultStatus(pgResult) != PGRES_COMMAND_OK)
	{
		cerr << "error during 'declare cursor': " << PQerrorMessage(pgConn) << endl << flush;
		exitNicely();
	}

	do
	{
		myId       = atoi( PQgetvalue( pgResult, rowNumber, 0 ) );
		blobOid    = atoi( PQgetvalue( pgResult, rowNumber, 1 ) );
		dataFormat = atoi( PQgetvalue( pgResult, rowNumber, 2 ) );

		// (2) --- open, read, close blob
		int fd = lo_open( pgConn, blobOid, INV_READ );		// open; manual tells no error indication
		size = lo_lseek( pgConn, fd, 0, SEEK_END );		// determine blob size; FIXME: more efficient method??
		(void) lo_lseek( pgConn, fd, 0, SEEK_SET );		// rewind for reading
		cells = (char*) mymalloc( size * sizeof(char) );		// allocate buffer for blob
		if (cells == NULL)
		{
			cerr << "error: readFromDb() - no tuples found " << endl;
			return;
		}
		int loResult = lo_read( pgConn, fd, cells, size );	
		if (loResult < 0)
		{
			cerr << "error: readFromDb() - no tuples found " << endl;
			return;
		}
		else if (loResult != size)				// did not get all
		{
			cerr << "error: readFromDb() - no tuples found " << endl;
			return;
		}
		int ignoredPgResult = lo_close( pgConn, fd );		// close blob

		if (withDump)
		{
			cout << "   id = " << myId << ", dataFormat = " << dataFormat << ", bloboid = " << blobOid << ", blob length = " << size << ", contents = ";
			for (int a = 0; a < size; a++)
	 			cout << " " << hex << (0xff & (unsigned int)(cells[a])) << dec;
			cout << endl;
		}
		else
		{
			cout << "   id = " << myId << ", dataFormat = " << dataFormat << ", bloboid = " << blobOid << ", blob length = " << size << endl;
		}

		rowNumber++;
	} while (PQresultStatus(pgResult) == PGRES_TUPLES_OK || PQresultStatus(pgResult) == PGRES_COMMAND_OK);

	(void) PQexec( pgConn, "CLOSE TileLoop" );
	PQclear( pgResult );
	free( cells );
}

void usage(const char *prog)
{
	cout << "Usage: " << prog << " -d db [-i id] [-c]" << endl;
	cout << "where:" << endl;
	cout << "   -d db    log into PG server using PG database name db (mandatory)" << endl;
	cout << "   -i id    dump blob with identifier id (default: dump whole table)" << endl;
	cout << "   -c       dump blob contents too (default: descriptor data only)" << endl;
	cout << "   -h       (help) print this overview" << endl;
}

int
main(int argc, char* argv[])
{
	char *prog = argv[0];	// prog name
	char db[100];		// DB connect string
	int blobOid = 0;	// tuple to be dumped
	bool withDump = false;	// by default, don't dump blob contents
	char c;			// getop var
	int result;		// for sscanf()

        cout << prog << ": dump rasdaman blob table." << endl;

	opterr = 0;
	while ((c = getopt(argc, argv, "d:i:c")) != -1)
	{
		switch (c)
		{
			case 'd':
				(void) strncpy( db, optarg, sizeof(db) );
				break;
			case 'i':
				result = sscanf( optarg, "%d", &blobOid );
				if (result != 1)
				{
					cerr << prog << ": positive integer expected: " << optarg << endl;
					exit( RC_ERROR );
				}
				break;
			case 'c':
				withDump = true;
				break;
			case 'h':
			case '?':
			case ':':
				usage(prog);
				exit( RC_OK );
				break;
		}
	}
	if (optind < argc || argc == 1)
	{
		usage( prog );
		exit( RC_ERROR );
	}

	connect( db );

	if (blobOid > 0)
		readFromDb( blobOid, withDump );
	else
		readAllFromDb( withDump );

	disconnect();

	cout << prog << ": done." << endl;
}