2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FIo_DataRowImpl.cpp
19 * @brief This is the implementation file for _DataRowImpl class.
22 #include <unique_ptr.h>
24 #include <FBaseInteger.h>
25 #include <FBaseLongLong.h>
26 #include <FBaseDouble.h>
27 #include <FBaseString.h>
28 #include <FBaseByteBuffer.h>
29 #include <FBaseDateTime.h>
30 #include <FBaseSysLog.h>
33 #include <FBase_StringConverter.h>
34 #include <FBase_NativeError.h>
35 #include <FIo_DataRowImpl.h>
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::App;
42 namespace Tizen { namespace Io
44 _DataItem::_DataItem(void)
45 : type(DB_COLUMNTYPE_NULL)
54 _DataItem::~_DataItem(void)
60 _DataItem::GetHashCode(void) const
66 str.Append(static_cast<int>(type));
69 str.Append(int64Value);
70 str.Append(doubleValue);
71 hash += str.GetHashCode();
73 if (type == DB_COLUMNTYPE_TEXT )
74 hash += ((String*)pObj)->GetHashCode();
75 else if (type == DB_COLUMNTYPE_BLOB)
76 hash += ((ByteBuffer*)pObj)->GetHashCode();
81 _DataRowImpl::_DataRowImpl(void)
83 , __pColumnTypeList(null)
85 , __dataSetDeleted(false)
89 _DataRowImpl::~_DataRowImpl(void)
91 if (!__dataSetDeleted)
93 __pRowImplList->Remove(*this);
98 _DataRowImpl::SetBlobAt(int columnIndex, Tizen::Base::ByteBuffer* pValue)
100 result r = E_SUCCESS;
101 Integer* pColumnType = null;
102 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
104 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
105 "Wrong column index.");
106 SysTryReturnResult(NID_IO, pValue != null, E_INVALID_ARG, "pValue is null");
108 //return E_SUCCESS without doing anything if the parent dataset class is destroyed.
109 if (__dataSetDeleted)
112 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
113 nColumnType = pColumnType->ToInt();
114 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_BLOB || nColumnType == DB_COLUMNTYPE_NULL,
115 E_TYPE_MISMATCH, "Type mismatch");
117 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
118 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
119 "The memory is insufficient.");
121 pItem->type = DB_COLUMNTYPE_BLOB;
122 pItem->size = pValue->GetLimit();
123 pItem->pObj = pValue;
125 r = __pDataRow->SetAt(pItem.release(), columnIndex);
126 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
127 "The memory is insufficient. Set failed");
129 if (nColumnType == DB_COLUMNTYPE_NULL)
130 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_BLOB), columnIndex);
136 _DataRowImpl::SetDateTimeAt(int columnIndex, const Tizen::Base::DateTime& value)
138 result r = E_SUCCESS;
139 Integer* pColumnType = null;
140 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
143 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
144 "Wrong column index.");
146 //return E_SUCCESS without doing anything if the parent dataset class is destroyed.
147 if (__dataSetDeleted)
150 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
151 nColumnType = pColumnType->ToInt();
152 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_TEXT || nColumnType == DB_COLUMNTYPE_NULL,
153 E_TYPE_MISMATCH, "Type mismatch");
155 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
156 pDateTimeStr = _StringConverter::CopyToCharArrayN(value.ToString());
157 unique_ptr<String> pString(new (std::nothrow) String(pDateTimeStr));
158 delete[] pDateTimeStr;
159 SysTryReturnResult(NID_IO, pItem != null && pString != null, E_OUT_OF_MEMORY,
160 "The memory is insufficient.");
162 pItem->type = DB_COLUMNTYPE_TEXT;
163 pItem->size = pString->GetLength();
164 pItem->pObj = pString.release();
166 r = __pDataRow->SetAt(pItem.release(), columnIndex);
167 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
168 "The memory is insufficient. Set failed");
170 if (nColumnType == DB_COLUMNTYPE_NULL)
171 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_TEXT), columnIndex);
178 _DataRowImpl::SetDoubleAt(int columnIndex, double value)
180 result r = E_SUCCESS;
181 int size = sizeof(double);
182 Integer* pColumnType = null;
183 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
185 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
186 "Wrong column index.");
188 //return E_SUCCESS without doing anything if the parent dataset class is destroyed.
189 if (__dataSetDeleted)
192 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
193 nColumnType = pColumnType->ToInt();
194 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_DOUBLE || nColumnType == DB_COLUMNTYPE_NULL,
195 E_TYPE_MISMATCH, "Type mismatch");
197 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
198 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
199 "The memory is insufficient.");
201 pItem->type = DB_COLUMNTYPE_DOUBLE;
203 pItem->doubleValue = value;
205 r = __pDataRow->SetAt(pItem.release(), columnIndex);
206 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
207 "The memory is insufficient. Set failed");
209 if (nColumnType == DB_COLUMNTYPE_NULL)
210 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_DOUBLE), columnIndex);
217 _DataRowImpl::SetIntAt(int columnIndex, int value)
219 result r = E_SUCCESS;
220 int size = sizeof(int);
221 Integer* pColumnType = null;
222 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
224 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
225 "Wrong column index.");
227 //return E_SUCCESS without doing anything if the parent dataset class is destroyed.
228 if (__dataSetDeleted)
231 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
232 nColumnType = pColumnType->ToInt();
233 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_INT || nColumnType == DB_COLUMNTYPE_NULL,
234 E_TYPE_MISMATCH, "Type mismatch");
236 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
237 SysTryReturnResult(NID_IO, pItem != null , E_OUT_OF_MEMORY,
238 "The memory is insufficient.");
240 pItem->type = DB_COLUMNTYPE_INT;
242 pItem->intValue = value;
244 r = __pDataRow->SetAt(pItem.release(), columnIndex);
245 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
246 "The memory is insufficient. Set failed");
248 if (nColumnType == DB_COLUMNTYPE_NULL)
249 __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 //return E_SUCCESS without doing anything if the parent dataset class is destroyed.
266 if (__dataSetDeleted)
269 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
270 nColumnType = pColumnType->ToInt();
271 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_INT64 || nColumnType == DB_COLUMNTYPE_NULL,
272 E_TYPE_MISMATCH, "Type mismatch");
274 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
275 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
276 "The memory is insufficient.");
278 pItem->type = DB_COLUMNTYPE_INT64;
280 pItem->int64Value = value;
282 r = __pDataRow->SetAt(pItem.release(), columnIndex);
283 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
284 "The memory is insufficient. Set failed");
286 if (nColumnType == DB_COLUMNTYPE_NULL)
287 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_INT64), columnIndex);
293 _DataRowImpl::SetStringAt(int columnIndex, const Tizen::Base::String& value)
295 result r = E_SUCCESS;
296 Integer* pColumnType = null;
297 int nColumnType = DB_COLUMNTYPE_UNDEFINED;
299 SysTryReturnResult(NID_IO, columnIndex >= 0 && columnIndex < __columnCount, E_INVALID_ARG,
300 "Wrong column index.");
302 //return E_SUCCESS without doing anything if the parent dataset class is destroyed.
303 if (__dataSetDeleted)
306 pColumnType = static_cast<Integer*>(__pColumnTypeList->GetAt(columnIndex));
307 nColumnType = pColumnType->ToInt();
308 SysTryReturnResult(NID_IO, nColumnType == DB_COLUMNTYPE_TEXT || nColumnType == DB_COLUMNTYPE_NULL,
309 E_TYPE_MISMATCH, "Type mismatch");
311 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
312 unique_ptr<String> pString(new (std::nothrow) String(value));
313 SysTryReturnResult(NID_IO, pItem != null && pString != null, E_OUT_OF_MEMORY,
314 "The memory is insufficient.");
316 pItem->type = DB_COLUMNTYPE_TEXT;
317 pItem->size = value.GetLength();
318 pItem->pObj = pString.release();
320 r = __pDataRow->SetAt(pItem.release(), columnIndex);
321 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
322 "The memory is insufficient. Set failed");
324 if (nColumnType == DB_COLUMNTYPE_NULL)
325 __pColumnTypeList->SetAt(new Integer(DB_COLUMNTYPE_TEXT), columnIndex);
331 _DataRowImpl::Clone(const ArrayList* pOthersRowArrayList) const
333 result r = E_SUCCESS;
335 for (int i = 0; i < __columnCount; i++)
337 const _DataItem* pOthersItem = static_cast<const _DataItem*>(pOthersRowArrayList->GetAt(i));
339 unique_ptr<_DataItem>pItem(new (std::nothrow) _DataItem());
340 SysTryReturnResult(NID_IO, pItem != null, E_OUT_OF_MEMORY,
341 "The memory is insufficient.");
343 pItem->type = pOthersItem->type;
344 pItem->size = pOthersItem->size;
345 pItem->intValue = pOthersItem->intValue;
346 pItem->int64Value = pOthersItem->int64Value;
347 pItem->doubleValue = pOthersItem->doubleValue;
349 if ( pItem->type == DB_COLUMNTYPE_TEXT)
351 pItem->pObj = new (std::nothrow) String(*static_cast<String*>(pOthersItem->pObj));
352 SysTryReturnResult(NID_IO, pItem->pObj != null, E_OUT_OF_MEMORY,
353 "The memory is insufficient.");
355 else if (pItem->type == DB_COLUMNTYPE_BLOB)
358 pBuffer = new (std::nothrow) ByteBuffer();
359 SysTryReturnResult(NID_IO, pBuffer != null, E_OUT_OF_MEMORY,
360 "The memory is insufficient.");
362 r = pBuffer->Construct(static_cast<ByteBuffer*>(pOthersItem->pObj)->GetCapacity());
363 SysTryLog(NID_IO, r == E_SUCCESS, "ByteBuffer construct failed");
364 r = static_cast<ByteBuffer*>(pOthersItem->pObj)->SetPosition(0);
365 SysTryLog(NID_IO, r == E_SUCCESS, "ByteBuffer SetPosition failed");
366 r = pBuffer->CopyFrom(*static_cast<ByteBuffer*>(pOthersItem->pObj));
367 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY,
368 "ByteBuffer copy failed.");
370 pItem->pObj = pBuffer;
373 r = __pDataRow->SetAt(pItem.release(), i);
382 _DataRowImpl::GetRowArrayList(void) const
388 _DataRowImpl::GetInstance(DataRow& dataRow)
390 return dataRow.__pDataRowImpl;
394 _DataRowImpl::GetInstance(const DataRow& dataRow)
396 return dataRow.__pDataRowImpl;
400 _DataRowImpl::CreateDataRowInstanceN(int columnCount, ArrayList* pArrayListRow)
402 unique_ptr<DataRow> pDataRow(new (std::nothrow) DataRow());
403 SysTryReturn(NID_IO, pDataRow != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
405 pDataRow->__pDataRowImpl->__pDataRow = pArrayListRow;
406 pDataRow->__pDataRowImpl->__columnCount = columnCount;
408 for (int i = 0 ; i < columnCount ; i++)
410 result r = pArrayListRow->Add(new (std::nothrow) _DataItem());
411 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
414 return pDataRow.release();
418 _DataRowImpl::GetHashCode(const ArrayList* pRowArrayList)
422 for (int i = 0; i < pRowArrayList->GetCount(); i++)
424 const _DataItem* pItem = static_cast<const _DataItem*>(pRowArrayList->GetAt(i));
425 hash += pItem->GetHashCode();