2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIoSqlStatementBuilder.cpp
20 * @brief This is the implementation for the %SqlStatementBuilder class.
22 #include <unique_ptr.h>
24 #include <FBaseString.h>
25 #include <FBaseColIList.h>
26 #include <FBaseColIMap.h>
27 #include <FIoSqlStatementBuilder.h>
29 #include <FBaseSysLog.h>
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
34 namespace Tizen { namespace Io
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)
41 String sql(L"SELECT ");
44 if (pColumnList != null)
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.");
51 while (i < columnCount)
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.");
59 if (i < columnCount - 1)
71 sql.Append(L" FROM ");
76 sql.Append(L" WHERE ");
82 sql.Append(L" ORDER BY ");
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.");
90 sql.Append(L" LIMIT ");
96 sql.Append(L" GROUP BY ");
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.");
104 sql.Append(L" HAVING ");
105 sql.Append(*pHaving);
108 SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
110 SetLastResult(E_SUCCESS);
115 SqlStatementBuilder::CreateInsertStatement(const String& table, const IMap& insertMap)
117 String sql(L"INSERT INTO ");
118 String sqlValue(L" VALUES (");
120 String* pColumn = null;
121 String* pValue = null;
125 columnCount = insertMap.GetCount();
126 SysTryReturn(NID_IO, columnCount > 0, empty, E_INVALID_ARG,
127 "[E_INVALID_ARG] The specified insertMap parameter is empty.");
132 std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(&insertMap)->GetMapEnumeratorN());
133 while (pMapEnum->MoveNext() == E_SUCCESS)
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.");
139 sql.Append(*pColumn);
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.");
145 sqlValue.Append(*pValue);
147 if (i < columnCount - 1)
150 sqlValue.Append(L", ");
156 sqlValue.Append(L')');
158 sql.Append(sqlValue);
160 SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
162 SetLastResult(E_SUCCESS);
167 SqlStatementBuilder::CreateUpdateStatement(const String& table, const IMap& updateMap, const String* pWhere)
169 String sql(L"UPDATE ");
171 String* pColumn = null;
172 String* pValue = null;
176 columnCount = updateMap.GetCount();
177 SysTryReturn(NID_IO, columnCount > 0, empty, E_INVALID_ARG,
178 "[E_INVALID_ARG] The specified insertMap parameter is empty.");
181 sql.Append(L" SET ");
183 std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(&updateMap)->GetMapEnumeratorN());
184 while (pMapEnum->MoveNext() == E_SUCCESS)
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.");
190 sql.Append(*pColumn);
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.");
199 if (i < columnCount - 1)
208 sql.Append(L" WHERE ");
212 SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
214 SetLastResult(E_SUCCESS);
219 SqlStatementBuilder::CreateDeleteStatement(const String& table, const String* pWhere)
221 String sql(L"DELETE FROM ");
222 result r = E_SUCCESS;
228 sql.Append(L" WHERE ");
232 SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());