Merge "GetErrorMessage() to print the error log" into tizen_2.1
[platform/framework/native/appfw.git] / src / app / FApp_SqlDataControlImpl.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        FApp_SqlDataControlImpl.cpp
20  * @brief       This is the implementation for the %_SqlDataControlImpl class.
21  */
22
23 #include <typeinfo>
24 #include <new>
25 #include <unique_ptr.h>
26
27 #include <appsvc/appsvc.h>
28
29 #include <FBaseInteger.h>
30 #include <FBaseString.h>
31 #include <FBaseLongLong.h>
32 #include <FBaseColIList.h>
33 #include <FBaseColIMap.h>
34 #include <FBaseColIMapEnumerator.h>
35 #include <FBaseRtIEventArg.h>
36 #include <FIoFile.h>
37 #include <FAppSqlDataControl.h>
38 #include <FAppISqlDataControlResponseListener.h>
39
40 #include <FBaseSysLog.h>
41
42 #include <FIo_DataControlResultSetEnumerator.h>
43
44 #include "FApp_AppControlManager.h"
45 #include "FApp_SqlDataControlImpl.h"
46 #include "FApp_AppArg.h"
47 #include "FApp_DataControlManager.h"
48
49 using namespace std;
50
51 using namespace Tizen::Base;
52 using namespace Tizen::Base::Collection;
53 using namespace Tizen::Base::Runtime;
54 using namespace Tizen::App;
55 using namespace Tizen::Io;
56
57 namespace Tizen { namespace App
58 {
59
60 static const int MAX_REQUEST_COUNT = 128;
61 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
62
63 class _SqlDataControlEventArg
64         : public IEventArg
65 {
66 public:
67                 _SqlDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
68                                 _DataControlResultSetEnumerator* pResultSetEnum, long long insertRowId, bool providerResult, String* pErrorMsg)
69                         : __requestType(requestType)
70                         , __reqId(reqId)
71                         , __providerId(providerId)
72                         , __dataId(dataId)
73                         , __pResultSetEnum(pResultSetEnum)
74                         , __insertRowId(insertRowId)
75                         , __providerResult(providerResult)
76                         , __pErrorMsg(pErrorMsg)
77                 {
78                 }
79                 ~_SqlDataControlEventArg(void)
80                 {
81                         delete __pResultSetEnum;
82                         delete __pErrorMsg;
83                 }
84
85                 _DataControlRequestType __requestType;
86                 RequestId __reqId;
87                 String __providerId;
88                 String __dataId;
89                 _DataControlResultSetEnumerator* __pResultSetEnum;
90                 long long __insertRowId;
91                 bool __providerResult;
92                 String* __pErrorMsg;
93 };
94
95 class _SqlDataControlEvent
96         : public Event
97 {
98 protected:
99                 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
100 };
101
102 void
103 _SqlDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
104 {
105         const _SqlDataControlEventArg* pArg = dynamic_cast<const _SqlDataControlEventArg*>(&arg);
106         if (pArg != null)
107         {
108                 ISqlDataControlResponseListener* pListener = dynamic_cast<ISqlDataControlResponseListener*> (&listener);
109                 if (pListener != null)
110                 {
111                         switch (pArg->__requestType)
112                         {
113                         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
114                                 pListener->OnSqlDataControlSelectResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
115                                                 *(pArg->__pResultSetEnum), pArg->__providerResult, pArg->__pErrorMsg);
116                                 break;
117                         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
118                                 pListener->OnSqlDataControlInsertResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
119                                                  pArg->__insertRowId, pArg->__providerResult, pArg->__pErrorMsg);
120                                 break;
121                         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
122                                 pListener->OnSqlDataControlUpdateResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
123                                                 pArg->__providerResult, pArg->__pErrorMsg);
124                                 break;
125                         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
126                                 pListener->OnSqlDataControlDeleteResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
127                                                 pArg->__providerResult, pArg->__pErrorMsg);
128                                 break;
129                         default:
130                                 break;
131                         }
132                 }
133         }
134 }
135
136 // private
137 _SqlDataControlImpl::_SqlDataControlImpl(void)
138         : __appId(L"")
139         , __providerId(L"")
140         , __access(_DATACONTROL_ACCESS_UNDEFINED)
141         , __pPreviousListener(null)
142         , __pSqlDataControlEvent(null)
143 {
144 }
145
146 _SqlDataControlImpl::~_SqlDataControlImpl(void)
147 {
148         delete __pSqlDataControlEvent;
149 }
150
151 _SqlDataControlImpl*
152 _SqlDataControlImpl::GetInstance(SqlDataControl& dc)
153 {
154         return dc.__pSqlDataControlImpl;
155 }
156
157 const _SqlDataControlImpl*
158 _SqlDataControlImpl::GetInstance(const SqlDataControl& dc)
159 {
160         return dc.__pSqlDataControlImpl;
161 }
162
163 result
164 _SqlDataControlImpl::StartSqlDataControl(int type, const IList* pDataList, int* pReq)
165 {
166         Integer* pReqId = null;
167         _DataControlRequestInfo* pReqInfo = null;
168         result r = E_SUCCESS;
169
170         int req = -1;
171         _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
172
173         // Check the request count of DataControl operation
174         int count = pAppManagerImpl->GetLaunchRequestCount();
175         SysLog(NID_APP, "Current launch request count: %d", count);
176
177         SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
178
179         _AppArg* pArg = new (std::nothrow) _AppArg;  // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
180         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "insufficient memory");
181
182         pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
183
184         if (__pSqlDataControlEvent)
185         {
186                 // reqId is system-wide id because the bundle is system-wide.
187 #if 0
188                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, __pSqlDataControlEvent, -1);
189                 req = reqObj.GetRequestNumber();
190 #else
191                 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
192                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, pDcMgr, -1);
193                 req = reqObj.GetRequestNumber();
194 #endif
195                 pReqId = new (std::nothrow) Integer(req);
196                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
197                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
198
199                 pReqInfo = new (std::nothrow) _DataControlRequestInfo(__pSqlDataControlEvent);
200                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
201                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
202
203                 r = pDcMgr->AddRequestInfo(pReqId, pReqInfo);
204                 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to add request info", GetErrorMessage(r));
205
206                 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
207                 if (IsFailed(r))
208                 {
209                         SysPropagate(NID_APP, r);
210                         reqObj.Invalidate();
211                         pDcMgr->RemoveRequestInfo(*pReqId);
212                         delete pArg;
213                         return r;
214                 }
215
216                 if (pReq)
217                 {
218                         *pReq = req;
219                 }
220         }
221         else
222         {
223                 r = pAppManagerImpl->LaunchApp(__appId, pArg);
224                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
225                 delete pArg;
226         }
227
228         return E_SUCCESS;
229
230 CATCH:
231         delete pArg;
232         delete pReqId;
233         delete pReqInfo;
234         return r;
235 }
236
237 result
238 _SqlDataControlImpl::Select(const String& dataId, const IList* pColumnList, const String* pWhere,
239                 const String *pOrder, RequestId& reqId, int pageNo, int countPerPage)
240 {
241         SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
242         SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
243         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
244                         "The SELECT query is not permitted by DataControl provider.");
245
246         const String* pColumn = null;
247         int id = 0;
248         result r = E_SUCCESS;
249         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl SELECT");
250
251         ArrayList* pArgList = new ArrayList();
252         pArgList->Construct();
253
254         pArgList->Add(*(new String(dataId))); // list(0): data ID
255         long long argSize = dataId.GetLength() * sizeof(wchar_t);
256
257         if (pColumnList != null)
258         {
259                 int columnCount = pColumnList->GetCount();
260                 SysTryCatch(NID_APP, columnCount > 0, r = E_INVALID_ARG, E_INVALID_ARG,
261                                 "[E_INVALID_ARG] The specified pColumnList parameter is empty.");
262
263                 pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): selected column count
264                 SysLog(NID_APP, "[DC_CALLER_SEND] selected column count: %d", columnCount);
265
266                 int i = 0;
267                 while (i < columnCount) // list(2): column list
268                 {
269                         pColumn = dynamic_cast< const String* >(pColumnList->GetAt(i));
270                         SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
271                                         "[E_INVALID_ARG] The object is not String class.");
272
273                         pArgList->Add(*(new String(*pColumn)));
274                         argSize += pColumn->GetLength() * sizeof(wchar_t);
275                         SysLog(NID_APP, "[DC_CALLER_SEND] column[%d]: %ls", i, pColumn->GetPointer());
276                         i++;
277                 }
278         }
279         else
280         {
281                 pArgList->Add(*(new String(L"NULL")));
282         }
283
284         if (pWhere != null)     // list(3): where clause
285         {
286                 pArgList->Add(*(new String(*pWhere)));
287                 argSize += pWhere->GetLength() * sizeof(wchar_t);
288                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
289         }
290         else
291         {
292                 pArgList->Add(*(new String(L"NULL")));
293         }
294
295         if (pOrder != null)     // list(4): order clause
296         {
297                 pArgList->Add(*(new String(*pOrder)));
298                 argSize += pOrder->GetLength() * sizeof(wchar_t);
299                 SysLog(NID_APP, "[DC_CALLER_SEND] pOrder: %ls", pOrder->GetPointer());
300         }
301         else
302         {
303                 pArgList->Add(*(new String(L"NULL")));
304         }
305         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
306                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
307
308         pArgList->Add(*(new String(Integer::ToString(pageNo)))); // list(5): page number
309
310         pArgList->Add(*(new String(Integer::ToString(countPerPage)))); // list(6): count per page
311
312         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, pArgList, &id);
313         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
314
315         reqId = static_cast< RequestId >(id);
316
317         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, pColumnList: 0x%x, pWhere: 0x%x, pOrder: 0x%x, reqId: %d, pageNo: %d, countPerPage: %d",
318                         dataId.GetPointer(), pColumnList, pWhere, pOrder, reqId, pageNo, countPerPage);
319
320         // fall through
321 CATCH:
322         pArgList->RemoveAll(true);
323         delete pArgList;
324
325         return r;
326 }
327
328 result
329 _SqlDataControlImpl::SqlDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int option)
330 {
331         ArrayList* pResultList = null;
332         String* pResult = null;
333         String* pProviderId = null;
334         String* pDataId = null;
335         String* pErrorMessage = null;
336         String* pErrorMsg = null;
337         String* pTmpPath = null;
338         String* pInsertRowId = null;
339         _DataControlResultSetEnumerator* pResultSetEnum = null;
340         int requestType = 0;
341         int reqId = 0;
342         int launchReqId = 0;
343         int providerRes = 0;
344         bool providerResult = true;
345         //bundle* origBundle = null;
346         bundle* resBundle = null;
347         _SqlDataControlEventArg* pEventArg = null;
348         const char* p = null;
349         result r = E_SUCCESS;
350
351         SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
352         SysLog(NID_APP, "appsvc result value: %d", res);
353
354         resBundle = pResArg->GetBundle();
355         if (resBundle)
356         {
357                 p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
358                 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
359                 reqId = atoi(p);
360                 Integer key(reqId);
361
362                 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
363                 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
364                 SysTryCatch(NID_APP, pReqInfo != null, r = E_SYSTEM, E_SYSTEM,
365                                 "[E_SYSTEM] Failed to get request info");
366
367                 _SqlDataControlEvent* pSqlDataControlEvent = dynamic_cast< _SqlDataControlEvent* >(pReqInfo->GetEvent());
368                 SysTryCatch(NID_APP, pSqlDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
369                                 "[E_SYSTEM] invalid request info");
370
371                 pDcMgr->RemoveRequestInfo(key);
372
373                 if (pSqlDataControlEvent != null && typeid(pSqlDataControlEvent) == typeid(_SqlDataControlEvent*))
374                 {
375                         // result list
376                         pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
377                         SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
378
379                         pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
380                         SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
381                         Integer::Parse(*pResult, providerRes);
382                         providerResult = static_cast< bool >(providerRes);
383
384                         pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
385                         SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
386                         pErrorMsg = new (std::nothrow) String(*pErrorMessage);
387                         SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
388                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
389
390                         // request info
391                         //origBundle = pArg->GetBundle();
392
393                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
394                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
395                         requestType = atoi(p);
396
397                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
398                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
399                         pProviderId = new (std::nothrow) String(p);
400                         SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
401                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
402
403                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
404                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
405                         pDataId = new (std::nothrow) String(p);
406                         SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
407                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
408
409                         SysLog(NID_APP, "[DC_CALLER_RECV] provider result: %ld, requestType: %d, reqId: %d, providerId: %ls, dataId: %ls, errorMsg: %ls ",
410                                         providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
411
412                         switch (static_cast <_DataControlRequestType>(requestType))
413                         {
414                         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
415                         {
416                                 pResultSetEnum = new (std::nothrow) _DataControlResultSetEnumerator;
417                                 SysTryCatch(NID_APP, pResultSetEnum, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
418                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
419
420                                 if (providerResult == true)
421                                 {
422                                         pTmpPath = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
423                                         if (pTmpPath == null)
424                                         {
425                                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
426                                                 delete pResultSetEnum;
427                                                 goto CATCH;
428                                         }
429
430                                         SysLog(NID_APP, "[DC_CALLER_RECV] tempPath: %ls", pTmpPath->GetPointer());
431                                         if (pTmpPath->Equals(L"NoResultSet", true) == false) // Result set exists
432                                         {
433                                                 pResultSetEnum->SetPath(*pTmpPath);
434                                         }
435                                 }
436
437                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, static_cast <RequestId>(reqId),
438                                                 *pProviderId, *pDataId, pResultSetEnum, -1, providerResult, pErrorMsg);
439                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
440                                 pSqlDataControlEvent->Fire(*pEventArg);
441
442                                 if (pTmpPath)
443                                 {
444                                         r = File::Remove(*pTmpPath); // Remove temporary file after releasing _ResultSetEnumerator.
445                                         SysTryLog(NID_APP, !IsFailed(r), "Failed to remove temp file for result set: %ls", pTmpPath->GetPointer());
446                                 }
447
448                                 break;
449                         }
450                         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
451                         {
452                                 long long insertRowId = -1;
453                                 if (providerResult == true)
454                                 {
455                                         pInsertRowId = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
456                                         SysTryCatch(NID_APP, pInsertRowId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
457
458                                         LongLong::Parse(*pInsertRowId, insertRowId);
459                                 }
460
461                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, static_cast <RequestId>(reqId),
462                                                 *pProviderId, *pDataId, null, insertRowId, providerResult, pErrorMsg);
463                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
464                                 pSqlDataControlEvent->Fire(*pEventArg);
465                                 break;
466                         }
467                         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
468                         {
469                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, static_cast <RequestId>(reqId),
470                                                 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
471                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
472                                 pSqlDataControlEvent->Fire(*pEventArg);
473                                 break;
474                         }
475                         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
476                         {
477                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, static_cast <RequestId>(reqId),
478                                                 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
479                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
480                                 pSqlDataControlEvent->Fire(*pEventArg);
481                                 break;
482                         }
483                         default:
484                                 break;
485                         }
486
487                         pResultList->RemoveAll(true);
488                         delete pResultList;
489                         delete pProviderId;
490                 }
491         }
492
493         p = appsvc_get_data(pArg->GetBundle(), OSP_K_REQUEST_ID);
494         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
495         launchReqId = atoi(p);
496
497         // Remove the request count
498         SysLog(NID_APP, "Remove a launch request: reqId: %d", launchReqId);
499         _AppControlManager::GetInstance()->RemoveLaunchRequest(launchReqId);
500
501         return E_SUCCESS;
502
503 CATCH:
504         if (pResultList)
505         {
506                 pResultList->RemoveAll(true);
507                 delete pResultList;
508         }
509         delete pProviderId;
510
511         return r;
512 }
513
514 result
515 _SqlDataControlImpl::Insert(const String& dataId, const IMap& insertMap, RequestId& reqId)
516 {
517         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
518                         "The INSERT query is not permitted by DataControl provider.");
519
520         int columnCount = 0;
521         int id = 0;
522         int i = 0;
523         result r = E_SUCCESS;
524         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl INSERT");
525
526         columnCount = insertMap.GetCount();
527         SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
528
529         ArrayList* pArgList = new ArrayList();
530         pArgList->Construct();
531
532         pArgList->Add(*(new String(dataId))); // list(0): data ID
533         long long argSize = dataId.GetLength() * sizeof(wchar_t);
534
535         pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): inserted column count
536         SysLog(NID_APP, "[DC_CALLER_SEND] inserted column count: %d", columnCount);
537
538         IMapEnumerator* pMapEnum = const_cast< IMap* >(&insertMap)->GetMapEnumeratorN();
539         while (pMapEnum->MoveNext() == E_SUCCESS) // list(2): column-value pairs
540         {
541                 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
542                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
543                                 "[E_INVALID_ARG] The object is not String class.");
544
545                 pArgList->Add(*(new String(*pColumn)));
546                 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
547
548                 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
549                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
550                                 "[E_INVALID_ARG] The object is not String class.");
551
552                 pArgList->Add(*(new String(*pValue)));
553                 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
554
555                 argSize += pColumn->GetLength() * sizeof(wchar_t);
556                 argSize += pValue->GetLength() * sizeof(wchar_t);
557                 i++;
558         }
559         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
560                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
561
562         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, pArgList, &id);
563         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
564
565         reqId = static_cast< RequestId >(id);
566
567         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, insertMap: 0x%x, reqId: %d", dataId.GetPointer(), &insertMap, reqId);
568
569         // fall through
570 CATCH:
571         pArgList->RemoveAll(true);
572         delete pArgList;
573         delete pMapEnum;
574
575         return r;
576 }
577
578 result
579 _SqlDataControlImpl::Update(const String& dataId, const IMap& updateMap, const String* pWhere, RequestId& reqId)
580 {
581         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
582                         "The UPDATE query is not permitted by DataControl provider.");
583
584         int columnCount = 0;
585         int id = 0;
586         int i = 0;
587         result r = E_SUCCESS;
588         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl UPDATE");
589
590         columnCount = updateMap.GetCount();
591         SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
592
593         ArrayList* pArgList = new ArrayList();
594         pArgList->Construct();
595
596         pArgList->Add(*(new String(dataId))); // list(0): data ID
597         long long argSize = dataId.GetLength() * sizeof(wchar_t);
598
599         pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): updated column count
600         SysLog(NID_APP, "[DC_CALLER_SEND] updated column count: %d", columnCount);
601
602         IMapEnumerator* pMapEnum = const_cast< IMap* >(&updateMap)->GetMapEnumeratorN();
603         while (pMapEnum->MoveNext() == E_SUCCESS) // list(2): column-value pairs
604         {
605                 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
606                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
607                                 "[E_INVALID_ARG] The object is not String class.");
608
609                 pArgList->Add(*(new String(*pColumn)));
610                 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
611
612                 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
613                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
614                                 "[E_INVALID_ARG] The object is not String class.");
615
616                 pArgList->Add(*(new String(*pValue)));
617                 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
618
619                 argSize += pColumn->GetLength() * sizeof(wchar_t);
620                 argSize += pValue->GetLength() * sizeof(wchar_t);
621                 i++;
622         }
623
624         if (pWhere != null)     // list(3): where clause
625         {
626                 pArgList->Add(*(new String(*pWhere)));
627                 argSize += pWhere->GetLength() * sizeof(wchar_t);
628                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
629         }
630         else
631         {
632                 pArgList->Add(*(new String(L"NULL")));
633         }
634         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
635                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
636
637         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, pArgList, &id);
638         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
639
640         reqId = static_cast< RequestId >(id);
641
642         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, updateMap: 0x%x, pWhere: 0x%x, reqId: %d",
643                                 dataId.GetPointer(), &updateMap, pWhere, reqId);
644
645         // fall through
646 CATCH:
647         pArgList->RemoveAll(true);
648         delete pArgList;
649         delete pMapEnum;
650
651         return r;
652 }
653
654 result
655 _SqlDataControlImpl::Delete(const String& dataId, const String* pWhere, RequestId& reqId)
656 {
657         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
658                         "The DELETE query is not permitted by DataControl provider.");
659
660         int id = 0;
661         result r = E_SUCCESS;
662         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl DELETE");
663
664         ArrayList* pArgList = new ArrayList();
665         pArgList->Construct();
666
667         pArgList->Add(*(new String(dataId))); // list(0): data ID
668         long long argSize = dataId.GetLength() * sizeof(wchar_t);
669
670         if (pWhere != null)     // list(1): where clause
671         {
672                 pArgList->Add(*(new String(*pWhere)));
673                 argSize += pWhere->GetLength() * sizeof(wchar_t);
674                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
675         }
676         else
677         {
678                 pArgList->Add(*(new String(L"NULL")));
679         }
680         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
681                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
682
683         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, pArgList, &id);
684         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
685
686         reqId = static_cast< RequestId >(id);
687
688         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, pWhere: 0x%x, reqId: %d", dataId.GetPointer(), pWhere, reqId);
689
690         // fall through
691 CATCH:
692         pArgList->RemoveAll(true);
693         delete pArgList;
694
695         return r;
696 }
697
698 result
699 _SqlDataControlImpl::SetSqlDataControlResponseListener(ISqlDataControlResponseListener* pListener)
700 {
701         result r = E_SUCCESS;
702
703         if (__pPreviousListener != null)
704         {
705                 r =  __pSqlDataControlEvent->RemoveListener(*__pPreviousListener);
706                 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
707                  __pPreviousListener = null;
708         }
709
710         if (pListener != null)
711         {
712                 r =  __pSqlDataControlEvent->AddListener(*pListener);
713                 if (IsFailed(r))
714                 {
715                         switch (r)
716                         {
717                         case E_OBJ_ALREADY_EXIST:
718                                 return E_SUCCESS;
719                         case E_INVALID_OPERATION:
720                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
721                                 return E_SYSTEM;
722                         default:
723                                 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
724                                 return r;
725                         }
726                 }
727         }
728
729         __pPreviousListener = pListener;
730
731         return E_SUCCESS;
732 }
733
734 // private
735 SqlDataControl*
736 _SqlDataControlImpl::CreateSqlDataControl(const String& appId, const String& providerId, const String& access)
737 {
738         unique_ptr<SqlDataControl> pDc(new (std::nothrow) SqlDataControl);
739         SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
740
741         _SqlDataControlImpl* pDcImpl = _SqlDataControlImpl::GetInstance(*pDc);
742         pDcImpl->__appId = appId;
743         pDcImpl->__providerId = providerId;
744         unique_ptr<_SqlDataControlEvent> pSqlDataControlEvent(new (std::nothrow) _SqlDataControlEvent);
745         SysTryReturn(NID_IO, pSqlDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
746
747         pDcImpl->__pSqlDataControlEvent = pSqlDataControlEvent.release();
748
749         if (access == L"readonly")
750         {
751                 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
752         }
753         else if (access == L"writeonly")
754         {
755                 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
756         }
757         else if (access == L"readwrite")
758         {
759                 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
760         }
761         else
762         {
763                 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
764                 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
765         }
766
767         return pDc.release();
768 }
769
770 }} // Tizen::App
771