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
|
/*
* OpenDBX - A simple but extensible database abstraction layer
* Copyright (C) 2006-2008 Norbert Sendetzky and others
*
* Distributed under the terms of the GNU Library General Public Licence
* version 2 or (at your option) any later version.
*/
#ifdef HAVE_OCILOBWRITE2
#define ORAXB8_DEFINED
// Oracle 10.2.0 workaround, bug 4901517
#ifndef oraub8
typedef unsigned long long oraub8;
#endif
#include "oracle_lo.h"
#include <ociap.h>
#include <stdlib.h>
/*
* Declaration of Oracle capabilities
*/
struct odbx_lo_ops oracle_odbx_lo_ops = {
.open = oracle_odbx_lo_open,
.close = oracle_odbx_lo_close,
.read = oracle_odbx_lo_read,
.write = oracle_odbx_lo_write,
};
/*
* ODBX large object operations
* Oracle style
*/
static int oracle_odbx_lo_open( odbx_result_t* result, odbx_lo_t** lo, const char* value )
{
struct oralob* oralob;
struct oraconn* conn = (struct oraconn*) result->handle->aux;
if( ( *lo = (odbx_lo_t*) malloc( sizeof( struct odbx_lo_t ) ) ) == NULL )
{
return -ODBX_ERR_NOMEM;
}
if( ( oralob = (struct oralob*) malloc( sizeof( struct oralob ) ) ) == NULL )
{
free( *lo );
*lo = NULL;
return -ODBX_ERR_NOMEM;
}
oralob->lob = (OCILobLocator*) value;
oralob->rpiece = OCI_FIRST_PIECE;
oralob->wpiece = OCI_FIRST_PIECE;
(*lo)->result = result;
(*lo)->generic = (void*) oralob;
if( ( conn->errcode = OCILobOpen( conn->ctx, conn->err, (OCILobLocator*) value, OCI_LOB_READONLY ) ) != OCI_SUCCESS ) // OCI_LOB_READWRITE
{
free( (*lo)->generic );
(*lo)->generic = NULL;
free( *lo );
*lo = NULL;
return -ODBX_ERR_BACKEND;
}
return ODBX_ERR_SUCCESS;
}
static int oracle_odbx_lo_close( odbx_lo_t* lo )
{
if( lo->result == NULL || lo->result->handle == NULL || lo->result->handle->aux == NULL )
{
return ODBX_ERR_HANDLE;
}
struct oraconn* conn = (struct oraconn*) lo->result->handle->aux;
struct oralob* oralob = (struct oralob*) lo->generic;
if( oralob->wpiece == OCI_NEXT_PIECE )
{
oraub8 blen = 0;
oraub8 clen = 0;
int buffer = 0;
if( ( conn->errcode = OCILobWrite2( conn->ctx, conn->err, lo->generic, &blen, &clen, 1, (dvoid*) &buffer, 0, OCI_LAST_PIECE, NULL, NULL, 0, SQLCS_IMPLICIT ) ) != OCI_SUCCESS )
{
return -ODBX_ERR_BACKEND;
}
}
if( ( conn->errcode = OCILobClose( conn->ctx, conn->err, lo->generic ) ) != OCI_SUCCESS )
{
return -ODBX_ERR_BACKEND;
}
free( lo->generic );
lo->generic = NULL;
free( lo );
return ODBX_ERR_SUCCESS;
}
static ssize_t oracle_odbx_lo_read( odbx_lo_t* lo, void* buffer, size_t buflen )
{
oraub8 blen = 0;
oraub8 clen = 0;
struct oraconn* conn = (struct oraconn*) lo->result->handle->aux;
struct oralob* oralob = (struct oralob*) lo->generic;
if( lo->generic == NULL ) { return -ODBX_ERR_HANDLE; }
if( buflen > 0x7FFFFFFF ) { buflen = 0x7FFFFFFF; } // we can only return ssize_t
conn->errcode = OCILobRead2( conn->ctx, conn->err, lo->generic, &blen, &clen, 1, buffer, (oraub8) buflen, oralob->rpiece, NULL, NULL, 0, SQLCS_IMPLICIT );
switch( conn->errcode )
{
case OCI_SUCCESS:
return (ssize_t) 0;
case OCI_NEED_DATA:
oralob->rpiece = OCI_NEXT_PIECE;
return (ssize_t) blen;
}
return -ODBX_ERR_BACKEND;
}
static ssize_t oracle_odbx_lo_write( odbx_lo_t* lo, void* buffer, size_t buflen )
{
oraub8 blen = 0;
oraub8 clen = 0;
struct oraconn* conn = (struct oraconn*) lo->result->handle->aux;
struct oralob* oralob = (struct oralob*) lo->generic;
if( lo->generic == NULL ) { return -ODBX_ERR_HANDLE; }
if( buflen > 0x7FFFFFFF ) { buflen = 0x7FFFFFFF; } // we can only return ssize_t
if( oralob->wpiece == OCI_FIRST_PIECE )
{
if( ( conn->errcode = OCILobTrim2( conn->ctx, conn->err, lo->generic, 0 ) ) != OCI_SUCCESS )
{
return -ODBX_ERR_BACKEND;
}
}
conn->errcode = OCILobWrite2( conn->ctx, conn->err, lo->generic, &blen, &clen, 1, buffer, (oraub8) buflen, oralob->wpiece, NULL, NULL, 0, SQLCS_IMPLICIT );
switch( conn->errcode )
{
case OCI_SUCCESS:
case OCI_NEED_DATA:
oralob->wpiece = OCI_NEXT_PIECE;
return (ssize_t) blen;
}
return -ODBX_ERR_BACKEND;
}
#endif
|