Implement DataSet/DataRow/DataSetEnumerator Class
[platform/framework/native/appfw.git] / src / io / FIo_DataSetImpl.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_DataSetImpl.cpp
20 * @brief                This is the implementation file for the _DataSetImpl class.
21 */
22
23 #include <new>
24 #include <unique_ptr.h>
25
26 #include <FBaseObject.h>
27 #include <FBaseInteger.h>
28 #include <FBaseSysLog.h>
29 #include <FIo_DataSetImpl.h>
30 #include <FIo_DataRowImpl.h>
31 #include <FIo_DataSetEnumeratorImpl.h>
32
33 using namespace std;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36
37 namespace Tizen { namespace Io
38 {
39 _DataSetImpl::_DataSetImpl(void)
40         :__rowCount(0)
41 {
42
43 }
44
45 _DataSetImpl::~_DataSetImpl(void)
46 {
47
48 }
49
50 result
51 _DataSetImpl::Construct(const IList& columnNames)
52 {
53         result r = E_SUCCESS;
54
55         r = __columnList.Construct(columnNames);
56         SysTryReturn(NID_IO, r == E_SUCCESS, E_INVALID_ARG, r, "[E_INVALID_ARG] columnNames is invalid.");
57
58         __columnCount = __columnList.GetCount();
59         SysTryReturn(NID_IO, __columnCount > 0, E_INVALID_ARG, r, "[E_INVALID_ARG] column count is 0.");
60
61         __columnTypeList.Construct(__columnCount);
62
63         for (int i = 0 ; i < __columnCount ; i++)
64         {
65                 result r = __columnTypeList.Add(new (std::nothrow) Integer(DB_COLUMNTYPE_NULL));
66                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
67         }
68         //SysLog(NID_IO, "column count is %d", __columnCount);
69
70         return r;
71 }
72
73 DataRow*
74 _DataSetImpl::CreateDataRowN(void)
75 {
76         result r;
77         unique_ptr<ArrayList> pNewRowArrayList(new (std::nothrow) ArrayList);
78         SysTryReturn(NID_IO, pNewRowArrayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
79
80         unique_ptr<DataRow> pDataRow(_DataRowImpl::CreateDataRowInstanceN(__columnCount, pNewRowArrayList.get()));
81         SysTryReturn(NID_IO, pDataRow != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
82
83         r = __dataSet.Add(pNewRowArrayList.release());
84         SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Add Row to DataSet is failed");
85
86         _DataRowImpl::GetInstance(*pDataRow)->__pColumnTypeList = &__columnTypeList;
87
88         __rowCount = __dataSet.GetCount();
89         //SysLog(NID_IO, "row count is %d", __rowCount);
90
91         return pDataRow.release();
92
93 }
94
95 DataSetEnumerator*
96 _DataSetImpl::GetDataSetEnumeratorN(void)
97 {
98         unique_ptr<DataSetEnumerator> pDataSetEnum(_DataSetEnumeratorImpl::CreateDataSetEnumeratorInstanceN());
99
100         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__pDataSet = &__dataSet;
101         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__pColumnList = &__columnList;
102         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__columnCount = __columnCount;
103         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__rowCount = __rowCount;
104
105         //SysLog(NID_IO, "row count is %d, column count is %d", __rowCount, __columnCount);
106
107         return pDataSetEnum.release();
108 }
109
110 DataSet*
111 _DataSetImpl::CloneN(void) const
112 {
113         result r;
114         unique_ptr< DataSet > pDataSet(new (std::nothrow) DataSet());
115         SysTryReturn(NID_IO, pDataSet != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
116
117         pDataSet->Construct(__columnList);
118
119         for (int i = 0; i < __rowCount; i++)
120         {
121                 DataRow* pNewRow = pDataSet->CreateDataRowN();
122                 r = _DataRowImpl::GetInstance(*pNewRow)->Clone((const ArrayList*)(__dataSet.GetAt(i)));
123                 delete pNewRow;
124
125                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] DataRow Clone Falied");
126         }
127
128         return pDataSet.release();
129 }
130
131 bool
132 _DataSetImpl::Equals(const Tizen::Base::Object& obj) const
133 {
134         int thisHash = this->GetHashCode();
135         int othersHash = obj.GetHashCode();
136
137         if (thisHash == othersHash )
138                 return true;
139         else
140                 return false;
141 }
142
143 int
144 _DataSetImpl::GetHashCode(void) const
145 {
146         int hash = 0;
147
148         for (int i = 0; i < __rowCount; i++)
149         {
150                 const ArrayList* pDataRowArrayList = static_cast<const ArrayList*>(__dataSet.GetAt(i));
151                 hash += _DataRowImpl::GetHashCode(pDataRowArrayList);
152         }
153
154         return hash;
155 }
156
157 }} // Tizen::Io
158
159