File: bfile_test_sql.c

package info (click to toggle)
db5.3 5.3.28%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 158,500 kB
  • sloc: ansic: 448,411; java: 111,824; tcl: 80,544; sh: 44,264; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; javascript: 1,998; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (324 lines) | stat: -rw-r--r-- 7,203 bytes parent folder | download | duplicates (10)
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
316
317
318
319
320
321
322
323
324
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2010 Oracle.  All rights reserved.
 *
 */

/*
 * This file show a exmple of using bfile interface for OSL.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sqlite3.h>
#include <bfile.h>

typedef sqlite3_int64 bfile_handle;

#define CHECK_ERR						\
	if (rc != SQLITE_OK) {					\
		printf("  SQL error: %s\n", zErrMsg);		\
		sqlite3_free(zErrMsg);				\
		_exit(-1);					\
	}

#define EXEC(sql)						\
	rc = sqlite3_exec(db, (sql), NULL, 0, &zErrMsg);	\
	CHECK_ERR

#define cleandb(db)										\
	sqlite3_exec(db, "DELETE FROM BFILE_DIRECTORY WHERE alias='IMG';", NULL, 0, &zErrMsg);	\
	sqlite3_exec(db, "DROP TABLE address_book;", NULL, 0, &zErrMsg);

static char pic[] = 
"       |||||       ""\n"\
"       (o o)       ""\n"\
"+-oooO--(_)-------+""\n"\
"|                 |""\n"\
"|    I am Tony    |""\n"\
"|                 |""\n"\
"+------------Ooo--+""\n"\
"      |__|__|      ""\n"\
"       || ||       ""\n"\
"      ooO Ooo      ""\n";

static void  creat_file(void) {
	int fd;
	char *zErrMsg = NULL;

	fd = creat("./Tony.jpg", S_IRWXU);

	if (fd != -1) {
		if (write(fd, pic, sizeof(pic) - 1 ) == -1) {
			perror(zErrMsg);
			fprintf(stderr, "%s\n", zErrMsg);
			_exit(-1);
		}
		close(fd);
	} else {
		perror(zErrMsg);
		fprintf(stderr, "%s\n", zErrMsg);
		_exit(-1);
	}

	printf("File ./Tony.jpg created.\n");
}

static void pre_test(sqlite3 **pdb) {
	char *zErrMsg = 0;
	int rc;
	sqlite3 *db;

	creat_file();

	rc = sqlite3_open("test.db", pdb);
	CHECK_ERR

	db = *pdb;

	sqlite3_enable_load_extension(db, 1);

	rc = sqlite3_load_extension(db, "libbfile_ext.so", NULL, &zErrMsg);
	CHECK_ERR

	/* clean the database */
	cleandb(db);

	/* create directory alias */
	//EXEC("INSERT INTO BFILE_DIRECTORY VALUES('IMG','./');")
	EXEC("SELECT BFILE_CREATE_DIRECTORY('IMG', './');")

	/* create a table with BFILE type column */
	EXEC("CREATE TABLE address_book(name TEXT, phone_num TEXT, address TEXT, photo BFILE);")

	/* insert an existed bfile */
	EXEC("INSERT INTO address_book VALUES('Tony', '+861082798880', 'China Beijing', BFILE_NAME('IMG','Tony.jpg'));")

	/* insert a not existed bfile */
	EXEC("INSERT INTO address_book VALUES('Rachel', '+861082798881', 'China Beijing', BFILE_NAME('IMG','Rachel.jpg'));")

	/* insert a null bfile */
	EXEC("INSERT INTO address_book VALUES('Jhon', '+861082798883', 'China Beijing', NULL);")
}

#define CHECK_API_ERR(title, errno, line)			\
	if (rc != (errno)) {					\
		printf(title" error @ line: %d\n", (line));	\
		_exit(-1);					\
	} else {						\
		printf(title" OK\n");				\
	}

#define BUFF_SIZE 20
static void test_BfileSize(sqlite3* db) {
	int rc, row, size;
	sqlite3_stmt *pStmt;
	char *sql = "SELECT BFILE_SIZE(photo) FROM address_book";

	/* get size of each bfile */
	printf("test BFILE_SIZE(icol)\n");

	pStmt = NULL;
	rc = sqlite3_prepare(db, sql, -1, &pStmt, 0);
	if (rc != SQLITE_OK) {
		printf("\t[Failed]\tBFILE_SIZE: prepare error\n");
		return;
	}

	row = 0;

	while (SQLITE_ROW == (rc = sqlite3_step(pStmt))) {
		row++;
		printf("-----------------row %d--------------------", row);

		size = sqlite3_column_int(pStmt, 0);
		switch (row) {
		case 1:	/* existed bfile */
			if (size != sizeof(pic) - 1)
				printf("\t[Failed]\tlogic error @ line: %d", __LINE__);
			else
				printf("\t[OK]\n");
			break;
		case 2: /* not existied bfile */
		case 3: /* null */
			if (size != -1)
				printf("\t[Failed]\tlogic error @ line: %d", __LINE__);
			else
				printf("\t[OK]\n");
			break;
		default:
			assert(0);
		}
	}

	if (pStmt != NULL)
		sqlite3_finalize(pStmt);

	if (rc && rc != SQLITE_DONE)
		printf("\t[Failed]\tBFILE_SIZE: step error\n");
}

