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
|
//-----------------------------------------------------------------------------
// Copyright (c) 2016, 2022, Oracle and/or its affiliates.
//
// This software is dual-licensed to you under the Universal Permissive License
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
// either license.
//
// If you elect to accept the software under the Apache License, Version 2.0,
// the following applies:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// DemoBFILE.c
// Demos whether BFILEs are handled properly using ODPI-C.
//
// NOTE: the program assumes that you have write access to the
// directory path pointed to by the directory object, i.e. that the
// program is being run on the same machine as the database.
//
// DIR_NAME is specified in the Makefile
//
//-----------------------------------------------------------------------------
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "SampleLib.h"
#define SQL_TEXT_QUERY_DIR "select directory_path " \
"from all_directories " \
"where directory_name = :1"
#define SQL_TEXT_DELETE "delete from DemoBFILEs"
#define SQL_TEXT_INSERT "insert into DemoBFILEs " \
"values (:IntValue, :BFILEValue)"
#define SQL_TEXT_QUERY "select IntCol, BFILECol " \
"from DemoBFILEs"
#define FILE_NAME "demo_contents.txt"
//-----------------------------------------------------------------------------
// main()
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
dpiData *intColValue, *bfileColValue, *pathValue, *bfileValue, intValue;
uint32_t numQueryColumns, bufferRowIndex, i;
dpiNativeTypeNum nativeTypeNum;
dpiSampleParams *params;
dpiQueryInfo queryInfo;
uint64_t blobSize;
dpiData bindValue;
dpiVar *bfileVar;
dpiStmt *stmt;
dpiConn *conn;
char *path;
int found;
FILE *fp;
// connect to database
params = dpiSamples_getParams();
conn = dpiSamples_getConn(0, NULL);
printf("Note: this demo must be run on the same machine as the database\n");
// find the directory path location by querying from the database
if (dpiConn_prepareStmt(conn, 0, SQL_TEXT_QUERY_DIR,
strlen(SQL_TEXT_QUERY_DIR), NULL, 0, &stmt) < 0)
return dpiSamples_showError();
dpiData_setBytes(&bindValue, (char*) params->dirName,
params->dirNameLength);
if (dpiStmt_bindValueByPos(stmt, 1, DPI_NATIVE_TYPE_BYTES, &bindValue) < 0)
return dpiSamples_showError();
if (dpiStmt_execute(stmt, 0, &numQueryColumns) < 0)
return dpiSamples_showError();
if (dpiStmt_fetch(stmt, &found, &bufferRowIndex) < 0)
return dpiSamples_showError();
if (dpiStmt_getQueryValue(stmt, 1, &nativeTypeNum, &pathValue) < 0)
return dpiSamples_showError();
path = malloc(pathValue->value.asBytes.length + 1);
if (!path) {
printf("ERROR: unable to duplicate path string!?\n");
return -1;
}
memcpy(path, pathValue->value.asBytes.ptr,
pathValue->value.asBytes.length);
path[pathValue->value.asBytes.length] = '\0';
dpiStmt_release(stmt);
printf("%.*s path is '%s'\n", params->dirNameLength, params->dirName,
path);
// write a temporary file at that location
if (chdir(path) < 0) {
printf("ERROR: unable to change directory to %.*s location\n",
params->dirNameLength, params->dirName);
return -1;
}
free(path);
printf("Writing file named '%s'\n", FILE_NAME);
fp = fopen(FILE_NAME, "w");
if (!fp) {
printf("ERROR: unable to open demo file for writing\n");
return -1;
}
fprintf(fp, "These are some demo comments.\nFile can be deleted.\n");
fclose(fp);
// delete existing rows in table
printf("Delete existing rows in table...\n");
if (dpiConn_prepareStmt(conn, 0, SQL_TEXT_DELETE, strlen(SQL_TEXT_DELETE),
NULL, 0, &stmt) < 0)
return dpiSamples_showError();
if (dpiStmt_execute(stmt, 0, &numQueryColumns) < 0)
return dpiSamples_showError();
dpiStmt_release(stmt);
// inserting row into table
printf("Inserting row into table...\n");
if (dpiConn_newVar(conn, DPI_ORACLE_TYPE_BFILE, DPI_NATIVE_TYPE_LOB, 1, 0,
0, 0, NULL, &bfileVar, &bfileValue) < 0)
return dpiSamples_showError();
bfileValue->isNull = 0;
if (dpiLob_setDirectoryAndFileName(bfileValue->value.asLOB,
params->dirName, params->dirNameLength, FILE_NAME,
strlen(FILE_NAME)) < 0)
return dpiSamples_showError();
intValue.isNull = 0;
intValue.value.asInt64 = 1;
if (dpiConn_prepareStmt(conn, 0, SQL_TEXT_INSERT, strlen(SQL_TEXT_INSERT),
NULL, 0, &stmt) < 0)
return dpiSamples_showError();
if (dpiStmt_bindValueByPos(stmt, 1, DPI_NATIVE_TYPE_INT64, &intValue) < 0)
return dpiSamples_showError();
if (dpiStmt_bindByPos(stmt, 2, bfileVar) < 0)
return dpiSamples_showError();
if (dpiStmt_execute(stmt, 0, &numQueryColumns) < 0)
return dpiSamples_showError();
if (dpiConn_commit(conn) < 0)
return dpiSamples_showError();
dpiStmt_release(stmt);
dpiVar_release(bfileVar);
// querying row from table
printf("Querying row from table...\n");
if (dpiConn_prepareStmt(conn, 0, SQL_TEXT_QUERY, strlen(SQL_TEXT_QUERY),
NULL, 0, &stmt) < 0)
return dpiSamples_showError();
if (dpiStmt_execute(stmt, 0, &numQueryColumns) < 0)
return dpiSamples_showError();
while (1) {
if (dpiStmt_fetch(stmt, &found, &bufferRowIndex) < 0)
return dpiSamples_showError();
if (!found)
break;
if (dpiStmt_getQueryValue(stmt, 1, &nativeTypeNum, &intColValue) < 0 ||
dpiStmt_getQueryValue(stmt, 2, &nativeTypeNum,
&bfileColValue) < 0)
return dpiSamples_showError();
if (dpiLob_getSize(bfileColValue->value.asLOB, &blobSize) < 0)
return dpiSamples_showError();
printf("Row: IntCol = %g, BfileCol = BFILE(%" PRIu64 ")\n",
intColValue->value.asDouble, blobSize);
}
// display description of each variable
for (i = 0; i < numQueryColumns; i++) {
if (dpiStmt_getQueryInfo(stmt, i + 1, &queryInfo) < 0)
return dpiSamples_showError();
printf("('%.*s', %d, %d, %d, %d, %d, %d)\n", queryInfo.nameLength,
queryInfo.name, queryInfo.typeInfo.oracleTypeNum,
queryInfo.typeInfo.sizeInChars,
queryInfo.typeInfo.clientSizeInBytes,
queryInfo.typeInfo.precision, queryInfo.typeInfo.scale,
queryInfo.nullOk);
}
// clean up
dpiStmt_release(stmt);
dpiConn_release(conn);
printf("Done.\n");
return 0;
}
|