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 FApp_SqlDataControlImpl.cpp
19 * @brief This is the implementation for the %_SqlDataControlImpl class.
23 #include <unique_ptr.h>
25 #include <appsvc/appsvc.h>
27 #include <FBaseInteger.h>
28 #include <FBaseString.h>
29 #include <FBaseLongLong.h>
30 #include <FBaseColLinkedList.h>
31 #include <FBaseColIList.h>
32 #include <FBaseColIMap.h>
33 #include <FBaseColIMapEnumerator.h>
34 #include <FBaseRtIEventArg.h>
35 #include <FBaseSysLog.h>
38 #include <FAppSqlDataControl.h>
39 #include <FAppISqlDataControlResponseListener.h>
41 #include <FBase_StringConverter.h>
42 #include <FIo_DataControlResultSetEnumerator.h>
44 #include "FApp_AppControlManager.h"
45 #include "FApp_SqlDataControlImpl.h"
46 #include "FApp_AppArg.h"
47 #include "FApp_DataControlManager.h"
48 #include "FAppPkg_PackageManagerImpl.h"
50 #define DATACONTROL_PROTOCOL_VER_2_1_0_2 // ver_2.1.0.2
51 #define DATACONTROL_PROTOCOL_VER_2_1_0_3 // ver_2.1.0.3
55 using namespace Tizen::Base;
56 using namespace Tizen::Base::Collection;
57 using namespace Tizen::Base::Runtime;
58 using namespace Tizen::Io;
59 using namespace Tizen::App;
60 using namespace Tizen::App::Package;
62 namespace Tizen { namespace App
65 static const int MAX_REQUEST_COUNT = 128;
66 static const int REQUEST_THRESHOLD = 100;
67 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
68 static const int _MAX_REQUEST_ARGUMENT_SIZE = 1048576; // 1MB
69 //static const char* _DATACONTROL_REQUEST_DIR = "/tmp/osp/DataControlRequest/\0";
70 static const char* _DATACONTROL_REQUEST_DIR = "/tmp/osp/data-control/request/\0";
72 class _SqlDataControlEventArg
76 _SqlDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
77 _DataControlResultSetEnumerator* pResultSetEnum, long long insertRowId, bool providerResult, String* pErrorMsg)
78 : __requestType(requestType)
80 , __providerId(providerId)
82 , __pResultSetEnum(pResultSetEnum)
83 , __insertRowId(insertRowId)
84 , __providerResult(providerResult)
85 , __pErrorMsg(pErrorMsg)
88 ~_SqlDataControlEventArg(void)
90 delete __pResultSetEnum;
94 _DataControlRequestType __requestType;
98 _DataControlResultSetEnumerator* __pResultSetEnum;
99 long long __insertRowId;
100 bool __providerResult;
104 class _SqlDataControlEvent
108 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
112 _SqlDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
114 const _SqlDataControlEventArg* pArg = dynamic_cast<const _SqlDataControlEventArg*>(&arg);
117 ISqlDataControlResponseListener* pListener = dynamic_cast<ISqlDataControlResponseListener*> (&listener);
118 if (pListener != null)
120 switch (pArg->__requestType)
122 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
123 pListener->OnSqlDataControlSelectResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
124 *(pArg->__pResultSetEnum), pArg->__providerResult, pArg->__pErrorMsg);
126 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
127 pListener->OnSqlDataControlInsertResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
128 pArg->__insertRowId, pArg->__providerResult, pArg->__pErrorMsg);
130 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
131 pListener->OnSqlDataControlUpdateResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
132 pArg->__providerResult, pArg->__pErrorMsg);
134 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
135 pListener->OnSqlDataControlDeleteResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
136 pArg->__providerResult, pArg->__pErrorMsg);
146 _SqlDataControlImpl::_SqlDataControlImpl(void)
147 : __access(_DATACONTROL_ACCESS_UNDEFINED)
148 , __pPreviousListener(null)
149 , __pSqlDataControlEvent(null)
153 _SqlDataControlImpl::~_SqlDataControlImpl(void)
155 delete __pSqlDataControlEvent;
157 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
159 IEnumerator* pEnum = __pRequestList->GetEnumeratorN();
160 while (pEnum->MoveNext() == E_SUCCESS)
162 Integer* pReqId = dynamic_cast< Integer* >(pEnum->GetCurrent());
165 pDcMgr->RemoveRequestInfo(*pReqId);
168 delete __pRequestList;
172 _SqlDataControlImpl::GetInstance(SqlDataControl& dc)
174 return dc.__pSqlDataControlImpl;
177 const _SqlDataControlImpl*
178 _SqlDataControlImpl::GetInstance(const SqlDataControl& dc)
180 return dc.__pSqlDataControlImpl;
184 _SqlDataControlImpl::StartSqlDataControl(int type, const IList* pDataList, int* pReq)
186 Integer* pReqId = null;
187 _DataControlRequestInfo* pReqInfo = null;
188 result r = E_SUCCESS;
191 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
193 // Check the request count of DataControl operation
194 int count = pDcMgr->GetRequestCount();
195 SysLog(NID_APP, "Current launch request count: %d", count);
197 if (count > REQUEST_THRESHOLD)
199 _AppManagerImpl* pImpl = _AppManagerImpl::GetInstance();
201 // Clear the request queue if the provider is terminated
202 if (!pImpl->IsRunning(__appId))
204 SysLog(NID_APP, "The request queue is cleared due to the invalid provider.");
206 pDcMgr->RemoveAllRequests();
210 SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
212 _AppArg* pArg = new (std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
213 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "insufficient memory");
215 pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
217 _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
219 if (__pSqlDataControlEvent)
221 // reqId is system-wide id because the bundle is system-wide.
223 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, __pSqlDataControlEvent, -1);
224 req = reqObj.GetRequestNumber();
226 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, pDcMgr, -1);
227 req = reqObj.GetRequestNumber();
229 pReqId = new (std::nothrow) Integer(req);
230 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
231 "[E_OUT_OF_MEMORY] The memory is insufficient");
233 pReqInfo = new (std::nothrow) _DataControlRequestInfo(__pSqlDataControlEvent);
234 SysTryCatch(NID_APP, pReqInfo != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
235 "[E_OUT_OF_MEMORY] The memory is insufficient");
236 pReqInfo->SetSqlDataControlImpl(this);
238 r = pDcMgr->AddRequestInfo(pReqId, pReqInfo);
239 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to add request info", GetErrorMessage(r));
241 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
244 SysPropagate(NID_APP, r);
246 pDcMgr->RemoveRequestInfo(*pReqId);
250 __pRequestList->Add(new (std::nothrow) Integer(req));
259 r = pAppManagerImpl->LaunchApp(__appId, pArg);
260 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
274 _SqlDataControlImpl::Select(const String& dataId, const IList* pColumnList, const String* pWhere,
275 const String *pOrder, RequestId& reqId, int pageNo, int countPerPage)
277 SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
278 SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
279 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
280 "The SELECT query is not permitted by DataControl provider.");
282 const String* pColumn = null;
284 result r = E_SUCCESS;
285 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl SELECT");
287 ArrayList* pArgList = new ArrayList();
288 pArgList->Construct();
290 pArgList->Add(*(new String(dataId))); // list(0): data ID
291 long long argSize = dataId.GetLength() * sizeof(wchar_t);
293 if (pColumnList != null)
295 int columnCount = pColumnList->GetCount();
296 SysTryCatch(NID_APP, columnCount > 0, r = E_INVALID_ARG, E_INVALID_ARG,
297 "[E_INVALID_ARG] The specified pColumnList parameter is empty.");
299 pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): selected column count
300 SysLog(NID_APP, "[DC_CALLER_SEND] selected column count: %d", columnCount);
303 while (i < columnCount) // list(2): column list
305 pColumn = dynamic_cast< const String* >(pColumnList->GetAt(i));
306 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
307 "[E_INVALID_ARG] The object is not String class.");
309 pArgList->Add(*(new String(*pColumn)));
310 argSize += pColumn->GetLength() * sizeof(wchar_t);
311 SysLog(NID_APP, "[DC_CALLER_SEND] column[%d]: %ls", i, pColumn->GetPointer());
317 pArgList->Add(*(new String(L"NULL")));
320 if (pWhere != null) // list(3): where clause
322 pArgList->Add(*(new String(*pWhere)));
323 argSize += pWhere->GetLength() * sizeof(wchar_t);
324 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
328 pArgList->Add(*(new String(L"NULL")));
331 if (pOrder != null) // list(4): order clause
333 pArgList->Add(*(new String(*pOrder)));
334 argSize += pOrder->GetLength() * sizeof(wchar_t);
335 SysLog(NID_APP, "[DC_CALLER_SEND] pOrder: %ls", pOrder->GetPointer());
339 pArgList->Add(*(new String(L"NULL")));
341 SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
342 "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
344 pArgList->Add(*(new String(Integer::ToString(pageNo)))); // list(5): page number
346 pArgList->Add(*(new String(Integer::ToString(countPerPage)))); // list(6): count per page
348 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, pArgList, &id);
349 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
351 reqId = static_cast< RequestId >(id);
353 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pColumnList: 0x%x, pWhere: 0x%x, pOrder: 0x%x, req: %d, pageNo: %d, countPerPage: %d",
354 dataId.GetPointer(), pColumnList, pWhere, pOrder, reqId, pageNo, countPerPage);
358 pArgList->RemoveAll(true);
365 _SqlDataControlImpl::SqlDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int option)
367 ArrayList* pResultList = null;
368 String* pResult = null;
369 String* pProviderId = null;
370 String* pDataId = null;
371 String* pErrorMessage = null;
372 String* pErrorMsg = null;
373 String* pTmpPath = null;
374 String* pInsertRowId = null;
375 _DataControlResultSetEnumerator* pResultSetEnum = null;
380 bool providerResult = true;
381 //bundle* origBundle = null;
382 bundle* resBundle = null;
383 _SqlDataControlEventArg* pEventArg = null;
384 const char* p = null;
385 result r = E_SUCCESS;
387 SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
388 SysLog(NID_APP, "appsvc result value: %d", res);
390 resBundle = pResArg->GetBundle();
393 p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
394 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
398 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
399 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
400 if (pReqInfo == null)
402 SysLog(NID_APP, "No request info of reqId %d", reqId);
406 _SqlDataControlEvent* pSqlDataControlEvent = dynamic_cast< _SqlDataControlEvent* >(pReqInfo->GetEvent());
407 SysTryCatch(NID_APP, pSqlDataControlEvent != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request info");
409 pDcMgr->RemoveRequestInfo(key);
411 _SqlDataControlImpl* pDcImpl = pReqInfo->GetSqlDataControlImpl();
412 SysTryCatch(NID_APP, pDcImpl != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request info");
413 pDcImpl->__pRequestList->Remove(key);
415 // Remove the request from the queue
416 SysLog(NID_APP, "Remove the request, req: %d", reqId);
418 if (pSqlDataControlEvent != null && typeid(pSqlDataControlEvent) == typeid(_SqlDataControlEvent*))
421 pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
422 SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
424 pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
425 SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
426 Integer::Parse(*pResult, providerRes);
427 providerResult = static_cast< bool >(providerRes);
429 pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
430 SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
431 pErrorMsg = new (std::nothrow) String(*pErrorMessage);
432 SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
433 "[E_OUT_OF_MEMORY] The memory is insufficient.");
436 //origBundle = pArg->GetBundle();
438 p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
439 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
440 requestType = atoi(p);
442 p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
443 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
444 pProviderId = new (std::nothrow) String(p);
445 SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
446 "[E_OUT_OF_MEMORY] The memory is insufficient.");
448 p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
449 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
450 pDataId = new (std::nothrow) String(p);
451 SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
452 "[E_OUT_OF_MEMORY] The memory is insufficient.");
454 SysSecureLog(NID_APP, "[DC_CALLER_RECV] provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls ",
455 providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
457 switch (static_cast <_DataControlRequestType>(requestType))
459 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
461 pResultSetEnum = new (std::nothrow) _DataControlResultSetEnumerator;
462 SysTryCatch(NID_APP, pResultSetEnum, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
463 "[E_OUT_OF_MEMORY] The memory is insufficient.");
465 if (providerResult == true)
467 pTmpPath = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
468 if (pTmpPath == null)
470 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
471 delete pResultSetEnum;
475 SysLog(NID_APP, "[DC_CALLER_RECV] tempPath: %ls", pTmpPath->GetPointer());
476 if (pTmpPath->Equals(L"NoResultSet", true) == false) // Result set exists
478 pResultSetEnum->SetPath(*pTmpPath);
482 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, static_cast <RequestId>(reqId),
483 *pProviderId, *pDataId, pResultSetEnum, -1, providerResult, pErrorMsg);
484 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
485 pSqlDataControlEvent->Fire(*pEventArg);
489 r = File::Remove(*pTmpPath); // Remove temporary file after releasing _ResultSetEnumerator.
490 SysTryLog(NID_APP, !IsFailed(r), "Failed to remove temp file for result set: %ls", pTmpPath->GetPointer());
495 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
497 long long insertRowId = -1;
498 if (providerResult == true)
500 pInsertRowId = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
501 SysTryCatch(NID_APP, pInsertRowId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
503 LongLong::Parse(*pInsertRowId, insertRowId);
506 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, static_cast <RequestId>(reqId),
507 *pProviderId, *pDataId, null, insertRowId, providerResult, pErrorMsg);
508 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
509 pSqlDataControlEvent->Fire(*pEventArg);
512 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
514 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, static_cast <RequestId>(reqId),
515 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
516 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
517 pSqlDataControlEvent->Fire(*pEventArg);
520 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
522 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, static_cast <RequestId>(reqId),
523 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
524 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
525 pSqlDataControlEvent->Fire(*pEventArg);
532 pResultList->RemoveAll(true);
538 p = appsvc_get_data(pArg->GetBundle(), OSP_K_REQUEST_ID);
539 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
540 launchReqId = atoi(p);
547 pResultList->RemoveAll(true);
556 _SqlDataControlImpl::Insert(const String& dataId, const IMap& insertMap, RequestId& reqId)
558 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
559 "The INSERT query is not permitted by DataControl provider.");
565 IMapEnumerator* pMapEnum = null;
566 _PackageManagerImpl* pPkgMgrImpl = null;
567 unique_ptr< String > pProviderAppId(null);
568 result r = E_SUCCESS;
569 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl INSERT");
571 columnCount = insertMap.GetCount();
572 SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
574 ArrayList* pArgList = new (std::nothrow) ArrayList();
575 pArgList->Construct();
577 pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
578 long long argSize = dataId.GetLength() * sizeof(wchar_t);
580 pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: inserted column count
581 SysLog(NID_APP, "[DC_CALLER_SEND] inserted column count: %d", columnCount);
583 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
584 String tmpPath(_DATACONTROL_REQUEST_DIR);
585 tmpPath.Append(App::GetInstance()->GetAppId());
587 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
588 SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
589 uniqueId = pDcMgr->GetUniqueId();
591 tmpPath.Append(uniqueId);
592 SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
594 r = request.Construct(tmpPath, L"w+", true);
595 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
596 GetErrorMessage(r), tmpPath.GetPointer());
598 pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path
601 pMapEnum = const_cast< IMap* >(&insertMap)->GetMapEnumeratorN();
602 while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
604 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
606 String* pColumn = null;
607 String* pValue = null;
608 unique_ptr< char[] > pData(null);
610 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
611 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
612 "[E_INVALID_ARG] The object is not String class.");
614 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
615 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
616 GetErrorMessage(GetLastResult()));
618 length = strlen(pData.get());
619 r = request.Write(&length, sizeof(int)); // data length
620 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
622 r = request.Write(pData.get(), length); // data
623 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
625 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
626 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
627 "[E_INVALID_ARG] The object is not String class.");
629 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
630 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
631 GetErrorMessage(GetLastResult()));
633 length = strlen(pData.get());
634 r = request.Write(&length, sizeof(int)); // data length
635 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
637 r = request.Write(pData.get(), length); // data
638 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
640 argSize += pColumn->GetLength() * sizeof(wchar_t);
641 argSize += pValue->GetLength() * sizeof(wchar_t);
643 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
644 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
645 "[E_INVALID_ARG] The object is not String class.");
647 pArgList->Add(*(new String(*pColumn)));
648 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
650 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
651 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
652 "[E_INVALID_ARG] The object is not String class.");
654 pArgList->Add(*(new String(*pValue)));
655 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
657 argSize += pColumn->GetLength() * sizeof(wchar_t);
658 argSize += pValue->GetLength() * sizeof(wchar_t);
662 SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
663 "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
665 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
668 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_3
669 pPkgMgrImpl = _PackageManagerImpl::GetInstance();
670 pProviderAppId.reset(pPkgMgrImpl->GetAppIdOfDataControlN(this->__providerId.GetPointer()));
671 SysTryCatch(NID_APP, pProviderAppId != null, r = E_SYSTEM, E_SYSTEM,
672 "[E_SYSTEM] The method cannot proceed due to a severe system error.");
673 r = pDcMgr->AllowAccess(*(pProviderAppId.get()));
674 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
677 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, pArgList, &id);
678 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
680 reqId = static_cast< RequestId >(id);
682 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, insertMap: 0x%x, req: %d", dataId.GetPointer(), &insertMap, reqId);
686 pArgList->RemoveAll(true);
694 _SqlDataControlImpl::Update(const String& dataId, const IMap& updateMap, const String* pWhere, RequestId& reqId)
696 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
697 "The UPDATE query is not permitted by DataControl provider.");
703 IMapEnumerator* pMapEnum = null;
704 _PackageManagerImpl* pPkgMgrImpl = null;
705 unique_ptr< String > pProviderAppId(null);
706 result r = E_SUCCESS;
707 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl UPDATE");
709 columnCount = updateMap.GetCount();
710 SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
712 ArrayList* pArgList = new ArrayList();
713 pArgList->Construct();
715 pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
716 long long argSize = dataId.GetLength() * sizeof(wchar_t);
718 pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: updated column count
719 SysLog(NID_APP, "[DC_CALLER_SEND] updated column count: %d", columnCount);
721 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
722 String tmpPath(_DATACONTROL_REQUEST_DIR);
723 tmpPath.Append(App::GetInstance()->GetAppId());
725 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
726 SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
727 uniqueId = pDcMgr->GetUniqueId();
729 tmpPath.Append(uniqueId);
730 SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
732 r = request.Construct(tmpPath, L"w+", true);
733 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
734 GetErrorMessage(r), tmpPath.GetPointer());
736 pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path
739 pMapEnum = const_cast< IMap* >(&updateMap)->GetMapEnumeratorN();
740 while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
742 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
744 String* pColumn = null;
745 String* pValue = null;
746 unique_ptr< char[] > pData(null);
748 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
749 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
750 "[E_INVALID_ARG] The object is not String class.");
752 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
753 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
754 GetErrorMessage(GetLastResult()));
756 length = strlen(pData.get());
757 r = request.Write(&length, sizeof(int)); // data length
758 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
760 r = request.Write(pData.get(), length); // data
761 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
763 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
764 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
765 "[E_INVALID_ARG] The object is not String class.");
767 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
768 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
769 GetErrorMessage(GetLastResult()));
771 length = strlen(pData.get());
772 r = request.Write(&length, sizeof(int)); // data length
773 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
775 r = request.Write(pData.get(), length); // data
776 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
778 argSize += pColumn->GetLength() * sizeof(wchar_t);
779 argSize += pValue->GetLength() * sizeof(wchar_t);
781 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
782 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
783 "[E_INVALID_ARG] The object is not String class.");
785 pArgList->Add(*(new String(*pColumn)));
786 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
788 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
789 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
790 "[E_INVALID_ARG] The object is not String class.");
792 pArgList->Add(*(new String(*pValue)));
793 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
795 argSize += pColumn->GetLength() * sizeof(wchar_t);
796 argSize += pValue->GetLength() * sizeof(wchar_t);
801 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
805 if (pWhere != null) // list: where clause
807 pArgList->Add(new (std::nothrow) String(*pWhere));
808 argSize += pWhere->GetLength() * sizeof(wchar_t);
809 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
813 pArgList->Add(new (std::nothrow) String(L"NULL"));
815 SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
816 "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
818 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_3
819 pPkgMgrImpl = _PackageManagerImpl::GetInstance();
820 pProviderAppId.reset(pPkgMgrImpl->GetAppIdOfDataControlN(this->__providerId.GetPointer()));
821 SysTryCatch(NID_APP, pProviderAppId != null, r = E_SYSTEM, E_SYSTEM,
822 "[E_SYSTEM] The method cannot proceed due to a severe system error.");
823 r = pDcMgr->AllowAccess(*(pProviderAppId.get()));
824 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
827 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, pArgList, &id);
828 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
830 reqId = static_cast< RequestId >(id);
832 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, updateMap: 0x%x, pWhere: 0x%x, req: %d",
833 dataId.GetPointer(), &updateMap, pWhere, reqId);
837 pArgList->RemoveAll(true);
845 _SqlDataControlImpl::Delete(const String& dataId, const String* pWhere, RequestId& reqId)
847 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
848 "The DELETE query is not permitted by DataControl provider.");
851 result r = E_SUCCESS;
852 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl DELETE");
854 ArrayList* pArgList = new ArrayList();
855 pArgList->Construct();
857 pArgList->Add(*(new String(dataId))); // list(0): data ID
858 long long argSize = dataId.GetLength() * sizeof(wchar_t);
860 if (pWhere != null) // list(1): where clause
862 pArgList->Add(*(new String(*pWhere)));
863 argSize += pWhere->GetLength() * sizeof(wchar_t);
864 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
868 pArgList->Add(*(new String(L"NULL")));
870 SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
871 "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
873 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, pArgList, &id);
874 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
876 reqId = static_cast< RequestId >(id);
878 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pWhere: 0x%x, req: %d", dataId.GetPointer(), pWhere, reqId);
882 pArgList->RemoveAll(true);
889 _SqlDataControlImpl::SetSqlDataControlResponseListener(ISqlDataControlResponseListener* pListener)
891 result r = E_SUCCESS;
893 if (__pPreviousListener != null)
895 r = __pSqlDataControlEvent->RemoveListener(*__pPreviousListener);
896 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
897 __pPreviousListener = null;
900 if (pListener != null)
902 r = __pSqlDataControlEvent->AddListener(*pListener);
907 case E_OBJ_ALREADY_EXIST:
909 case E_INVALID_OPERATION:
910 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
913 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
919 __pPreviousListener = pListener;
926 _SqlDataControlImpl::CreateSqlDataControl(const String& appId, const String& providerId, const String& access)
928 unique_ptr<SqlDataControl> pDc(new (std::nothrow) SqlDataControl);
929 SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
931 _SqlDataControlImpl* pDcImpl = _SqlDataControlImpl::GetInstance(*pDc);
932 pDcImpl->__appId = appId;
933 pDcImpl->__providerId = providerId;
934 unique_ptr<_SqlDataControlEvent> pSqlDataControlEvent(new (std::nothrow) _SqlDataControlEvent);
935 SysTryReturn(NID_IO, pSqlDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
937 pDcImpl->__pRequestList = new (std::nothrow) LinkedList(SingleObjectDeleter);
938 SysTryReturn(NID_IO, pDcImpl->__pRequestList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
940 pDcImpl->__pSqlDataControlEvent = pSqlDataControlEvent.release();
942 if (access == L"readonly")
944 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
946 else if (access == L"writeonly")
948 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
950 else if (access == L"readwrite")
952 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
956 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
957 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
960 return pDc.release();