Merge "Remove primitive SettingClient" into tizen_2.2
[platform/framework/native/appfw.git] / src / io / FIo_DataSetImpl.cpp
1 //
2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18 * @file         FIo_DataSetImpl.cpp
19 * @brief                This is the implementation file for the _DataSetImpl class.
20 */
21
22 #include <new>
23 #include <unique_ptr.h>
24
25 #include <FBaseObject.h>
26 #include <FBaseInteger.h>
27 #include <FBaseSysLog.h>
28 #include <FIo_DataSetImpl.h>
29 #include <FIo_DataRowImpl.h>
30 #include <FIo_DataSetEnumeratorImpl.h>
31
32 using namespace std;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35
36 namespace Tizen { namespace Io
37 {
38 _DataSetImpl::_DataSetImpl(void)
39         :__rowCount(0),
40         __pDataSet(null),
41         __pColumnList(null),
42         __pColumnTypeList(null)
43 {
44 }
45
46 _DataSetImpl::~_DataSetImpl(void)
47 {
48         for (int i = 0; i< __enumImplList.GetCount() ; i++)
49                 (static_cast<_DataSetEnumeratorImpl*>(__enumImplList.GetAt(i)))->__dataSetDeleted = true;
50         for (int i = 0; i< __rowImplList.GetCount() ; i++)
51                 (static_cast<_DataRowImpl*>(__rowImplList.GetAt(i)))->__dataSetDeleted = true;
52
53         delete __pDataSet;
54         delete __pColumnList;
55         delete __pColumnTypeList;
56 }
57
58 result
59 _DataSetImpl::Construct(const IList& columnNames)
60 {
61         result r = E_SUCCESS;
62
63         __pDataSet = new (std::nothrow) LinkedList(SingleObjectDeleter);
64         SysTryReturnResult(NID_IO, __pDataSet != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
65         __pColumnList = new (std::nothrow) ArrayList(SingleObjectDeleter);
66         SysTryReturnResult(NID_IO, __pColumnList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
67         __pColumnTypeList = new (std::nothrow) ArrayList(SingleObjectDeleter);
68         SysTryReturnResult(NID_IO, __pColumnTypeList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
69
70         __columnCount = columnNames.GetCount();
71         SysTryReturnResult(NID_IO, __columnCount > 0, E_INVALID_ARG, "column count is 0.");
72
73         r = __pColumnList->Construct(__columnCount);
74         SysTryReturn(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] column list construct failed");
75         r = __pColumnTypeList->Construct(__columnCount);
76         SysTryReturn(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] column type list construct failed");
77
78         for (int i = 0 ; i < __columnCount ; i++)
79         {
80                 r = __pColumnList->Add(new (std::nothrow) String(*(String*)(columnNames.GetAt(i))));
81                 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY, "The memory is insufficient.");
82
83                 r = __pColumnTypeList->Add(new (std::nothrow) Integer(DB_COLUMNTYPE_NULL));
84                 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY, "The memory is insufficient.");
85         }
86         //SysLog(NID_IO, "column count is %d", __columnCount);
87
88         return r;
89 }
90
91 DataRow*
92 _DataSetImpl::CreateDataRowN(void)
93 {
94         result r;
95         unique_ptr<ArrayList> pNewRowArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
96         SysTryReturn(NID_IO, pNewRowArrayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
97
98         unique_ptr<DataRow> pDataRow(_DataRowImpl::CreateDataRowInstanceN(__columnCount, pNewRowArrayList.get()));
99         SysTryReturn(NID_IO, pDataRow != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
100
101         r = __pDataSet->Add(pNewRowArrayList.release());
102         SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Add Row to DataSet is failed");
103
104         _DataRowImpl::GetInstance(*pDataRow)->__pColumnTypeList = __pColumnTypeList;
105
106         r = __rowImplList.Add(_DataRowImpl::GetInstance(*pDataRow));
107         SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Add to LinkedList Falied");
108
109         _DataRowImpl::GetInstance(*pDataRow)->__pRowImplList = &__rowImplList;
110
111         __rowCount = __pDataSet->GetCount();
112         //SysLog(NID_IO, "row count is %d", __rowCount);
113
114         return pDataRow.release();
115
116 }
117
118 DataSetEnumerator*
119 _DataSetImpl::GetDataSetEnumeratorN(void)
120 {
121         result r;
122
123         unique_ptr<DataSetEnumerator> pDataSetEnum(_DataSetEnumeratorImpl::CreateDataSetEnumeratorInstanceN());
124
125         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__pDataSet = __pDataSet;
126         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__pColumnList = __pColumnList;
127         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__columnCount = __columnCount;
128         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__rowCount = __rowCount;
129
130         //SysLog(NID_IO, "row count is %d, column count is %d", __rowCount, __columnCount);
131
132         r = __enumImplList.Add(_DataSetEnumeratorImpl::GetInstance(*pDataSetEnum));
133         SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Add to LinkedList Falied");
134
135         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__pEnumImplList = &__enumImplList;
136
137         return pDataSetEnum.release();
138 }
139
140 DataSet*
141 _DataSetImpl::CloneN(void) const
142 {
143         result r;
144
145         unique_ptr< DataSet > pDataSet(new (std::nothrow) DataSet());
146         SysTryReturn(NID_IO, pDataSet != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
147
148         pDataSet->Construct(*__pColumnList);
149
150         for (int i = 0; i < __columnCount; i++)
151         {
152                 r = _DataSetImpl::GetInstance(*pDataSet)->__pColumnTypeList->SetAt(new Integer(*(Integer*)__pColumnTypeList->GetAt(i)), i);
153                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ColumnTypeList Copy Falied");
154         }
155
156         for (int i = 0; i < __rowCount; i++)
157         {
158                 DataRow* pNewRow = pDataSet->CreateDataRowN();
159                 r = _DataRowImpl::GetInstance(*pNewRow)->Clone((const ArrayList*)(__pDataSet->GetAt(i)));
160                 delete pNewRow;
161
162                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] DataRow Clone Falied");
163         }
164
165         return pDataSet.release();
166 }
167
168 bool
169 _DataSetImpl::Equals(const Tizen::Base::Object& obj) const
170 {
171         int thisHash = this->GetHashCode();
172         int othersHash = obj.GetHashCode();
173
174         if (thisHash == othersHash )
175                 return true;
176         else
177                 return false;
178 }
179
180 int
181 _DataSetImpl::GetHashCode(void) const
182 {
183         int hash = 0;
184
185         for (int i = 0; i < __rowCount; i++)
186         {
187                 const ArrayList* pDataRowArrayList = static_cast<const ArrayList*>(__pDataSet->GetAt(i));
188                 hash += _DataRowImpl::GetHashCode(pDataRowArrayList);
189         }
190
191         return hash;
192 }
193
194 _DataSetImpl*
195 _DataSetImpl::GetInstance(DataSet& dataSet)
196 {
197         return dataSet.__pDataSetImpl;
198 }
199
200 const _DataSetImpl*
201 _DataSetImpl::GetInstance(const DataSet& dataSet)
202 {
203         return dataSet.__pDataSetImpl;
204 }
205
206 }} // Tizen::Io
207
208