1 /*
2  Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License, version 2.0,
6  as published by the Free Software Foundation.
7 
8  This program is also distributed with certain software (including
9  but not limited to OpenSSL) that is licensed under separate terms,
10  as designated in a particular file or component or in included license
11  documentation.  The authors of MySQL hereby grant you an additional
12  permission to link the program and your derivative works with the
13  separately licensed software that they have included with MySQL.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License, version 2.0, for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23  */
24 
25 
26 #include <NdbApi.hpp>
27 
28 #include "adapter_global.h"
29 #include "js_wrapper_macros.h"
30 #include "Record.h"
31 #include "NdbWrappers.h"
32 #include "BatchImpl.h"
33 
34 
BatchImpl(TransactionImpl * ctx,int _sz)35 BatchImpl::BatchImpl(TransactionImpl * ctx, int _sz) :
36   keyOperations(new KeyOperation[_sz]),
37   ops(new const NdbOperation *[_sz]),
38   errors(new NdbError[_sz]),
39   size(_sz),
40   doesReadBlobs(false),
41   transactionImpl(ctx),
42   transactionNdbError(0)
43 {};
44 
45 
~BatchImpl()46 BatchImpl::~BatchImpl() {
47   DEBUG_PRINT("BatchImpl destructor [size %d]", size);
48   delete[] keyOperations;
49   delete[] ops;
50   delete[] errors;
51   delete transactionNdbError;
52 }
53 
setOperationNdbError(int i,const NdbError & err)54 void BatchImpl::setOperationNdbError(int i, const NdbError & err) {
55   if(err.code > 0) {
56     errors[i].status = err.status;
57     errors[i].classification = err.classification;
58     errors[i].code = err.code;
59     errors[i].mysql_code = err.mysql_code;
60     errors[i].message = err.message;
61   }
62 }
63 
prepare(NdbTransaction * ndbtx)64 void BatchImpl::prepare(NdbTransaction *ndbtx) {
65   for(int i = 0 ; i < size ; i++) {
66     ops[i] = 0;
67     if(keyOperations[i].opcode > 0) {
68       const NdbOperation *op = keyOperations[i].prepare(ndbtx);
69       if(op) {
70         ops[i] = op;
71       } else {
72         setOperationNdbError(i, ndbtx->getNdbError());
73       }
74       DEBUG_PRINT("prepare %s [%s]", keyOperations[i].getOperationName(),
75                   op ? "ok" : errors[i].message);
76       if(keyOperations[i].isBlobReadOperation()) doesReadBlobs = true;
77     }
78   }
79 }
80 
tryImmediateStartTransaction()81 bool BatchImpl::tryImmediateStartTransaction() {
82   if(doesReadBlobs) {
83     return false;
84   }
85   return transactionImpl->tryImmediateStartTransaction(& keyOperations[0]);
86 }
87 
saveNdbErrors()88 void BatchImpl::saveNdbErrors() {
89   transactionNdbError = new NdbError(transactionImpl->getNdbError());
90   for(int i = 0 ; i < size ; i++)
91     if(ops[i])
92       setOperationNdbError(i, ops[i]->getNdbError());
93 }
94 
getError(int n)95 const NdbError * BatchImpl::getError(int n) {
96   if(n < size) {  // If operation is not open, use saved error
97     return ops[n] ? & ops[n]->getNdbError() : & errors[n];
98   }
99   return 0;  // This becomes JavaScript "true"
100 }
101 
getNdbError()102 const NdbError & BatchImpl::getNdbError() {
103   return transactionNdbError ?
104     * transactionNdbError : transactionImpl->getNdbError();
105 }
106 
transactionIsClosed()107 void BatchImpl::transactionIsClosed() {
108   for(int i = 0 ; i < size ; i++)
109     ops[i] = 0;
110 }
111