2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIo_DataRowImpl.cpp
20 * @brief This is the implementation file for _DataRowImpl class.
23 #include <unique_ptr.h>
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>
34 #include <FBase_StringConverter.h>
35 #include <FBase_NativeError.h>
36 #include <FIo_DataRowImpl.h>
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::App;
43 namespace Tizen { namespace Io
45 _DataItem::_DataItem(void)
46 : type(DB_COLUMNTYPE_NULL)
55 _DataItem::~_DataItem(void)
61 _DataItem::GetHashCode(void) const
67 str.Append(static_cast<int>(type));
70 str.Append(int64Value);
71 str.Append(doubleValue);
72 hash += str.GetHashCode();
74 if (type == DB_COLUMNTYPE_TEXT )
75 hash += ((String*)pObj)->GetHashCode();
76 else if (type == DB_COLUMNTYPE_BLOB)
77 hash += ((ByteBuffer*)pObj)->GetHashCode();
82 _DataRowImpl::_DataRowImpl(void)
84 , __pColumnTypeList(null)
89 _DataRowImpl::~_DataRowImpl(void)
94 _DataRowImpl::SetBlobAt(int columnIndex, Tizen::Base::ByteBuffer* pValue)
97 Integer* pColumnType = null;
98 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
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");
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");
109 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
110 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
111 "The memory is insufficient.");
113 pItem->type = DB_COLUMNTYPE_BLOB;
114 pItem->size = pValue->GetLimit();
115 pItem->pObj = pValue;
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");
123 if (nColumnType == DB_COLUMNTYPE_NULL)
125 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_BLOB), columnIndex);
133 _DataRowImpl::SetDateTimeAt(int columnIndex, const Tizen::Base::DateTime& value)
135 result r = E_SUCCESS;
136 Integer* pColumnType = null;
137 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
140 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
141 "Wrong column index.");
143 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
144 nColumnType = pColumnType->ToInt();
145 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_TEXT || nColumnType == DB_COLUMNTYPE_NULL,
146 E_TYPE_MISMATCH, "Type mismatch");
148 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
149 pDateTimeStr = _StringConverter::CopyToCharArrayN(value.ToString());
150 unique_ptr<String> pString(new (std::nothrow) String(pDateTimeStr));
152 SysTryReturnResult(NID_IO, pItem != null && pString != null, E_OUT_OF_MEMORY,
153 "The memory is insufficient.");
155 pItem->type = DB_COLUMNTYPE_TEXT;
156 pItem->size = pString->GetLength();
157 pItem->pObj = pString.release();
159 _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
160 r = __pDataRow->SetAt(pItem.release(), columnIndex);
161 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
162 "The memory is insufficient. Set failed");
165 if (nColumnType == DB_COLUMNTYPE_NULL)
167 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_TEXT), columnIndex);
176 _DataRowImpl::SetDoubleAt(int columnIndex, double value)
178 result r = E_SUCCESS;
179 int size = sizeof(double);
180 Integer* pColumnType = null;
181 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
183 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
184 "Wrong column index.");
186 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
187 nColumnType = pColumnType->ToInt();
188 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_DOUBLE || nColumnType == DB_COLUMNTYPE_NULL,
189 E_TYPE_MISMATCH, "Type mismatch");
191 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
192 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
193 "The memory is insufficient.");
195 pItem->type = DB_COLUMNTYPE_DOUBLE;
197 pItem->doubleValue = value;
199 _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
200 r = __pDataRow->SetAt(pItem.release(), columnIndex);
201 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
202 "The memory is insufficient. Set failed");
205 if (nColumnType == DB_COLUMNTYPE_NULL)
207 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_DOUBLE), columnIndex);
216 _DataRowImpl::SetIntAt(int columnIndex, int value)
218 result r = E_SUCCESS;
219 int size = sizeof(int);
220 Integer* pColumnType = null;
221 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
223 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
224 "Wrong column index.");
226 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
227 nColumnType = pColumnType->ToInt();
228 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_INT || nColumnType == DB_COLUMNTYPE_NULL,
229 E_TYPE_MISMATCH, "Type mismatch");
231 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
232 SysTryReturnResult(NID_IO, pItem != null , E_OUT_OF_MEMORY,
233 "The memory is insufficient.");
235 pItem->type = DB_COLUMNTYPE_INT;
237 pItem->intValue = value;
239 _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
240 r = __pDataRow->SetAt(pItem.release(), columnIndex);
241 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
242 "The memory is insufficient. Set failed");
245 if (nColumnType == DB_COLUMNTYPE_NULL)
247 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_INT), columnIndex);
255 _DataRowImpl::SetInt64At(int columnIndex, long long value)
257 result r = E_SUCCESS;
258 int size = sizeof(long long);
259 Integer* pColumnType = null;
260 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
262 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
263 "Wrong column index.");
265 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
266 nColumnType = pColumnType->ToInt();
267 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_INT64 || nColumnType == DB_COLUMNTYPE_NULL,
268 E_TYPE_MISMATCH, "Type mismatch");
270 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
271 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
272 "The memory is insufficient.");
274 pItem->type = DB_COLUMNTYPE_INT64;
276 pItem->int64Value = value;
278 _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
279 r = __pDataRow->SetAt(pItem.release(), columnIndex);
280 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
281 "The memory is insufficient. Set failed");
284 if (nColumnType == DB_COLUMNTYPE_NULL)
286 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_INT64), columnIndex);
294 _DataRowImpl::SetStringAt(int columnIndex, const Tizen::Base::String& value)
296 result r = E_SUCCESS;
297 Integer* pColumnType = null;
298 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
300 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
301 "Wrong column index.");
303 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
304 nColumnType = pColumnType->ToInt();
305 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_TEXT || nColumnType == DB_COLUMNTYPE_NULL,
306 E_TYPE_MISMATCH, "Type mismatch");
308 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
309 unique_ptr<String> pString(new (std::nothrow) String(value));
310 SysTryReturnResult(NID_IO, pItem != null && pString != null, E_OUT_OF_MEMORY,
311 "The memory is insufficient.");
313 pItem->type = DB_COLUMNTYPE_TEXT;
314 pItem->size = value.GetLength();
315 pItem->pObj = pString.release();
317 _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(columnIndex));
318 r = __pDataRow->SetAt(pItem.release(), columnIndex);
319 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
320 "The memory is insufficient. Set failed");
323 if (nColumnType == DB_COLUMNTYPE_NULL)
325 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_TEXT), columnIndex);
333 _DataRowImpl::Clone(const ArrayList* pOthersRowArrayList) const
335 result r = E_SUCCESS;
337 for (int i = 0; i < __columnCount; i++)
339 const _DataItem* pOthersItem = static_cast<const _DataItem*>(pOthersRowArrayList->GetAt(i));
341 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
342 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
343 "The memory is insufficient.");
345 pItem->type = pOthersItem->type;
346 pItem->size = pOthersItem->size;
347 pItem->intValue = pOthersItem->intValue;
348 pItem->int64Value = pOthersItem->int64Value;
349 pItem->doubleValue = pOthersItem->doubleValue;
351 if ( pItem->type == DB_COLUMNTYPE_TEXT)
353 pItem->pObj = new (std::nothrow) String(*static_cast<String*>(pOthersItem->pObj));
354 SysTryReturnResult(NID_IO, pItem->pObj != null, E_OUT_OF_MEMORY,
355 "The memory is insufficient.");
357 else if (pItem->type == DB_COLUMNTYPE_BLOB)
360 pBuffer = new (std::nothrow) ByteBuffer();
361 SysTryReturnResult(NID_IO, pBuffer != null, E_OUT_OF_MEMORY,
362 "The memory is insufficient.");
364 r = pBuffer->Construct(static_cast<ByteBuffer*>(pOthersItem->pObj)->GetCapacity());
365 SysTryLog(NID_IO, r == E_SUCCESS, "ByteBuffer construct failed");
366 r = static_cast<ByteBuffer*>(pOthersItem->pObj)->SetPosition(0);
367 SysTryLog(NID_IO, r == E_SUCCESS, "ByteBuffer SetPosition failed");
368 r = pBuffer->CopyFrom(*static_cast<ByteBuffer*>(pOthersItem->pObj));
369 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
370 "ByteBuffer copy failed.");
372 pItem->pObj = pBuffer;
375 _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(i));
376 r = __pDataRow->SetAt(pItem.release(), i);
386 _DataRowImpl::GetRowArrayList(void) const
392 _DataRowImpl::GetInstance(DataRow& dataRow)
394 return dataRow.__pDataRowImpl;
398 _DataRowImpl::GetInstance(const DataRow& dataRow)
400 return dataRow.__pDataRowImpl;
404 _DataRowImpl::CreateDataRowInstanceN(int columnCount, ArrayList* pArrayListRow)
406 unique_ptr<DataRow> pDataRow(new (std::nothrow) DataRow());
407 SysTryReturn(NID_IO, pDataRow != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
409 pDataRow->__pDataRowImpl->__pDataRow = pArrayListRow;
410 pDataRow->__pDataRowImpl->__columnCount = columnCount;
412 for (int i = 0 ; i < columnCount ; i++)
414 result r = pArrayListRow->Add(new (std::nothrow) _DataItem());
415 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
418 return pDataRow.release();
422 _DataRowImpl::GetHashCode(const ArrayList* pRowArrayList)
426 for (int i = 0; i < pRowArrayList->GetCount(); i++)
428 const _DataItem* pItem = static_cast<const _DataItem*>(pRowArrayList->GetAt(i));
429 hash += pItem->GetHashCode();