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 #include <node.h>
26 #include <node_buffer.h>
27 #include <NdbApi.hpp>
28 
29 #include "adapter_global.h"
30 #include "JsWrapper.h"
31 #include "NdbRecordObject.h"
32 #include "BlobHandler.h"
33 
34 
NdbRecordObject(const Record * _record,ColumnHandlerSet * _handlers,const Arguments & args)35 NdbRecordObject::NdbRecordObject(const Record *_record,
36                                  ColumnHandlerSet * _handlers,
37                                  const Arguments & args) :
38   record(_record),
39   handlers(_handlers),
40   ncol(record->getNoOfColumns()),
41   proxy(new ColumnProxy[record->getNoOfColumns()]),
42   nWrites(0),
43   isolate(args.GetIsolate())
44 {
45   EscapableHandleScope scope(isolate);
46   const Handle<Value> & jsBuffer = args[0];
47   const Handle<Value> & blobBufferArray = args[1];
48 
49   unsigned int nblobs = 0;
50 
51   /* Retain a handle on the buffer for our whole lifetime */
52   persistentBufferHandle.Reset(isolate, jsBuffer);
53   buffer = node::Buffer::Data(jsBuffer);
54 
55   /* Initialize the list of masked-in columns */
56   resetMask();
57 
58   /* Attach the column proxies to their handlers */
59   for(unsigned int i = 0 ; i < ncol ; i++)
60     proxy[i].setHandler(handlers->getHandler(i));
61 
62   /* Attach BLOB buffers */
63   if(blobBufferArray->IsObject()) {
64     for(unsigned int i = 0 ; i < ncol ; i++) {
65       Handle<Value> b = blobBufferArray->ToObject()->Get(i);
66       if(b->IsObject()) {
67         nblobs++;
68         Handle<Object> buf = b->ToObject();
69         assert(node::Buffer::HasInstance(buf));
70         proxy[i].setBlobBuffer(isolate, buf);
71         record->setNotNull(i, buffer);
72       } else if(b->IsNull()) {
73        nblobs++;
74        record->setNull(i, buffer);
75       }
76     }
77   }
78   DEBUG_PRINT("    ___Constructor___       [%d col, bufsz %d, %d blobs]",
79               ncol, record->getBufferSize(), nblobs);
80   assert(nblobs == record->getNoOfBlobColumns());
81   assert(node::Buffer::Length(jsBuffer) == record->getBufferSize());
82 }
83 
84 
~NdbRecordObject()85 NdbRecordObject::~NdbRecordObject() {
86   DEBUG_PRINT(" << Destructor");
87   persistentBufferHandle.Reset();
88   delete[] proxy;
89 }
90 
91 
getField(int nField)92 Local<Value> NdbRecordObject::getField(int nField) {
93   if(record->isNull(nField, buffer))
94     return Null(isolate);
95   else
96     return proxy[nField].get(isolate, buffer);
97 }
98 
99 
prepare()100 Local<Value> NdbRecordObject::prepare() {
101   EscapableHandleScope scope(isolate);
102   int n = 0;
103   Local<Value> writeStatus;
104   Local<Value> savedError = Undefined(isolate);
105   for(unsigned int i = 0 ; i < ncol ; i++) {
106     if(isMaskedIn(i)) {
107       n++;
108       if(proxy[i].valueIsNull()) {
109         record->setNull(i, buffer);
110       }
111       else {
112         writeStatus = proxy[i].write(isolate, buffer);
113         if(! writeStatus->IsUndefined()) savedError = writeStatus;
114       }
115     }
116   }
117   DEBUG_PRINT("Prepared %d column%s. Mask %u.", n, (n == 1 ? "" : "s"), u.maskvalue);
118   return scope.Escape(savedError);
119 }
120 
121 
createBlobWriteHandles(KeyOperation & op)122 int NdbRecordObject::createBlobWriteHandles(KeyOperation & op) {
123   int ncreated = 0;
124   for(unsigned int i = 0 ; i < ncol ; i++) {
125     if(isMaskedIn(i)) {
126       BlobWriteHandler * b = proxy[i].createBlobWriteHandle(i);
127       if(b) {
128         DEBUG_PRINT(" createBlobWriteHandles -- for column %d", i);
129         op.setBlobHandler(b);
130         ncreated++;
131       }
132     }
133   }
134   return ncreated;
135 }
136