|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: StatementUtils |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "javacommonutils.h" |
|
20 #include "logger.h" |
|
21 #include "statementutils.h" |
|
22 |
|
23 using namespace java::storage; |
|
24 using namespace java::util; |
|
25 using namespace std; |
|
26 |
|
27 StatementUtils::StatementUtils() |
|
28 { |
|
29 } |
|
30 |
|
31 StatementUtils::~StatementUtils() |
|
32 { |
|
33 } |
|
34 |
|
35 void StatementUtils::createWhereStatement( |
|
36 const JavaStorageApplicationEntry_t& aEntry, |
|
37 wstring& aStatement) |
|
38 { |
|
39 aStatement.append(L" WHERE "); |
|
40 |
|
41 wstring insertion = L" AND "; |
|
42 JavaStorageApplicationEntry_t::const_iterator iter; |
|
43 for (iter = aEntry.begin(); iter != aEntry.end(); iter++) |
|
44 { |
|
45 aStatement.append((*iter).entryName()).append(L"="); |
|
46 addValueByType(aStatement, (*iter)); |
|
47 appendCond(aStatement, insertion, iter != --(aEntry.end())); |
|
48 } |
|
49 } |
|
50 |
|
51 void StatementUtils::appendCond( |
|
52 wstring& aStatement, const wstring& aValue, bool aCond) |
|
53 { |
|
54 if (aCond) |
|
55 { |
|
56 aStatement.append(aValue); |
|
57 } |
|
58 } |
|
59 |
|
60 void StatementUtils::createWriteStatement( |
|
61 const JavaStorageApplicationEntry_t& aEntry, |
|
62 const string& aTableName, |
|
63 const wstring& aInsertion, |
|
64 wstring& aStatement) |
|
65 { |
|
66 aStatement.append(L"INSERT INTO ") |
|
67 .append(JavaCommonUtils::utf8ToWstring(aTableName.c_str())) |
|
68 .append(L"("); |
|
69 |
|
70 JavaStorageApplicationEntry_t::const_iterator iter; |
|
71 |
|
72 for (iter = aEntry.begin(); iter != aEntry.end(); iter++) |
|
73 { |
|
74 aStatement.append((*iter).entryName()); |
|
75 appendCond(aStatement, aInsertion, iter != --(aEntry.end())); |
|
76 } |
|
77 |
|
78 aStatement.append(L") VALUES ("); |
|
79 |
|
80 for (iter = aEntry.begin(); iter != aEntry.end(); iter++) |
|
81 { |
|
82 addValueByType(aStatement, (*iter)); |
|
83 appendCond(aStatement, aInsertion, iter != --(aEntry.end())); |
|
84 } |
|
85 |
|
86 aStatement.append(L");"); |
|
87 } |
|
88 |
|
89 void StatementUtils::createTableStatement( |
|
90 const JavaStorageApplicationEntry_t& aEntry, |
|
91 const string& aTableName, |
|
92 const wstring& aInsertion, |
|
93 wstring& aStatement) |
|
94 { |
|
95 aStatement.append(L"CREATE TABLE ") |
|
96 .append(JavaCommonUtils::utf8ToWstring(aTableName.c_str())) |
|
97 .append(L"("); |
|
98 |
|
99 JavaStorageApplicationEntry_t::const_iterator colIter; |
|
100 for (colIter = aEntry.begin(); colIter != aEntry.end(); colIter++) |
|
101 { |
|
102 if ((*colIter).entryName() == L"") |
|
103 { |
|
104 throw JavaStorageException(EInvalidDataStructure, |
|
105 "Column name not defined", |
|
106 __FILE__, __FUNCTION__, __LINE__); |
|
107 } |
|
108 |
|
109 aStatement.append((*colIter).entryName()); |
|
110 |
|
111 if (JavaStorageEntry::STRING == (*colIter).entryType()) |
|
112 { |
|
113 aStatement.append(L" varchar"); |
|
114 } |
|
115 else if (JavaStorageEntry::INT == (*colIter).entryType()) |
|
116 { |
|
117 aStatement.append(L" int"); |
|
118 } |
|
119 else |
|
120 { |
|
121 ELOG(EJavaStorage, "Unknown column type"); |
|
122 throw JavaStorageException(EInvalidDataStructure, |
|
123 "Unknown column type", |
|
124 __FILE__, __FUNCTION__, __LINE__); |
|
125 } |
|
126 |
|
127 appendCond(aStatement, aInsertion, colIter != --(aEntry.end())); |
|
128 } |
|
129 |
|
130 aStatement.append(L");"); |
|
131 } |
|
132 |
|
133 void StatementUtils::createSearchStatement( |
|
134 const JavaStorageApplicationEntry_t& aEntry, |
|
135 const std::string& aTableName, |
|
136 std::wstring& aStatement) |
|
137 { |
|
138 wstring selectStatement; |
|
139 wstring searchPattern; |
|
140 |
|
141 JavaStorageApplicationEntry_t::const_iterator colIter; |
|
142 |
|
143 for (colIter = aEntry.begin(); colIter != aEntry.end(); colIter++) |
|
144 { |
|
145 // Value is used as search query. |
|
146 if (((*colIter).entryValue()).size() > 0) |
|
147 { |
|
148 searchPattern.append((*colIter).entryName()).append(L"="); |
|
149 addValueByType(searchPattern, (*colIter)); |
|
150 searchPattern += L" AND "; |
|
151 } |
|
152 else |
|
153 { |
|
154 selectStatement.append((*colIter).entryName()).append(L", "); |
|
155 } |
|
156 } |
|
157 |
|
158 if (selectStatement.size() > 0) |
|
159 { |
|
160 // Remove trailing ',' string. Preserve white space |
|
161 selectStatement.erase((selectStatement.size() - 2), 1); |
|
162 } |
|
163 else |
|
164 { |
|
165 selectStatement = L"*"; // Select * search; |
|
166 } |
|
167 |
|
168 aStatement.append(L"SELECT ").append(selectStatement).append(L" FROM ") |
|
169 .append(JavaCommonUtils::utf8ToWstring(aTableName.c_str())); |
|
170 |
|
171 if (searchPattern.size() > 0) |
|
172 { |
|
173 // Remove railing ' AND' string. |
|
174 searchPattern.erase((searchPattern.size() - 5), 5); |
|
175 |
|
176 // Define search pattern |
|
177 aStatement.append(L" WHERE " + searchPattern + L";"); |
|
178 } |
|
179 else |
|
180 { |
|
181 aStatement.append(L";"); |
|
182 } |
|
183 } |
|
184 |
|
185 void StatementUtils::createUpdateStatement( |
|
186 const JavaStorageApplicationEntry_t& aUpdate, |
|
187 const JavaStorageApplicationEntry_t& aMatch, |
|
188 const std::string& aTableName, |
|
189 std::wstring& aStatement) |
|
190 { |
|
191 aStatement.append(L"UPDATE ") |
|
192 .append(JavaCommonUtils::utf8ToWstring(aTableName.c_str())) |
|
193 .append(L" SET "); |
|
194 |
|
195 JavaStorageApplicationEntry_t::const_iterator colIter; |
|
196 wstring insertion(L", "); |
|
197 |
|
198 for (colIter = aUpdate.begin(); |
|
199 colIter != aUpdate.end(); |
|
200 colIter++) |
|
201 { |
|
202 aStatement.append((*colIter).entryName()).append(L"="); |
|
203 addValueByType(aStatement, (*colIter)); |
|
204 appendCond( |
|
205 aStatement, insertion, colIter != --(aUpdate.end())); |
|
206 } |
|
207 |
|
208 createWhereStatement(aMatch, aStatement); |
|
209 } |
|
210 |
|
211 void StatementUtils::addValueByType(wstring& aStatement, |
|
212 const JavaStorageEntry& aEntry) |
|
213 { |
|
214 if (aEntry.entryType() == JavaStorageEntry::STRING |
|
215 || aEntry.entryType() == JavaStorageEntry::NOT_DEFINED) |
|
216 { |
|
217 aStatement.append(L"'"); |
|
218 addValue(aStatement, aEntry); |
|
219 aStatement.append(L"'"); |
|
220 } |
|
221 else if (aEntry.entryType() == JavaStorageEntry::INT) |
|
222 { |
|
223 addValue(aStatement, aEntry); |
|
224 } |
|
225 else |
|
226 { |
|
227 aStatement.append(L"NULL"); |
|
228 } |
|
229 } |
|
230 |
|
231 void StatementUtils::addValue(wstring& aStatement, |
|
232 const JavaStorageEntry& aEntry) |
|
233 { |
|
234 wstring val = aEntry.entryValue(); |
|
235 escape(val); |
|
236 aStatement.append(val); |
|
237 } |
|
238 |
|
239 void StatementUtils::escape(wstring& aUnescaped) |
|
240 { |
|
241 wstring escapedChar = L"\'"; |
|
242 wstring::size_type idx = 0; |
|
243 if ((idx = aUnescaped.find(escapedChar)) != string::npos) |
|
244 { |
|
245 do |
|
246 { |
|
247 aUnescaped.insert(idx, escapedChar); |
|
248 idx += escapedChar.size() + 1; |
|
249 } |
|
250 while ((idx = aUnescaped.find(escapedChar, idx)) != string::npos); |
|
251 } |
|
252 } |
|
253 |