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 "adapter_global.h"
26 #include "ColumnHandler.h"
27 #include "BlobHandler.h"
28 #include "JsWrapper.h"
29 #include "js_wrapper_macros.h"
30 
31 using namespace v8;
32 
33 
ColumnHandler()34 ColumnHandler::ColumnHandler() :
35   column(0), offset(0),
36   isLob(false), isText(false)
37 {
38 }
39 
40 
~ColumnHandler()41 ColumnHandler::~ColumnHandler() {
42   // Persistent handles will be disposed by calling of their destructors
43 }
44 
init(v8::Isolate * isolate,const NdbDictionary::Column * _column,uint32_t _offset)45 void ColumnHandler::init(v8::Isolate * isolate,
46                          const NdbDictionary::Column *_column,
47                          uint32_t _offset) {
48   column = _column;
49   encoder = getEncoderForColumn(column);
50   offset = _offset;
51 
52   switch(column->getType()) {
53     case NDB_TYPE_TEXT:
54       isText = true;   // fall through to also set isLob
55     case NDB_TYPE_BLOB:
56       isLob = true;
57       break;
58     default:
59       break;
60   }
61 }
62 
63 
read(char * rowBuffer,Handle<Object> blobBuffer) const64 Handle<Value> ColumnHandler::read(char * rowBuffer, Handle<Object> blobBuffer) const {
65   Handle<Value> val;  // HandleScope is in ValueObject.cpp nroGetter
66 
67   if(isText) {
68     DEBUG_PRINT("text read");
69     val = getTextFromBuffer(column, blobBuffer);
70   } else if(isLob) {
71     DEBUG_PRINT("blob read");
72     val = Handle<Value>(blobBuffer);
73   } else {
74     val = encoder->read(column, rowBuffer, offset);
75   }
76   return val;
77 }
78 
79 
80 // If column is a blob, val is the blob buffer
write(Handle<Value> val,char * buffer) const81 Handle<Value> ColumnHandler::write(Handle<Value> val, char *buffer) const {
82   DEBUG_PRINT("write %s", column->getName());
83   return encoder->write(column, val, buffer, offset);
84 }
85 
86 
createBlobWriteHandle(Local<Value> val,int fieldNo) const87 BlobWriteHandler * ColumnHandler::createBlobWriteHandle(Local<Value> val,
88                                                         int fieldNo) const {
89   DEBUG_MARKER(UDEB_DETAIL);
90   BlobWriteHandler * b = 0;
91   Handle<Object> nodeBuffer;
92   if(isLob) {
93     nodeBuffer = (isText && val->IsString()) ?
94        getBufferForText(column, val->ToString()) :  // TEXT
95        val->ToObject();                             // BLOB
96     b = new BlobWriteHandler(column->getColumnNo(), fieldNo, nodeBuffer);
97   }
98   return b;
99 }
100 
101