Backout 128 limits in DataControl
[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         }
350
351         r = DateTime::Parse(*pStr, value);
352
353         // fall thru
354 CATCH:
355         return r;
356 }
357
358 int
359 _DataSetEnumeratorImpl::GetColumnCount(void) const
360 {
361         SysTryReturnResult(NID_IO, __pDataSet != null, E_INVALID_STATE,
362                                 "The Object is not constructed or the dataset is already been deleted.");
363
364         return __columnCount;
365 }
366
367 DbColumnType
368 _DataSetEnumeratorImpl::GetColumnType(int columnIndex) const
369 {
370         DbColumnType type = DB_COLUMNTYPE_UNDEFINED;
371
372         SysTryReturn(NID_IO, __pDataSet != null, DB_COLUMNTYPE_UNDEFINED, E_INVALID_STATE,
373                                 "[E_INVALID_STATE] The instance is not constructed or the dataset is already been deleted.");
374
375         SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, DB_COLUMNTYPE_UNDEFINED, E_INVALID_ARG,
376                                 "[E_INVALID_ARG] Given column index is out of range.");
377
378         if (__pCurrentRow)
379         {
380                 _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
381                 if (!pDataItem)
382                 {
383                         SetLastResult(E_INVALID_STATE);
384                         return DB_COLUMNTYPE_UNDEFINED;
385                 }
386
387                 type = pDataItem->type;
388                 switch (type)
389                 {
390                 case DB_COLUMNTYPE_INT:
391                 case DB_COLUMNTYPE_DOUBLE:
392                 case DB_COLUMNTYPE_TEXT:
393                 case DB_COLUMNTYPE_BLOB:
394                 case DB_COLUMNTYPE_NULL:
395                         type = pDataItem->type;
396                         break;
397
398                 default:
399                         SetLastResult(E_INVALID_STATE);
400                         break;
401                 }
402         }
403
404         return type;
405 }
406
407 String
408 _DataSetEnumeratorImpl::GetColumnName(int columnIndex) const
409 {
410         SysTryReturn(NID_IO, __pDataSet != null, null, E_INVALID_STATE,
411                                 "[E_INVALID_STATE] The instance is not constructed or the dataset is already been deleted.");
412
413         SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, null, E_INVALID_ARG,
414                                 "[E_INVALID_ARG] Given column index is out of range.");
415
416
417         String* pString = dynamic_cast <String *> (__pColumnList->GetAt(columnIndex));
418
419         if (!pString)
420         {
421                 SetLastResult(E_INVALID_STATE);
422                 return String("");
423         }
424
425         return String(pString->GetPointer());
426 }
427
428 int
429 _DataSetEnumeratorImpl::GetColumnSize(int columnIndex) const
430 {
431         int bytes = 0;
432
433         SysTryReturn(NID_IO, __pDataSet != null, 0, E_INVALID_STATE,
434                                 "[E_INVALID_STATE] The instance is not constructed or the dataset is already been deleted.");
435
436         SysTryReturn(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, 0, E_INVALID_ARG,
437                                 "[E_INVALID_ARG] Given column index is out of range.");
438
439         SysTryReturn(NID_IO, __pCurrentRow != null, 0, E_INVALID_STATE,
440                                 "[E_INVALID_STATE] The method has tried to fetch the column data of a result set that is not activated.");
441
442         _DataItem* pDataItem = dynamic_cast < _DataItem* >(__pCurrentRow->GetAt(columnIndex));
443         if (!pDataItem)
444         {
445                 SetLastResult(E_INVALID_STATE);
446                 return 0;
447         }
448
449         bytes = pDataItem->size;
450         //SysLog(NID_IO, "Size is %d", bytes);
451
452         return bytes;
453 }
454
455 _DataSetEnumeratorImpl*
456 _DataSetEnumeratorImpl::GetInstance(DataSetEnumerator& dataSetEnumerator)
457 {
458         return dataSetEnumerator.__pDataSetEnumeratorImpl;
459 }
460
461 const _DataSetEnumeratorImpl*
462 _DataSetEnumeratorImpl::GetInstance(const DataSetEnumerator& dataSetEnumerator)
463 {
464         return dataSetEnumerator.__pDataSetEnumeratorImpl;
465 }
466
467 DataSetEnumerator*
468 _DataSetEnumeratorImpl::CreateDataSetEnumeratorInstanceN(void)
469 {
470         unique_ptr<DataSetEnumerator> pDataSetEnumerator(new (std::nothrow) DataSetEnumerator());
471         SysTryReturn(NID_IO, pDataSetEnumerator != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
472         return pDataSetEnumerator.release();
473 }
474
475 }} // Tizen::Io
476