Refactor DataControl request list (2nd)
[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)
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 providerRes = 0;
343         bool providerResult = true;
344         //bundle* origBundle = null;
345         bundle* resBundle = null;
346         _SqlDataControlEventArg* pEventArg = null;
347         result r = E_SUCCESS;
348
349         SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
350         SysLog(NID_APP, "appsvc result value: %d", res);
351
352         resBundle = pResArg->GetBundle();
353         if (resBundle)
354         {
355                 const char* p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
356                 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
357                 reqId = atoi(p);
358                 Integer key(reqId);
359
360                 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
361                 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
362                 SysTryCatch(NID_APP, pReqInfo != null, r = E_SYSTEM, E_SYSTEM,
363                                 "[E_SYSTEM] Failed to get request info");
364
365                 _SqlDataControlEvent* pSqlDataControlEvent = dynamic_cast< _SqlDataControlEvent* >(pReqInfo->GetEvent());
366                 SysTryCatch(NID_APP, pSqlDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
367                                 "[E_SYSTEM] invalid request info");
368
369                 pDcMgr->RemoveRequestInfo(key);
370
371                 if (pSqlDataControlEvent != null && typeid(pSqlDataControlEvent) == typeid(_SqlDataControlEvent*))
372                 {
373                         // result list
374                         pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
375                         SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
376
377                         pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
378                         SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
379                         Integer::Parse(*pResult, providerRes);
380                         providerResult = static_cast< bool >(providerRes);
381
382                         pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
383                         SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
384                         pErrorMsg = new (std::nothrow) String(*pErrorMessage);
385                         SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
386                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
387
388                         // request info
389                         //origBundle = pArg->GetBundle();
390
391                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
392                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
393                         requestType = atoi(p);
394
395                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
396                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
397                         pProviderId = new (std::nothrow) String(p);
398                         SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
399                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
400
401                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
402                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
403                         pDataId = new (std::nothrow) String(p);
404                         SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
405                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
406
407                         SysLog(NID_APP, "[DC_CALLER_RECV] provider result: %ld, requestType: %d, reqId: %d, providerId: %ls, dataId: %ls, errorMsg: %ls ",
408                                         providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
409
410                         switch (static_cast <_DataControlRequestType>(requestType))
411                         {
412                         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
413                         {
414                                 pResultSetEnum = new (std::nothrow) _DataControlResultSetEnumerator;
415                                 SysTryCatch(NID_APP, pResultSetEnum, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
416                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
417
418                                 if (providerResult == true)
419                                 {
420                                         pTmpPath = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
421                                         if (pTmpPath == null)
422                                         {
423                                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
424                                                 delete pResultSetEnum;
425                                                 goto CATCH;
426                                         }
427
428                                         SysLog(NID_APP, "[DC_CALLER_RECV] tempPath: %ls", pTmpPath->GetPointer());
429                                         if (pTmpPath->Equals(L"NoResultSet", true) == false) // Result set exists
430                                         {
431                                                 pResultSetEnum->SetPath(*pTmpPath);
432                                         }
433                                 }
434
435                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, static_cast <RequestId>(reqId),
436                                                 *pProviderId, *pDataId, pResultSetEnum, -1, providerResult, pErrorMsg);
437                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
438                                 pSqlDataControlEvent->Fire(*pEventArg);
439
440                                 if (pTmpPath)
441                                 {
442                                         r = File::Remove(*pTmpPath); // Remove temporary file after releasing _ResultSetEnumerator.
443                                         SysTryLog(NID_APP, !IsFailed(r), "Failed to remove temp file for result set: %ls", pTmpPath->GetPointer());
444                                 }
445
446                                 break;
447                         }
448                         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
449                         {
450                                 long long insertRowId = -1;
451                                 if (providerResult == true)
452                                 {
453                                         pInsertRowId = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
454                                         SysTryCatch(NID_APP, pInsertRowId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
455
456                                         LongLong::Parse(*pInsertRowId, insertRowId);
457                                 }
458
459                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, static_cast <RequestId>(reqId),
460                                                 *pProviderId, *pDataId, null, insertRowId, providerResult, pErrorMsg);
461                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
462                                 pSqlDataControlEvent->Fire(*pEventArg);
463                                 break;
464                         }
465                         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
466                         {
467                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, static_cast <RequestId>(reqId),
468                                                 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
469                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
470                                 pSqlDataControlEvent->Fire(*pEventArg);
471                                 break;
472                         }
473                         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
474                         {
475                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, static_cast <RequestId>(reqId),
476                                                 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
477                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
478                                 pSqlDataControlEvent->Fire(*pEventArg);
479                                 break;
480                         }
481                         default:
482                                 break;
483                         }
484
485                         pResultList->RemoveAll(true);
486                         delete pResultList;
487                         delete pProviderId;
488                 }
489         }
490
491         // Remove the request count
492         SysLog(NID_APP, "Remove a launch request: reqId: %d", reqId);
493         _AppControlManager::GetInstance()->RemoveLaunchRequest(reqId);
494
495         return E_SUCCESS;
496
497 CATCH:
498         if (pResultList)
499         {
500                 pResultList->RemoveAll(true);
501                 delete pResultList;
502         }
503         delete pProviderId;
504
505         return r;
506 }
507
508 result
509 _SqlDataControlImpl::Insert(const String& dataId, const IMap& insertMap, RequestId& reqId)
510 {
511         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
512                         "The INSERT query is not permitted by DataControl provider.");
513
514         int columnCount = 0;
515         int id = 0;
516         int i = 0;
517         result r = E_SUCCESS;
518         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl INSERT");
519
520         columnCount = insertMap.GetCount();
521         SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
522
523         ArrayList* pArgList = new ArrayList();
524         pArgList->Construct();
525
526         pArgList->Add(*(new String(dataId))); // list(0): data ID
527         long long argSize = dataId.GetLength() * sizeof(wchar_t);
528
529         pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): inserted column count
530         SysLog(NID_APP, "[DC_CALLER_SEND] inserted column count: %d", columnCount);
531
532         IMapEnumerator* pMapEnum = const_cast< IMap* >(&insertMap)->GetMapEnumeratorN();
533         while (pMapEnum->MoveNext() == E_SUCCESS) // list(2): column-value pairs
534         {
535                 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
536                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
537                                 "[E_INVALID_ARG] The object is not String class.");
538
539                 pArgList->Add(*(new String(*pColumn)));
540                 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
541
542                 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
543                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
544                                 "[E_INVALID_ARG] The object is not String class.");
545
546                 pArgList->Add(*(new String(*pValue)));
547                 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
548
549                 argSize += pColumn->GetLength() * sizeof(wchar_t);
550                 argSize += pValue->GetLength() * sizeof(wchar_t);
551                 i++;
552         }
553         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
554                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
555
556         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, pArgList, &id);
557         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
558
559         reqId = static_cast< RequestId >(id);
560
561         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, insertMap: 0x%x, reqId: %d", dataId.GetPointer(), &insertMap, reqId);
562
563         // fall through
564 CATCH:
565         pArgList->RemoveAll(true);
566         delete pArgList;
567         delete pMapEnum;
568
569         return r;
570 }
571
572 result
573 _SqlDataControlImpl::Update(const String& dataId, const IMap& updateMap, const String* pWhere, RequestId& reqId)
574 {
575         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
576                         "The UPDATE query is not permitted by DataControl provider.");
577
578         int columnCount = 0;
579         int id = 0;
580         int i = 0;
581         result r = E_SUCCESS;
582         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl UPDATE");
583
584         columnCount = updateMap.GetCount();
585         SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
586
587         ArrayList* pArgList = new ArrayList();
588         pArgList->Construct();
589
590         pArgList->Add(*(new String(dataId))); // list(0): data ID
591         long long argSize = dataId.GetLength() * sizeof(wchar_t);
592
593         pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): updated column count
594         SysLog(NID_APP, "[DC_CALLER_SEND] updated column count: %d", columnCount);
595
596         IMapEnumerator* pMapEnum = const_cast< IMap* >(&updateMap)->GetMapEnumeratorN();
597         while (pMapEnum->MoveNext() == E_SUCCESS) // list(2): column-value pairs
598         {
599                 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
600                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
601                                 "[E_INVALID_ARG] The object is not String class.");
602
603                 pArgList->Add(*(new String(*pColumn)));
604                 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
605
606                 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
607                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
608                                 "[E_INVALID_ARG] The object is not String class.");
609
610                 pArgList->Add(*(new String(*pValue)));
611                 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
612
613                 argSize += pColumn->GetLength() * sizeof(wchar_t);
614                 argSize += pValue->GetLength() * sizeof(wchar_t);
615                 i++;
616         }
617
618         if (pWhere != null)     // list(3): where clause
619         {
620                 pArgList->Add(*(new String(*pWhere)));
621                 argSize += pWhere->GetLength() * sizeof(wchar_t);
622                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
623         }
624         else
625         {
626                 pArgList->Add(*(new String(L"NULL")));
627         }
628         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
629                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
630
631         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, pArgList, &id);
632         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
633
634         reqId = static_cast< RequestId >(id);
635
636         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, updateMap: 0x%x, pWhere: 0x%x, reqId: %d",
637                                 dataId.GetPointer(), &updateMap, pWhere, reqId);
638
639         // fall through
640 CATCH:
641         pArgList->RemoveAll(true);
642         delete pArgList;
643         delete pMapEnum;
644
645         return r;
646 }
647
648 result
649 _SqlDataControlImpl::Delete(const String& dataId, const String* pWhere, RequestId& reqId)
650 {
651         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
652                         "The DELETE query is not permitted by DataControl provider.");
653
654         int id = 0;
655         result r = E_SUCCESS;
656         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl DELETE");
657
658         ArrayList* pArgList = new ArrayList();
659         pArgList->Construct();
660
661         pArgList->Add(*(new String(dataId))); // list(0): data ID
662         long long argSize = dataId.GetLength() * sizeof(wchar_t);
663
664         if (pWhere != null)     // list(1): where clause
665         {
666                 pArgList->Add(*(new String(*pWhere)));
667                 argSize += pWhere->GetLength() * sizeof(wchar_t);
668                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
669         }
670         else
671         {
672                 pArgList->Add(*(new String(L"NULL")));
673         }
674         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
675                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
676
677         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, pArgList, &id);
678         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
679
680         reqId = static_cast< RequestId >(id);
681
682         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, pWhere: 0x%x, reqId: %d", dataId.GetPointer(), pWhere, reqId);
683
684         // fall through
685 CATCH:
686         pArgList->RemoveAll(true);
687         delete pArgList;
688
689         return r;
690 }
691
692 result
693 _SqlDataControlImpl::SetSqlDataControlResponseListener(ISqlDataControlResponseListener* pListener)
694 {
695         result r = E_SUCCESS;
696
697         if (__pPreviousListener != null)
698         {
699                 r =  __pSqlDataControlEvent->RemoveListener(*__pPreviousListener);
700                 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
701                  __pPreviousListener = null;
702         }
703
704         if (pListener != null)
705         {
706                 r =  __pSqlDataControlEvent->AddListener(*pListener);
707                 if (IsFailed(r))
708                 {
709                         switch (r)
710                         {
711                         case E_OBJ_ALREADY_EXIST:
712                                 return E_SUCCESS;
713                         case E_INVALID_OPERATION:
714                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
715                                 return E_SYSTEM;
716                         default:
717                                 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
718                                 return r;
719                         }
720                 }
721         }
722
723         __pPreviousListener = pListener;
724
725         return E_SUCCESS;
726 }
727
728 // private
729 SqlDataControl*
730 _SqlDataControlImpl::CreateSqlDataControl(const String& appId, const String& providerId, const String& access)
731 {
732         unique_ptr<SqlDataControl> pDc(new (std::nothrow) SqlDataControl);
733         SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
734
735         _SqlDataControlImpl* pDcImpl = _SqlDataControlImpl::GetInstance(*pDc);
736         pDcImpl->__appId = appId;
737         pDcImpl->__providerId = providerId;
738         unique_ptr<_SqlDataControlEvent> pSqlDataControlEvent(new (std::nothrow) _SqlDataControlEvent);
739         SysTryReturn(NID_IO, pSqlDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
740
741         pDcImpl->__pSqlDataControlEvent = pSqlDataControlEvent.release();
742
743         if (access == L"readonly")
744         {
745                 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
746         }
747         else if (access == L"writeonly")
748         {
749                 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
750         }
751         else if (access == L"readwrite")
752         {
753                 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
754         }
755         else
756         {
757                 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
758                 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
759         }
760
761         return pDc.release();
762 }
763
764 }} // Tizen::App
765