forked from oracle/node-oracledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnjsSodaDocument.c
243 lines (206 loc) · 8.98 KB
/
njsSodaDocument.c
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
// Copyright (c) 2018, 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.
//
// NAME
// njsSodaDocument.c
//
// DESCRIPTION
// SodaDocument class implementation.
//
//-----------------------------------------------------------------------------
#include "njsModule.h"
// class methods
NJS_NAPI_METHOD_DECL_SYNC(njsSodaDocument_getContentAsBuffer);
NJS_NAPI_METHOD_DECL_SYNC(njsSodaDocument_getContentAsString);
NJS_NAPI_METHOD_DECL_SYNC(njsSodaDocument_getCreatedOn);
NJS_NAPI_METHOD_DECL_SYNC(njsSodaDocument_getKey);
NJS_NAPI_METHOD_DECL_SYNC(njsSodaDocument_getLastModified);
NJS_NAPI_METHOD_DECL_SYNC(njsSodaDocument_getMediaType);
NJS_NAPI_METHOD_DECL_SYNC(njsSodaDocument_getVersion);
// finalize
static NJS_NAPI_FINALIZE(njsSodaDocument_finalize);
// properties defined by the class
static const napi_property_descriptor njsClassProperties[] = {
{ "getContentAsBuffer", NULL, njsSodaDocument_getContentAsBuffer,
NULL, NULL, NULL, napi_default, NULL },
{ "getContentAsString", NULL, njsSodaDocument_getContentAsString,
NULL, NULL, NULL, napi_default, NULL },
{ "getCreatedOn", NULL, njsSodaDocument_getCreatedOn, NULL, NULL, NULL,
napi_default, NULL },
{ "getKey", NULL, njsSodaDocument_getKey, NULL, NULL, NULL, napi_default,
NULL },
{ "getLastModified", NULL, njsSodaDocument_getLastModified, NULL, NULL,
NULL, napi_default, NULL },
{ "getMediaType", NULL, njsSodaDocument_getMediaType, NULL, NULL, NULL,
napi_default, NULL },
{ "getVersion", NULL, njsSodaDocument_getVersion, NULL, NULL, NULL,
napi_default, NULL },
{ NULL, NULL, NULL, NULL, NULL, NULL, napi_default, NULL }
};
// class definition
const njsClassDef njsClassDefSodaDocument = {
"SodaDocumentImpl", sizeof(njsSodaDocument), njsSodaDocument_finalize,
njsClassProperties, false
};
// other methods used internally
static bool njsSodaDocument_genericGetter(napi_env env,
njsModuleGlobals *globals, void *instance,
int (*dpiGetterFn)(dpiSodaDoc*, const char**, uint32_t *),
napi_value *returnValue);
//-----------------------------------------------------------------------------
// njsSodaDocument_createFromHandle()
// Creates a new SODA document object given the ODPI-C handle.
//-----------------------------------------------------------------------------
bool njsSodaDocument_createFromHandle(napi_env env, dpiSodaDoc *handle,
njsModuleGlobals *globals, napi_value *docObj)
{
njsSodaDocument *doc;
// create new instance
if (!njsUtils_genericNew(env, &njsClassDefSodaDocument,
globals->jsSodaDocumentConstructor, docObj, (void**) &doc))
return false;
// perform initializations
doc->handle = handle;
return true;
}
//-----------------------------------------------------------------------------
// njsSodaDocument_finalize()
// Invoked when the njsSodaDocument object is garbage collected.
//-----------------------------------------------------------------------------
static void njsSodaDocument_finalize(napi_env env, void *finalizeData,
void *finalizeHint)
{
njsSodaDocument *doc = (njsSodaDocument*) finalizeData;
if (doc->handle) {
dpiSodaDoc_release(doc->handle);
doc->handle = NULL;
}
free(doc);
}
//-----------------------------------------------------------------------------
// njsSodaDocument_genericGetter()
// Generic function which performs the work of getting an attribute from the
// SODA document.
//-----------------------------------------------------------------------------
static bool njsSodaDocument_genericGetter(napi_env env,
njsModuleGlobals *globals, void *instance,
int (*dpiGetterFn)(dpiSodaDoc*, const char**, uint32_t *),
napi_value *returnValue)
{
njsSodaDocument *doc = (njsSodaDocument*) instance;
uint32_t valueLength;
const char *value;
if ((*dpiGetterFn)(doc->handle, &value, &valueLength) < 0)
return njsUtils_throwErrorDPI(env, globals);
if (valueLength == 0) {
NJS_CHECK_NAPI(env, napi_get_null(env, returnValue))
} else {
NJS_CHECK_NAPI(env, napi_create_string_utf8(env, value, valueLength,
returnValue))
}
return true;
}
//-----------------------------------------------------------------------------
// njsSodaDocument_getContentAsBuffer()
// Returns the contents of the SODA document as a buffer.
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_SYNC(njsSodaDocument_getContentAsBuffer, 0, NULL)
{
njsSodaDocument *doc = (njsSodaDocument*) callingInstance;
const char *value, *encoding;
uint32_t valueLength;
if (dpiSodaDoc_getContent(doc->handle, &value, &valueLength,
&encoding) < 0)
return njsUtils_throwErrorDPI(env, globals);
NJS_CHECK_NAPI(env, napi_create_buffer_copy(env, valueLength, value, NULL,
returnValue))
return true;
}
//-----------------------------------------------------------------------------
// njsSodaDocument_getContentAsString()
// Returns the contents of the SODA document as a string.
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_SYNC(njsSodaDocument_getContentAsString, 0, NULL)
{
njsSodaDocument *doc = (njsSodaDocument*) callingInstance;
const char *value, *encoding;
uint32_t valueLength;
if (dpiSodaDoc_getContent(doc->handle, &value, &valueLength,
&encoding) < 0)
return njsUtils_throwErrorDPI(env, globals);
if (valueLength == 0) {
NJS_CHECK_NAPI(env, napi_get_null(env, returnValue))
} else if (!encoding || strcmp(encoding, "UTF-8") == 0) {
NJS_CHECK_NAPI(env, napi_create_string_utf8(env, value, valueLength,
returnValue))
} else {
NJS_CHECK_NAPI(env, napi_create_string_utf16(env, (char16_t*) value,
(size_t) (valueLength / 2), returnValue))
}
return true;
}
//-----------------------------------------------------------------------------
// njsSodaDocument_getCreatedOn()
// Get accessor of "createdOn" property.
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_SYNC(njsSodaDocument_getCreatedOn, 0, NULL)
{
return njsSodaDocument_genericGetter(env, globals, callingInstance,
dpiSodaDoc_getCreatedOn, returnValue);
}
//-----------------------------------------------------------------------------
// njsSodaDocument_getKey()
// Get accessor of "key" property.
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_SYNC(njsSodaDocument_getKey, 0, NULL)
{
return njsSodaDocument_genericGetter(env, globals, callingInstance,
dpiSodaDoc_getKey, returnValue);
}
//-----------------------------------------------------------------------------
// njsSodaDocument_getLastModified()
// Get accessor of "lastModified" property.
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_SYNC(njsSodaDocument_getLastModified, 0, NULL)
{
return njsSodaDocument_genericGetter(env, globals, callingInstance,
dpiSodaDoc_getLastModified, returnValue);
}
//-----------------------------------------------------------------------------
// njsSodaDocument_getMediaType()
// Get accessor of "mediaType" property.
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_SYNC(njsSodaDocument_getMediaType, 0, NULL)
{
return njsSodaDocument_genericGetter(env, globals, callingInstance,
dpiSodaDoc_getMediaType, returnValue);
}
//-----------------------------------------------------------------------------
// njsSodaDocument_getVersion()
// Get accessor of "version" property.
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_SYNC(njsSodaDocument_getVersion, 0, NULL)
{
return njsSodaDocument_genericGetter(env, globals, callingInstance,
dpiSodaDoc_getVersion, returnValue);
}