static void test_BfileOpenReadClose(sqlite3* db) {
	int rc, rc2, rc3, len, row;
	bfile_handle bHdl;
	sqlite3_stmt *pStmt, *pBfileStmt;
	off_t amount, offset;
	char *sql[] =  {"SELECT BFILE_OPEN(photo) FROM address_book",
			"SELECT BFILE_READ(?, ?, ?)",
			"SELECT BFILE_CLOSE(?)" };
	const char *result;

	/* manipulate each bfile */
	printf("%s%s%s", "test BFILE_OPEN(icol)\n     ",
			"BFILE_READ(bHdl, amount, offset)\n     ",
			"BFILE_CLOSE(bHdl)\n");
	
	pStmt = NULL;
	/* BFILE_OPEN */
	rc = sqlite3_prepare(db, sql[0], -1, &pStmt, 0);
	if (rc != SQLITE_OK) {
		printf("\t[Failed]\tBFILE_OPEN: prepare error\n");
		return;
	}

	row = 0;

	pBfileStmt = NULL;
	while (SQLITE_ROW == (rc = sqlite3_step(pStmt))) {
		row++;
		printf("-----------------row %d--------------------", row);

		bHdl = sqlite3_column_int64(pStmt, 0);
		if ((void*)bHdl == NULL) {
			printf("\t[OK]\n");
			continue;
		}

		amount = BUFF_SIZE;
		offset = 0;

		/* BFILE_READ */
	        rc2 = sqlite3_prepare(db, sql[1], -1, &pBfileStmt, 0);
		if (rc2) {
			printf("\t[Failed]\tBFILE_READ: prepare error\n");
			continue;
		}

		while (1) {
			rc2 = sqlite3_bind_int64(pBfileStmt, 1, bHdl);
                	if (rc2) {
				printf("\t[Failed]\tBFILE_READ: bind error\n");
				break;
			}

			rc2 = sqlite3_bind_int(pBfileStmt, 2, amount);
                	if (rc2) {
				printf("\t[Failed]\tBFILE_READ: bind error\n");
				break;
			}
			
			rc2 = sqlite3_bind_int(pBfileStmt, 3, offset);
                	if (rc2) {
				printf("\t[Failed]\tBFILE_READ: bind error\n");
				break;
			}

			rc2 = sqlite3_step(pBfileStmt);
			if (rc2 == SQLITE_ROW) {
				result = (const char*)sqlite3_column_blob(pBfileStmt, 0);
				len = sqlite3_column_bytes(pBfileStmt, 0);
				/* EOF */
				if (len == 0)
					break;
				if (memcmp(result, &(pic[offset]), len)) {
					rc2 = -1;
					printf("\t[Failed]\tlogic error @ line: %d", __LINE__);
					break;
				}
			} else if (rc2) {
				printf("\t[Failed]\tBFILE_READ: step error\n");
				break;
			}

			sqlite3_reset(pBfileStmt);
			sqlite3_clear_bindings(pBfileStmt);

			offset += BUFF_SIZE;
		}

		if (pBfileStmt != NULL) {
			sqlite3_finalize(pBfileStmt);
			pBfileStmt = NULL;
		}

		rc3 = rc2;
		/* BFILE_CLOSE */
	        rc2 = sqlite3_prepare(db, sql[2], -1, &pBfileStmt, 0);
               	if (rc2) {
			printf("\t[Failed]\tBFILE_CLOSE: prepare error\n");
			continue;
		}

		rc2 = sqlite3_bind_int64(pBfileStmt, 1, bHdl);
               	if (rc2)
			printf("\t[Failed]\tBFILE_CLOSE: bind error\n");
		else {
			rc2 = sqlite3_step(pBfileStmt);
			if (rc2 && rc2 != SQLITE_ROW && rc2 != SQLITE_DONE)
				printf("\t[Failed]\tBFILE_CLOSE: step error\n");
		}

		if (pBfileStmt != NULL) {
			sqlite3_finalize(pBfileStmt);
			pBfileStmt = NULL;
		}

		if ((rc2 == SQLITE_OK || rc2 == SQLITE_ROW) &&
				(rc3 == SQLITE_OK || rc3 == SQLITE_ROW))
		{
			printf("\t[OK]\n");
		}
	}

	if (pStmt != NULL)
		sqlite3_finalize(pStmt);

	if (rc && rc != SQLITE_DONE)
		printf("\t[Failed]\tBFILE_OPEN: step error\n");
}

static int query_with_api(sqlite3* db) {
        test_BfileSize(db);
	printf("\n\n");

	test_BfileOpenReadClose(db);
	printf("\n\n");
	return 0;
}

static void test_api(sqlite3 *db) {
	int rc;

	/* query */
	rc = query_with_api(db);
	if (rc)
		_exit(-1);
}

int main(int argc, char **argv) {
	sqlite3 *db;
	
	pre_test(&db);

	test_api(db);

	sqlite3_close(db);

	return 0;
}