2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_DataSetEnumeratorImpl.cpp
19 * @brief This is the implementation file for _DataSetEnumeratorImpl class.
23 #include <semaphore.h>
25 #include <unique_ptr.h>
27 #include <FBaseInteger.h>
28 #include <FBaseDouble.h>
29 #include <FBaseString.h>
30 #include <FBaseByteBuffer.h>
31 #include <FBaseDateTime.h>
32 #include <FBaseUtilStringUtil.h>
33 #include <FBaseColLinkedList.h>
34 #include <FBaseColArrayList.h>
35 #include <FBaseColIEnumerator.h>
36 #include <FBaseSysLog.h>
38 #include <FIoDbTypes.h>
39 #include <FIo_DataSetEnumeratorImpl.h>
40 #include <FIo_DataRowImpl.h>
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Utility;
45 using namespace Tizen::Base::Runtime;
46 using namespace Tizen::System;
47 using namespace Tizen::App;
48 using namespace Tizen::Base::Collection;
50 namespace Tizen { namespace Io
53 _DataSetEnumeratorImpl::_DataSetEnumeratorImpl(void)
59 , __currentRowIndex(-1)
60 , __dataSetDeleted(false)
65 _DataSetEnumeratorImpl::~_DataSetEnumeratorImpl(void)
67 if (!__dataSetDeleted)
69 __pEnumImplList->Remove(*this);
74 _DataSetEnumeratorImpl::MoveNext(void)
76 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
77 "The Object is not constructed.");
78 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
79 "The dataset is already been deleted.");
81 if (__currentRowIndex +1 == __rowCount)
82 return E_OUT_OF_RANGE;
85 __pCurrentRow = static_cast<ArrayList*>(__pDataSet->GetAt(__currentRowIndex));
87 if (__pCurrentRow == null)
88 return E_INVALID_STATE;
94 _DataSetEnumeratorImpl::MovePrevious(void)
96 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
97 "The Object is not constructed.");
98 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
99 "The dataset is already been deleted.");
101 if (__currentRowIndex == 0)
102 return E_OUT_OF_RANGE;
105 __pCurrentRow = static_cast<ArrayList*>(__pDataSet->GetAt(__currentRowIndex));
107 if (__pCurrentRow == null)
108 return E_INVALID_STATE;
114 _DataSetEnumeratorImpl::MoveFirst(void)
116 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
117 "The Object is not constructed.");
118 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
119 "The dataset is already been deleted.");
121 __currentRowIndex = 0;
122 __pCurrentRow = static_cast<ArrayList*>(__pDataSet->GetAt(__currentRowIndex));
124 if (__pCurrentRow == null)
125 return E_INVALID_STATE;
131 _DataSetEnumeratorImpl::MoveLast(void)
133 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
134 "The Object is not constructed.");
135 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
136 "The dataset is already been deleted.");
138 __currentRowIndex = __rowCount -1;
139 __pCurrentRow = static_cast<ArrayList*>(__pDataSet->GetAt(__currentRowIndex));
141 if (__pCurrentRow == null)
142 return E_INVALID_STATE;
148 _DataSetEnumeratorImpl::Reset(void)
150 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
151 "The Object is not constructed.");
152 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
153 "The dataset is already been deleted.");
155 __currentRowIndex = -1;
156 __pCurrentRow = null;
162 _DataSetEnumeratorImpl::GetIntAt(int columnIndex, int& value) const
164 result r = E_SUCCESS;
166 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
167 "The Object is not constructed.");
168 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
169 "The dataset is already been deleted.");
170 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
171 "Given column index is out of range.");
172 SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
173 "The method has tried to fetch the column data of a result set that is not activated.");
175 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
178 if (pDataItem->type != DB_COLUMNTYPE_INT)
181 SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
185 value = pDataItem->intValue;
194 _DataSetEnumeratorImpl::GetInt64At(int columnIndex, long long& value) const
196 result r = E_SUCCESS;
198 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
199 "The Object is not constructed.");
200 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
201 "The dataset is already been deleted.");
202 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
203 "Given column index is out of range.");
204 SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
205 "The method has tried to fetch the column data of a result set that is not activated.");
207 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
210 if (pDataItem->type != DB_COLUMNTYPE_INT64)
213 SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
217 value = pDataItem->int64Value;
226 _DataSetEnumeratorImpl::GetDoubleAt(int columnIndex, double& value) const
228 result r = E_SUCCESS;
230 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
231 "The Object is not constructed.");
232 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
233 "The dataset is already been deleted.");
234 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
235 "Given column index is out of range.");
236 SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
237 "The method has tried to fetch the column data of a result set that is not activated.");
239 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
242 if (pDataItem->type != DB_COLUMNTYPE_DOUBLE)
245 SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
249 value = pDataItem->doubleValue;
258 _DataSetEnumeratorImpl::GetStringAt(int columnIndex, String& value) const
260 result r = E_SUCCESS;
262 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
263 "The Object is not constructed.");
264 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
265 "The dataset is already been deleted.");
266 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
267 "Given column index is out of range.");
268 SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
269 "The method has tried to fetch the column data of a result set that is not activated.");
271 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
274 if (pDataItem->type != DB_COLUMNTYPE_TEXT)
277 SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
281 value = *((String*)pDataItem->pObj);
289 _DataSetEnumeratorImpl::GetBlobAt(int columnIndex, ByteBuffer& value) const
291 result r = E_SUCCESS;
292 ByteBuffer* pBuffer = null;
294 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
295 "The Object is not constructed.");
296 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
297 "The dataset is already been deleted.");
298 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
299 "Given column index is out of range.");
300 SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
301 "The method has tried to fetch the column data of a result set that is not activated.");
303 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
306 if (pDataItem->type != DB_COLUMNTYPE_BLOB)
309 SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
313 pBuffer = (ByteBuffer*)pDataItem->pObj;
314 pBuffer->SetPosition(0);
315 r = value.CopyFrom(*pBuffer);
324 _DataSetEnumeratorImpl::GetBlobAt(int columnIndex, void* buffer, int size) const
326 result r = E_SUCCESS;
328 ByteBuffer* pBuffer = null;
330 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
331 "The Object is not constructed.");
332 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
333 "The dataset is already been deleted.");
334 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
335 "Given column index is out of range.");
336 SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
337 "The method has tried to fetch the column data of a result set that is not activated.");
339 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
342 if (pDataItem->type != DB_COLUMNTYPE_BLOB)
345 SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
349 pBuffer = (ByteBuffer*)pDataItem->pObj;
350 pBuffer->SetPosition(0);
351 blobLen = pBuffer->GetLimit();
352 memcpy(buffer, pBuffer->GetPointer(), (blobLen < size) ? blobLen : size);
366 _DataSetEnumeratorImpl::GetDateTimeAt(int columnIndex, DateTime& value) const
368 result r = E_SUCCESS;
371 SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
372 "The Object is not constructed.");
373 SysTryReturnResult(NID_IO, __dataSetDeleted != true, E_INVALID_STATE,
374 "The dataset is already been deleted.");
375 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
376 "Given column index is out of range.");
377 SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
378 "The method has tried to fetch the column data of a result set that is not activated.");
380 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
383 if (pDataItem->type != DB_COLUMNTYPE_TEXT)
386 SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
390 pStr = (String*)pDataItem->pObj;
391 r = DateTime::Parse(*pStr, value);
400 _DataSetEnumeratorImpl::GetColumnCount(void) const
402 SysTryReturn(NID_IO, __pDataSet != null, -1, E_INVALID_STATE,
403 "[E_INVALID_STATE] The Object is not constructed.");
404 SysTryReturn(NID_IO, __dataSetDeleted != true, -1, E_INVALID_STATE,
405 "[E_INVALID_STATE] The dataset is already been deleted.");
407 return __columnCount;
411 _DataSetEnumeratorImpl::GetColumnType(int columnIndex) const
413 DbColumnType type = DB_COLUMNTYPE_UNDEFINED;
415 SysTryReturn(NID_IO, __pDataSet != null, DB_COLUMNTYPE_UNDEFINED, E_INVALID_STATE,
416 "[E_INVALID_STATE] The instance is not constructed.");
417 SysTryReturn(NID_IO, __dataSetDeleted != true, DB_COLUMNTYPE_UNDEFINED, E_INVALID_STATE,
418 "[E_INVALID_STATE] The dataset is already been deleted.");
419 SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, DB_COLUMNTYPE_UNDEFINED, E_INVALID_ARG,
420 "[E_INVALID_ARG] Given column index is out of range.");
424 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
427 SetLastResult(E_INVALID_STATE);
428 return DB_COLUMNTYPE_UNDEFINED;
431 switch (pDataItem->type)
433 case DB_COLUMNTYPE_INT:
434 case DB_COLUMNTYPE_INT64:
435 case DB_COLUMNTYPE_DOUBLE:
436 case DB_COLUMNTYPE_TEXT:
437 case DB_COLUMNTYPE_BLOB:
438 case DB_COLUMNTYPE_NULL:
439 type = pDataItem->type;
443 SetLastResult(E_INVALID_STATE);
452 _DataSetEnumeratorImpl::GetColumnName(int columnIndex) const
454 SysTryReturn(NID_IO, __pDataSet != null, null, E_INVALID_STATE,
455 "[E_INVALID_STATE] The instance is not constructed.");
456 SysTryReturn(NID_IO, __dataSetDeleted != true, null, E_INVALID_STATE,
457 "[E_INVALID_STATE] The dataset is already been deleted.");
458 SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, null, E_INVALID_ARG,
459 "[E_INVALID_ARG] Given column index is out of range.");
462 String* pString = dynamic_cast <String *> (__pColumnList->GetAt(columnIndex));
466 SetLastResult(E_INVALID_STATE);
470 return String(pString->GetPointer());
474 _DataSetEnumeratorImpl::GetColumnSize(int columnIndex) const
478 SysTryReturn(NID_IO, __pDataSet != null, -1, E_INVALID_STATE,
479 "[E_INVALID_STATE] The instance is not constructed.");
480 SysTryReturn(NID_IO, __dataSetDeleted != true, -1, E_INVALID_STATE,
481 "[E_INVALID_STATE] The dataset is already been deleted.");
482 SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, -1, E_INVALID_ARG,
483 "[E_INVALID_ARG] Given column index is out of range.");
484 SysTryReturn(NID_IO, __pCurrentRow != null, -1, E_INVALID_STATE,
485 "[E_INVALID_STATE] The method has tried to fetch the column data of a result set that is not activated.");
487 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
490 SetLastResult(E_INVALID_STATE);
494 bytes = pDataItem->size;
495 //SysLog(NID_IO, "Size is %d", bytes);
500 _DataSetEnumeratorImpl*
501 _DataSetEnumeratorImpl::GetInstance(DataSetEnumerator& dataSetEnumerator)
503 return dataSetEnumerator.__pDataSetEnumeratorImpl;
506 const _DataSetEnumeratorImpl*
507 _DataSetEnumeratorImpl::GetInstance(const DataSetEnumerator& dataSetEnumerator)
509 return dataSetEnumerator.__pDataSetEnumeratorImpl;
513 _DataSetEnumeratorImpl::CreateDataSetEnumeratorInstanceN(void)
515 unique_ptr<DataSetEnumerator> pDataSetEnumerator(new (std::nothrow) DataSetEnumerator());
516 SysTryReturn(NID_IO, pDataSetEnumerator != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
517 return pDataSetEnumerator.release();