1 /*
2  Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights
3  reserved.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation.  The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24  02110-1301  USA
25 */
26 
27 
28 #include <NdbApi.hpp>
29 
30 #include "adapter_global.h"
31 #include "js_wrapper_macros.h"
32 #include "Record.h"
33 #include "NativeMethodCall.h"
34 #include "NdbWrapperErrors.h"
35 #include "NdbJsConverters.h"
36 
37 using namespace v8;
38 
39 typedef Handle<Value> _Wrapper_(const Arguments &);
40 
41 _Wrapper_ begin;
42 _Wrapper_ end;
43 _Wrapper_ istrue;
44 _Wrapper_ isfalse;
45 _Wrapper_ cmp;
46 _Wrapper_ isnull;
47 _Wrapper_ isnotnull;
48 _Wrapper_ getInterpretedCode;
49 _Wrapper_ getNdbOperation;
50 
51 #define WRAPPER_FUNCTION(A) DEFINE_JS_FUNCTION(Envelope::stencil, #A, A)
52 
53 class NdbScanFilterEnvelopeClass : public Envelope {
54 public:
NdbScanFilterEnvelopeClass()55   NdbScanFilterEnvelopeClass() : Envelope("NdbScanFilter") {
56     WRAPPER_FUNCTION( begin);
57     WRAPPER_FUNCTION( end);
58     WRAPPER_FUNCTION( istrue);
59     WRAPPER_FUNCTION( isfalse);
60     WRAPPER_FUNCTION( cmp);
61     WRAPPER_FUNCTION( isnull);
62     WRAPPER_FUNCTION( isnotnull);
63     WRAPPER_FUNCTION( getInterpretedCode);
64     WRAPPER_FUNCTION( getNdbOperation);
65     DEFINE_JS_FUNCTION(Envelope::stencil, "getNdbError", getNdbError<NdbScanFilter>);
66   }
67 };
68 
69 NdbScanFilterEnvelopeClass NdbScanFilterEnvelope;
70 
newNdbScanFilter(const Arguments & args)71 Handle<Value> newNdbScanFilter(const Arguments & args) {
72   DEBUG_MARKER(UDEB_DETAIL);
73   HandleScope scope;
74 
75   PROHIBIT_CONSTRUCTOR_CALL();
76   REQUIRE_ARGS_LENGTH(1);
77 
78   JsValueConverter<NdbInterpretedCode *> arg0(args[0]);
79 
80   NdbScanFilter * f = new NdbScanFilter(arg0.toC());
81 
82   Local<Object> jsObject = NdbScanFilterEnvelope.newWrapper();
83   wrapPointerInObject(f, NdbScanFilterEnvelope, jsObject);
84   freeFromGC(f, jsObject);
85   return scope.Close(jsObject);
86 }
87 
88 
begin(const Arguments & args)89 Handle<Value> begin(const Arguments & args) {
90   DEBUG_MARKER(UDEB_DETAIL);
91   HandleScope scope;
92   typedef NativeMethodCall_1_<int, NdbScanFilter, NdbScanFilter::Group> NCALL;
93   NCALL ncall(& NdbScanFilter::begin, args);
94   ncall.run();
95   return scope.Close(ncall.jsReturnVal());
96 }
97 
end(const Arguments & args)98 Handle<Value> end(const Arguments & args) {
99   DEBUG_MARKER(UDEB_DETAIL);
100   HandleScope scope;
101   typedef NativeMethodCall_0_<int, NdbScanFilter> NCALL;
102   NCALL ncall(& NdbScanFilter::end, args);
103   ncall.run();
104   return scope.Close(ncall.jsReturnVal());
105 }
106 
istrue(const Arguments & args)107 Handle<Value> istrue(const Arguments & args) {
108   DEBUG_MARKER(UDEB_DETAIL);
109   HandleScope scope;
110   typedef NativeMethodCall_0_<int, NdbScanFilter> NCALL;
111   NCALL ncall(& NdbScanFilter::istrue, args);
112   ncall.run();
113   return scope.Close(ncall.jsReturnVal());
114 }
115 
isfalse(const Arguments & args)116 Handle<Value> isfalse(const Arguments & args) {
117   DEBUG_MARKER(UDEB_DETAIL);
118   HandleScope scope;
119   typedef NativeMethodCall_0_<int, NdbScanFilter> NCALL;
120   NCALL ncall(& NdbScanFilter::isfalse, args);
121   ncall.run();
122   return scope.Close(ncall.jsReturnVal());
123 }
124 
125 
126 /* cmp()
127    ARG0: BinaryCondition
128    ARG1: Column ID
129    ARG2: Buffer
130    ARG3: Offset
131    ARG4: Length
132 */
cmp(const Arguments & args)133 Handle<Value> cmp(const Arguments &args) {
134   HandleScope scope;
135 
136   NdbScanFilter * filter = unwrapPointer<NdbScanFilter *>(args.Holder());
137   int condition = args[0]->Int32Value();
138   int columnId  = args[1]->Uint32Value();
139   char * buffer = node::Buffer::Data(args[2]->ToObject());
140   size_t offset = args[3]->Uint32Value();
141   size_t length = args[4]->Uint32Value();
142 
143   int rval = filter->cmp(NdbScanFilter::BinaryCondition(condition),
144                          columnId, buffer + offset, length);
145 
146   return scope.Close(toJS(rval));
147 }
148 
149 
isnull(const Arguments & args)150 Handle<Value> isnull(const Arguments & args) {
151   DEBUG_MARKER(UDEB_DETAIL);
152   HandleScope scope;
153   typedef NativeMethodCall_1_<int, NdbScanFilter, int> NCALL;
154   NCALL ncall(& NdbScanFilter::isnull, args);
155   ncall.run();
156   return scope.Close(ncall.jsReturnVal());
157 }
158 
isnotnull(const Arguments & args)159 Handle<Value> isnotnull(const Arguments & args) {
160   DEBUG_MARKER(UDEB_DETAIL);
161   HandleScope scope;
162   typedef NativeMethodCall_1_<int, NdbScanFilter, int> NCALL;
163   NCALL ncall(& NdbScanFilter::isnotnull, args);
164   ncall.run();
165   return scope.Close(ncall.jsReturnVal());
166 }
167 
getInterpretedCode(const Arguments & args)168 Handle<Value> getInterpretedCode(const Arguments & args) {
169   DEBUG_MARKER(UDEB_DETAIL);
170   HandleScope scope;
171   typedef NativeConstMethodCall_0_<const NdbInterpretedCode *, NdbScanFilter> NCALL;
172   NCALL ncall(& NdbScanFilter::getInterpretedCode, args);
173   ncall.wrapReturnValueAs(getConstNdbInterpretedCodeEnvelope());
174   ncall.run();
175   return scope.Close(ncall.jsReturnVal());
176 }
177 
getNdbOperation(const Arguments & args)178 Handle<Value> getNdbOperation(const Arguments & args) {
179   DEBUG_MARKER(UDEB_DETAIL);
180   HandleScope scope;
181   typedef NativeConstMethodCall_0_<NdbOperation *, NdbScanFilter> NCALL;
182   NCALL ncall(& NdbScanFilter::getNdbOperation, args);
183   ncall.run();
184   return scope.Close(ncall.jsReturnVal());
185 }
186 
187 
188 #define WRAP_CONSTANT(X) DEFINE_JS_INT(sfObj, #X, NdbScanFilter::X)
NdbScanFilter_initOnLoad(Handle<Object> target)189 void NdbScanFilter_initOnLoad(Handle<Object> target) {
190   HandleScope scope;
191 
192   Persistent<String> sfKey = Persistent<String>(String::NewSymbol("NdbScanFilter"));
193   Persistent<Object> sfObj = Persistent<Object>(Object::New());
194 
195   target->Set(sfKey, sfObj);
196 
197   DEFINE_JS_FUNCTION(sfObj, "create", newNdbScanFilter);
198   WRAP_CONSTANT(AND);
199   WRAP_CONSTANT(OR);
200   WRAP_CONSTANT(NAND);
201   WRAP_CONSTANT(NOR);
202   WRAP_CONSTANT(COND_LE);
203   WRAP_CONSTANT(COND_LT);
204   WRAP_CONSTANT(COND_GE);
205   WRAP_CONSTANT(COND_GT);
206   WRAP_CONSTANT(COND_EQ);
207   WRAP_CONSTANT(COND_NE);
208   WRAP_CONSTANT(COND_LIKE);
209   WRAP_CONSTANT(COND_NOT_LIKE);
210   WRAP_CONSTANT(COND_AND_EQ_MASK);
211   WRAP_CONSTANT(COND_AND_NE_MASK);
212   WRAP_CONSTANT(COND_AND_EQ_ZERO);
213   WRAP_CONSTANT(COND_AND_NE_ZERO);
214   WRAP_CONSTANT(FilterTooLarge);
215 }
216 
217