Fix a memory leak in Deserializer
[platform/framework/native/appfw.git] / src / io / FIoSqlStatementBuilder.cpp
1 //
2 // Copyright (c) 2012 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        FIoSqlStatementBuilder.cpp
19  * @brief       This is the implementation for the %SqlStatementBuilder class.
20  */
21 #include <unique_ptr.h>
22
23 #include <FBaseString.h>
24 #include <FBaseColIList.h>
25 #include <FBaseColIMap.h>
26 #include <FIoSqlStatementBuilder.h>
27
28 #include <FBaseSysLog.h>
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32
33 namespace Tizen { namespace Io
34 {
35
36 String
37 SqlStatementBuilder::CreateSelectStatement(const String& table, const IList* pColumnList, const String* pWhere,
38                 const String* pOrder, const String* pLimit, const String* pGroup, const String* pHaving)
39 {
40         String sql(L"SELECT ");
41         String empty;
42
43         if (pColumnList != null)
44         {
45                 int columnCount = pColumnList->GetCount();
46                 SysTryReturn(NID_IO, columnCount > 0, empty, E_INVALID_ARG,
47                                 "[E_INVALID_ARG] The specified pColumnList parameter is empty.");
48
49                 int i = 0;
50                 while (i < columnCount)
51                 {
52                         const String* pColumn = dynamic_cast< const String* >(pColumnList->GetAt(i));
53                         SysTryReturn(NID_IO, pColumn != null, empty, E_INVALID_ARG,
54                                         "[E_INVALID_ARG] The object is not a String class.");
55
56                         sql.Append(*pColumn);
57
58                         if (i < columnCount - 1)
59                         {
60                                 sql.Append(L", ");
61                         }
62                         i++;
63                 }
64         }
65         else
66         {
67                 sql.Append(L'*');
68         }
69
70         sql.Append(L" FROM ");
71         sql.Append(table);
72
73         if (pWhere != null)
74         {
75                 sql.Append(L" WHERE ");
76                 sql.Append(*pWhere);
77         }
78
79         if (pOrder != null)
80         {
81                 sql.Append(L" ORDER BY ");
82                 sql.Append(*pOrder);
83         }
84         SysTryReturn(NID_IO, !(pOrder == null && pLimit != null), empty, E_INVALID_ARG,
85                         "[E_INVALID_ARG] The specified pOrder parameter is null and pLimit parameter is not null.");
86
87         if (pLimit != null)
88         {
89                 sql.Append(L" LIMIT ");
90                 sql.Append(*pLimit);
91         }
92
93         if (pGroup != null)
94         {
95                 sql.Append(L" GROUP BY ");
96                 sql.Append(*pGroup);
97         }
98         SysTryReturn(NID_IO, !(pGroup == null && pHaving != null), empty, E_INVALID_ARG,
99                         "[E_INVALID_ARG] The specified pGroup parameter is null and pHaving parameter is not null.");
100
101         if (pHaving != null)
102         {
103                 sql.Append(L" HAVING ");
104                 sql.Append(*pHaving);
105         }
106
107         SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
108
109         SetLastResult(E_SUCCESS);
110         return sql;
111 }
112
113 String
114 SqlStatementBuilder::CreateInsertStatement(const String& table, const IMap& insertMap)
115 {
116         String sql(L"INSERT INTO ");
117         String sqlValue(L" VALUES (");
118         String empty;
119         String* pColumn = null;
120         String* pValue = null;
121         int columnCount = 0;
122         int i = 0;
123
124         columnCount = insertMap.GetCount();
125         SysTryReturn(NID_IO, columnCount > 0, empty, E_INVALID_ARG,
126                         "[E_INVALID_ARG] The specified insertMap parameter is empty.");
127
128         sql.Append(table);
129         sql.Append(L" (");
130
131         std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(&insertMap)->GetMapEnumeratorN());
132         while (pMapEnum->MoveNext() == E_SUCCESS)
133         {
134                 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
135                 SysTryReturn(NID_IO, pColumn != null, empty, E_INVALID_ARG,
136                                 "[E_INVALID_ARG] The object is not a String class.");
137
138                 sql.Append(*pColumn);
139
140                 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
141                 SysTryReturn(NID_IO, pValue != null, empty, E_INVALID_ARG,
142                                 "[E_INVALID_ARG] The object is not a String class.");
143
144                 sqlValue.Append(*pValue);
145
146                 if (i < columnCount - 1)
147                 {
148                         sql.Append(L", ");
149                         sqlValue.Append(L", ");
150                 }
151                 i++;
152         }
153
154         sql.Append(L')');
155         sqlValue.Append(L')');
156
157         sql.Append(sqlValue);
158
159         SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
160
161         SetLastResult(E_SUCCESS);
162         return sql;
163 }
164
165 String
166 SqlStatementBuilder::CreateUpdateStatement(const String& table, const IMap& updateMap, const String* pWhere)
167 {
168         String sql(L"UPDATE ");
169         String empty;
170         String* pColumn = null;
171         String* pValue = null;
172         int columnCount = 0;
173         int i = 0;
174
175         columnCount = updateMap.GetCount();
176         SysTryReturn(NID_IO, columnCount > 0, empty, E_INVALID_ARG,
177                         "[E_INVALID_ARG] The specified insertMap parameter is empty.");
178
179         sql.Append(table);
180         sql.Append(L" SET ");
181
182         std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(&updateMap)->GetMapEnumeratorN());
183         while (pMapEnum->MoveNext() == E_SUCCESS)
184         {
185                 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
186                 SysTryReturn(NID_IO, pColumn != null, empty, E_INVALID_ARG,
187                                 "[E_INVALID_ARG] The object is not a String class.");
188
189                 sql.Append(*pColumn);
190                 sql.Append(L" = ");
191
192                 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
193                 SysTryReturn(NID_IO, pValue != null, empty, E_INVALID_ARG,
194                                 "[E_INVALID_ARG] The object is not a String class.");
195
196                 sql.Append(*pValue);
197
198                 if (i < columnCount - 1)
199                 {
200                         sql.Append(L", ");
201                 }
202                 i++;
203         }
204
205         if (pWhere != null)
206         {
207                 sql.Append(L" WHERE ");
208                 sql.Append(*pWhere);
209         }
210
211         SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
212
213         SetLastResult(E_SUCCESS);
214         return sql;
215 }
216
217 String
218 SqlStatementBuilder::CreateDeleteStatement(const String& table, const String* pWhere)
219 {
220         String sql(L"DELETE FROM ");
221         result r = E_SUCCESS;
222
223         sql.Append(table);
224
225         if (pWhere != null)
226         {
227                 sql.Append(L" WHERE ");
228                 sql.Append(*pWhere);
229         }
230
231         SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
232
233         SetLastResult(r);
234         return sql;
235 }
236
237 }} // Tizen::Io
238