2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIoSqlStatementBuilder.cpp
19 * @brief This is the implementation for the %SqlStatementBuilder class.
21 #include <unique_ptr.h>
23 #include <FBaseString.h>
24 #include <FBaseColIList.h>
25 #include <FBaseColIMap.h>
26 #include <FIoSqlStatementBuilder.h>
28 #include <FBaseSysLog.h>
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
33 namespace Tizen { namespace Io
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)
40 String sql(L"SELECT ");
43 if (pColumnList != null)
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.");
50 while (i < columnCount)
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.");
58 if (i < columnCount - 1)
70 sql.Append(L" FROM ");
75 sql.Append(L" WHERE ");
81 sql.Append(L" ORDER BY ");
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.");
89 sql.Append(L" LIMIT ");
95 sql.Append(L" GROUP BY ");
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.");
103 sql.Append(L" HAVING ");
104 sql.Append(*pHaving);
107 SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
109 SetLastResult(E_SUCCESS);
114 SqlStatementBuilder::CreateInsertStatement(const String& table, const IMap& insertMap)
116 String sql(L"INSERT INTO ");
117 String sqlValue(L" VALUES (");
119 String* pColumn = null;
120 String* pValue = null;
124 columnCount = insertMap.GetCount();
125 SysTryReturn(NID_IO, columnCount > 0, empty, E_INVALID_ARG,
126 "[E_INVALID_ARG] The specified insertMap parameter is empty.");
131 std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(&insertMap)->GetMapEnumeratorN());
132 while (pMapEnum->MoveNext() == E_SUCCESS)
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.");
138 sql.Append(*pColumn);
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.");
144 sqlValue.Append(*pValue);
146 if (i < columnCount - 1)
149 sqlValue.Append(L", ");
155 sqlValue.Append(L')');
157 sql.Append(sqlValue);
159 SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
161 SetLastResult(E_SUCCESS);
166 SqlStatementBuilder::CreateUpdateStatement(const String& table, const IMap& updateMap, const String* pWhere)
168 String sql(L"UPDATE ");
170 String* pColumn = null;
171 String* pValue = null;
175 columnCount = updateMap.GetCount();
176 SysTryReturn(NID_IO, columnCount > 0, empty, E_INVALID_ARG,
177 "[E_INVALID_ARG] The specified insertMap parameter is empty.");
180 sql.Append(L" SET ");
182 std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(&updateMap)->GetMapEnumeratorN());
183 while (pMapEnum->MoveNext() == E_SUCCESS)
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.");
189 sql.Append(*pColumn);
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.");
198 if (i < columnCount - 1)
207 sql.Append(L" WHERE ");
211 SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());
213 SetLastResult(E_SUCCESS);
218 SqlStatementBuilder::CreateDeleteStatement(const String& table, const String* pWhere)
220 String sql(L"DELETE FROM ");
221 result r = E_SUCCESS;
227 sql.Append(L" WHERE ");
231 SysLog(NID_IO, "SQL statement: %ls", sql.GetPointer());