|
1 // sql.cpp |
|
2 // |
|
3 // Copyright (c) 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "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 // Accenture - Initial contribution |
|
11 // |
|
12 #include <fshell/common.mmh> |
|
13 |
|
14 #include <fshell/ioutils.h> |
|
15 using namespace IoUtils; |
|
16 |
|
17 class TResponseLex |
|
18 { |
|
19 public: |
|
20 TResponseLex(const TDesC& aResponse); |
|
21 |
|
22 TInt GetIntColon(TInt& aValue); |
|
23 TInt GetStringColon(TPtrC& aValue); |
|
24 TInt GetIntEof(TInt& aValue); |
|
25 TPtrC Remainder(); |
|
26 private: |
|
27 TPtrC iRemainder; |
|
28 }; |
|
29 |
|
30 class CCmdSqlCli : public CClientBase |
|
31 { |
|
32 public: |
|
33 /* |
|
34 static const TDesC* PowerConsumptionToString(TPositionQuality::TPowerConsumption aPower); |
|
35 static void CapabilitiesToString(TPositionModuleInfo::TCapabilities aCap, TDes &aDes); |
|
36 */ |
|
37 void PrintTime(const TTime& aTime, TBool aNewline); |
|
38 static CCommandBase* NewLC(); |
|
39 ~CCmdSqlCli(); |
|
40 private: |
|
41 enum TLineProcessingBehaviour |
|
42 { |
|
43 EIgnoreLines, |
|
44 EProcessLines, |
|
45 }; |
|
46 |
|
47 CCmdSqlCli(); |
|
48 |
|
49 void ProcessLineL(const TDesC& aLine); |
|
50 TBool MatchLineStart(const TDesC& aPrefix, TPtrC& aRemainder); |
|
51 void ProcessErrorL(const TDesC& aDes); |
|
52 void ProcessFailureL(const TDesC& aDes); |
|
53 void UnrecognizedLineL(const TDesC& aDes); |
|
54 void ProcessAsyncIdL(const TDesC& aDes); |
|
55 void ProcessTimeoutL(const TDesC& aDes); |
|
56 void ProcessResultL(const TDesC& aDes); |
|
57 |
|
58 void ProcessLogL(const TDesC& aDes); |
|
59 |
|
60 |
|
61 private: // From CCommandBase. |
|
62 virtual const TDesC& Name() const; |
|
63 virtual void OptionsL(RCommandOptionList& aOptions); |
|
64 // From CClientBase. |
|
65 virtual void HandleServerResponseL(RPointerArray<HBufC> aLines); |
|
66 }; |
|
67 |
|
68 /////////////////////////////////////////////////////////////////////////////// |
|
69 |
|
70 _LIT(KPersistentConsoleName, "fshell_sqlsrv"); |
|
71 _LIT(KSrvExeName, "fshell_sqlsrv"); |
|
72 _LIT(KNewLine, "\r\n"); |
|
73 _LIT(KPrompt, "ok\r\n"); |
|
74 _LIT(KErrorPrefix, "error:"); |
|
75 _LIT(KFailurePrefix, "failure:"); |
|
76 _LIT(KAsyncIdPrefix, "id:"); |
|
77 _LIT(KTimeoutPrefix, "timedout:"); |
|
78 _LIT(KResultPrefix, "result:"); |
|
79 _LIT(KLogPrefix, "log:"); |
|
80 _LIT(KColon, ":"); |
|
81 |
|
82 |
|
83 CCommandBase* CCmdSqlCli::NewLC() |
|
84 { |
|
85 CCmdSqlCli* self = new (ELeave)CCmdSqlCli(); |
|
86 CleanupStack::PushL(self); |
|
87 self->BaseConstructL(); |
|
88 return self; |
|
89 } |
|
90 |
|
91 CCmdSqlCli::CCmdSqlCli() |
|
92 : CClientBase(0, KSrvExeName, KPersistentConsoleName, KPrompt) |
|
93 { |
|
94 //iActiveWait = new (ELeave) CActiveSchedulerWait; |
|
95 } |
|
96 |
|
97 |
|
98 CCmdSqlCli::~CCmdSqlCli() |
|
99 { |
|
100 } |
|
101 |
|
102 ////////////////////////////////////////////////////// |
|
103 |
|
104 |
|
105 ////////////////////////////////////////////////////// |
|
106 |
|
107 const TDesC& CCmdSqlCli::Name() const |
|
108 { |
|
109 _LIT(KName, "sql"); |
|
110 return KName; |
|
111 } |
|
112 |
|
113 void CCmdSqlCli::OptionsL(RCommandOptionList& aOptions) |
|
114 { |
|
115 _LIT(KVerbose, "verbose"); |
|
116 aOptions.AppendBoolL(iVerbose, KVerbose); |
|
117 } |
|
118 |
|
119 void CCmdSqlCli::HandleServerResponseL(RPointerArray<HBufC> aLines) |
|
120 { |
|
121 const TInt numLines = aLines.Count(); |
|
122 for (TInt i = 0; i < numLines; ++i) |
|
123 { |
|
124 ProcessLineL(*aLines[i]); |
|
125 } |
|
126 } |
|
127 |
|
128 TBool CCmdSqlCli::MatchLineStart(const TDesC& aPrefix, TPtrC& aRemainder) |
|
129 { |
|
130 if (aRemainder.Left(aPrefix.Length()).Compare(aPrefix)==0) |
|
131 { |
|
132 aRemainder.Set(aRemainder.Mid(aPrefix.Length())); |
|
133 return ETrue; |
|
134 } |
|
135 return EFalse; |
|
136 } |
|
137 |
|
138 void CCmdSqlCli::UnrecognizedLineL(const TDesC& aDes) |
|
139 { |
|
140 if (!iVerbose) |
|
141 { |
|
142 Write(aDes); |
|
143 Write(KNewLine); |
|
144 } |
|
145 } |
|
146 |
|
147 void CCmdSqlCli::ProcessLogL(const TDesC& aDes) |
|
148 { |
|
149 TResponseLex lex(aDes); |
|
150 TInt id; |
|
151 TInt err = lex.GetIntColon(id); |
|
152 if (err != KErrNone) |
|
153 { |
|
154 UnrecognizedLineL(aDes); |
|
155 } |
|
156 } |
|
157 |
|
158 void CCmdSqlCli::ProcessLineL(const TDesC& aLine) |
|
159 { |
|
160 TPtrC remainder(aLine); |
|
161 if (MatchLineStart(KErrorPrefix, remainder)) |
|
162 { |
|
163 ProcessErrorL(remainder); |
|
164 } |
|
165 else if (MatchLineStart(KFailurePrefix, remainder)) |
|
166 { |
|
167 ProcessFailureL(remainder); |
|
168 } |
|
169 else if (MatchLineStart(KAsyncIdPrefix, remainder)) |
|
170 { |
|
171 ProcessAsyncIdL(remainder); |
|
172 } |
|
173 else if (MatchLineStart(KTimeoutPrefix, remainder)) |
|
174 { |
|
175 ProcessTimeoutL(remainder); |
|
176 } |
|
177 else if (MatchLineStart(KResultPrefix, remainder)) |
|
178 { |
|
179 ProcessResultL(remainder); |
|
180 } |
|
181 else if (MatchLineStart(KLogPrefix, remainder)) |
|
182 { |
|
183 ProcessLogL(remainder); |
|
184 } |
|
185 else |
|
186 { |
|
187 UnrecognizedLineL(aLine); |
|
188 } |
|
189 } |
|
190 |
|
191 void CCmdSqlCli::ProcessErrorL(const TDesC& aDes) |
|
192 { |
|
193 TResponseLex lex(aDes); |
|
194 TInt commandId; |
|
195 TInt errorFromServer; |
|
196 TInt err = lex.GetIntColon(errorFromServer); |
|
197 if (err == KErrNone) |
|
198 { |
|
199 err = lex.GetIntColon(commandId); |
|
200 } |
|
201 |
|
202 if (err == KErrNone) |
|
203 { |
|
204 TPtrC remander = lex.Remainder(); |
|
205 LeaveIfErr(errorFromServer, remander); |
|
206 } |
|
207 else |
|
208 { |
|
209 UnrecognizedLineL(aDes); |
|
210 } |
|
211 } |
|
212 |
|
213 void CCmdSqlCli::ProcessFailureL(const TDesC&) |
|
214 { |
|
215 LeaveIfErr(KErrGeneral, _L("Failed")); |
|
216 } |
|
217 |
|
218 void CCmdSqlCli::ProcessAsyncIdL(const TDesC& aDes) |
|
219 { |
|
220 TResponseLex lex(aDes); |
|
221 TInt id; |
|
222 TInt err = lex.GetIntEof(id); |
|
223 if (err != KErrNone) |
|
224 { |
|
225 UnrecognizedLineL(aDes); |
|
226 } |
|
227 } |
|
228 |
|
229 void CCmdSqlCli::ProcessTimeoutL(const TDesC& aDes) |
|
230 { |
|
231 TResponseLex lex(aDes); |
|
232 TInt id; |
|
233 TInt err = lex.GetIntEof(id); |
|
234 if (err == KErrNone) |
|
235 { |
|
236 LeaveIfErr(KErrTimedOut, _L("Command %d timed-out"), id); |
|
237 } |
|
238 else |
|
239 { |
|
240 UnrecognizedLineL(aDes); |
|
241 } |
|
242 } |
|
243 |
|
244 void CCmdSqlCli::ProcessResultL(const TDesC& aDes) |
|
245 { |
|
246 TResponseLex lex(aDes); |
|
247 TInt id; |
|
248 TInt err = lex.GetIntColon(id); |
|
249 if (err == KErrNone) |
|
250 { |
|
251 if (id == 0) |
|
252 { |
|
253 TPtrC name; |
|
254 err = lex.GetStringColon(name); |
|
255 if (err == KErrNone) |
|
256 { |
|
257 Write(lex.Remainder()); |
|
258 Write(KNewLine); |
|
259 } |
|
260 } |
|
261 } |
|
262 if (err != KErrNone) |
|
263 { |
|
264 UnrecognizedLineL(aDes); |
|
265 } |
|
266 } |
|
267 |
|
268 |
|
269 |
|
270 EXE_BOILER_PLATE(CCmdSqlCli) |
|
271 |
|
272 #define CASE_MODELIT(x) case x: { _LIT(KName, #x); pString = &KName; break; } |
|
273 |
|
274 void CCmdSqlCli::PrintTime(const TTime& aTime, TBool aNewline) |
|
275 { |
|
276 TTime NullTime = Time::NullTTime(); |
|
277 if (aTime == NullTime) |
|
278 { |
|
279 Printf(_L("(NullTime)")); |
|
280 } |
|
281 else |
|
282 { |
|
283 _LIT8(KDateTimeFormat, "%d-%02d-%02d %02d:%02d:%02d"); |
|
284 TDateTime dt = aTime.DateTime(); |
|
285 Printf(KDateTimeFormat, dt.Year(), dt.Month()+1, dt.Day()+1, dt.Hour(), dt.Minute(), dt.Second()); |
|
286 } |
|
287 |
|
288 if (aNewline) Printf(_L("\r\n")); |
|
289 } |
|
290 |
|
291 |
|
292 /////////////////////////////////////////////////////////////////////////////// |
|
293 /////////////////////////////////////////////////////////////////////////////// |
|
294 TResponseLex::TResponseLex(const TDesC& aResponse) |
|
295 : iRemainder(aResponse) |
|
296 { |
|
297 } |
|
298 |
|
299 TInt TResponseLex::GetIntColon(TInt& aValue) |
|
300 { |
|
301 TInt pos = iRemainder.Find(KColon); |
|
302 if (pos == KErrNotFound) |
|
303 { |
|
304 return pos; |
|
305 } |
|
306 TPtrC intStr(iRemainder.Left(pos)); |
|
307 TLex lex(intStr); |
|
308 TInt err = lex.Val(aValue); |
|
309 if (err!=KErrNone) return err; |
|
310 if (lex.Remainder().Length()) return KErrArgument; |
|
311 iRemainder.Set(iRemainder.Mid(pos+1)); |
|
312 return KErrNone; |
|
313 } |
|
314 |
|
315 TInt TResponseLex::GetStringColon(TPtrC& aValue) |
|
316 { |
|
317 TInt pos = iRemainder.Find(KColon); |
|
318 if (pos == KErrNotFound) |
|
319 { |
|
320 return pos; |
|
321 } |
|
322 aValue.Set(iRemainder.Left(pos)); |
|
323 iRemainder.Set(iRemainder.Mid(pos+1)); |
|
324 return KErrNone; |
|
325 } |
|
326 |
|
327 TInt TResponseLex::GetIntEof(TInt& aValue) |
|
328 { |
|
329 TLex lex(iRemainder); |
|
330 TInt err = lex.Val(aValue); |
|
331 if (err==KErrNone) |
|
332 { |
|
333 if (lex.Remainder().Length()) err = KErrArgument; |
|
334 } |
|
335 if (err==KErrNone) |
|
336 { |
|
337 iRemainder.Set(lex.Remainder()); |
|
338 } |
|
339 return err; |
|
340 } |
|
341 |
|
342 TPtrC TResponseLex::Remainder() |
|
343 { |
|
344 return iRemainder; |
|
345 } |