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 "NativeMethodCall.h"
31 
32 using namespace v8;
33 
34 V8WrapperFn Ndb_cluster_connection_set_name;
35 V8WrapperFn Ndb_cluster_connection_connect;
36 V8WrapperFn Ndb_cluster_connection_wait_until_ready;
37 V8WrapperFn Ndb_cluster_connection_node_id;
38 V8WrapperFn get_latest_error_msg_wrapper;
39 V8WrapperFn Ndb_cluster_connection_delete_wrapper;
40 
41 
42 class NdbccEnvelopeClass : public Envelope {
43 public:
NdbccEnvelopeClass()44   NdbccEnvelopeClass() : Envelope("Ndb_cluster_connection") {
45     addMethod("set_name", Ndb_cluster_connection_set_name);
46     addMethod("connect", Ndb_cluster_connection_connect);
47     addMethod("wait_until_ready", Ndb_cluster_connection_wait_until_ready);
48     addMethod("node_id", Ndb_cluster_connection_node_id);
49     addMethod("get_latest_error_msg", get_latest_error_msg_wrapper);
50     addMethod("delete", Ndb_cluster_connection_delete_wrapper);
51   }
52 };
53 
54 NdbccEnvelopeClass NdbccEnvelope;
55 Envelope ErrorMessageEnvelope("Error Message from const char *");
56 
57 /*  Ndb_cluster_connection(const char * connectstring = 0);
58 */
Ndb_cluster_connection_new_wrapper(const Arguments & args)59 void Ndb_cluster_connection_new_wrapper(const Arguments &args) {
60   DEBUG_MARKER(UDEB_DETAIL);
61   EscapableHandleScope scope(args.GetIsolate());
62 
63   REQUIRE_CONSTRUCTOR_CALL();
64   REQUIRE_ARGS_LENGTH(1);
65 
66   JsValueConverter<const char *> arg0(args[0]);
67 
68   Ndb_cluster_connection * c = new Ndb_cluster_connection(arg0.toC());
69 
70   /* We do not expose set_max_adaptive_send_time() to JavaScript nor even
71      consider using the default value of 10 ms.
72   */
73   c->set_max_adaptive_send_time(1);
74 
75   Local<Value> wrapper = NdbccEnvelope.wrap(c);
76   NdbccEnvelope.freeFromGC(c, wrapper);
77 
78   args.GetReturnValue().Set(wrapper);
79 }
80 
81 
82 /*   void set_name(const char *name);
83 */
Ndb_cluster_connection_set_name(const Arguments & args)84 void Ndb_cluster_connection_set_name(const Arguments &args) {
85   DEBUG_MARKER(UDEB_DETAIL);
86 
87   REQUIRE_ARGS_LENGTH(1);
88   typedef NativeVoidMethodCall_1_<Ndb_cluster_connection, const char *> MCALL;
89   MCALL mcall(& Ndb_cluster_connection::set_name, args);
90   mcall.run();
91 
92   args.GetReturnValue().SetUndefined();
93 }
94 
95 /* int connect(int no_retries=30, int retry_delay_in_seconds=1, int verbose=0);
96    3 args SYNC / 4 args ASYNC
97 */
Ndb_cluster_connection_connect(const Arguments & args)98 void Ndb_cluster_connection_connect(const Arguments &args) {
99   DEBUG_MARKER(UDEB_DETAIL);
100   EscapableHandleScope scope(args.GetIsolate());
101 
102   args.GetReturnValue().SetUndefined();
103   REQUIRE_MIN_ARGS(3);
104   REQUIRE_MAX_ARGS(4);
105 
106   typedef NativeMethodCall_3_ <int, Ndb_cluster_connection, int, int, int> MCALL;
107 
108   if(args.Length() == 4) {
109     DEBUG_PRINT_DETAIL("async");
110     MCALL * mcallptr = new MCALL(& Ndb_cluster_connection::connect, args);
111     mcallptr->runAsync();
112   }
113   else {
114     DEBUG_PRINT_DETAIL("sync");
115     MCALL mcall(& Ndb_cluster_connection::connect, args);
116     mcall.run();
117     args.GetReturnValue().Set(mcall.jsReturnVal());
118   }
119 }
120 
121 
122 /*   int wait_until_ready(int timeout_for_first_alive,
123                           int timeout_after_first_alive,
124                           callback);
125      2 args SYNC / 3 args ASYNC
126 */
Ndb_cluster_connection_wait_until_ready(const Arguments & args)127 void Ndb_cluster_connection_wait_until_ready(const Arguments &args) {
128   DEBUG_MARKER(UDEB_DETAIL);
129   EscapableHandleScope scope(args.GetIsolate());
130 
131   args.GetReturnValue().SetUndefined();
132   REQUIRE_MIN_ARGS(2);
133   REQUIRE_MAX_ARGS(3);
134 
135   typedef NativeMethodCall_2_<int, Ndb_cluster_connection, int, int> MCALL;
136 
137   if(args.Length() == 3) {
138     MCALL * mcallptr = new MCALL(& Ndb_cluster_connection::wait_until_ready, args);
139     mcallptr->runAsync();
140   }
141   else {
142     MCALL mcall(& Ndb_cluster_connection::wait_until_ready, args);
143     mcall.run();
144     args.GetReturnValue().Set(mcall.jsReturnVal());
145   };
146 }
147 
148 
149 /*  unsigned node_id();
150     IMMEDIATE
151 */
Ndb_cluster_connection_node_id(const Arguments & args)152 void Ndb_cluster_connection_node_id(const Arguments &args) {
153   DEBUG_MARKER(UDEB_DETAIL);
154   EscapableHandleScope scope(args.GetIsolate());
155 
156   REQUIRE_ARGS_LENGTH(0);
157 
158   typedef NativeMethodCall_0_<unsigned int, Ndb_cluster_connection> MCALL;
159   MCALL mcall(& Ndb_cluster_connection::node_id, args);
160   mcall.run();
161   args.GetReturnValue().Set(mcall.jsReturnVal());
162  }
163 
164 
Ndb_cluster_connection_delete_wrapper(const Arguments & args)165 void Ndb_cluster_connection_delete_wrapper(const Arguments &args) {
166   DEBUG_MARKER(UDEB_DETAIL);
167   EscapableHandleScope scope(args.GetIsolate());
168   typedef NativeDestructorCall<Ndb_cluster_connection> MCALL;
169   MCALL * mcallptr = new MCALL(args);
170   mcallptr->runAsync();
171   args.GetReturnValue().SetUndefined();
172 }
173 
174 
get_latest_error_msg_wrapper(const Arguments & args)175 void get_latest_error_msg_wrapper(const Arguments &args) {
176   DEBUG_MARKER(UDEB_DETAIL);
177   EscapableHandleScope scope(args.GetIsolate());
178 
179   REQUIRE_ARGS_LENGTH(0);
180 
181   typedef NativeConstMethodCall_0_<const char *, Ndb_cluster_connection> MCALL;
182   MCALL mcall(& Ndb_cluster_connection::get_latest_error_msg, args);
183   mcall.wrapReturnValueAs(& ErrorMessageEnvelope);
184   mcall.run();
185 
186   args.GetReturnValue().Set(mcall.jsReturnVal());
187 }
188 
189 
Ndb_cluster_connection_initOnLoad(Handle<Object> target)190 void Ndb_cluster_connection_initOnLoad(Handle<Object> target) {
191   DEFINE_JS_FUNCTION(target, "Ndb_cluster_connection", Ndb_cluster_connection_new_wrapper);
192 }
193 
194