1 /*
2  Copyright (c) 2016, 2017, 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 <stdlib.h>
26 #include <assert.h>
27 
28 #include <NdbApi.hpp>
29 #include <node_buffer.h>
30 
31 #include "adapter_global.h"
32 #include "unified_debug.h"
33 #include "BlobHandler.h"
34 #include "JsWrapper.h"
35 
36 
37 // BlobHandler constructor
BlobHandler(int colId,int fieldNo)38 BlobHandler::BlobHandler(int colId, int fieldNo) :
39   ndbBlob(0),
40   next(0),
41   content(0),
42   length(0),
43   columnId(colId),
44   fieldNumber(fieldNo)
45 {
46 }
47 
48 
49 // Helper functions for BlobReadHandler
blobHandlerActiveHook(NdbBlob * ndbBlob,void * handler)50 int blobHandlerActiveHook(NdbBlob * ndbBlob, void * handler) {
51   BlobReadHandler * blobHandler = static_cast<BlobReadHandler *>(handler);
52   return blobHandler->runActiveHook(ndbBlob);
53 }
54 
freeBufferContentsFromJs(char * data,void *)55 void freeBufferContentsFromJs(char *data, void *) {
56   DEBUG_PRINT("Free %p", data);
57   free(data);                                                // here is free
58 }
59 
60 
61 // BlobReadHandler methods
prepare(const NdbOperation * ndbop)62 void BlobReadHandler::prepare(const NdbOperation * ndbop) {
63   ndbBlob = ndbop->getBlobHandle(columnId);
64   assert(ndbBlob);
65   ndbBlob->setActiveHook(blobHandlerActiveHook, this);
66 
67   if(next) next->prepare(ndbop);
68 }
69 
runActiveHook(NdbBlob * b)70 int BlobReadHandler::runActiveHook(NdbBlob *b) {
71   assert(b == ndbBlob);
72   int isNull;
73   ndbBlob->getNull(isNull);
74   if(! isNull) {
75     ndbBlob->getLength(length);
76     uint32_t nBytes = static_cast<uint32_t>(length);
77     content = (char *) malloc(length);                        // here is malloc
78     if(content) {
79       int rv = ndbBlob->readData(content, nBytes);
80       DEBUG_PRINT("BLOB read: column %d, length %llu, read %d/%d",
81                   columnId, length, rv, nBytes);
82     } else {
83       return -1;
84     }
85   }
86   return 0;
87 }
88 
getResultBuffer(v8::Isolate * iso)89 v8::Local<v8::Object> BlobReadHandler::getResultBuffer(v8::Isolate * iso) {
90   v8::Local<v8::Object> buffer;
91   if(content) {
92     buffer = LOCAL_BUFFER(node::Buffer::New(iso, content, length, freeBufferContentsFromJs, 0));
93     /* Content belongs to someone else now; clear it for the next user */
94     content = 0;
95     length = 0;
96   }
97   return buffer;
98 }
99 
100 
101 // BlobWriteHandler methods
102 
BlobWriteHandler(int colId,int fieldNo,v8::Handle<v8::Object> blobValue)103 BlobWriteHandler::BlobWriteHandler(int colId, int fieldNo,
104                                    v8::Handle<v8::Object> blobValue) :
105   BlobHandler(colId, fieldNo)
106 {
107   length = node::Buffer::Length(blobValue);
108   content = node::Buffer::Data(blobValue);
109 }
110 
prepare(const NdbOperation * ndbop)111 void BlobWriteHandler::prepare(const NdbOperation * ndbop) {
112   ndbBlob = ndbop->getBlobHandle(columnId);
113   if(! ndbBlob) {
114     DEBUG_PRINT("getBlobHandle %d: [%d] %s", columnId,
115                 ndbop->getNdbError().code, ndbop->getNdbError().message);
116     assert(false);
117   }
118 
119   DEBUG_PRINT("Prepare write for BLOB column %d, length %llu", columnId, length);
120   ndbBlob->setValue(content, static_cast<uint32_t>(length));
121   if(next) next->prepare(ndbop);
122 }
123 
124