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 #include <NdbApi.hpp>
26 
27 #include "adapter_global.h"
28 #include "js_wrapper_macros.h"
29 #include "TransactionImpl.h"
30 #include "QueryOperation.h"
31 #include "SessionImpl.h"
32 #include "NativeCFunctionCall.h"
33 #include "NativeMethodCall.h"
34 #include "NdbWrappers.h"
35 
36 using namespace v8;
37 
38 V8WrapperFn newSessionImpl;
39 V8WrapperFn seizeTransaction;
40 V8WrapperFn releaseTransaction;
41 V8WrapperFn freeTransactions;
42 V8WrapperFn SessionImplDestructor;
43 
44 class SessionImplEnvelopeClass : public Envelope {
45 public:
SessionImplEnvelopeClass()46   SessionImplEnvelopeClass() : Envelope("SessionImpl") {
47     addMethod("seizeTransaction", seizeTransaction);
48     addMethod("releaseTransaction", releaseTransaction);
49     addMethod("freeTransactions", freeTransactions);
50     addMethod("destroy", SessionImplDestructor);
51   }
52 };
53 
54 SessionImplEnvelopeClass SessionImplEnvelope;
55 
SessionImpl_Wrapper(SessionImpl * dbsi)56 Handle<Value> SessionImpl_Wrapper(SessionImpl *dbsi) {
57   Local<Value> jsobj = SessionImplEnvelope.wrap(dbsi);
58   SessionImplEnvelope.freeFromGC(dbsi, jsobj);
59   return jsobj;
60 }
61 
asyncNewSessionImpl(Ndb_cluster_connection * conn,AsyncNdbContext * ctx,const char * db,int maxTx)62 SessionImpl * asyncNewSessionImpl(Ndb_cluster_connection *conn,
63                                   AsyncNdbContext *ctx,
64                                   const char *db, int maxTx) {
65   return new SessionImpl(conn, ctx, db, maxTx);
66 }
67 
68 
newSessionImpl(const Arguments & args)69 void newSessionImpl(const Arguments & args) {
70   DEBUG_MARKER(UDEB_DETAIL);
71   EscapableHandleScope scope(args.GetIsolate());
72 
73   PROHIBIT_CONSTRUCTOR_CALL();
74   REQUIRE_ARGS_LENGTH(5);
75 
76   typedef NativeCFunctionCall_4_<SessionImpl *, Ndb_cluster_connection *,
77                                  AsyncNdbContext *, const char *, int> MCALL;
78   MCALL * mcallptr = new MCALL(& asyncNewSessionImpl, args);
79   mcallptr->wrapReturnValueAs(& SessionImplEnvelope);
80   mcallptr->runAsync();
81   args.GetReturnValue().SetUndefined();
82 }
83 
84 /* The seizeTransaction() wrapper is unusual because a
85    TransactionImpl holds a reference to its own JS wrapper
86 */
seizeTransaction(const Arguments & args)87 void seizeTransaction(const Arguments & args) {
88   SessionImpl * session = unwrapPointer<SessionImpl *>(args.Holder());
89   TransactionImpl * ctx = session->seizeTransaction();
90   if(ctx)
91     args.GetReturnValue().Set(ctx->getJsWrapper());
92   else
93     args.GetReturnValue().SetNull();
94 }
95 
releaseTransaction(const Arguments & args)96 void releaseTransaction(const Arguments & args) {
97   EscapableHandleScope scope(args.GetIsolate());
98   typedef NativeMethodCall_1_<bool, SessionImpl, TransactionImpl *> MCALL;
99   MCALL mcall(& SessionImpl::releaseTransaction, args);
100   mcall.run();
101   args.GetReturnValue().Set(scope.Escape(mcall.jsReturnVal()));
102 }
103 
freeTransactions(const Arguments & args)104 void freeTransactions(const Arguments & args) {
105   EscapableHandleScope scope(args.GetIsolate());
106   SessionImpl * session = unwrapPointer<SessionImpl *>(args.Holder());
107   session->freeTransactions();
108   args.GetReturnValue().SetUndefined();
109 }
110 
SessionImplDestructor(const Arguments & args)111 void SessionImplDestructor(const Arguments &args) {
112   DEBUG_MARKER(UDEB_DETAIL);
113   typedef NativeDestructorCall<SessionImpl> DCALL;
114   DCALL * dcall = new DCALL(args);
115   dcall->runAsync();
116   args.GetReturnValue().SetUndefined();
117 }
118 
SessionImpl_initOnLoad(Handle<Object> target)119 void SessionImpl_initOnLoad(Handle<Object> target) {
120   Local<String> jsKey = NEW_SYMBOL("DBSession");
121   Local<Object> jsObj = Object::New(Isolate::GetCurrent());
122 
123   target->Set(jsKey, jsObj);
124 
125   DEFINE_JS_FUNCTION(jsObj, "create", newSessionImpl);
126 }
127 
128 
129