1 /*
2  Copyright (c) 2014, 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 #include "NativeMethodCall.h"
34 #include "NdbWrapperErrors.h"
35 
36 using namespace v8;
37 
38 V8WrapperFn getOperationError,
39             tryImmediateStartTransaction,
40             execute,
41             executeAsynch,
42             readBlobResults,
43             BatchImpl_freeImpl;
44 
45 class BatchImplEnvelopeClass : public Envelope {
46 public:
BatchImplEnvelopeClass()47   BatchImplEnvelopeClass() : Envelope("BatchImpl") {
48     addMethod("tryImmediateStartTransaction", tryImmediateStartTransaction);
49     addMethod("getOperationError", getOperationError);
50     addMethod("execute", execute);
51     addMethod("executeAsynch", executeAsynch);
52     addMethod("readBlobResults", readBlobResults);
53     addMethod("free", BatchImpl_freeImpl);
54   }
55 };
56 
57 BatchImplEnvelopeClass BatchImplEnvelope;
58 
59 // CALLER in DBOperationHelper has a HandleScope
BatchImpl_Wrapper(BatchImpl * set)60 Local<Value> BatchImpl_Wrapper(BatchImpl *set) {
61   Local<Value> jsobj = BatchImplEnvelope.wrap(set);
62   BatchImplEnvelope.freeFromGC(set, jsobj);
63   return jsobj;
64 }
65 
66 // This version is *not* freed from GC
getWrappedObject(BatchImpl * set)67 Local<Object> getWrappedObject(BatchImpl *set) {
68   return BatchImplEnvelope.wrap(set)->ToObject();
69 }
70 
BatchImpl_Recycle(Handle<Object> oldWrapper,BatchImpl * newSet)71 Local<Value> BatchImpl_Recycle(Handle<Object> oldWrapper,
72                                BatchImpl * newSet) {
73   DEBUG_PRINT("BatchImpl *Recycle*");
74   BatchImpl * oldSet = unwrapPointer<BatchImpl *>(oldWrapper);
75   assert(oldSet == 0);
76   assert(newSet != 0);
77   wrapPointerInObject(newSet, BatchImplEnvelope, oldWrapper);
78   return oldWrapper;
79 }
80 
81 
getOperationError(const Arguments & args)82 void getOperationError(const Arguments & args) {
83   DEBUG_MARKER(UDEB_DETAIL);
84   EscapableHandleScope scope(args.GetIsolate());
85 
86   BatchImpl * set = unwrapPointer<BatchImpl *>(args.Holder());
87   int n = args[0]->Int32Value();
88 
89   const NdbError * err = set->getError(n);
90 
91   Local<Value> opErrHandle;
92   if(err == 0) opErrHandle = True(args.GetIsolate());
93   else if(err->code == 0) opErrHandle = Null(args.GetIsolate());
94   else opErrHandle = NdbError_Wrapper(*err);
95 
96   args.GetReturnValue().Set(scope.Escape(opErrHandle));
97 }
98 
tryImmediateStartTransaction(const Arguments & args)99 void tryImmediateStartTransaction(const Arguments &args) {
100   BatchImpl * ctx = unwrapPointer<BatchImpl *>(args.Holder());
101   args.GetReturnValue().Set((bool) ctx->tryImmediateStartTransaction());
102 }
103 
104 
105 
106 /* ASYNC.
107 */
108 /* Execute NdbTransaction.
109    BatchImpl will close the transaction if exectype is not NoCommit;
110    in this case, an extra call is made in the js main thread to register the
111    transaction as closed.
112 */
113 class TxExecuteAndCloseCall :
114   public NativeMethodCall_3_<int, BatchImpl, int, int, int> {
115 public:
116   /* Constructor */
TxExecuteAndCloseCall(const Arguments & args)117   TxExecuteAndCloseCall(const Arguments &args) :
118     NativeMethodCall_3_<int, BatchImpl, int, int, int>(
119       & BatchImpl::execute, args)
120   {
121     errorHandler = getNdbErrorIfLessThanZero;
122   }
123   void doAsyncCallback(Local<Object>);
124 };
125 
doAsyncCallback(Local<Object> context)126 void TxExecuteAndCloseCall::doAsyncCallback(Local<Object> context) {
127   if(arg0 != NdbTransaction::NoCommit) {
128     native_obj->registerClosedTransaction();
129   }
130   NativeMethodCall_3_<int, BatchImpl, int, int, int>::doAsyncCallback(context);
131 }
132 
execute(const Arguments & args)133 void execute(const Arguments &args) {
134   EscapableHandleScope scope(args.GetIsolate());
135   REQUIRE_ARGS_LENGTH(4);
136   TxExecuteAndCloseCall * ncallptr = new TxExecuteAndCloseCall(args);
137   ncallptr->runAsync();
138   args.GetReturnValue().SetUndefined();
139 }
140 
141 
142 /* IMMEDIATE.
143 */
executeAsynch(const Arguments & args)144 void executeAsynch(const Arguments &args) {
145   EscapableHandleScope scope(args.GetIsolate());
146   typedef NativeMethodCall_4_<int, BatchImpl,
147                               int, int, int, Handle<Function> > MCALL;
148   MCALL mcall(& BatchImpl::executeAsynch, args);
149   mcall.run();
150   args.GetReturnValue().Set(mcall.jsReturnVal());
151 }
152 
153 
readBlobResults(const Arguments & args)154 void readBlobResults(const Arguments &args) {
155   BatchImpl * set = unwrapPointer<BatchImpl *>(args.Holder());
156   int n = args[0]->Int32Value();
157   set->getKeyOperation(n)->readBlobResults(args);
158 //  args.GetReturnValue().Set(set->getKeyOperation(n)->readBlobResults());
159 }
160 
161 
BatchImpl_freeImpl(const Arguments & args)162 void BatchImpl_freeImpl(const Arguments &args) {
163   BatchImpl * set = unwrapPointer<BatchImpl *>(args.Holder());
164   delete set;
165   set = 0;
166   wrapPointerInObject(set, BatchImplEnvelope, args.Holder());
167   args.GetReturnValue().SetUndefined();
168 }
169 
170 
171