-
Notifications
You must be signed in to change notification settings - Fork 1
/
LongVar.c
236 lines (209 loc) · 9.3 KB
/
LongVar.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
//-----------------------------------------------------------------------------
// LongVar.c
// Defines the routines specific to the long type.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// long type
//-----------------------------------------------------------------------------
typedef struct {
Variable_HEAD
char *data;
} udt_LongVar;
//-----------------------------------------------------------------------------
// declaration of long variable functions.
//-----------------------------------------------------------------------------
static int LongVar_SetValue(udt_LongVar*, unsigned, PyObject*);
static PyObject *LongVar_GetValue(udt_LongVar*, unsigned);
static ub4 LongVar_GetBufferSize(udt_LongVar*);
//-----------------------------------------------------------------------------
// Python type declarations
//-----------------------------------------------------------------------------
static PyTypeObject g_LongStringVarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cx_Oracle.LONG_STRING", // tp_name
sizeof(udt_LongVar), // tp_basicsize
0, // tp_itemsize
0, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
0 // tp_doc
};
static PyTypeObject g_LongNCharVarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cx_Oracle.LONG_NCHAR", // tp_name
sizeof(udt_LongVar), // tp_basicsize
0, // tp_itemsize
0, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
0 // tp_doc
};
static PyTypeObject g_LongBinaryVarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cx_Oracle.LONG_BINARY", // tp_name
sizeof(udt_LongVar), // tp_basicsize
0, // tp_itemsize
0, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
0 // tp_doc
};
//-----------------------------------------------------------------------------
// variable type declarations
//-----------------------------------------------------------------------------
static udt_VariableType vt_LongString = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PostDefineProc) NULL,
(PostBindProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) LongVar_SetValue,
(GetValueProc) LongVar_GetValue,
(GetBufferSizeProc) LongVar_GetBufferSize,
&g_LongStringVarType, // Python type
SQLT_LVC, // Oracle type
SQLCS_IMPLICIT, // charset form
128 * 1024, // element length (default)
1, // is character data
1, // is variable length
1, // can be copied
0 // can be in array
};
static udt_VariableType vt_LongNationalCharString = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PostDefineProc) NULL,
(PostBindProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) LongVar_SetValue,
(GetValueProc) LongVar_GetValue,
(GetBufferSizeProc) LongVar_GetBufferSize,
&g_LongNCharVarType, // Python type
SQLT_LVC, // Oracle type
SQLCS_NCHAR, // charset form
128 * 1024, // element length (default)
1, // is character data
1, // is variable length
1, // can be copied
0 // can be in array
};
static udt_VariableType vt_LongBinary = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PostDefineProc) NULL,
(PostBindProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) LongVar_SetValue,
(GetValueProc) LongVar_GetValue,
(GetBufferSizeProc) LongVar_GetBufferSize,
&g_LongBinaryVarType, // Python type
SQLT_LVB, // Oracle type
SQLCS_IMPLICIT, // charset form
128 * 1024, // element length (default)
0, // is character data
1, // is variable length
1, // can be copied
0 // can be in array
};
//-----------------------------------------------------------------------------
// LongVar_SetValue()
// Set the value of the variable.
//-----------------------------------------------------------------------------
static int LongVar_SetValue(
udt_LongVar *var, // variable to set value for
unsigned pos, // array position to set
PyObject *value) // value to set
{
udt_Buffer buffer;
char *ptr;
// get the buffer data and size for binding
if (cxBuffer_FromObject(&buffer, value, var->environment->encoding) < 0)
return -1;
// verify there is enough space to store the value
if (buffer.numCharacters > var->size) {
if (Variable_Resize((udt_Variable*) var, buffer.numCharacters) < 0) {
cxBuffer_Clear(&buffer);
return -1;
}
}
// copy the string to the Oracle buffer
ptr = var->data + var->bufferSize * pos;
*((ub4 *) ptr) = (ub4) buffer.size;
if (buffer.size)
memcpy(ptr + sizeof(ub4), buffer.ptr, buffer.size);
cxBuffer_Clear(&buffer);
return 0;
}
//-----------------------------------------------------------------------------
// LongVar_GetValue()
// Returns the value stored at the given array position.
//-----------------------------------------------------------------------------
static PyObject *LongVar_GetValue(
udt_LongVar *var, // variable to determine value for
unsigned pos) // array position
{
char *ptr;
ub4 size;
ptr = var->data + var->bufferSize * pos;
size = *((ub4 *) ptr);
ptr += sizeof(ub4);
if (var->type == &vt_LongBinary)
return PyBytes_FromStringAndSize(ptr, size);
return cxString_FromEncodedString(ptr, size, var->environment->encoding);
}
//-----------------------------------------------------------------------------
// LongVar_GetBufferSize()
// Returns the size of the buffer to use for data of the given size.
//-----------------------------------------------------------------------------
static ub4 LongVar_GetBufferSize(
udt_LongVar *self) // variable to get buffer size
{
if (!self->type->isCharacterData)
return self->size + sizeof(ub4);
return sizeof(ub4) + self->size * self->environment->maxBytesPerCharacter;
}