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 <FBaseColIList.h>
31 #include <FBaseColIMap.h>
32 #include <FBaseColIMapEnumerator.h>
33 #include <FBaseRtIEventArg.h>
34 #include <FBaseSysLog.h>
37 #include <FAppSqlDataControl.h>
38 #include <FAppISqlDataControlResponseListener.h>
40 #include <FBase_StringConverter.h>
41 #include <FIo_DataControlResultSetEnumerator.h>
43 #include "FApp_AppControlManager.h"
44 #include "FApp_SqlDataControlImpl.h"
45 #include "FApp_AppArg.h"
46 #include "FApp_DataControlManager.h"
47 #include "FAppPkg_PackageManagerImpl.h"
49 #define DATACONTROL_PROTOCOL_VER_2_1_0_2 // ver_2.1.0.2
50 #define DATACONTROL_PROTOCOL_VER_2_1_0_3 // ver_2.1.0.3
54 using namespace Tizen::Base;
55 using namespace Tizen::Base::Collection;
56 using namespace Tizen::Base::Runtime;
57 using namespace Tizen::Io;
58 using namespace Tizen::App;
59 using namespace Tizen::App::Package;
61 namespace Tizen { namespace App
64 static const int MAX_REQUEST_COUNT = 128;
65 static const int REQUEST_THRESHOLD = 100;
66 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
67 static const int _MAX_REQUEST_ARGUMENT_SIZE = 1048576; // 1MB
68 //static const char* _DATACONTROL_REQUEST_DIR = "/tmp/osp/DataControlRequest/\0";
69 static const char* _DATACONTROL_REQUEST_DIR = "/tmp/osp/data-control/request/\0";
71 class _SqlDataControlEventArg
75 _SqlDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
76 _DataControlResultSetEnumerator* pResultSetEnum, long long insertRowId, bool providerResult, String* pErrorMsg)
77 : __requestType(requestType)
79 , __providerId(providerId)
81 , __pResultSetEnum(pResultSetEnum)
82 , __insertRowId(insertRowId)
83 , __providerResult(providerResult)
84 , __pErrorMsg(pErrorMsg)
87 ~_SqlDataControlEventArg(void)
89 delete __pResultSetEnum;
93 _DataControlRequestType __requestType;
97 _DataControlResultSetEnumerator* __pResultSetEnum;
98 long long __insertRowId;
99 bool __providerResult;
103 class _SqlDataControlEvent
107 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
111 _SqlDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
113 const _SqlDataControlEventArg* pArg = dynamic_cast<const _SqlDataControlEventArg*>(&arg);
116 ISqlDataControlResponseListener* pListener = dynamic_cast<ISqlDataControlResponseListener*> (&listener);
117 if (pListener != null)
119 switch (pArg->__requestType)
121 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
122 pListener->OnSqlDataControlSelectResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
123 *(pArg->__pResultSetEnum), pArg->__providerResult, pArg->__pErrorMsg);
125 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
126 pListener->OnSqlDataControlInsertResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
127 pArg->__insertRowId, pArg->__providerResult, pArg->__pErrorMsg);
129 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
130 pListener->OnSqlDataControlUpdateResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
131 pArg->__providerResult, pArg->__pErrorMsg);
133 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
134 pListener->OnSqlDataControlDeleteResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
135 pArg->__providerResult, pArg->__pErrorMsg);
145 _SqlDataControlImpl::_SqlDataControlImpl(void)
146 : __access(_DATACONTROL_ACCESS_UNDEFINED)
147 , __pPreviousListener(null)
148 , __pSqlDataControlEvent(null)
152 _SqlDataControlImpl::~_SqlDataControlImpl(void)
154 delete __pSqlDataControlEvent;
158 _SqlDataControlImpl::GetInstance(SqlDataControl& dc)
160 return dc.__pSqlDataControlImpl;
163 const _SqlDataControlImpl*
164 _SqlDataControlImpl::GetInstance(const SqlDataControl& dc)
166 return dc.__pSqlDataControlImpl;
170 _SqlDataControlImpl::StartSqlDataControl(int type, const IList* pDataList, int* pReq)
172 Integer* pReqId = null;
173 _DataControlRequestInfo* pReqInfo = null;
174 result r = E_SUCCESS;
177 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
179 // Check the request count of DataControl operation
180 int count = pDcMgr->GetRequestCount();
181 SysLog(NID_APP, "Current launch request count: %d", count);
183 if (count > REQUEST_THRESHOLD)
185 _AppManagerImpl* pImpl = _AppManagerImpl::GetInstance();
187 // Clear the request queue if the provider is terminated
188 if (!pImpl->IsRunning(__appId))
190 SysLog(NID_APP, "The request queue is cleared due to the invalid provider.");
192 pDcMgr->RemoveAllRequests();
196 SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
198 _AppArg* pArg = new (std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
199 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "insufficient memory");
201 pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
203 _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
205 if (__pSqlDataControlEvent)
207 // reqId is system-wide id because the bundle is system-wide.
209 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, __pSqlDataControlEvent, -1);
210 req = reqObj.GetRequestNumber();
212 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, pDcMgr, -1);
213 req = reqObj.GetRequestNumber();
215 pReqId = new (std::nothrow) Integer(req);
216 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
217 "[E_OUT_OF_MEMORY] The memory is insufficient");
219 pReqInfo = new (std::nothrow) _DataControlRequestInfo(__pSqlDataControlEvent);
220 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
221 "[E_OUT_OF_MEMORY] The memory is insufficient");
223 r = pDcMgr->AddRequestInfo(pReqId, pReqInfo);
224 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to add request info", GetErrorMessage(r));
226 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
229 SysPropagate(NID_APP, r);
231 pDcMgr->RemoveRequestInfo(*pReqId);
242 r = pAppManagerImpl->LaunchApp(__appId, pArg);
243 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
257 _SqlDataControlImpl::Select(const String& dataId, const IList* pColumnList, const String* pWhere,
258 const String *pOrder, RequestId& reqId, int pageNo, int countPerPage)
260 SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
261 SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
262 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
263 "The SELECT query is not permitted by DataControl provider.");
265 const String* pColumn = null;
267 result r = E_SUCCESS;
268 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl SELECT");
270 ArrayList* pArgList = new ArrayList();
271 pArgList->Construct();
273 pArgList->Add(*(new String(dataId))); // list(0): data ID
274 long long argSize = dataId.GetLength() * sizeof(wchar_t);
276 if (pColumnList != null)
278 int columnCount = pColumnList->GetCount();
279 SysTryCatch(NID_APP, columnCount > 0, r = E_INVALID_ARG, E_INVALID_ARG,
280 "[E_INVALID_ARG] The specified pColumnList parameter is empty.");
282 pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): selected column count
283 SysLog(NID_APP, "[DC_CALLER_SEND] selected column count: %d", columnCount);
286 while (i < columnCount) // list(2): column list
288 pColumn = dynamic_cast< const String* >(pColumnList->GetAt(i));
289 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
290 "[E_INVALID_ARG] The object is not String class.");
292 pArgList->Add(*(new String(*pColumn)));
293 argSize += pColumn->GetLength() * sizeof(wchar_t);
294 SysLog(NID_APP, "[DC_CALLER_SEND] column[%d]: %ls", i, pColumn->GetPointer());
300 pArgList->Add(*(new String(L"NULL")));
303 if (pWhere != null) // list(3): where clause
305 pArgList->Add(*(new String(*pWhere)));
306 argSize += pWhere->GetLength() * sizeof(wchar_t);
307 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
311 pArgList->Add(*(new String(L"NULL")));
314 if (pOrder != null) // list(4): order clause
316 pArgList->Add(*(new String(*pOrder)));
317 argSize += pOrder->GetLength() * sizeof(wchar_t);
318 SysLog(NID_APP, "[DC_CALLER_SEND] pOrder: %ls", pOrder->GetPointer());
322 pArgList->Add(*(new String(L"NULL")));
324 SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
325 "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
327 pArgList->Add(*(new String(Integer::ToString(pageNo)))); // list(5): page number
329 pArgList->Add(*(new String(Integer::ToString(countPerPage)))); // list(6): count per page
331 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, pArgList, &id);
332 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
334 reqId = static_cast< RequestId >(id);
336 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pColumnList: 0x%x, pWhere: 0x%x, pOrder: 0x%x, req: %d, pageNo: %d, countPerPage: %d",
337 dataId.GetPointer(), pColumnList, pWhere, pOrder, reqId, pageNo, countPerPage);
341 pArgList->RemoveAll(true);
348 _SqlDataControlImpl::SqlDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int option)
350 ArrayList* pResultList = null;
351 String* pResult = null;
352 String* pProviderId = null;
353 String* pDataId = null;
354 String* pErrorMessage = null;
355 String* pErrorMsg = null;
356 String* pTmpPath = null;
357 String* pInsertRowId = null;
358 _DataControlResultSetEnumerator* pResultSetEnum = null;
363 bool providerResult = true;
364 //bundle* origBundle = null;
365 bundle* resBundle = null;
366 _SqlDataControlEventArg* pEventArg = null;
367 const char* p = null;
368 result r = E_SUCCESS;
370 SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
371 SysLog(NID_APP, "appsvc result value: %d", res);
373 resBundle = pResArg->GetBundle();
376 p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
377 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
381 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
382 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
383 if (pReqInfo == null)
385 SysLog(NID_APP, "No request info of reqId %d", reqId);
389 _SqlDataControlEvent* pSqlDataControlEvent = dynamic_cast< _SqlDataControlEvent* >(pReqInfo->GetEvent());
390 SysTryCatch(NID_APP, pSqlDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
391 "[E_SYSTEM] invalid request info");
393 // Remove the request from the queue
394 SysLog(NID_APP, "Remove the request, req: %d", reqId);
396 pDcMgr->RemoveRequestInfo(key);
398 if (pSqlDataControlEvent != null && typeid(pSqlDataControlEvent) == typeid(_SqlDataControlEvent*))
401 pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
402 SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
404 pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
405 SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
406 Integer::Parse(*pResult, providerRes);
407 providerResult = static_cast< bool >(providerRes);
409 pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
410 SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
411 pErrorMsg = new (std::nothrow) String(*pErrorMessage);
412 SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
413 "[E_OUT_OF_MEMORY] The memory is insufficient.");
416 //origBundle = pArg->GetBundle();
418 p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
419 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
420 requestType = atoi(p);
422 p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
423 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
424 pProviderId = new (std::nothrow) String(p);
425 SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
426 "[E_OUT_OF_MEMORY] The memory is insufficient.");
428 p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
429 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
430 pDataId = new (std::nothrow) String(p);
431 SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
432 "[E_OUT_OF_MEMORY] The memory is insufficient.");
434 SysSecureLog(NID_APP, "[DC_CALLER_RECV] provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls ",
435 providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
437 switch (static_cast <_DataControlRequestType>(requestType))
439 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
441 pResultSetEnum = new (std::nothrow) _DataControlResultSetEnumerator;
442 SysTryCatch(NID_APP, pResultSetEnum, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
443 "[E_OUT_OF_MEMORY] The memory is insufficient.");
445 if (providerResult == true)
447 pTmpPath = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
448 if (pTmpPath == null)
450 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
451 delete pResultSetEnum;
455 SysLog(NID_APP, "[DC_CALLER_RECV] tempPath: %ls", pTmpPath->GetPointer());
456 if (pTmpPath->Equals(L"NoResultSet", true) == false) // Result set exists
458 pResultSetEnum->SetPath(*pTmpPath);
462 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, static_cast <RequestId>(reqId),
463 *pProviderId, *pDataId, pResultSetEnum, -1, providerResult, pErrorMsg);
464 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
465 pSqlDataControlEvent->Fire(*pEventArg);
469 r = File::Remove(*pTmpPath); // Remove temporary file after releasing _ResultSetEnumerator.
470 SysTryLog(NID_APP, !IsFailed(r), "Failed to remove temp file for result set: %ls", pTmpPath->GetPointer());
475 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
477 long long insertRowId = -1;
478 if (providerResult == true)
480 pInsertRowId = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
481 SysTryCatch(NID_APP, pInsertRowId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
483 LongLong::Parse(*pInsertRowId, insertRowId);
486 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, static_cast <RequestId>(reqId),
487 *pProviderId, *pDataId, null, insertRowId, providerResult, pErrorMsg);
488 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
489 pSqlDataControlEvent->Fire(*pEventArg);
492 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
494 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, static_cast <RequestId>(reqId),
495 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
496 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
497 pSqlDataControlEvent->Fire(*pEventArg);
500 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
502 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, static_cast <RequestId>(reqId),
503 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
504 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
505 pSqlDataControlEvent->Fire(*pEventArg);
512 pResultList->RemoveAll(true);
518 p = appsvc_get_data(pArg->GetBundle(), OSP_K_REQUEST_ID);
519 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
520 launchReqId = atoi(p);
527 pResultList->RemoveAll(true);
536 _SqlDataControlImpl::Insert(const String& dataId, const IMap& insertMap, RequestId& reqId)
538 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
539 "The INSERT query is not permitted by DataControl provider.");
546 IMapEnumerator* pMapEnum = null;
547 _PackageManagerImpl* pPkgMgrImpl = null;
548 unique_ptr< String > pProviderAppId(null);
549 result r = E_SUCCESS;
550 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl INSERT");
552 columnCount = insertMap.GetCount();
553 SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
555 ArrayList* pArgList = new (std::nothrow) ArrayList();
556 pArgList->Construct();
558 pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
559 long long argSize = dataId.GetLength() * sizeof(wchar_t);
561 pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: inserted column count
562 SysLog(NID_APP, "[DC_CALLER_SEND] inserted column count: %d", columnCount);
564 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
565 String tmpPath(_DATACONTROL_REQUEST_DIR);
566 tmpPath.Append(App::GetInstance()->GetAppId());
568 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
569 SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
570 uniqueId = pDcMgr->GetUniqueId();
572 tmpPath.Append(uniqueId);
573 SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
575 r = request.Construct(tmpPath, L"w+", true);
576 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
577 GetErrorMessage(r), tmpPath.GetPointer());
579 pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path
582 pMapEnum = const_cast< IMap* >(&insertMap)->GetMapEnumeratorN();
583 while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
585 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
587 String* pColumn = null;
588 String* pValue = null;
589 unique_ptr< char[] > pData(null);
591 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
592 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
593 "[E_INVALID_ARG] The object is not String class.");
595 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
596 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
597 GetErrorMessage(GetLastResult()));
599 length = strlen(pData.get());
600 r = request.Write(&length, sizeof(int)); // data length
601 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
603 r = request.Write(pData.get(), length); // data
604 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
606 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.");
610 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
611 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
612 GetErrorMessage(GetLastResult()));
614 length = strlen(pData.get());
615 r = request.Write(&length, sizeof(int)); // data length
616 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
618 r = request.Write(pData.get(), length); // data
619 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
621 argSize += pColumn->GetLength() * sizeof(wchar_t);
622 argSize += pValue->GetLength() * sizeof(wchar_t);
624 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
625 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
626 "[E_INVALID_ARG] The object is not String class.");
628 pArgList->Add(*(new String(*pColumn)));
629 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
631 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
632 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
633 "[E_INVALID_ARG] The object is not String class.");
635 pArgList->Add(*(new String(*pValue)));
636 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
638 argSize += pColumn->GetLength() * sizeof(wchar_t);
639 argSize += pValue->GetLength() * sizeof(wchar_t);
643 SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
644 "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
646 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
649 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_3
650 pPkgMgrImpl = _PackageManagerImpl::GetInstance();
651 pProviderAppId.reset(pPkgMgrImpl->GetAppIdOfDataControlN(this->__providerId.GetPointer()));
652 SysTryCatch(NID_APP, pProviderAppId != null, r = E_SYSTEM, E_SYSTEM,
653 "[E_SYSTEM] The method cannot proceed due to a severe system error.");
654 r = pDcMgr->AllowAccess(*(pProviderAppId.get()));
655 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
658 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, pArgList, &id);
659 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
661 reqId = static_cast< RequestId >(id);
663 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, insertMap: 0x%x, req: %d", dataId.GetPointer(), &insertMap, reqId);
667 pArgList->RemoveAll(true);
675 _SqlDataControlImpl::Update(const String& dataId, const IMap& updateMap, const String* pWhere, RequestId& reqId)
677 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
678 "The UPDATE query is not permitted by DataControl provider.");
685 IMapEnumerator* pMapEnum = null;
686 _PackageManagerImpl* pPkgMgrImpl = null;
687 unique_ptr< String > pProviderAppId(null);
688 result r = E_SUCCESS;
689 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl UPDATE");
691 columnCount = updateMap.GetCount();
692 SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
694 ArrayList* pArgList = new ArrayList();
695 pArgList->Construct();
697 pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
698 long long argSize = dataId.GetLength() * sizeof(wchar_t);
700 pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: updated column count
701 SysLog(NID_APP, "[DC_CALLER_SEND] updated column count: %d", columnCount);
703 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
704 String tmpPath(_DATACONTROL_REQUEST_DIR);
705 tmpPath.Append(App::GetInstance()->GetAppId());
707 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
708 SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
709 uniqueId = pDcMgr->GetUniqueId();
711 tmpPath.Append(uniqueId);
712 SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
714 r = request.Construct(tmpPath, L"w+", true);
715 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
716 GetErrorMessage(r), tmpPath.GetPointer());
718 pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path
721 pMapEnum = const_cast< IMap* >(&updateMap)->GetMapEnumeratorN();
722 while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
724 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
726 String* pColumn = null;
727 String* pValue = null;
728 unique_ptr< char[] > pData(null);
730 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
731 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
732 "[E_INVALID_ARG] The object is not String class.");
734 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
735 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
736 GetErrorMessage(GetLastResult()));
738 length = strlen(pData.get());
739 r = request.Write(&length, sizeof(int)); // data length
740 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
742 r = request.Write(pData.get(), length); // data
743 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
745 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
746 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
747 "[E_INVALID_ARG] The object is not String class.");
749 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
750 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
751 GetErrorMessage(GetLastResult()));
753 length = strlen(pData.get());
754 r = request.Write(&length, sizeof(int)); // data length
755 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
757 r = request.Write(pData.get(), length); // data
758 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
760 argSize += pColumn->GetLength() * sizeof(wchar_t);
761 argSize += pValue->GetLength() * sizeof(wchar_t);
763 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
764 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
765 "[E_INVALID_ARG] The object is not String class.");
767 pArgList->Add(*(new String(*pColumn)));
768 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
770 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
771 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
772 "[E_INVALID_ARG] The object is not String class.");
774 pArgList->Add(*(new String(*pValue)));
775 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
777 argSize += pColumn->GetLength() * sizeof(wchar_t);
778 argSize += pValue->GetLength() * sizeof(wchar_t);
783 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
787 if (pWhere != null) // list: where clause
789 pArgList->Add(new (std::nothrow) String(*pWhere));
790 argSize += pWhere->GetLength() * sizeof(wchar_t);
791 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
795 pArgList->Add(new (std::nothrow) String(L"NULL"));
797 SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
798 "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
800 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_3
801 pPkgMgrImpl = _PackageManagerImpl::GetInstance();
802 pProviderAppId.reset(pPkgMgrImpl->GetAppIdOfDataControlN(this->__providerId.GetPointer()));
803 SysTryCatch(NID_APP, pProviderAppId != null, r = E_SYSTEM, E_SYSTEM,
804 "[E_SYSTEM] The method cannot proceed due to a severe system error.");
805 r = pDcMgr->AllowAccess(*(pProviderAppId.get()));
806 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
809 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, pArgList, &id);
810 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
812 reqId = static_cast< RequestId >(id);
814 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, updateMap: 0x%x, pWhere: 0x%x, req: %d",
815 dataId.GetPointer(), &updateMap, pWhere, reqId);
819 pArgList->RemoveAll(true);
827 _SqlDataControlImpl::Delete(const String& dataId, const String* pWhere, RequestId& reqId)
829 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
830 "The DELETE query is not permitted by DataControl provider.");
833 result r = E_SUCCESS;
834 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl DELETE");
836 ArrayList* pArgList = new ArrayList();
837 pArgList->Construct();
839 pArgList->Add(*(new String(dataId))); // list(0): data ID
840 long long argSize = dataId.GetLength() * sizeof(wchar_t);
842 if (pWhere != null) // list(1): where clause
844 pArgList->Add(*(new String(*pWhere)));
845 argSize += pWhere->GetLength() * sizeof(wchar_t);
846 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
850 pArgList->Add(*(new String(L"NULL")));
852 SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
853 "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
855 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, pArgList, &id);
856 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
858 reqId = static_cast< RequestId >(id);
860 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pWhere: 0x%x, req: %d", dataId.GetPointer(), pWhere, reqId);
864 pArgList->RemoveAll(true);
871 _SqlDataControlImpl::SetSqlDataControlResponseListener(ISqlDataControlResponseListener* pListener)
873 result r = E_SUCCESS;
875 if (__pPreviousListener != null)
877 r = __pSqlDataControlEvent->RemoveListener(*__pPreviousListener);
878 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
879 __pPreviousListener = null;
882 if (pListener != null)
884 r = __pSqlDataControlEvent->AddListener(*pListener);
889 case E_OBJ_ALREADY_EXIST:
891 case E_INVALID_OPERATION:
892 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
895 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
901 __pPreviousListener = pListener;
908 _SqlDataControlImpl::CreateSqlDataControl(const String& appId, const String& providerId, const String& access)
910 unique_ptr<SqlDataControl> pDc(new (std::nothrow) SqlDataControl);
911 SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
913 _SqlDataControlImpl* pDcImpl = _SqlDataControlImpl::GetInstance(*pDc);
914 pDcImpl->__appId = appId;
915 pDcImpl->__providerId = providerId;
916 unique_ptr<_SqlDataControlEvent> pSqlDataControlEvent(new (std::nothrow) _SqlDataControlEvent);
917 SysTryReturn(NID_IO, pSqlDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
919 pDcImpl->__pSqlDataControlEvent = pSqlDataControlEvent.release();
921 if (access == L"readonly")
923 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
925 else if (access == L"writeonly")
927 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
929 else if (access == L"readwrite")
931 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
935 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
936 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
939 return pDc.release();