Merge "Fix System Cache issue" into tizen_2.1
[platform/framework/native/appfw.git] / src / io / FIo_DataSetEnumeratorImpl.cpp
1 //
2 // Open Service Platform
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 /**
19  * @file        FIo_DataSetEnumeratorImpl.cpp
20  * @brief       This is the implementation file for _DataSetEnumeratorImpl class.
21  */
22
23 #include <stdio.h>
24 #include <semaphore.h>
25 #include <errno.h>
26 #include <unique_ptr.h>
27
28 #include <FBaseInteger.h>
29 #include <FBaseDouble.h>
30 #include <FBaseString.h>
31 #include <FBaseByteBuffer.h>
32 #include <FBaseDateTime.h>
33 #include <FBaseUtilStringUtil.h>
34 #include <FBaseColLinkedList.h>
35 #include <FBaseColArrayList.h>
36 #include <FBaseColIEnumerator.h>
37 #include <FBaseSysLog.h>
38
39 #include <FIoDbTypes.h>
40 #include <FIo_DataSetEnumeratorImpl.h>
41 #include <FIo_DataRowImpl.h>
42
43 using namespace std;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Utility;
46 using namespace Tizen::Base::Runtime;
47 using namespace Tizen::System;
48 using namespace Tizen::App;
49 using namespace Tizen::Base::Collection;
50
51 namespace Tizen { namespace Io
52 {
53
54 _DataSetEnumeratorImpl::_DataSetEnumeratorImpl(void)
55         : __pDataSet(null),
56         __pColumnList(null),
57         __pCurrentRow(null),
58         __columnCount(0),
59         __rowCount(0),
60         __currentRowIndex(-1)
61 {
62
63 }
64
65 _DataSetEnumeratorImpl::~_DataSetEnumeratorImpl(void)
66 {
67
68 }
69
70 result
71 _DataSetEnumeratorImpl::MoveNext(void)
72 {
73         if (__currentRowIndex +1  == __rowCount)
74                 return E_OUT_OF_RANGE;
75
76         ++__currentRowIndex;
77         __pCurrentRow = static_cast<ArrayList*>(__pDataSet->GetAt(__currentRowIndex));
78
79         if (__pCurrentRow == null)
80                 return E_INVALID_STATE;
81
82         return E_SUCCESS;
83 }
84
85 result
86 _DataSetEnumeratorImpl::MovePrevious(void)
87 {
88         if (__currentRowIndex == 0)
89                 return E_OUT_OF_RANGE;
90
91         --__currentRowIndex;
92         __pCurrentRow = static_cast<ArrayList*>(__pDataSet->GetAt(__currentRowIndex));
93
94         if (__pCurrentRow == null)
95                 return E_INVALID_STATE;
96
97         return E_SUCCESS;
98 }
99
100 result
101 _DataSetEnumeratorImpl::MoveFirst(void)
102 {
103         __currentRowIndex = 0;
104         __pCurrentRow = static_cast<ArrayList*>(__pDataSet->GetAt(__currentRowIndex));
105
106         if (__pCurrentRow == null)
107                 return E_INVALID_STATE;
108
109         return E_SUCCESS;
110 }
111
112 result
113 _DataSetEnumeratorImpl::MoveLast(void)
114 {
115         __currentRowIndex = __rowCount -1;
116         __pCurrentRow = static_cast<ArrayList*>(__pDataSet->GetAt(__currentRowIndex));
117
118         if (__pCurrentRow == null)
119                 return E_INVALID_STATE;
120
121         return E_SUCCESS;
122 }
123
124 result
125 _DataSetEnumeratorImpl::Reset(void)
126 {
127         __currentRowIndex = -1;
128         __pCurrentRow = null;
129
130         return E_SUCCESS;
131 }
132
133 result
134 _DataSetEnumeratorImpl::GetIntAt(int columnIndex, int& value) const
135 {
136         result r = E_SUCCESS;
137
138         SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
139                                 "The Object is not constructed or the dataset is already been deleted.");
140         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
141                                 "Given column index is out of range.");
142         SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
143                                 "The method has tried to fetch the column data of a result set that is not activated.");
144
145         _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
146         if (pDataItem)
147         {
148                 if (pDataItem->type != DB_COLUMNTYPE_INT)
149                 {
150                         r = E_TYPE_MISMATCH;
151                         SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
152                         goto CATCH;
153                 }
154
155                 value = pDataItem->intValue;
156         }
157         return E_SUCCESS;
158
159         CATCH:
160                 return r;
161 }
162
163 result
164 _DataSetEnumeratorImpl::GetInt64At(int columnIndex, long long& value) const
165 {
166         result r = E_SUCCESS;
167
168         SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
169                                 "The Object is not constructed or 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.");
174
175         _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
176         if (pDataItem)
177         {
178                 if (pDataItem->type != DB_COLUMNTYPE_INT64)
179                 {
180                         r = E_TYPE_MISMATCH;
181                         SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
182                         goto CATCH;
183                 }
184
185                 value = pDataItem->int64Value;
186         }
187         return E_SUCCESS;
188
189 CATCH:
190         return r;
191 }
192
193 result
194 _DataSetEnumeratorImpl::GetDoubleAt(int columnIndex, double& value) const
195 {
196         result r = E_SUCCESS;
197
198         SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
199                                 "The Object is not constructed or the dataset is already been deleted.");
200         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
201                                 "Given column index is out of range.");
202         SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
203                                 "The method has tried to fetch the column data of a result set that is not activated.");
204
205         _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
206         if (pDataItem)
207         {
208                 if (pDataItem->type != DB_COLUMNTYPE_DOUBLE)
209                 {
210                         r = E_TYPE_MISMATCH;
211                         SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
212                         goto CATCH;
213                 }
214
215                 value = pDataItem->doubleValue;
216         }
217         return E_SUCCESS;
218
219 CATCH:
220         return r;
221 }
222
223 result
224 _DataSetEnumeratorImpl::GetStringAt(int columnIndex, String& value) const
225 {
226         result r = E_SUCCESS;
227
228         SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
229                                 "The Object is not constructed or the dataset is already been deleted.");
230         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
231                                 "Given column index is out of range.");
232         SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
233                                 "The method has tried to fetch the column data of a result set that is not activated.");
234
235         _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
236         if (pDataItem)
237         {
238                 if (pDataItem->type != DB_COLUMNTYPE_TEXT)
239                 {
240                         r = E_TYPE_MISMATCH;
241                         SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
242                         goto CATCH;
243                 }
244
245                 value = *((String*)pDataItem->pObj);
246         }
247
248 CATCH:
249         return r;
250 }
251
252 result
253 _DataSetEnumeratorImpl::GetBlobAt(int columnIndex, ByteBuffer& value) const
254 {
255         result r = E_SUCCESS;
256         ByteBuffer* pBuffer = null;
257
258         SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
259                                 "The Object is not constructed or the dataset is already been deleted.");
260         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
261                                 "Given column index is out of range.");
262         SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
263                                 "The method has tried to fetch the column data of a result set that is not activated.");
264
265         _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
266         if (pDataItem)
267         {
268                 if (pDataItem->type != DB_COLUMNTYPE_BLOB)
269                 {
270                         r = E_TYPE_MISMATCH;
271                         SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
272                         goto CATCH;
273                 }
274
275                 pBuffer = (ByteBuffer*)pDataItem->pObj;
276                 pBuffer->SetPosition(0);
277                 r = value.CopyFrom(*pBuffer);
278         }
279
280         // fall thru
281 CATCH:
282         return r;
283 }
284
285 result
286 _DataSetEnumeratorImpl::GetBlobAt(int columnIndex, void* buffer, int size) const
287 {
288         result r = E_SUCCESS;
289         int blobLen = 0;
290         ByteBuffer* pBuffer = null;
291
292         SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
293                                 "The Object is not constructed or the dataset is already been deleted.");
294         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
295                                 "Given column index is out of range.");
296         SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
297                                 "The method has tried to fetch the column data of a result set that is not activated.");
298
299         _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
300         if (pDataItem)
301         {
302                 if (pDataItem->type != DB_COLUMNTYPE_BLOB)
303                 {
304                         r = E_TYPE_MISMATCH;
305                         SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
306                         goto CATCH;
307                 }
308
309                 pBuffer = (ByteBuffer*)pDataItem->pObj;
310                 pBuffer->SetPosition(0);
311                 blobLen = pBuffer->GetLimit();
312                 memcpy(buffer, pBuffer->GetPointer(), (blobLen < size) ? blobLen : size);
313         }
314
315         if (size < blobLen)
316         {
317                 r = E_OVERFLOW;
318         }
319
320         // fall thru
321 CATCH:
322         return r;
323 }
324
325 result
326 _DataSetEnumeratorImpl::GetDateTimeAt(int columnIndex, DateTime& value) const
327 {
328         result r = E_SUCCESS;
329         String* pStr = null;
330
331         SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
332                                 "The instance is not constructed or the dataset is already been deleted.");
333         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
334                                 "Given column index is out of range.");
335         SysTryReturnResult(NID_IO, __pCurrentRow != null, E_INVALID_STATE,
336                                 "The method has tried to fetch the column data of a result set that is not activated.");
337
338         _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
339         if (pDataItem)
340         {
341                 if (pDataItem->type != DB_COLUMNTYPE_TEXT)
342                 {
343                         r = E_TYPE_MISMATCH;
344                         SysLog(NID_IO, "[E_TYPE_MISMATCH] Trying to access column of different type.");
345                         goto CATCH;
346                 }
347
348                 pStr = (String*)pDataItem->pObj;
349                 r = DateTime::Parse(*pStr, value);
350         }
351
352         // fall thru
353 CATCH:
354         return r;
355 }
356
357 int
358 _DataSetEnumeratorImpl::GetColumnCount(void) const
359 {
360         SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
361                                 "The Object is not constructed or the dataset is already been deleted.");
362
363         return __columnCount;
364 }
365
366 DbColumnType
367 _DataSetEnumeratorImpl::GetColumnType(int columnIndex) const
368 {
369         DbColumnType type = DB_COLUMNTYPE_UNDEFINED;
370
371         SysTryReturn(NID_IO, __pDataSet != null, DB_COLUMNTYPE_UNDEFINED, E_INVALID_STATE,
372                                 "[E_INVALID_STATE] The instance is not constructed or the dataset is already been deleted.");
373
374         SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, DB_COLUMNTYPE_UNDEFINED, E_INVALID_ARG,
375                                 "[E_INVALID_ARG] Given column index is out of range.");
376
377         if (__pCurrentRow)
378         {
379                 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
380                 if (!pDataItem)
381                 {
382                         SetLastResult(E_INVALID_STATE);
383                         return DB_COLUMNTYPE_UNDEFINED;
384                 }
385
386                 type = pDataItem->type;
387                 switch (type)
388                 {
389                 case DB_COLUMNTYPE_INT:
390                 case DB_COLUMNTYPE_DOUBLE:
391                 case DB_COLUMNTYPE_TEXT:
392                 case DB_COLUMNTYPE_BLOB:
393                 case DB_COLUMNTYPE_NULL:
394                         type = pDataItem->type;
395                         break;
396
397                 default:
398                         SetLastResult(E_INVALID_STATE);
399                         break;
400                 }
401         }
402
403         return type;
404 }
405
406 String
407 _DataSetEnumeratorImpl::GetColumnName(int columnIndex) const
408 {
409         SysTryReturn(NID_IO, __pDataSet != null, null, E_INVALID_STATE,
410                                 "[E_INVALID_STATE] The instance is not constructed or the dataset is already been deleted.");
411
412         SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, null, E_INVALID_ARG,
413                                 "[E_INVALID_ARG] Given column index is out of range.");
414
415
416         String* pString = dynamic_cast <String *> (__pColumnList->GetAt(columnIndex));
417
418         if (!pString)
419         {
420                 SetLastResult(E_INVALID_STATE);
421                 return String("");
422         }
423
424         return String(pString->GetPointer());
425 }
426
427 int
428 _DataSetEnumeratorImpl::GetColumnSize(int columnIndex) const
429 {
430         int bytes = 0;
431
432         SysTryReturn(NID_IO, __pDataSet != null, 0, E_INVALID_STATE,
433                                 "[E_INVALID_STATE] The instance is not constructed or the dataset is already been deleted.");
434
435         SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, 0, E_INVALID_ARG,
436                                 "[E_INVALID_ARG] Given column index is out of range.");
437
438         SysTryReturn(NID_IO, __pCurrentRow != null, 0, E_INVALID_STATE,
439                                 "[E_INVALID_STATE] The method has tried to fetch the column data of a result set that is not activated.");
440
441         _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
442         if (!pDataItem)
443         {
444                 SetLastResult(E_INVALID_STATE);
445                 return 0;
446         }
447
448         bytes = pDataItem->size;
449         //SysLog(NID_IO, "Size is %d", bytes);
450
451         return bytes;
452 }
453
454 _DataSetEnumeratorImpl*
455 _DataSetEnumeratorImpl::GetInstance(DataSetEnumerator& dataSetEnumerator)
456 {
457         return dataSetEnumerator.__pDataSetEnumeratorImpl;
458 }
459
460 const _DataSetEnumeratorImpl*
461 _DataSetEnumeratorImpl::GetInstance(const DataSetEnumerator& dataSetEnumerator)
462 {
463         return dataSetEnumerator.__pDataSetEnumeratorImpl;
464 }
465
466 DataSetEnumerator*
467 _DataSetEnumeratorImpl::CreateDataSetEnumeratorInstanceN(void)
468 {
469         unique_ptr<DataSetEnumerator> pDataSetEnumerator(new (std::nothrow) DataSetEnumerator());
470         SysTryReturn(NID_IO, pDataSetEnumerator != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
471         return pDataSetEnumerator.release();
472 }
473
474 }} // Tizen::Io
475