Merge "Modify operator[] to get the right LastResult" into devel_3.0_main
[platform/framework/native/appfw.git] / src / io / FIo_DataControlResultSetEnumerator.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FIo_DataControlResultSetEnumerator.cpp
19  * @brief       This is the implementation for the %_DataControlResultSetEnumerator class.
20  */
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <new>
25 #include <unique_ptr.h>
26
27 #include <FIoFile.h>
28 #include <FBaseInteger.h>
29 #include <FBaseSysLog.h>
30
31 #include <FIo_FileImpl.h>
32 #include <FIo_DataControlResultSetEnumerator.h>
33
34 using namespace std;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37
38 namespace Tizen { namespace Io
39 {
40
41 _DataControlResultSetEnumerator::_DataControlResultSetEnumerator(void)
42         : __pFile(null)
43         , __rowCount(0)
44         , __columnCount(0)
45         , __columnTypeOffset(0)
46         , __columnNameOffset(0)
47         , __contentOffset(0)
48         , __currentOffset(0)
49         , __currentRowCount(0)
50 {
51 }
52
53 _DataControlResultSetEnumerator::~_DataControlResultSetEnumerator(void)
54 {
55         delete static_cast <File*>(__pFile);
56         __rowOffsetList.RemoveAll(true);
57 }
58
59 result
60 _DataControlResultSetEnumerator::SetPath(const String filePath)
61 {
62         result r = E_SUCCESS;
63         SysAssertf(__pFile == null, "Already Set. Calling SetPath() twice or more on a same instance is not allowed for this class.");
64         unique_ptr<File> pFile(new (std::nothrow) File);
65         SysTryReturnResult(NID_IO, pFile != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
66
67         r = pFile->Construct(filePath, L"r");
68         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
69
70         __pFile = pFile.release();
71
72         return E_SUCCESS;
73 }
74
75 result
76 _DataControlResultSetEnumerator::MoveNext(void)
77 {
78         int totalColumnNameSize = 0;
79         result r = E_SUCCESS;
80         int ret = 0;
81         Integer* pCurrentOffset = null;
82
83         SysTryReturnResult(NID_IO, __pFile != null, E_OUT_OF_RANGE, "The result set does not exist.");
84
85         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
86
87         SysTryReturnResult(NID_IO, pFile != null, E_INVALID_STATE,
88                                 "This instance has not been properly constructed yet or has already been finalized.");
89
90         if (__currentOffset == 0)
91         {
92                 r = pFile->Seek(FILESEEKPOSITION_BEGIN, 0);
93                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
94
95                 ret = pFile->Read(&__rowCount, sizeof(int));
96                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
97                 ret = pFile->Read(&__columnCount, sizeof(int));
98                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
99                 ret = pFile->Read(&totalColumnNameSize, sizeof(int));
100                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
101
102                 __columnTypeOffset = sizeof(int) * 3;
103                 __columnNameOffset = __columnTypeOffset + __columnCount * sizeof(int);
104                 __contentOffset = __columnNameOffset + totalColumnNameSize;
105
106                 __currentOffset = __contentOffset;
107
108                 r = __rowOffsetList.Add(* new (std::nothrow) Integer(__currentOffset));
109                 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s]Propagated.", GetErrorMessage(r));
110         }
111         else
112         {
113                 // check if dataoffset list contains next offset
114                 pCurrentOffset = dynamic_cast < Integer* >(__rowOffsetList.GetAt(__currentRowCount + 1));
115
116                 if (pCurrentOffset == null) // Move to next offset
117                 {
118                         int size = 0;
119                         int i = 0;
120
121                         // Reading till EOF is not required as we might have already cached the last content offset. So check and return
122                         SysTryReturnResult(NID_IO, __currentRowCount < (__rowCount -1), E_OUT_OF_RANGE, "Reached to the end of the result set");
123
124                         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __currentOffset);
125                         SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
126
127                         for (i = 0; i < __columnCount; i++)
128                         {
129                                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, sizeof(int)); // type
130                                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
131                                 ret = pFile->Read(&size, sizeof(int));
132                                 if (ret == 0)
133                                 {
134                                         r = GetLastResult();
135                                         SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_OUT_OF_RANGE, "Reached to the end of the result set");
136                                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
137                                 }
138                                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, size);
139                                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
140
141                                 __currentOffset += sizeof(int) * 2 + size;
142                         }
143
144                         r = __rowOffsetList.Add(* new (std::nothrow) Integer(__currentOffset));
145                         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s]Propagated.", GetErrorMessage(r));
146                 }
147                 else
148                 {
149                         __currentOffset = pCurrentOffset->ToInt();
150                 }
151                 __currentRowCount++;
152
153         }
154
155         return r;
156 }
157
158 result
159 _DataControlResultSetEnumerator::MovePrevious(void)
160 {
161         result r = E_SUCCESS;
162         Integer* pPreviousOffset = null;
163
164         SysTryReturnResult(NID_IO, __pFile != null, E_OUT_OF_RANGE, "The result set does not exist.");
165
166         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
167
168         SysTryReturnResult(NID_IO, pFile != null, E_INVALID_STATE,
169                                 "This instance has not been properly constructed yet or has already been finalized.");
170
171         SysTryReturnResult(NID_IO, __currentRowCount > 0, E_OUT_OF_RANGE,
172                                 "The Method has reached out of the result set.");
173
174         pPreviousOffset = dynamic_cast < Integer* >(__rowOffsetList.GetAt(__currentRowCount - 1));
175         if (!pPreviousOffset)
176         {
177                 r = GetLastResult();
178                 SysTryReturnResult(NID_IO, !IsFailed(r), r, "[%s]Propagated.", GetErrorMessage(r));
179                 // should not happen
180                 SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error occurred.", GetErrorMessage(r));
181         }
182
183         __currentRowCount--;
184         __currentOffset = pPreviousOffset->ToInt();
185
186         return r;
187 }
188
189 result
190 _DataControlResultSetEnumerator::MoveFirst(void)
191 {
192         if (__currentOffset > 0)
193         {
194                 __currentOffset = __contentOffset;
195                 __currentRowCount = 0;
196                 return E_SUCCESS;
197         }
198
199         // MoveFirst is called for the first time before MoveNext() or MoveLast()
200         __currentOffset = 0;
201         return this->MoveNext();
202 }
203
204 result
205 _DataControlResultSetEnumerator::MoveLast(void)
206 {
207         result r = E_SUCCESS;
208         Integer* pRowOffset = null;
209         int offsetCount = 0;
210
211         SysTryReturnResult(NID_IO, __pFile != null, E_OUT_OF_RANGE, "The result set does not exist.");
212
213         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
214
215         SysTryReturnResult(NID_IO, pFile != null, E_INVALID_STATE,
216                                 "This instance has not been properly constructed yet or has already been finalized.");
217
218         if (__currentRowCount == (__rowCount - 1))
219         {
220                 return E_SUCCESS; // Already @ last row
221         }
222
223         offsetCount = __rowOffsetList.GetCount();
224
225         if (__currentOffset == 0 && offsetCount == 0)
226         {
227                 r = this->MoveNext(); // make a first move
228                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to MoveNext.");
229         }
230
231         // check if the rowOffsetList contains last row offset
232
233         if (offsetCount == __rowCount)
234         {
235                 pRowOffset = dynamic_cast < Integer* >(__rowOffsetList.GetAt(__rowCount - 1)); // get last item
236                 if (!pRowOffset)
237                 {
238                         r = GetLastResult();
239                         SysTryReturnResult(NID_IO, !IsFailed(r), r, "[%s]Propagated.", GetErrorMessage(r));
240                         // should not happen
241                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error occurred.", GetErrorMessage(r));
242                 }
243
244                 __currentOffset = pRowOffset->ToInt();
245                 __currentRowCount = __rowCount - 1;
246         }
247         else
248         {
249                 // @ this time, List will not be empty.
250                 pRowOffset = dynamic_cast < Integer* >(__rowOffsetList.GetAt(offsetCount - 1)); // get the last item in the list
251                 if (pRowOffset)
252                 {
253                         __currentOffset = pRowOffset->ToInt();
254                         __currentRowCount = offsetCount - 1;
255                 }
256
257                                 // Move till last row offset.
258                 for (int i = (__currentRowCount + 1); i < __rowCount; i++)
259                 {
260                         r = this->MoveNext(); // move till last row data offset
261                         SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to MoveNext.");
262
263                 }
264         }
265
266         return r;
267 }
268
269 result
270 _DataControlResultSetEnumerator::Reset(void)
271 {
272         result r = E_SUCCESS;
273
274         //SysTryReturn(NID_IO, __pFile != null, E_INVALID_STATE, E_INVALID_STATE,
275         //              "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
276
277         //SysTryReturn(NID_IO, __columnCount > 0, E_INVALID_STATE, E_INVALID_STATE,
278         //              "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
279
280         __currentOffset = 0;
281         __currentRowCount = 0;
282         __rowOffsetList.RemoveAll(true);
283
284         return r;
285 }
286
287 result
288 _DataControlResultSetEnumerator::GetIntAt(int columnIndex, int& value) const
289 {
290         long long int64Value = 0;
291         result r = E_SUCCESS;
292
293         r = GetInt64At(columnIndex, int64Value);
294         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
295
296         value = (int) int64Value;
297
298         return r;
299 }
300
301 result
302 _DataControlResultSetEnumerator::GetInt64At(int columnIndex, long long& value) const
303 {
304         int type = -1;
305         int size = 0;
306         long long readInt64 = 0;
307         int i = 0;
308         result r = E_SUCCESS;
309         int ret = 0;
310
311         //SysTryReturnResult(NID_IO, __pFile != null, E_INVALID_STATE,
312         //              "This instance has not been properly constructed yet or has already been finalized.");
313
314         SysTryReturnResult(NID_IO, __columnCount > 0, E_INVALID_STATE,
315                                 "This instance has not been properly constructed yet or has already been finalized.");
316
317         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
318                                 "columnIndex is out of range");
319
320         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
321
322         SysTryReturnResult(NID_IO, pFile != null, E_INVALID_STATE,
323                                 "This instance has not been properly constructed yet or has already been finalized.");
324
325         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __currentOffset);
326         SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
327
328         for (i = 0; i < columnIndex; i++) // move to column index
329         {
330                 ret = pFile->Read(&type, sizeof(int));
331                 if (ret == 0)
332                 {
333                         r = GetLastResult();
334                         SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "The columnIndex is out of range.");
335                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
336                 }
337                 ret = pFile->Read(&size, sizeof(int));
338                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
339
340                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, size);
341                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
342         }
343
344         ret = pFile->Read(&type, sizeof(int));
345         if (ret == 0)
346         {
347                 r = GetLastResult();
348                 SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "The columnIndex is out of range.");
349                 SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
350         }
351
352         SysTryReturnResult(NID_IO, type == DB_COLUMNTYPE_INT64, E_TYPE_MISMATCH,
353                                           "Trying to access column of different type.");
354
355         ret = pFile->Read(&size, sizeof(int));
356         SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
357
358         ret = pFile->Read(&readInt64, size);
359         SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
360
361         value = readInt64;
362
363         return r;
364 }
365
366 result
367 _DataControlResultSetEnumerator::GetDoubleAt(int columnIndex, double& value) const
368 {
369         int type = -1;
370         int size = 0;
371         double readDouble = 0;
372         int i = 0;
373         result r = E_SUCCESS;
374         int ret = 0;
375
376         //SysTryReturnResult(NID_IO, __pFile != null, E_INVALID_STATE,
377         //              "This instance has not been properly constructed yet or has already been finalized.");
378
379         SysTryReturnResult(NID_IO, __columnCount > 0, E_INVALID_STATE,
380                                 "This instance has not been properly constructed yet or has already been finalized.");
381
382         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
383                                 "columnIndex is out of range");
384
385         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
386
387         SysTryReturnResult(NID_IO, pFile != null, E_INVALID_STATE,
388                                 "This instance has not been properly constructed yet or has already been finalized.");
389
390         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __currentOffset);
391         SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
392
393         for (i = 0; i < columnIndex; i++) // move to column index
394         {
395                 ret = pFile->Read(&type, sizeof(int));
396                 if (ret == 0)
397                 {
398                         r = GetLastResult();
399                         SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "The columnIndex is out of range.");
400                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
401                 }
402                 ret = pFile->Read(&size, sizeof(int));
403                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
404
405                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, size);
406                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
407         }
408
409         ret = pFile->Read(&type, sizeof(int));
410         if (ret == 0)
411         {
412                 r = GetLastResult();
413                 SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "columnIndex is out of range");
414                 SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
415         }
416
417         SysTryReturnResult(NID_IO, type == DB_COLUMNTYPE_DOUBLE, E_TYPE_MISMATCH,
418                                           "Trying to access column of different type.");
419
420         ret = pFile->Read(&size, sizeof(int));
421         SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
422
423         ret = pFile->Read(&readDouble, size);
424         SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
425
426         value = readDouble;
427
428         return r;
429 }
430
431 result
432 _DataControlResultSetEnumerator::GetStringAt(int columnIndex, String& value) const
433 {
434         int type = -1;
435         int size = 0;
436         int i = 0;
437         result r = E_SUCCESS;
438         int ret = 0;
439
440         //SysTryReturnResult(NID_IO, __pFile != null, E_INVALID_STATE,
441         //              "This instance has not been properly constructed yet or has already been finalized.");
442
443         SysTryReturnResult(NID_IO, __columnCount > 0, E_INVALID_STATE,
444                                 "This instance has not been properly constructed yet or has already been finalized.");
445
446         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
447                                 "columnIndex is out of range");
448
449         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
450
451         SysTryReturnResult(NID_IO, pFile != null, E_INVALID_STATE,
452                                 "This instance has not been properly constructed yet or has already been finalized.");
453
454         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __currentOffset);
455         SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
456
457         for (i = 0; i < columnIndex; i++) // move to column index
458         {
459                 ret = pFile->Read(&type, sizeof(int));
460                 if (ret == 0)
461                 {
462                         r = GetLastResult();
463                         SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "The columnIndex is out of range.");
464                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
465                 }
466                 ret = pFile->Read(&size, sizeof(int));
467                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
468
469                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, size);
470                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
471         }
472
473         ret = pFile->Read(&type, sizeof(int));
474         if (ret == 0)
475         {
476                 r = GetLastResult();
477                 SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "The columnIndex is out of range.");
478                 SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
479         }
480
481         SysTryReturnResult(NID_IO, type == DB_COLUMNTYPE_TEXT, E_TYPE_MISMATCH,
482                                           "Trying to access column of different type.");
483
484         ret = pFile->Read(&size, sizeof(int));
485         SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
486
487         if (size > 0)
488         {
489                 unique_ptr<char[]> pReadBuffer(new (std::nothrow) char[size + 1]);
490                 SysTryReturnResult(NID_IO, pReadBuffer != null, E_OUT_OF_MEMORY, "Failed to allocate memory for the read buffer.");
491
492                 memset(pReadBuffer.get(), 0, size + 1);
493
494                 ret = pFile->Read(pReadBuffer.get(), size);
495                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
496                 value = String(pReadBuffer.get());
497         }
498
499         return E_SUCCESS;
500 }
501
502 result
503 _DataControlResultSetEnumerator::GetBlobAt(int columnIndex, ByteBuffer& value) const
504 {
505         int type = -1;
506         int size = 0;
507         int i = 0;
508         result r = E_SUCCESS;
509         int ret = 0;
510
511         //SysTryReturnResult(NID_IO, __pFile != null, E_INVALID_STATE,
512         //              "This instance has not been properly constructed yet or has already been finalized.");
513
514         SysTryReturnResult(NID_IO, __columnCount > 0, E_INVALID_STATE,
515                                 "This instance has not been properly constructed yet or has already been finalized.");
516
517         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
518                                 "columnIndex is out of range");
519
520         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
521
522         SysTryReturnResult(NID_IO, pFile != null, E_INVALID_STATE,
523                                 "This instance has not been properly constructed yet or has already been finalized.");
524
525         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __currentOffset);
526         SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
527
528         for (i = 0; i < columnIndex; i++) // move to column index
529         {
530                 ret = pFile->Read(&type, sizeof(int));
531                 if (ret == 0)
532                 {
533                         r = GetLastResult();
534                         SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "The columnIndex is out of range.");
535                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
536                 }
537                 ret = pFile->Read(&size, sizeof(int));
538                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
539
540                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, size);
541                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
542         }
543
544         ret = pFile->Read(&type, sizeof(int));
545         if (ret == 0)
546         {
547                 r = GetLastResult();
548                 SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "The columnIndex is out of range.");
549                 SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
550         }
551         SysTryReturnResult(NID_IO, type == DB_COLUMNTYPE_BLOB, E_TYPE_MISMATCH,
552                                           "Trying to access column of different type.");
553
554         ret = pFile->Read(&size, sizeof(int));
555         SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
556
557         if (size > 0)
558         {
559                 unique_ptr<byte[]> pReadBuffer(new (std::nothrow) byte[size + 1]);
560                 SysTryReturnResult(NID_IO, pReadBuffer != null, E_OUT_OF_MEMORY, "Failed to allocate memory for the read buffer.");
561
562                 memset(pReadBuffer.get(), 0, size + 1);
563                 ret = pFile->Read(pReadBuffer.get(), size);
564                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
565
566                 r = value.SetArray(pReadBuffer.get(), 0, size);
567                 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
568         }
569
570         return E_SUCCESS;
571 }
572
573 result
574 _DataControlResultSetEnumerator::GetBlobAt(int columnIndex, void* buffer, int bufSize) const
575 {
576         int type = -1;
577         int size = 0;
578         int i = 0;
579         result r = E_SUCCESS;
580         int ret = 0;
581
582         //SysTryReturnResult(NID_IO, __pFile != null, E_INVALID_STATE,
583         //              "This instance has not been properly constructed yet or has already been finalized.");
584
585         SysTryReturnResult(NID_IO, __columnCount > 0, E_INVALID_STATE,
586                                 "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
587
588         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
589                                 "columnIndex is out of range");
590
591         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
592
593         SysTryReturnResult(NID_IO, pFile != null, E_INVALID_STATE,
594                                 "This instance has not been properly constructed yet or has already been finalized.");
595
596         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __currentOffset);
597         SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
598
599         for (i = 0; i < columnIndex; i++) // move to column index
600         {
601                 ret = pFile->Read(&type, sizeof(int));
602                 if (ret == 0)
603                 {
604                         r = GetLastResult();
605                         SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "The columnIndex is out of range.");
606                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
607                 }
608                 ret = pFile->Read(&size, sizeof(int));
609                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
610
611                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, size);
612                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
613         }
614
615         ret = pFile->Read(&type, sizeof(int));
616         if (ret == 0)
617         {
618                 r = GetLastResult();
619                 SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "The columnIndex is out of range.");
620                 SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
621         }
622         SysTryReturnResult(NID_IO, type == DB_COLUMNTYPE_BLOB, E_TYPE_MISMATCH,
623                                           "Trying to access column of different type.");
624
625         ret = pFile->Read(&size, sizeof(int));
626         SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
627
628         SysTryReturnResult(NID_IO, size <= bufSize, E_OVERFLOW, "The output buffer is insufficient.");
629
630         if (size > 0)
631         {
632                 unique_ptr<byte[]> pReadBuffer(new (std::nothrow) byte[size + 1]);
633                 SysTryReturnResult(NID_IO, pReadBuffer != null, E_OUT_OF_MEMORY, "Failed to allocate memory for the read buffer.");
634
635                 memset(pReadBuffer.get(), 0, size + 1);
636                 ret = pFile->Read(pReadBuffer.get(), size);
637                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
638
639                 memcpy(buffer, pReadBuffer.get(), size);
640         }
641
642         return E_SUCCESS;
643 }
644
645 result
646 _DataControlResultSetEnumerator::GetDateTimeAt(int columnIndex, DateTime& value) const
647 {
648         int type = -1;
649         int size = 0;
650         char* pReadBuffer = null;
651         int i = 0;
652         result r = E_SUCCESS;
653         int ret = 0;
654
655         //SysTryReturnResult(NID_IO, __pFile != null, E_INVALID_STATE,
656         //              "This instance has not been properly constructed yet or has already been finalized.");
657
658         SysTryReturnResult(NID_IO, __columnCount > 0, E_INVALID_STATE,
659                                 "This instance has not been properly constructed yet or has already been finalized.");
660
661         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
662                                 "columnIndex is out of range");
663
664         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
665
666         SysTryReturnResult(NID_IO, pFile != null, E_INVALID_STATE,
667                                 "This instance has not been properly constructed yet or has already been finalized.");
668
669         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __currentOffset);
670         SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
671
672         for (i = 0; i < columnIndex; i++) // move to column index
673         {
674                 ret = pFile->Read(&type, sizeof(int));
675                 if (ret == 0)
676                 {
677                         r = GetLastResult();
678                         SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "columnIndex is out of range");
679                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
680                 }
681                 ret = pFile->Read(&size, sizeof(int));
682                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
683
684                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, size);
685                 SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "Failed to seek.");
686         }
687
688         ret = pFile->Read(&type, sizeof(int));
689         if (ret == 0)
690         {
691                 r = GetLastResult();
692                 SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "columnIndex is out of range");
693                 SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
694         }
695         SysTryReturnResult(NID_IO, type == DB_COLUMNTYPE_TEXT, E_TYPE_MISMATCH,
696                                           "Trying to access column of different type.");
697
698         ret = pFile->Read(&size, sizeof(int));
699         if (ret == 0)
700         {
701                 r = GetLastResult();
702                 SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "columnIndex is out of range");
703                 SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
704         }
705
706         if (size > 0)
707         {
708                 pReadBuffer = (char*) malloc(size + 1);
709                 SysTryReturn(NID_IO, pReadBuffer != NULL, E_SYSTEM, E_SYSTEM, "[E_OUT_OF_MEMORY] system error occurred.");
710                 memset(pReadBuffer, 0, size + 1);
711
712                 ret = pFile->Read(pReadBuffer, size);
713                 if (ret == 0)
714                 {
715                         r = GetLastResult();
716                         free(pReadBuffer);
717                         SysTryReturnResult(NID_IO, !(r == E_END_OF_FILE), E_INVALID_ARG, "columnIndex is out of range");
718                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
719                 }
720
721                 r = DateTime::Parse(pReadBuffer, value);
722                 free(pReadBuffer);
723         }
724
725         return r;
726 }
727
728 int
729 _DataControlResultSetEnumerator::GetColumnCount(void) const
730 {
731         //SysTryReturn(NID_IO, __pFile != null, -1, E_INVALID_STATE,
732         //              "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
733
734         SysTryReturn(NID_IO, __columnCount > 0, -1, E_INVALID_STATE,
735                                 "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
736
737         SetLastResult(E_SUCCESS);
738         return __columnCount;
739 }
740
741 DbColumnType
742 _DataControlResultSetEnumerator::GetColumnType(int columnIndex) const
743 {
744         DbColumnType columnType = DB_COLUMNTYPE_UNDEFINED;
745         int type = -1;
746         int i = 0;
747         int size = 0;
748         result r = E_SUCCESS;
749         int ret = 0;
750
751         //SysTryReturn(NID_IO, __pFile != null, columnType, E_INVALID_STATE,
752         //              "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
753
754         SysTryReturn(NID_IO, __columnCount > 0, columnType, E_INVALID_STATE,
755                                 "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
756
757         SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, columnType, E_INVALID_ARG,
758                                 "[E_INVALID_ARG] columnIndex is out of range");
759
760         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
761
762         SysTryReturn(NID_IO, pFile != null, columnType, E_INVALID_STATE,
763                                 "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
764
765         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __currentOffset);
766         SysTryReturn(NID_IO, !IsFailed(r), columnType, E_SYSTEM, "[E_SYSTEM] Failed to seek.");
767
768         for (i = 0; i < columnIndex; i++) // move to column index
769         {
770                 ret = pFile->Read(&type, sizeof(int));
771                 if (ret == 0)
772                 {
773                         r = GetLastResult();
774                         SysTryReturn(NID_IO, !(r == E_END_OF_FILE), columnType, E_INVALID_ARG, "[E_INVALID_ARG] The columnIndex is out of range.");
775                         SysTryReturn(NID_IO, false, columnType, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
776                 }
777                 ret = pFile->Read(&size, sizeof(int));
778                 SysTryReturn(NID_IO, ret, columnType, E_SYSTEM, "[E_SYSTEM] Failed to read data from the result set of the data control provider.");
779
780                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, size);
781                 SysTryReturn(NID_IO, !IsFailed(r), columnType, E_SYSTEM, "[E_SYSTEM] Failed to seek.");
782         }
783
784         ret = pFile->Read(&type, sizeof(int));
785         if (ret == 0)
786         {
787                 r = GetLastResult();
788                 SysTryReturn(NID_IO, !(r == E_END_OF_FILE), DB_COLUMNTYPE_UNDEFINED, E_INVALID_ARG,
789                                         "[E_INVALID_ARG] columnIndex is out of range");
790                 SysTryReturn(NID_IO, false, DB_COLUMNTYPE_UNDEFINED, E_SYSTEM,
791                                         "[%s] system error, errno: %d", GetErrorMessage(r), errno);
792         }
793
794         SetLastResult(E_SUCCESS);
795
796         switch (type)
797         {
798         case 1: // int64
799                 columnType = DB_COLUMNTYPE_INT64;
800                 break;
801
802         case 2: // double
803                 columnType = DB_COLUMNTYPE_DOUBLE;
804                 break;
805
806         case 3: // text
807                 columnType = DB_COLUMNTYPE_TEXT;
808                 break;
809
810         case 4: // blob
811                 columnType = DB_COLUMNTYPE_BLOB;
812                 break;
813
814         case 5: // null
815                 columnType = DB_COLUMNTYPE_NULL;
816                 break;
817
818         default:
819                 SysLogException(NID_IO, E_SYSTEM, "Found undefined column type (%d).", type);
820                 break;
821         }
822
823         return columnType;
824 }
825
826 String
827 _DataControlResultSetEnumerator::GetColumnName(int columnIndex) const
828 {
829         String columnName;
830         int i = 0;
831         result r = E_SUCCESS;
832
833         //SysTryReturn(NID_IO, __pFile != null, columnName, E_INVALID_STATE,
834         //              "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
835
836         SysTryReturn(NID_IO, __columnCount > 0, columnName, E_INVALID_STATE,
837                                 "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
838
839         SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, columnName, E_INVALID_ARG,
840                                 "[E_INVALID_ARG] columnIndex is out of range");
841
842         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
843
844         SysTryReturn(NID_IO, pFile != null, columnName, E_INVALID_STATE,
845                                 "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
846
847         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __columnNameOffset);
848         SysTryReturn(NID_IO, !IsFailed(r), columnName, E_SYSTEM, "Failed to seek.");
849         for (i = 0; i < columnIndex + 1; i++)
850         {
851                 r = pFile->Read(columnName);
852                 if (IsFailed(r))
853                 {
854                         SysTryReturn(NID_IO, !(r == E_END_OF_FILE), L"", E_INVALID_ARG, "[E_INVALID_ARG] columnIndex is out of range");
855                         SysTryReturn(NID_IO, false, L"", E_SYSTEM, "[%s] system error", GetErrorMessage(r));
856                 }
857         }
858         columnName.SetLength(columnName.GetLength() - 1);
859
860         SetLastResult(E_SUCCESS);
861         return columnName;
862 }
863
864 int
865 _DataControlResultSetEnumerator::GetColumnSize(int columnIndex) const
866 {
867         //SysTryReturn(NID_IO, __pFile != null, -1, E_INVALID_STATE,
868         //              "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
869
870         SysTryReturn(NID_IO, __columnCount > 0, -1, E_INVALID_STATE,
871                                 "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
872
873         SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG, E_INVALID_ARG,
874                                 "[E_INVALID_ARG] columnIndex is out of range");
875
876         int type = -1;
877         int size = 0;
878         int i = 0;
879         int ret = 0;
880         result r = E_SUCCESS;
881
882         _FileImpl* pFile = _FileImpl::GetInstance(*(static_cast< File* >(__pFile)));
883
884         SysTryReturn(NID_IO, pFile != null, -1, E_INVALID_STATE,
885                                 "[E_INVALID_STATE] This instance has not been properly constructed yet or has already been finalized.");
886
887         r = pFile->Seek(FILESEEKPOSITION_BEGIN, __currentOffset);
888         SysTryReturn(NID_IO, !IsFailed(r), -1, E_SYSTEM, "Failed to seek.");
889
890         for (i = 0; i < columnIndex; i++) // move to column index
891         {
892                 ret = pFile->Read(&type, sizeof(int));
893                 if (ret == 0)
894                 {
895                         r = GetLastResult();
896                         SysTryReturn(NID_IO, !(r == E_END_OF_FILE), -1, E_INVALID_ARG, "[E_INVALID_ARG] columnIndex is out of range");
897                         SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
898                 }
899                 ret = pFile->Read(&size, sizeof(int));
900                 SysTryReturn(NID_IO, ret, -1, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
901
902                 r = pFile->Seek(FILESEEKPOSITION_CURRENT, size);
903                 SysTryReturn(NID_IO, !IsFailed(r), -1, E_SYSTEM, "Failed to seek.");
904         }
905
906         ret = pFile->Read(&type, sizeof(int));
907         if (ret == 0)
908         {
909                 r = GetLastResult();
910                 SysTryReturn(NID_IO, !(r == E_END_OF_FILE), -1, E_INVALID_ARG, "[E_INVALID_ARG] columnIndex is out of range");
911                 SysTryReturn(NID_IO, false, E_SYSTEM, E_SYSTEM, "[%s] system error", GetErrorMessage(r));
912         }
913         ret = pFile->Read(&size, sizeof(int));
914         SysTryReturn(NID_IO, ret, -1, E_SYSTEM, "Failed to read data from the result set of the data control provider.");
915
916         SetLastResult(E_SUCCESS);
917         return size;
918 }
919
920 }} // Tizen::Io
921