Backout 128 limits in DataControl
[platform/framework/native/appfw.git] / src / io / FIo_DataRowImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 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_DataRowImpl.cpp
20  * @brief       This is the implementation file for _DataRowImpl class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <FBaseInteger.h>
26 #include <FBaseLongLong.h>
27 #include <FBaseDouble.h>
28 #include <FBaseString.h>
29 #include <FBaseByteBuffer.h>
30 #include <FBaseDateTime.h>
31 #include <FBaseSysLog.h>
32 #include <FBaseCol.h>
33
34 #include <FBase_StringConverter.h>
35 #include <FBase_NativeError.h>
36 #include <FIo_DataRowImpl.h>
37
38 using namespace std;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::App;
42
43 namespace Tizen { namespace Io
44 {
45 _DataItem::_DataItem(void)
46         : type(DB_COLUMNTYPE_NULL)
47         , size(0)
48         , intValue(0)
49         , int64Value(0)
50         , doubleValue(0)
51         , pObj(null)
52 {
53 }
54
55 _DataItem::~_DataItem(void)
56 {
57         delete pObj;
58 }
59
60 int
61 _DataItem::GetHashCode(void) const
62 {
63         int hash = 0;
64
65         String str;
66
67         str.Append(static_cast<int>(type));
68         str.Append(size);
69         str.Append(intValue);
70         str.Append(int64Value);
71         str.Append(doubleValue);
72         hash += str.GetHashCode();
73
74         if (type == DB_COLUMNTYPE_TEXT )
75                 hash += ((String*)pObj)->GetHashCode();
76         else if (type == DB_COLUMNTYPE_BLOB)
77                 hash += ((ByteBuffer*)pObj)->GetHashCode();
78
79         return hash;
80 }
81
82 _DataRowImpl::_DataRowImpl(void)
83         : __pDataRow(null)
84         , __pColumnTypeList(null)
85         , __columnCount(0)
86 {
87 }
88
89 _DataRowImpl::~_DataRowImpl(void)
90 {
91 }
92
93 result
94 _DataRowImpl::SetBlobAt(int columnIndex, Tizen::Base::ByteBuffer* pValue)
95 {
96         result r = E_SUCCESS;
97         Integer* pColumnType = null;
98         int nColumnType = DB_COLUMNTYPE_UNDEFINED;
99
100         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
101                                    "Wrong column index.");
102         SysTryReturnResult(NID_IO, pValue != null, E_INVALID_ARG, "pValue is null");
103
104         pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
105         nColumnType = pColumnType->ToInt();
106         SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_BLOB || nColumnType == DB_COLUMNTYPE_NULL,
107                 E_TYPE_MISMATCH, "Type mismatch");
108
109         unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
110         SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
111                         "The memory is insufficient.");
112
113         pItem->type =  DB_COLUMNTYPE_BLOB;
114         pItem->size = pValue->GetLimit();
115         pItem->pObj = pValue;
116
117         _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
118         r = __pDataRow->SetAt(pItem.release(), columnIndex);
119         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
120                         "The memory is insufficient. Set failed");
121         delete pOldItem;
122
123         if (nColumnType == DB_COLUMNTYPE_NULL)
124         {
125                 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_BLOB), columnIndex);
126                 delete pColumnType;
127         }
128
129         return r;
130 }
131
132 result
133 _DataRowImpl::SetDateTimeAt(int columnIndex, const Tizen::Base::DateTime& value)
134 {
135         result r = E_SUCCESS;
136         Integer* pColumnType = null;
137         int nColumnType = DB_COLUMNTYPE_UNDEFINED;
138
139         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
140                                    "Wrong column index.");
141
142         pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
143         nColumnType = pColumnType->ToInt();
144         SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_TEXT || nColumnType == DB_COLUMNTYPE_NULL,
145                 E_TYPE_MISMATCH, "Type mismatch");
146
147         unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
148         unique_ptr<String> pString(new (std::nothrow) String(_StringConverter::CopyToCharArrayN(value.ToString())));
149         SysTryReturnResult(NID_IO, pItem != null && pString != null, E_OUT_OF_MEMORY,
150                         "The memory is insufficient.");
151
152         pItem->type =  DB_COLUMNTYPE_TEXT;
153         pItem->size = pString->GetLength();
154         pItem->pObj = pString.release();
155
156         _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
157         r = __pDataRow->SetAt(pItem.release(), columnIndex);
158         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
159                         "The memory is insufficient. Set failed");
160         delete pOldItem;
161
162         if (nColumnType == DB_COLUMNTYPE_NULL)
163         {
164                 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_TEXT), columnIndex);
165                 delete pColumnType;
166         }
167
168         return r;
169
170 }
171
172 result
173 _DataRowImpl::SetDoubleAt(int columnIndex, double value)
174 {
175         result r = E_SUCCESS;
176         int size = sizeof(double);
177         Integer* pColumnType = null;
178         int nColumnType = DB_COLUMNTYPE_UNDEFINED;
179
180         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
181                                    "Wrong column index.");
182
183         pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
184         nColumnType = pColumnType->ToInt();
185         SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_DOUBLE || nColumnType == DB_COLUMNTYPE_NULL,
186                 E_TYPE_MISMATCH, "Type mismatch");
187
188         unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
189         SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
190                         "The memory is insufficient.");
191
192         pItem->type =  DB_COLUMNTYPE_DOUBLE;
193         pItem->size = size;
194         pItem->doubleValue = value;
195
196         _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
197         r = __pDataRow->SetAt(pItem.release(), columnIndex);
198         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
199                         "The memory is insufficient. Set failed");
200         delete pOldItem;
201
202         if (nColumnType == DB_COLUMNTYPE_NULL)
203         {
204                 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_DOUBLE), columnIndex);
205                 delete pColumnType;
206         }
207
208         return r;
209
210 }
211
212 result
213 _DataRowImpl::SetIntAt(int columnIndex, int value)
214 {
215         result r = E_SUCCESS;
216         int size = sizeof(int);
217         Integer* pColumnType = null;
218         int nColumnType = DB_COLUMNTYPE_UNDEFINED;
219
220         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
221                                    "Wrong column index.");
222
223         pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
224         nColumnType = pColumnType->ToInt();
225         SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_INT || nColumnType == DB_COLUMNTYPE_NULL,
226                 E_TYPE_MISMATCH, "Type mismatch");
227
228         unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
229         SysTryReturnResult(NID_IO, pItem != null , E_OUT_OF_MEMORY,
230                         "The memory is insufficient.");
231
232         pItem->type =  DB_COLUMNTYPE_INT;
233         pItem->size = size;
234         pItem->intValue = value;
235
236         _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
237         r = __pDataRow->SetAt(pItem.release(), columnIndex);
238         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
239                         "The memory is insufficient. Set failed");
240         delete pOldItem;
241
242         if (nColumnType == DB_COLUMNTYPE_NULL)
243         {
244                 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_INT), columnIndex);
245                 delete pColumnType;
246         }
247
248         return r;
249 }
250
251 result
252 _DataRowImpl::SetInt64At(int columnIndex, long long value)
253 {
254         result r = E_SUCCESS;
255         int size = sizeof(long long);
256         Integer* pColumnType = null;
257         int nColumnType = DB_COLUMNTYPE_UNDEFINED;
258
259         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
260                                    "Wrong column index.");
261
262         pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
263         nColumnType = pColumnType->ToInt();
264         SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_INT64 || nColumnType == DB_COLUMNTYPE_NULL,
265                 E_TYPE_MISMATCH, "Type mismatch");
266
267         unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
268         SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
269                         "The memory is insufficient.");
270
271         pItem->type =  DB_COLUMNTYPE_INT64;
272         pItem->size = size;
273         pItem->int64Value = value;
274
275         _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
276         r = __pDataRow->SetAt(pItem.release(), columnIndex);
277         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
278                         "The memory is insufficient. Set failed");
279         delete pOldItem;
280
281         if (nColumnType == DB_COLUMNTYPE_NULL)
282         {
283                 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_INT64), columnIndex);
284                 delete pColumnType;
285         }
286
287         return r;
288 }
289
290 result
291 _DataRowImpl::SetStringAt(int columnIndex, const Tizen::Base::String& value)
292 {
293         result r = E_SUCCESS;
294         Integer* pColumnType = null;
295         int nColumnType = DB_COLUMNTYPE_UNDEFINED;
296
297         SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
298                                    "Wrong column index.");
299
300         pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
301         nColumnType = pColumnType->ToInt();
302         SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_TEXT || nColumnType == DB_COLUMNTYPE_NULL,
303                 E_TYPE_MISMATCH, "Type mismatch");
304
305         unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
306         unique_ptr<String> pString(new (std::nothrow) String(value));
307         SysTryReturnResult(NID_IO, pItem != null && pString != null, E_OUT_OF_MEMORY,
308                         "The memory is insufficient.");
309
310         pItem->type =  DB_COLUMNTYPE_TEXT;
311         pItem->size = value.GetLength();
312         pItem->pObj = pString.release();
313
314         _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
315         r = __pDataRow->SetAt(pItem.release(), columnIndex);
316         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
317                         "The memory is insufficient. Set failed");
318         delete pOldItem;
319
320         if (nColumnType == DB_COLUMNTYPE_NULL)
321         {
322                 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_TEXT), columnIndex);
323                 delete pColumnType;
324         }
325
326         return r;
327 }
328
329 result
330 _DataRowImpl::Clone(const ArrayList* pOthersRowArrayList) const
331 {
332         result r = E_SUCCESS;
333
334         for (int i = 0; i < __columnCount; i++)
335         {
336                 const _DataItem* pOthersItem = static_cast<const _DataItem*>(pOthersRowArrayList->GetAt(i));
337
338                 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
339                 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
340                                 "The memory is insufficient.");
341
342                 pItem->type = pOthersItem->type;
343                 pItem->size = pOthersItem->size;
344                 pItem->intValue = pOthersItem->intValue;
345                 pItem->int64Value = pOthersItem->int64Value;
346                 pItem->doubleValue = pOthersItem->doubleValue;
347
348                 if ( pItem->type == DB_COLUMNTYPE_TEXT)
349                 {
350                         pItem->pObj = new (std::nothrow) String(*static_cast<String*>(pOthersItem->pObj));
351                         SysTryReturnResult(NID_IO, pItem->pObj != null, E_OUT_OF_MEMORY,
352                                         "The memory is insufficient.");
353                 }
354                 else if (pItem->type == DB_COLUMNTYPE_BLOB)
355                 {
356                         ByteBuffer* pBuffer;
357                         pBuffer = new (std::nothrow) ByteBuffer();
358                         SysTryReturnResult(NID_IO, pBuffer != null, E_OUT_OF_MEMORY,
359                                         "The memory is insufficient.");
360
361                         r = pBuffer->Construct(static_cast<ByteBuffer*>(pOthersItem->pObj)->GetCapacity());
362                         SysTryLog(NID_IO, r == E_SUCCESS, "ByteBuffer construct failed");
363                         r = static_cast<ByteBuffer*>(pOthersItem->pObj)->SetPosition(0);
364                         SysTryLog(NID_IO, r == E_SUCCESS, "ByteBuffer SetPosition failed");
365                         r = pBuffer->CopyFrom(*static_cast<ByteBuffer*>(pOthersItem->pObj));
366                         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
367                                         "ByteBuffer copy failed.");
368
369                         pItem->pObj = pBuffer;
370                 }
371
372                 _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(i));
373                 r = __pDataRow->SetAt(pItem.release(), i);
374                 delete pOldItem;
375                 if (r != E_SUCCESS)
376                         break;
377         }
378
379         return r;
380 }
381
382 ArrayList*
383 _DataRowImpl::GetRowArrayList(void) const
384 {
385         return __pDataRow;
386 }
387
388 _DataRowImpl*
389 _DataRowImpl::GetInstance(DataRow& dataRow)
390 {
391         return dataRow.__pDataRowImpl;
392 }
393
394 const _DataRowImpl*
395 _DataRowImpl::GetInstance(const DataRow& dataRow)
396 {
397         return dataRow.__pDataRowImpl;
398 }
399
400 DataRow*
401 _DataRowImpl::CreateDataRowInstanceN(int columnCount, ArrayList* pArrayListRow)
402 {
403         unique_ptr<DataRow> pDataRow(new (std::nothrow) DataRow());
404         SysTryReturn(NID_IO, pDataRow != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
405
406         pDataRow->__pDataRowImpl->__pDataRow = pArrayListRow;
407         pDataRow->__pDataRowImpl->__columnCount = columnCount;
408
409         for (int i = 0 ; i < columnCount ; i++)
410         {
411                 result r = pArrayListRow->Add(new (std::nothrow) _DataItem());
412                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
413         }
414
415         return pDataRow.release();
416 }
417
418 int
419 _DataRowImpl::GetHashCode(const ArrayList* pRowArrayList)
420 {
421         int hash = 0;
422
423         for (int i = 0; i < pRowArrayList->GetCount(); i++)
424         {
425                 const _DataItem* pItem = static_cast<const _DataItem*>(pRowArrayList->GetAt(i));
426                 hash += pItem->GetHashCode();
427         }
428
429         return hash;
430 }
431
432 }} // Tizen::Io
433