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