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
|
/*-
* 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 <sqlite3.h>
#include <bfile.h>
#define CHECK_ERR \
if (rc != SQLITE_OK) { \
fprintf(stderr, " SQL error: %s\n", zErrMsg); \
sqlite3_free(zErrMsg); \
return -1; \
}
#define EXEC(sql) \
printf("\nRuning:\t%s\n",(sql)); \
rc = sqlite3_exec(db, (sql), NULL, 0, &zErrMsg); \
CHECK_ERR
#define QUERY(sql) \
printf("\nRuning:\t%s\n",(sql)); \
rc = sqlite3_prepare(db, (sql), -1, &pStmt, 0); \
if (rc != SQLITE_OK) { \
fprintf(stderr," SQL error: %d\n", rc); \
return -1; \
} \
printf("\tname\tphone \taddress \tphoto_path\n"); \
while (SQLITE_ROW == sqlite3_step(pStmt)) { \
const char *name = \
(const char *)sqlite3_column_text(pStmt, 0);\
const char *phone_num = \
(const char *)sqlite3_column_text(pStmt, 1);\
const char *address = \
(const char *)sqlite3_column_text(pStmt, 2);\
const char *photo_path = \
(const char *)sqlite3_column_text(pStmt, 3);\
printf("\t%s\t%s\t%s\t%s\n", name, phone_num, \
address, photo_path); \
}
#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);\
#define BUFF_SIZE 80
static int query_with_api(sqlite3* db, char * sql) {
int rc, len, i;
sqlite3_stmt *pStmt;
sqlite3_bfile *pBfile;
char *name, *phone_num, *address, buf[BUFF_SIZE];
off_t offset;
printf("\nRuning:\t%s with BFILE APIs\n",(sql));
rc = sqlite3_prepare(db, sql, -1, &pStmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr," SQL error: %d\n", rc);
return -1;
}
printf("\tname\tphone \taddress \n");
while (SQLITE_ROW == sqlite3_step(pStmt)) {
name = (char *)sqlite3_column_text(pStmt, 0);
phone_num = (char *)sqlite3_column_text(pStmt, 1);
address = (char *)sqlite3_column_text(pStmt, 2);
printf("\t%s\t%s\t%s\n", name, phone_num, address);
/* Bfile APIs */
sqlite3_column_bfile(pStmt, 3, &pBfile);
rc = sqlite3_bfile_open(pBfile);
if (rc != SQLITE_OK)
return -1;
offset = 0;
do {
rc = sqlite3_bfile_read(pBfile, buf, BUFF_SIZE,
offset, &len);
if (rc != SQLITE_OK)
return -1;
for (i = 0; i < len; i++)
putchar(buf[i]);
offset += len;
} while(len > 0);
printf("\n");
rc = sqlite3_bfile_close(pBfile);
if (rc != SQLITE_OK)
return -1;
rc = sqlite3_bfile_final(pBfile);
if (rc != SQLITE_OK)
return -1;
}
return 0;
}
int main(int argc, char **argv) {
sqlite3 *db;
sqlite3_stmt *pStmt;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("example.db", &db);
CHECK_ERR
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','./temp');")
EXEC("SELECT BFILE_CREATE_DIRECTORY('IMG', './temp');")
/* create a table with BFILE type column */
EXEC("CREATE TABLE address_book(name TEXT, phone_num TEXT, address TEXT, photo BFILE);")
/* insert */
EXEC("INSERT INTO address_book VALUES('Tony', '+861082798880', 'China Beijing', BFILE_NAME('IMG','Tony.jpg'));")
/* insert */
EXEC("INSERT INTO address_book VALUES('Rachel', '+861082798881', 'China Beijing', BFILE_NAME('IMG','Rachel.jpg'));")
/* insert */
EXEC("INSERT INTO address_book VALUES('Tracy', '+861082798882', 'China Beijing', BFILE_NAME('IMG','Tracy.jpg'));")
/* query */
QUERY("SELECT name, phone_num, address, BFILE_FULLPATH(photo) FROM address_book;")
rc = query_with_api(db, "SELECT name, phone_num, address, photo FROM address_book;");
if (rc) return -1;
/* update */
EXEC("UPDATE address_book SET photo = BFILE_NAME('IMG', 'Tony_new.jpg') where name = 'Tony';")
/* query */
QUERY("SELECT name, phone_num, address, BFILE_FULLPATH(photo) FROM address_book;")
rc = query_with_api(db, "SELECT name, phone_num, address, photo FROM address_book;");
if (rc) return -1;
/* update alias */
//EXEC("UPDATE BFILE_DIRECTORY SET PATH = './photo' WHERE ALIAS = 'IMG';");
EXEC("SELECT BFILE_REPLACE_DIRECTORY('IMG', './photo');")
/* query */
QUERY("SELECT name, phone_num, address, BFILE_FULLPATH(photo) FROM address_book;")
rc = query_with_api(db, "SELECT name, phone_num, address, photo FROM address_book;");
if (rc) return -1;
sqlite3_close(db);
return 0;
}
|