2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include "SelectDataObject.h"
28 namespace DataControl {
33 SelectDataObject::SelectDataObject()
42 SelectDataObject::~SelectDataObject()
47 if (unlink(m_filePath.c_str()) != 0)
49 LogError("Error while removing SelectDataObject.");
54 void SelectDataObject::openResultPath(const std::string& filepath)
58 m_filePath = filepath;
59 m_istream.open(m_filePath.c_str(), std::ios::binary);
61 if (m_istream.is_open())
63 LogDebug("load header");
65 LogDebug("print header"); // debug
70 LogDebug("fail open" << filepath);
75 void SelectDataObject::getIndexedRow(const EventGetIndexedRowPtr& event)
77 WrtDeviceApis::Commons::EventRequestReceiver<EventGetIndexedRow>::PostRequest(event);
80 void SelectDataObject::OnRequestReceived(const EventGetIndexedRowPtr& event)
88 size_t dataStart = sizeof(int) * 3 + m_colTotalSize + sizeof(int) * m_headerInfo.size();
90 int memorizedSize = 255;
93 int rowIndex = event->getIndex();
94 // move stream to current index
97 std::vector<std::string> keys, values;
99 std::stringstream ssdata;
101 long long ospBigIntData = 0;
102 double ospDoubleData = 0;
105 if (rowIndex >= m_rowCount)
107 ThrowMsg(WrtDeviceApis::Commons::OutOfRangeException, "The requested index bigger than result row count");
110 m_istream.seekg(dataStart, std::ios::beg);
112 for (index = 0; index < m_headerInfo.size(); index++)
114 keys.push_back(m_headerInfo[index].m_name);
118 for (index = 0; index < rowIndex; index++)
123 buf = new char[memorizedSize];
127 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Out of memory");
130 for (index = 0; index < m_colCount; index++)
134 m_istream.read((char*)&type, sizeof(type)); // skip type
135 m_istream.read((char*)&size, sizeof(int)); // read size
136 LogDebug(type << " - " << size << " ");
138 if (memorizedSize < size)
140 memorizedSize = size;
147 buf = new char[memorizedSize];
152 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Out of memory");
155 memset(buf, 0, memorizedSize);
159 case DB_COLUMNTYPE_TEXT:
160 case DB_COLUMNTYPE_BLOB:
161 m_istream.read((char*)buf, size);
164 case DB_COLUMNTYPE_INT:
167 m_istream.read((char*)&ospIntData, size);
168 ssdata << ospIntData;
171 case DB_COLUMNTYPE_INT64:
173 m_istream.read((char*)&ospBigIntData, size);
174 ssdata << ospBigIntData;
177 case DB_COLUMNTYPE_DOUBLE:
179 m_istream.read((char*)&ospDoubleData, size);
180 ssdata << ospDoubleData;
183 case DB_COLUMNTYPE_NULL:
184 case DB_COLUMNTYPE_UNDEFINED:
188 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Not support type");
191 values.push_back(data);
193 RowDataPtr rowData(new RowData(keys, values));
194 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
195 event->setRowData(rowData);
197 catch (const WrtDeviceApis::Commons::Exception& ex)
199 LogError("Exception: " << ex.GetMessage());
200 event->setExceptionCode(ex.getCode());
210 void SelectDataObject::setResultSetPath(const std::string& path)
215 int SelectDataObject::getRowNumber()
221 void SelectDataObject::loadHeader()
223 char buf[255] = {0,};
225 m_istream.seekg(0, std::ios::beg);
226 m_istream.read((char*)&m_rowCount, sizeof(int));
227 m_istream.read((char*)&m_colCount, sizeof(int));
228 m_istream.read((char*)&m_colTotalSize, sizeof(int));
230 size_t index = 0, j = 0;
233 for (index = 0; index < m_colCount; index++)
240 m_istream.read((char*)&header.m_type, sizeof(int));
242 std::cout << header.m_type;
245 m_headerInfo.push_back(header);
248 for (index = 0; index < m_colCount; index++)
250 memset(buf, 0, sizeof(buf));
252 for (j = 0; (c = (char)m_istream.get()) != TEXT_DEL && m_istream.good() ; j++)
260 m_headerInfo[index].m_name = buf;
264 void SelectDataObject::printHeader()
268 for (size_t index = 0; index < m_headerInfo.size(); index++)
270 LogDebug(m_headerInfo[index].m_type << " " << m_headerInfo[index].m_name);
274 void SelectDataObject::moveOneRow()
279 if (m_istream.good() == false)
282 for (index = 0; index < m_colCount; index++)
285 m_istream.seekg(sizeof(int), std::ios::cur); // skip type
286 m_istream.read((char*)&size, sizeof(int)); // read size
287 LogDebug(size << " ");
288 m_istream.seekg(size, std::ios::cur); // skip data