[go: up one dir, main page]

Menu

[r108]: / trunk / core / blob.cpp  Maximize  Restore  History

Download this file

387 lines (332 with data), 11.4 kB

  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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
///////////////////////////////////////////////////////////////////////////////
//
// File : $Id$
// Subject : IBPP, Blob class implementation
//
///////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright 2000-2007 T.I.P. Group S.A. and the IBPP Team (www.ibpp.org)
//
// The contents of this file are subject to the IBPP License (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at http://www.ibpp.org or in the 'license.txt'
// file which must have been distributed along with this file.
//
// This software, distributed under the License, is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
///////////////////////////////////////////////////////////////////////////////
//
// COMMENTS
// * Tabulations should be set every four characters when editing this file.
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
#pragma warning(disable: 4786 4996)
#ifndef _DEBUG
#pragma warning(disable: 4702)
#endif
#endif
#include "_ibpp.h"
#ifdef HAS_HDRSTOP
#pragma hdrstop
#endif
using namespace ibpp_internals;
// (((((((( OBJECT INTERFACE IMPLEMENTATION ))))))))
void BlobImpl::Open()
{
if (mHandle != 0)
throw LogicExceptionImpl("Blob::Open", _("Blob already opened."));
if (mDatabase == 0)
throw LogicExceptionImpl("Blob::Open", _("No Database is attached."));
if (mTransaction == 0)
throw LogicExceptionImpl("Blob::Open", _("No Transaction is attached."));
if (! mIdAssigned)
throw LogicExceptionImpl("Blob::Open", _("Blob Id is not assigned."));
IBS status(mDriver);
(mDriver->m_open_blob2)(status.Self(), mDatabase->GetHandlePtr(),
mTransaction->GetHandlePtr(), &mHandle, &mId, 0, 0);
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Open", _("isc_open_blob2 failed."));
mWriteMode = false;
}
void BlobImpl::Create()
{
if (mHandle != 0)
throw LogicExceptionImpl("Blob::Create", _("Blob already opened."));
if (mDatabase == 0)
throw LogicExceptionImpl("Blob::Create", _("No Database is attached."));
if (mTransaction == 0)
throw LogicExceptionImpl("Blob::Create", _("No Transaction is attached."));
IBS status(mDriver);
(mDriver->m_create_blob2)(status.Self(), mDatabase->GetHandlePtr(),
mTransaction->GetHandlePtr(), &mHandle, &mId, 0, 0);
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Create",
_("isc_create_blob failed."));
mIdAssigned = true;
mWriteMode = true;
}
void BlobImpl::Close()
{
if (mHandle == 0) return; // Not opened anyway
IBS status(mDriver);
(mDriver->m_close_blob)(status.Self(), &mHandle);
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Close", _("isc_close_blob failed."));
mHandle = 0;
}
void BlobImpl::Cancel()
{
if (mHandle == 0) return; // Not opened anyway
if (! mWriteMode)
throw LogicExceptionImpl("Blob::Cancel", _("Can't cancel a Blob opened for read"));
IBS status(mDriver);
(mDriver->m_cancel_blob)(status.Self(), &mHandle);
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Cancel", _("isc_cancel_blob failed."));
mHandle = 0;
mIdAssigned = false;
}
int BlobImpl::Read(void* buffer, int size)
{
if (mHandle == 0)
throw LogicExceptionImpl("Blob::Read", _("The Blob is not opened"));
if (mWriteMode)
throw LogicExceptionImpl("Blob::Read", _("Can't read from Blob opened for write"));
if (size < 1 || size > (64*1024-1))
throw LogicExceptionImpl("Blob::Read", _("Invalid segment size (max 64Kb-1)"));
IBS status(mDriver);
unsigned short bytesread;
int result = (mDriver->m_get_segment)(status.Self(), &mHandle, &bytesread,
(unsigned short)size, (char*)buffer);
if (result == isc_segstr_eof) return 0; // Fin du blob
if (result != isc_segment && status.Errors())
throw SQLExceptionImpl(status, "Blob::Read", _("isc_get_segment failed."));
return (int)bytesread;
}
void BlobImpl::Write(const void* buffer, int size)
{
if (mHandle == 0)
throw LogicExceptionImpl("Blob::Write", _("The Blob is not opened"));
if (! mWriteMode)
throw LogicExceptionImpl("Blob::Write", _("Can't write to Blob opened for read"));
if (size < 1 || size > (64*1024-1))
throw LogicExceptionImpl("Blob::Write", _("Invalid segment size (max 64Kb-1)"));
IBS status(mDriver);
(mDriver->m_put_segment)(status.Self(), &mHandle,
(unsigned short)size, (char*)buffer);
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Write", _("isc_put_segment failed."));
}
void BlobImpl::Info(int* Size, int* Largest, int* Segments)
{
char items[] = {isc_info_blob_total_length,
isc_info_blob_max_segment,
isc_info_blob_num_segments};
if (mHandle == 0)
throw LogicExceptionImpl("Blob::GetInfo", _("The Blob is not opened"));
IBS status(mDriver);
RB result(mDriver, 100);
(mDriver->m_blob_info)(status.Self(), &mHandle, sizeof(items), items,
(short)result.Size(), result.Self());
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::GetInfo", _("isc_blob_info failed."));
if (Size != 0) *Size = result.GetValue(isc_info_blob_total_length);
if (Largest != 0) *Largest = result.GetValue(isc_info_blob_max_segment);
if (Segments != 0) *Segments = result.GetValue(isc_info_blob_num_segments);
}
void BlobImpl::Save(const std::string& data)
{
if (mHandle != 0)
throw LogicExceptionImpl("Blob::Save", _("Blob already opened."));
if (mDatabase == 0)
throw LogicExceptionImpl("Blob::Save", _("No Database is attached."));
if (mTransaction == 0)
throw LogicExceptionImpl("Blob::Save", _("No Transaction is attached."));
IBS status(mDriver);
(mDriver->m_create_blob2)(status.Self(), mDatabase->GetHandlePtr(),
mTransaction->GetHandlePtr(), &mHandle, &mId, 0, 0);
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Save",
_("isc_create_blob failed."));
mIdAssigned = true;
mWriteMode = true;
size_t pos = 0;
size_t len = data.size();
while (len != 0)
{
size_t blklen = (len < 32*1024-1) ? len : 32*1024-1;
status.Reset();
(mDriver->m_put_segment)(status.Self(), &mHandle,
(unsigned short)blklen, const_cast<char*>(data.data()+pos));
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Save",
_("isc_put_segment failed."));
pos += blklen;
len -= blklen;
}
status.Reset();
(mDriver->m_close_blob)(status.Self(), &mHandle);
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Save", _("isc_close_blob failed."));
mHandle = 0;
}
void BlobImpl::Load(std::string& data)
{
if (mHandle != 0)
throw LogicExceptionImpl("Blob::Load", _("Blob already opened."));
if (mDatabase == 0)
throw LogicExceptionImpl("Blob::Load", _("No Database is attached."));
if (mTransaction == 0)
throw LogicExceptionImpl("Blob::Load", _("No Transaction is attached."));
if (! mIdAssigned)
throw LogicExceptionImpl("Blob::Load", _("Blob Id is not assigned."));
IBS status(mDriver);
(mDriver->m_open_blob2)(status.Self(), mDatabase->GetHandlePtr(),
mTransaction->GetHandlePtr(), &mHandle, &mId, 0, 0);
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Load", _("isc_open_blob2 failed."));
mWriteMode = false;
size_t blklen = 32*1024-1;
data.resize(blklen);
size_t size = 0;
size_t pos = 0;
for (;;)
{
status.Reset();
unsigned short bytesread;
int result = (mDriver->m_get_segment)(status.Self(), &mHandle,
&bytesread, (unsigned short)blklen,
const_cast<char*>(data.data()+pos));
if (result == isc_segstr_eof) break; // End of blob
if (result != isc_segment && status.Errors())
throw SQLExceptionImpl(status, "Blob::Load", _("isc_get_segment failed."));
pos += bytesread;
size += bytesread;
data.resize(size + blklen);
}
data.resize(size);
status.Reset();
(mDriver->m_close_blob)(status.Self(), &mHandle);
if (status.Errors())
throw SQLExceptionImpl(status, "Blob::Load", _("isc_close_blob failed."));
mHandle = 0;
}
IBPP::Database BlobImpl::DatabasePtr() const
{
if (mDatabase == 0) throw LogicExceptionImpl("Blob::DatabasePtr",
_("No Database is attached."));
return mDatabase;
}
IBPP::Transaction BlobImpl::TransactionPtr() const
{
if (mTransaction == 0) throw LogicExceptionImpl("Blob::TransactionPtr",
_("No Transaction is attached."));
return mTransaction;
}
IBPP::IBlob* BlobImpl::AddRef()
{
ASSERTION(mRefCount >= 0);
++mRefCount;
return this;
}
void BlobImpl::Release()
{
// Release cannot throw, except in DEBUG builds on assertion
ASSERTION(mRefCount >= 0);
--mRefCount;
try
{
if (mRefCount <= 0)
{
mDriver->Detach(this);
delete this;
}
}
catch (...) { }
}
// (((((((( OBJECT INTERNAL METHODS ))))))))
void BlobImpl::Init()
{
mIdAssigned = false;
mWriteMode = false;
mHandle = 0;
mDatabase = 0;
mTransaction = 0;
}
void BlobImpl::SetId(ISC_QUAD* quad)
{
if (mHandle != 0)
throw LogicExceptionImpl("BlobImpl::SetId", _("Can't set Id on an opened BlobImpl."));
if (quad == 0)
throw LogicExceptionImpl("BlobImpl::SetId", _("Null Id reference detected."));
memcpy(&mId, quad, sizeof(mId));
mIdAssigned = true;
}
void BlobImpl::GetId(ISC_QUAD* quad)
{
if (mHandle != 0)
throw LogicExceptionImpl("BlobImpl::GetId", _("Can't get Id on an opened BlobImpl."));
if (! mWriteMode)
throw LogicExceptionImpl("BlobImpl::GetId", _("Can only get Id of a newly created Blob."));
if (quad == 0)
throw LogicExceptionImpl("BlobImpl::GetId", _("Null Id reference detected."));
memcpy(quad, &mId, sizeof(mId));
}
void BlobImpl::AttachDatabaseImpl(DatabaseImpl* database)
{
if (database == 0) throw LogicExceptionImpl("Blob::AttachDatabase",
_("Can't attach a NULL Database object."));
if (mDatabase != 0) mDatabase->DetachBlobImpl(this);
mDatabase = database;
mDatabase->AttachBlobImpl(this);
}
void BlobImpl::AttachTransactionImpl(TransactionImpl* transaction)
{
if (transaction == 0) throw LogicExceptionImpl("Blob::AttachTransaction",
_("Can't attach a NULL Transaction object."));
if (mTransaction != 0) mTransaction->DetachBlobImpl(this);
mTransaction = transaction;
mTransaction->AttachBlobImpl(this);
}
void BlobImpl::DetachDatabaseImpl()
{
if (mDatabase == 0) return;
mDatabase->DetachBlobImpl(this);
mDatabase = 0;
}
void BlobImpl::DetachTransactionImpl()
{
if (mTransaction == 0) return;
mTransaction->DetachBlobImpl(this);
mTransaction = 0;
}
BlobImpl::BlobImpl(DriverImpl* drv, DatabaseImpl* database, TransactionImpl* transaction)
: mRefCount(0), mDriver(drv)
{
Init();
AttachDatabaseImpl(database);
if (transaction != 0) AttachTransactionImpl(transaction);
}
BlobImpl::~BlobImpl()
{
try
{
if (mHandle != 0)
{
if (mWriteMode) BlobImpl::Cancel();
else BlobImpl::Close();
}
}
catch (...) { }
try { if (mTransaction != 0) mTransaction->DetachBlobImpl(this); }
catch (...) { }
try { if (mDatabase != 0) mDatabase->DetachBlobImpl(this); }
catch (...) { }
}
//
// EOF
//