Skip to content

Commit 9be4fae

Browse files
committed
Added a type property to the Lob class to distinguish CLOB and BLOB types
1 parent 6f4c1d2 commit 9be4fae

File tree

5 files changed

+100
-14
lines changed

5 files changed

+100
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
- Fixed encoding issues with several files that caused compilation warnings in some Windows environments.
1818

19+
- Added a `type` property to the Lob class to distinguish CLOB and BLOB types.
20+
1921
## node-oracledb v1.1.0 (3 Sep 2015)
2022

2123
- Enhanced pool.release() to drop the session if it is known to be unusable, allowing a new session to be created.

doc/api.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ limitations under the License.
7373
- 5.1.1 [chunkSize](#proplobchunksize)
7474
- 5.1.2 [length](#proploblength)
7575
- 5.1.3 [pieceSize](#proplobpiecesize)
76+
- 5.1.4 [type](#proplobtype)
7677
6. [Pool Class](#poolclass)
7778
- 6.1 [Pool Properties](#poolproperties)
7879
- 6.1.1 [connectionsInUse](#proppoolconnectionsinuse)
@@ -253,7 +254,7 @@ Oracledb.ARRAY // Fetch each row as array of column values
253254
Oracledb.OBJECT // Fetch each row as an object
254255
```
255256

256-
#### Constants for `execute()` [bind parameter](#executebindParams) `type` properties, for [`fetchAsString`](#propdbfetchasstring) and for [`fetchInfo`](#propfetchinfo):
257+
#### Type constants for `execute()` [bind parameter](#executebindParams) and [Lob](#proplobpiecesize) `type` properties, for [`fetchAsString`](#propdbfetchasstring), and for [`fetchInfo`](#propfetchinfo):
257258

258259
```
259260
Oracledb.BLOB // Bind a BLOB to return a Node.js buffer
@@ -264,7 +265,7 @@ Oracledb.CURSOR // Bind a REF CURSOR to a node-oracledb Resul
264265
265266
Oracledb.DATE // Bind as JavaScript date type. Can also be used for fetchAsString and fetchInfo
266267
267-
Oracledb.DEFAULT // Used with [`fetchInfo`](#propfetchinfo) to reset the fetch type to the database type
268+
Oracledb.DEFAULT // Used with fetchInfo to reset the fetch type to the database type
268269
269270
Oracledb.NUMBER // Bind as JavaScript number type. Can also be used for fetchAsString and fetchInfo
270271
@@ -1401,6 +1402,19 @@ For efficiency, it is recommended that `pieceSize` be a multiple of
14011402

14021403
The maximum value for `pieceSize` is limited to the value of UINT_MAX.
14031404

1405+
#### <a name="proplobtype"></a> 5.1.4 type
1406+
1407+
```
1408+
readonly Number type
1409+
```
1410+
1411+
This read-only attribute shows the type of Lob being used. It will
1412+
have the value of one of the constants
1413+
[`Oracledb.BLOB`](#oracledbconstants) or
1414+
[`Oracledb.CLOB`](#oracledbconstants). The value is derived from the
1415+
bind type when using LOB bind variables, or from the column type when
1416+
a LOB is returned by a query.
1417+
14041418
## <a name="poolclass"></a> 6. Pool Class
14051419

14061420
A connection *Pool* object is created by calling the
@@ -2756,11 +2770,10 @@ writing it to a file. It is similar to the example
27562770
The returned column value is a Lob stream which is piped to an opened
27572771
file stream.
27582772
2759-
By default the Lob stream is a buffer, so the `setEncoding()` call is
2760-
used to indicate data should be a string. This CLOB example uses
2761-
'utf8'. (A buffer would be useful for BLOB data). Both the Lob
2762-
stream and output data stream have 'error' events to handle unexpected
2763-
issues:
2773+
By default the Lob stream is a Node.js buffer - which is useful for
2774+
BLOB data. Since this example uses a CLOB, the `setEncoding()` call
2775+
is used to indicate data should be a string. Both the Lob stream and
2776+
output data stream have 'error' events to handle unexpected issues:
27642777
27652778
```javascript
27662779
var oracledb = require('oracledb');

lib/oracledb.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ function Lob(iLob, opt)
4949

5050
"pieceSize": {get: function() {return iLob.pieceSize;},
5151
set: function(newPieceSize) {iLob.pieceSize = newPieceSize;}},
52+
53+
"type": {get: function() {return iLob.type}}
5254
});
5355
}
5456

src/njs/src/njsIntLob.cpp

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Persistent<FunctionTemplate> ILob::iLobTemplate_s;
8989
ILob::ILob():
9090
lobLocator_(NULL), njsconn_(NULL), dpiconn_(NULL), svch_(NULL), errh_(NULL),
9191
isValid_(false), state_(INACTIVE), buf_(NULL), bufSize_(0), chunkSize_(0),
92-
length_(0), offset_(1), amountRead_(0)
92+
length_(0), offset_(1), amountRead_(0), type_(DATA_UNKNOWN)
9393
{
9494

9595
}
@@ -227,10 +227,12 @@ void ILob::setILob(eBaton *executeBaton, ProtoILob *protoILob)
227227
{
228228
// accommodate multi-byte charsets
229229
buf_ = new char[bufSize_ * dpiconn_->getByteExpansionRatio()];
230+
type_ = DATA_CLOB;
230231
}
231-
else
232+
else if (fetchType_ == DpiBlob)
232233
{
233234
buf_ = new char[bufSize_];
235+
type_ = DATA_BLOB;
234236
}
235237

236238
// Now the ILob object is valid
@@ -293,6 +295,10 @@ void ILob::Init(Handle<Object> target)
293295
ILob::GetOffset,
294296
ILob::SetOffset);
295297

298+
tpl->InstanceTemplate()->SetAccessor(NanNew<v8::String>("type"),
299+
ILob::GetType,
300+
ILob::SetType);
301+
296302
NanAssignPersistent(iLobTemplate_s, tpl);
297303
target->Set(NanNew<v8::String>("ILob"), tpl->GetFunction());
298304
}
@@ -521,7 +527,7 @@ NAN_PROPERTY_GETTER(ILob::GetLength)
521527
/*****************************************************************************/
522528
/*
523529
DESCRIPTION
524-
Set Accessor of length property - throws error as lenght is a read-only
530+
Set Accessor of length property - throws error as length is a read-only
525531
property.
526532
527533
PARAMETERS
@@ -739,6 +745,66 @@ NAN_SETTER(ILob::SetOffset)
739745
}
740746

741747

748+
/*****************************************************************************/
749+
/*
750+
DESCRIPTION
751+
Get Accessor of type property
752+
753+
PARAMETERS
754+
args - ILob object
755+
756+
RETURNS
757+
the type of the LOB (either CLOB or BLOB)
758+
759+
NOTES
760+
761+
*/
762+
763+
NAN_PROPERTY_GETTER(ILob::GetType)
764+
{
765+
NanScope();
766+
767+
ILob *iLob = ObjectWrap::Unwrap<ILob>(args.Holder());
768+
769+
try
770+
{
771+
Local<Number> value = NanNew<v8::Number>((unsigned long)iLob->type_);
772+
NanReturnValue(value);
773+
}
774+
775+
catch(dpi::Exception &e)
776+
{
777+
NJS_SET_EXCEPTION(e.what(), strlen(e.what()));
778+
NanReturnUndefined();
779+
}
780+
781+
NanReturnUndefined();
782+
}
783+
784+
785+
/*****************************************************************************/
786+
/*
787+
DESCRIPTION
788+
Set Accessor of type property - throws error as type is a read-only
789+
property.
790+
791+
PARAMETERS
792+
args - ILob object
793+
794+
RETURNS
795+
throws error
796+
797+
NOTES
798+
799+
*/
800+
801+
NAN_SETTER(ILob::SetType)
802+
{
803+
lobPropertyException(ObjectWrap::Unwrap<ILob>(args.Holder()), errReadOnly,
804+
"type");
805+
}
806+
807+
742808

743809
/*****************************************************************************/
744810
/*
@@ -825,7 +891,7 @@ void ILob::Async_Read(uv_work_t *req)
825891
try
826892
{
827893
unsigned long long byteAmount = (unsigned long int)iLob->bufSize_;
828-
unsigned long long charAmount = 0;
894+
unsigned long long charAmount = 0;
829895

830896
// Clobs read by characters
831897
if (iLob->fetchType_ == DpiClob)

src/njs/src/njsIntLob.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,15 @@ class ILob : public ObjectWrap
170170
static NAN_PROPERTY_GETTER(GetLength);
171171
static NAN_PROPERTY_GETTER(GetPieceSize);
172172
static NAN_PROPERTY_GETTER(GetOffset);
173+
static NAN_PROPERTY_GETTER(GetType);
173174

174175

175176
// Setters for properties
176177
static NAN_SETTER(SetChunkSize);
177178
static NAN_SETTER(SetLength);
178179
static NAN_SETTER(SetPieceSize);
179180
static NAN_SETTER(SetOffset);
181+
static NAN_SETTER(SetType);
180182

181183

182184
// Read Method on ILob class
@@ -205,13 +207,14 @@ class ILob : public ObjectWrap
205207
bool isValid_;
206208
State state_;
207209

208-
char *buf_;
209-
unsigned int bufSize_;
210-
unsigned int chunkSize_;
210+
char *buf_;
211+
unsigned int bufSize_;
212+
unsigned int chunkSize_;
211213
unsigned long long length_;
212214
unsigned long long offset_;
213215
unsigned long amountRead_;
214216
unsigned long long amountWritten_;
217+
unsigned int type_;
215218
};
216219

217220

0 commit comments

Comments
 (0)