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;
139 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
140 "Wrong column index.");
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");
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.");
152 pItem->type = DB_COLUMNTYPE_TEXT;
153 pItem->size = pString->GetLength();
154 pItem->pObj = pString.release();
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");
162 if (nColumnType == DB_COLUMNTYPE_NULL)
164 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_TEXT), columnIndex);
173 _DataRowImpl::SetDoubleAt(int columnIndex, double value)
175 result r = E_SUCCESS;
176 int size = sizeof(double);
177 Integer* pColumnType = null;
178 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
180 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
181 "Wrong column index.");
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");
188 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
189 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
190 "The memory is insufficient.");
192 pItem->type = DB_COLUMNTYPE_DOUBLE;
194 pItem->doubleValue = value;
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");
202 if (nColumnType == DB_COLUMNTYPE_NULL)
204 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_DOUBLE), columnIndex);
213 _DataRowImpl::SetIntAt(int columnIndex, int value)
215 result r = E_SUCCESS;
216 int size = sizeof(int);
217 Integer* pColumnType = null;
218 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
220 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
221 "Wrong column index.");
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");
228 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
229 SysTryReturnResult(NID_IO, pItem != null , E_OUT_OF_MEMORY,
230 "The memory is insufficient.");
232 pItem->type = DB_COLUMNTYPE_INT;
234 pItem->intValue = value;
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");
242 if (nColumnType == DB_COLUMNTYPE_NULL)
244 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_INT), columnIndex);
252 _DataRowImpl::SetInt64At(int columnIndex, long long value)
254 result r = E_SUCCESS;
255 int size = sizeof(long long);
256 Integer* pColumnType = null;
257 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
259 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
260 "Wrong column index.");
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");
267 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
268 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
269 "The memory is insufficient.");
271 pItem->type = DB_COLUMNTYPE_INT64;
273 pItem->int64Value = value;
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");
281 if (nColumnType == DB_COLUMNTYPE_NULL)
283 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_INT64), columnIndex);
291 _DataRowImpl::SetStringAt(int columnIndex, const Tizen::Base::String& value)
293 result r = E_SUCCESS;
294 Integer* pColumnType = null;
295 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
297 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
298 "Wrong column index.");
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");
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.");
310 pItem->type = DB_COLUMNTYPE_TEXT;
311 pItem->size = value.GetLength();
312 pItem->pObj = pString.release();
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");
320 if (nColumnType == DB_COLUMNTYPE_NULL)
322 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_TEXT), columnIndex);
330 _DataRowImpl::Clone(const ArrayList* pOthersRowArrayList) const
332 result r = E_SUCCESS;
334 for (int i = 0; i < __columnCount; i++)
336 const _DataItem* pOthersItem = static_cast<const _DataItem*>(pOthersRowArrayList->GetAt(i));
338 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
339 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
340 "The memory is insufficient.");
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;
348 if ( pItem->type == DB_COLUMNTYPE_TEXT)
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.");
354 else if (pItem->type == DB_COLUMNTYPE_BLOB)
357 pBuffer = new (std::nothrow) ByteBuffer();
358 SysTryReturnResult(NID_IO, pBuffer != null, E_OUT_OF_MEMORY,
359 "The memory is insufficient.");
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.");
369 pItem->pObj = pBuffer;
372 _DataItem* pOldItem = static_cast<_DataItem*>(__pDataRow->GetAt(i));
373 r = __pDataRow->SetAt(pItem.release(), i);
383 _DataRowImpl::GetRowArrayList(void) const
389 _DataRowImpl::GetInstance(DataRow& dataRow)
391 return dataRow.__pDataRowImpl;
395 _DataRowImpl::GetInstance(const DataRow& dataRow)
397 return dataRow.__pDataRowImpl;
401 _DataRowImpl::CreateDataRowInstanceN(int columnCount, ArrayList* pArrayListRow)
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.");
406 pDataRow->__pDataRowImpl->__pDataRow = pArrayListRow;
407 pDataRow->__pDataRowImpl->__columnCount = columnCount;
409 for (int i = 0 ; i < columnCount ; i++)
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.");
415 return pDataRow.release();
419 _DataRowImpl::GetHashCode(const ArrayList* pRowArrayList)
423 for (int i = 0; i < pRowArrayList->GetCount(); i++)
425 const _DataItem* pItem = static_cast<const _DataItem*>(pRowArrayList->GetAt(i));
426 hash += pItem->GetHashCode();