4e4e435f621d078409291a1104fe7d8a11874873
[framework/web/wrt-plugins-tizen.git] / src / DataControl / SelectDataObject.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include "SelectDataObject.h"
19
20 #include <iostream>
21 #include <fstream>
22 #include <string>
23 #include <vector>
24 #include <string.h>
25 #include "DataType.h"
26
27 namespace DeviceAPI {
28 namespace DataControl {
29
30 #define TEXT_DEL 0x0a
31
32
33 SelectDataObject::SelectDataObject()
34 {
35         LogDebug("Enter");
36         
37         m_colCount = 0;
38         m_rowCount = 0;
39         m_colTotalSize = 0;
40 }
41
42 SelectDataObject::~SelectDataObject() 
43 {
44         LogDebug("Enter");
45         m_istream.close();
46
47         if (unlink(m_filePath.c_str()) != 0) 
48         {
49                 LogError("Error while removing SelectDataObject.");
50         }
51
52 }
53
54 void SelectDataObject::openResultPath(const std::string& filepath)
55 {
56         LogDebug("Enter");
57
58         m_filePath = filepath;
59         m_istream.open(m_filePath.c_str(), std::ios::binary);
60
61         if (m_istream.is_open()) 
62         {
63                 LogDebug("load header");
64                 loadHeader();
65                 LogDebug("print header"); // debug
66                 printHeader();
67         }
68         else
69         {
70                 LogDebug("fail open" << filepath);
71         }
72 }
73
74
75 void SelectDataObject::getIndexedRow(const EventGetIndexedRowPtr& event)
76 {
77         WrtDeviceApis::Commons::EventRequestReceiver<EventGetIndexedRow>::PostRequest(event);
78 }
79
80 void SelectDataObject::OnRequestReceived(const EventGetIndexedRowPtr& event)
81 {
82         LogDebug("Enter");
83         char *buf =  NULL;
84
85         try 
86         {
87
88                 size_t dataStart = sizeof(int) * 3 + m_colTotalSize + sizeof(int) * m_headerInfo.size();
89
90                 int memorizedSize = 255;
91                 int size = 0;
92                 int type = 0;
93                 int rowIndex = event->getIndex();
94                 // move stream to current index
95                 size_t index = 0;
96                 
97                 std::vector<std::string> keys, values;
98                 std::string data;       
99                 std::stringstream ssdata;
100                 int ospIntData = 0;
101                 long long ospBigIntData = 0;
102                 double ospDoubleData = 0;
103
104
105                 if (rowIndex >= m_rowCount)
106                 {
107                         ThrowMsg(WrtDeviceApis::Commons::OutOfRangeException, "The requested index bigger than result row count");
108                 }
109
110                 m_istream.seekg(dataStart, std::ios::beg);
111                 
112                 for (index = 0; index < m_headerInfo.size(); index++)
113                 {
114                         keys.push_back(m_headerInfo[index].m_name);
115                 }
116                 
117
118                 for (index = 0; index < rowIndex; index++)
119                 {
120                         moveOneRow();   
121                 }
122
123                 buf = new char[memorizedSize];
124
125                 if (buf == NULL)
126                 {
127                         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Out of memory");
128                 }
129
130                 for (index = 0; index < m_colCount; index++)
131                 {
132                         size = 0;
133                         
134                         m_istream.read((char*)&type, sizeof(type)); // skip type
135                         m_istream.read((char*)&size, sizeof(int)); // read size
136                         LogDebug(type << " - " << size << " ");
137
138                         if (memorizedSize < size)
139                         {
140                                 memorizedSize = size;
141
142                                 if (buf)
143                                 {
144                                         delete[] buf;
145                                 }
146
147                                 buf = new char[memorizedSize];
148                         }
149
150                         if (buf == NULL)
151                         {
152                                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Out of memory");
153                         }
154                         
155                         memset(buf, 0, memorizedSize);
156
157                         switch (type)
158                         {
159                         case DB_COLUMNTYPE_TEXT:
160                         case DB_COLUMNTYPE_BLOB:
161                                 m_istream.read((char*)buf, size);
162                                 data = buf;
163                                 break;
164                         case DB_COLUMNTYPE_INT:
165                                 ssdata.str("");
166                                 ospIntData = 0;
167                                 m_istream.read((char*)&ospIntData, size);
168                                 ssdata << ospIntData;
169                                 data = ssdata.str();
170                                 break;
171                         case DB_COLUMNTYPE_INT64:
172                                 ssdata.str("");
173                                 m_istream.read((char*)&ospBigIntData, size);
174                                 ssdata << ospBigIntData;
175                                 data = ssdata.str();
176                                 break;
177                         case DB_COLUMNTYPE_DOUBLE:
178                                 ssdata.flush();
179                                 m_istream.read((char*)&ospDoubleData, size);                            
180                                 ssdata << ospDoubleData;
181                                 data = ssdata.str();
182                                 break;
183                         case DB_COLUMNTYPE_NULL:
184                         case DB_COLUMNTYPE_UNDEFINED:
185                                 data = "";
186                                 break;
187                         default:
188                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Not support type");                                
189                                 break;
190                         }
191                         values.push_back(data);
192                 }
193                 RowDataPtr rowData(new RowData(keys, values));
194                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
195                 event->setRowData(rowData);
196         }
197     catch (const WrtDeviceApis::Commons::Exception& ex) 
198     {
199         LogError("Exception: " << ex.GetMessage());
200         event->setExceptionCode(ex.getCode());
201     }
202
203         if (buf)
204         {
205                 delete[] buf;
206         }
207 }
208
209
210 void SelectDataObject::setResultSetPath(const std::string& path)
211 {
212         m_filePath = path;
213 }
214
215 int SelectDataObject::getRowNumber()
216 {
217         return m_rowCount;
218 }
219
220
221 void SelectDataObject::loadHeader()
222 {
223         char buf[255] = {0,};
224
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));
229
230         size_t index = 0, j = 0;
231         char c;
232
233         for (index = 0; index < m_colCount; index++)
234         {
235                 ColScheme header;
236                 
237                 header.m_type = 0;
238                 header.m_name = "";
239
240                 m_istream.read((char*)&header.m_type, sizeof(int)); 
241
242                 std::cout << header.m_type;
243
244                                 
245                 m_headerInfo.push_back(header);
246         }
247
248         for (index = 0; index < m_colCount; index++)
249         {
250                 memset(buf, 0, sizeof(buf));
251                 
252                 for (j = 0; (c = (char)m_istream.get()) != TEXT_DEL && m_istream.good() ; j++)
253                 {
254                         buf[j] = c;     
255                 }
256                 buf[j] = '\0';
257                 
258                 std::cout << buf;
259
260                 m_headerInfo[index].m_name = buf;
261         }
262
263 }
264 void SelectDataObject::printHeader()
265 {
266         LogDebug("Eneter");
267         
268         for (size_t index = 0; index < m_headerInfo.size(); index++)
269         {
270                 LogDebug(m_headerInfo[index].m_type << " " << m_headerInfo[index].m_name);
271         }
272
273 }
274 void SelectDataObject::moveOneRow()
275 {
276         int size = 0;
277         size_t index = 0;
278
279         if (m_istream.good() == false)
280                 LogDebug("Error");
281
282         for (index = 0; index < m_colCount; index++)
283         {
284                 size = 0;
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
289         }
290
291 }
292
293
294 }
295 }