forked from mariadb-corporation/mariadb-connector-odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ma_bulk.c
387 lines (338 loc) · 12.5 KB
/
ma_bulk.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
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
387
/************************************************************************************
Copyright (C) 2017,2018 MariaDB Corporation AB
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
/* Code allowing to deploy MariaDB bulk operation functionality.
* i.e. adapting ODBC param arrays to MariaDB arrays */
#include <ma_odbc.h>
#define MAODBC_DATTIME_AS_PTR_ARR 1
static BOOL CanUseStructArrForDatetime(MADB_Stmt *Stmt)
{
#ifdef MAODBC_DATTIME_AS_PTR_ARR
return FALSE;
#endif
return TRUE;
}
char MADB_MapIndicatorValue(SQLLEN OdbcInd)
{
switch (OdbcInd)
{
case SQL_NTS: return STMT_INDICATOR_NTS;
case SQL_COLUMN_IGNORE: return STMT_INDICATOR_IGNORE;
case SQL_NULL_DATA: return STMT_INDICATOR_NULL;
case SQL_PARAM_IGNORE: return STMT_INDICATOR_IGNORE_ROW;
/*STMT_INDICATOR_DEFAULT*/
}
return '\0';
}
BOOL MADB_AppBufferCanBeUsed(SQLSMALLINT CType, SQLSMALLINT SqlType)
{
switch (CType)
{
case CHAR_BINARY_TYPES:
/*if (SqlType != SQL_BIT)
{
break;
}*/
case WCHAR_TYPES:
case SQL_C_NUMERIC:
case DATETIME_TYPES:
return FALSE;
}
return TRUE;
}
void MADB_CleanBulkOperData(MADB_Stmt *Stmt, unsigned int ParamOffset)
{
if (MADB_DOING_BULK_OPER(Stmt))
{
MADB_DescRecord *CRec;
void *DataPtr= NULL;
MYSQL_BIND *MaBind= NULL;
int i;
for (i= ParamOffset; i < MADB_STMT_PARAM_COUNT(Stmt); ++i)
{
if ((CRec= MADB_DescGetInternalRecord(Stmt->Apd, i, MADB_DESC_READ)) != NULL)
{
MaBind= &Stmt->params[i - ParamOffset];
DataPtr= GetBindOffset(Stmt->Apd, CRec, CRec->DataPtr, 0, CRec->OctetLength);
if (MaBind->buffer != DataPtr)
{
switch (CRec->ConciseType)
{
case DATETIME_TYPES:
if (CanUseStructArrForDatetime(Stmt) == FALSE)
{
MADB_FREE(MaBind->buffer);
break;
}
/* Otherwise falling through and do the same as for others */
case SQL_C_WCHAR:
case SQL_C_NUMERIC:
{
unsigned int i;
for (i= 0; i < Stmt->Bulk.ArraySize; ++i)
{
MADB_FREE(((char**)MaBind->buffer)[i]);
}
}
/* falling through */
default:
MADB_FREE(MaBind->buffer);
}
}
MADB_FREE(MaBind->length);
MADB_FREE(MaBind->u.indicator);
}
}
Stmt->Bulk.ArraySize= 0;
Stmt->Bulk.HasRowsToSkip= 0;
}
}
SQLRETURN MADB_InitIndicatorArray(MADB_Stmt *Stmt, MYSQL_BIND *MaBind, char InitValue)
{
MaBind->u.indicator= MADB_ALLOC(Stmt->Bulk.ArraySize);
if (MaBind->u.indicator == NULL)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}
memset(MaBind->u.indicator, InitValue, Stmt->Bulk.ArraySize);
return SQL_SUCCESS;
}
SQLRETURN MADB_SetBulkOperLengthArr(MADB_Stmt *Stmt, MADB_DescRecord *CRec, SQLLEN *OctetLengthPtr, SQLLEN *IndicatorPtr,
void *DataPtr, MYSQL_BIND *MaBind, BOOL VariableLengthMadbType)
{
/* Leaving it so far here commented, but it comlicates things w/out much gains */
/*if (sizeof(SQLLEN) == sizeof(long) && MADB_AppBufferCanBeUsed())
{
if (OctetLengthPtr)
{
MaBind->length= OctetLengthPtr;
}
}
else
{*/
unsigned int row;
if (VariableLengthMadbType)
{
MaBind->length= MADB_REALLOC(MaBind->length, Stmt->Bulk.ArraySize*sizeof(long));
if (MaBind->length == NULL)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}
}
for (row= 0; row < Stmt->Apd->Header.ArraySize; ++row, DataPtr= (char*)DataPtr + CRec->OctetLength)
{
if (Stmt->Apd->Header.ArrayStatusPtr != NULL && Stmt->Apd->Header.ArrayStatusPtr[row] == SQL_PARAM_IGNORE)
{
Stmt->Bulk.HasRowsToSkip= 1;
continue;
}
if ((OctetLengthPtr != NULL && OctetLengthPtr[row] == SQL_NULL_DATA)
|| (IndicatorPtr != NULL && IndicatorPtr[row] != SQL_NULL_DATA))
{
RETURN_ERROR_OR_CONTINUE(MADB_SetIndicatorValue(Stmt, MaBind, row, SQL_NULL_DATA));
continue;
}
if ((OctetLengthPtr != NULL && OctetLengthPtr[row] == SQL_COLUMN_IGNORE)
|| (IndicatorPtr != NULL && IndicatorPtr[row] != SQL_COLUMN_IGNORE))
{
RETURN_ERROR_OR_CONTINUE(MADB_SetIndicatorValue(Stmt, MaBind, row, SQL_COLUMN_IGNORE));
continue;
}
if (VariableLengthMadbType)
{
MaBind->length[row]= (unsigned long)MADB_CalculateLength(Stmt, OctetLengthPtr != NULL ? &OctetLengthPtr[row] : NULL, CRec, DataPtr);
}
}
return SQL_SUCCESS;
}
/* {{{ MADB_InitBulkOperBuffers */
/* Allocating data and length arrays, if needed, and initing them in certain cases.
DataPtr should be ensured to be not NULL */
SQLRETURN MADB_InitBulkOperBuffers(MADB_Stmt *Stmt, MADB_DescRecord *CRec, void *DataPtr, SQLLEN *OctetLengthPtr,
SQLLEN *IndicatorPtr, SQLSMALLINT SqlType, MYSQL_BIND *MaBind)
{
BOOL VariableLengthMadbType= TRUE;
MaBind->buffer_length= 0;
MaBind->buffer_type= MADB_GetMaDBTypeAndLength(CRec->ConciseType, &MaBind->is_unsigned, &MaBind->buffer_length);
/* For fixed length types MADB_GetMaDBTypeAndLength has set buffer_length */
if (MaBind->buffer_length != 0)
{
VariableLengthMadbType= FALSE;
}
switch (CRec->ConciseType)
{
case CHAR_BINARY_TYPES:
if (SqlType == SQL_BIT)
{
CRec->InternalBuffer= MADB_CALLOC(Stmt->Bulk.ArraySize);
MaBind->buffer_length= 1;
break;
}
case DATETIME_TYPES:
if (CanUseStructArrForDatetime(Stmt) == TRUE)
{
CRec->InternalBuffer= MADB_ALLOC(Stmt->Bulk.ArraySize*sizeof(MYSQL_TIME));
MaBind->buffer_length= sizeof(MYSQL_TIME);
break;
}
/* Otherwise falling thru and allocating array of pointers */
case WCHAR_TYPES:
case SQL_C_NUMERIC:
CRec->InternalBuffer= MADB_CALLOC(Stmt->Bulk.ArraySize*sizeof(char*));
MaBind->buffer_length= sizeof(char*);
break;
default:
MaBind->buffer= DataPtr;
if (MaBind->buffer_length == 0)
{
MaBind->buffer_length= sizeof(char*);
}
}
if (MaBind->buffer != DataPtr)
{
MaBind->buffer= CRec->InternalBuffer;
if (MaBind->buffer == NULL)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}
CRec->InternalBuffer= NULL; /* Need to reset this pointer, so the memory won't be freed (accidentally) */
}
return MADB_SetBulkOperLengthArr(Stmt, CRec, OctetLengthPtr, IndicatorPtr, DataPtr, MaBind, VariableLengthMadbType);
}
/* }}} */
/* {{{ MADB_SetIndicatorValue */
SQLRETURN MADB_SetIndicatorValue(MADB_Stmt *Stmt, MYSQL_BIND *MaBind, unsigned int row, SQLLEN OdbcIndicator)
{
if (MaBind->u.indicator == NULL)
{
RETURN_ERROR_OR_CONTINUE(MADB_InitIndicatorArray(Stmt, MaBind, STMT_INDICATOR_NONE));
}
MaBind->u.indicator[row]= MADB_MapIndicatorValue(OdbcIndicator);
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_ExecuteBulk */
/* Assuming that bulk insert can't go with DAE(and that unlikely ever changes). And that it has been checked before this call,
and we can't have DAE here */
SQLRETURN MADB_ExecuteBulk(MADB_Stmt *Stmt, unsigned int ParamOffset)
{
unsigned int i, IndIdx= -1;
unsigned long Dummy;
for (i= ParamOffset; i < ParamOffset + MADB_STMT_PARAM_COUNT(Stmt); ++i)
{
MADB_DescRecord *CRec, *SqlRec;
SQLLEN *IndicatorPtr= NULL;
SQLLEN *OctetLengthPtr= NULL;
void *DataPtr= NULL;
MYSQL_BIND *MaBind= &Stmt->params[i - ParamOffset];
SQLULEN row, Start= Stmt->ArrayOffset;
if ((CRec= MADB_DescGetInternalRecord(Stmt->Apd, i, MADB_DESC_READ)) &&
(SqlRec= MADB_DescGetInternalRecord(Stmt->Ipd, i, MADB_DESC_READ)))
{
/* check if parameter was bound */
if (!CRec->inUse)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_07002, NULL, 0);
}
if (MADB_ConversionSupported(CRec, SqlRec) == FALSE)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_07006, NULL, 0);
}
MaBind->length= NULL;
IndicatorPtr= (SQLLEN *)GetBindOffset(Stmt->Apd, CRec, CRec->IndicatorPtr, 0, sizeof(SQLLEN));
OctetLengthPtr= (SQLLEN *)GetBindOffset(Stmt->Apd, CRec, CRec->OctetLengthPtr, 0, sizeof(SQLLEN));
DataPtr= GetBindOffset(Stmt->Apd, CRec, CRec->DataPtr, 0, CRec->OctetLength);
/* If these are the same pointers, setting indicator to NULL to simplify things a bit */
if (IndicatorPtr == OctetLengthPtr)
{
IndicatorPtr= NULL;
}
/* Well, specs kinda say, that both values and lenghts arrays should be set(in instruction to param array operations)
But there is no error/sqlstate for the case if any of those pointers is not set. Thus we assume that is possible */
if (DataPtr == NULL)
{
/* Special case - DataPtr is not set, we treat it as all values are NULL. Setting indicators and moving on next param */
RETURN_ERROR_OR_CONTINUE(MADB_InitIndicatorArray(Stmt, MaBind, MADB_MapIndicatorValue(SQL_NULL_DATA)));
continue;
}
/* Sets Stmt->Bulk.HasRowsToSkip if needed, since it traverses and checks status array anyway */
RETURN_ERROR_OR_CONTINUE(MADB_InitBulkOperBuffers(Stmt, CRec, DataPtr, OctetLengthPtr, IndicatorPtr, SqlRec->ConciseType, MaBind));
if (MaBind->u.indicator != NULL && IndIdx == (unsigned int)-1)
{
IndIdx= i - ParamOffset;
}
/* Doing it on last parameter - just to do do this once, and to use already allocated indicator array.
Little stupid optimization. But it's actually even a bit simpler this way */
if (i == ParamOffset + MADB_STMT_PARAM_COUNT(Stmt) - 1 && Stmt->Bulk.HasRowsToSkip)
{
if (IndIdx == (unsigned int)-1)
{
IndIdx= 0;
}
for (row= Start; row < Start + Stmt->Apd->Header.ArraySize; ++row)
{
if (Stmt->Apd->Header.ArrayStatusPtr[row] == SQL_PARAM_IGNORE)
{
MADB_SetIndicatorValue(Stmt, &Stmt->params[IndIdx], (unsigned int)row, SQL_PARAM_IGNORE);
}
}
}
if (MADB_AppBufferCanBeUsed(CRec->ConciseType, SqlRec->ConciseType))
{
/* Everything has been done for such column already */
continue;
}
/* We either have skipped rows or need to convert parameter values/convert array */
for (row= Start; row < Start + Stmt->Apd->Header.ArraySize; ++row, DataPtr= (char*)DataPtr + CRec->OctetLength)
{
void *Buffer= (char*)MaBind->buffer + row*MaBind->buffer_length;
void **BufferPtr= (void**)Buffer; /* For the case when Buffer points to the pointer already */
if (Stmt->Apd->Header.ArrayStatusPtr != NULL && Stmt->Apd->Header.ArrayStatusPtr[row] == SQL_PARAM_IGNORE)
{
continue;
}
if (MaBind->u.indicator && MaBind->u.indicator[row] > STMT_INDICATOR_NONE)
{
continue;
}
switch (CRec->ConciseType)
{
case SQL_C_CHAR:
if (SqlRec->ConciseType != SQL_BIT)
{
break;
}
case DATETIME_TYPES:
if (CanUseStructArrForDatetime(Stmt))
{
BufferPtr= &Buffer;
}
}
/* Need &Dummy here as a length ptr, since NULL is not good here.
It would make MADB_ConvertC2Sql to use MaBind->buffer_length by default */
if (!SQL_SUCCEEDED(MADB_ConvertC2Sql(Stmt, CRec, DataPtr, MaBind->length != NULL ? MaBind->length[row] : 0,
SqlRec, MaBind, BufferPtr, MaBind->length != NULL ? MaBind->length + row : &Dummy)))
{
/* Perhaps it's better to move to Clean function */
CRec->InternalBuffer= NULL;
return Stmt->Error.ReturnValue;
}
CRec->InternalBuffer= NULL;
}
}
}
return MADB_DoExecute(Stmt, FALSE);
}
/* }}} */