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"
48 #define DATACONTROL_PROTOCOL_VER_2_1_0_2 // ver_2.1.0.2
52 using namespace Tizen::Base;
53 using namespace Tizen::Base::Collection;
54 using namespace Tizen::Base::Runtime;
55 using namespace Tizen::App;
56 using namespace Tizen::Io;
58 namespace Tizen { namespace App
61 static const int MAX_REQUEST_COUNT = 128;
62 static const int REQUEST_THRESHOLD = 100;
63 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
64 static const int _MAX_REQUEST_ARGUMENT_SIZE = 1048576; // 1MB
65 static const char* _DATACONTROL_REQUEST_DIR = "/tmp/osp/DataControlRequest/\0";
67 class _SqlDataControlEventArg
71 _SqlDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
72 _DataControlResultSetEnumerator* pResultSetEnum, long long insertRowId, bool providerResult, String* pErrorMsg)
73 : __requestType(requestType)
75 , __providerId(providerId)
77 , __pResultSetEnum(pResultSetEnum)
78 , __insertRowId(insertRowId)
79 , __providerResult(providerResult)
80 , __pErrorMsg(pErrorMsg)
83 ~_SqlDataControlEventArg(void)
85 delete __pResultSetEnum;
89 _DataControlRequestType __requestType;
93 _DataControlResultSetEnumerator* __pResultSetEnum;
94 long long __insertRowId;
95 bool __providerResult;
99 class _SqlDataControlEvent
103 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
107 _SqlDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
109 const _SqlDataControlEventArg* pArg = dynamic_cast<const _SqlDataControlEventArg*>(&arg);
112 ISqlDataControlResponseListener* pListener = dynamic_cast<ISqlDataControlResponseListener*> (&listener);
113 if (pListener != null)
115 switch (pArg->__requestType)
117 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
118 pListener->OnSqlDataControlSelectResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
119 *(pArg->__pResultSetEnum), pArg->__providerResult, pArg->__pErrorMsg);
121 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
122 pListener->OnSqlDataControlInsertResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
123 pArg->__insertRowId, pArg->__providerResult, pArg->__pErrorMsg);
125 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
126 pListener->OnSqlDataControlUpdateResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
127 pArg->__providerResult, pArg->__pErrorMsg);
129 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
130 pListener->OnSqlDataControlDeleteResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
131 pArg->__providerResult, pArg->__pErrorMsg);
141 _SqlDataControlImpl::_SqlDataControlImpl(void)
142 : __access(_DATACONTROL_ACCESS_UNDEFINED)
143 , __pPreviousListener(null)
144 , __pSqlDataControlEvent(null)
148 _SqlDataControlImpl::~_SqlDataControlImpl(void)
150 delete __pSqlDataControlEvent;
154 _SqlDataControlImpl::GetInstance(SqlDataControl& dc)
156 return dc.__pSqlDataControlImpl;
159 const _SqlDataControlImpl*
160 _SqlDataControlImpl::GetInstance(const SqlDataControl& dc)
162 return dc.__pSqlDataControlImpl;
166 _SqlDataControlImpl::StartSqlDataControl(int type, const IList* pDataList, int* pReq)
168 Integer* pReqId = null;
169 _DataControlRequestInfo* pReqInfo = null;
170 result r = E_SUCCESS;
173 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
175 // Check the request count of DataControl operation
176 int count = pDcMgr->GetRequestCount();
177 SysLog(NID_APP, "Current launch request count: %d", count);
179 if (count > REQUEST_THRESHOLD)
181 _AppManagerImpl* pImpl = _AppManagerImpl::GetInstance();
183 // Clear the request queue if the provider is terminated
184 if (!pImpl->IsRunning(__appId))
186 SysLog(NID_APP, "The request queue is cleared due to the invalid provider.");
188 pDcMgr->RemoveAllRequests();
192 SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
194 _AppArg* pArg = new (std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
195 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "insufficient memory");
197 pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
199 _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
201 if (__pSqlDataControlEvent)
203 // reqId is system-wide id because the bundle is system-wide.
205 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, __pSqlDataControlEvent, -1);
206 req = reqObj.GetRequestNumber();
208 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, pDcMgr, -1);
209 req = reqObj.GetRequestNumber();
211 pReqId = new (std::nothrow) Integer(req);
212 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
213 "[E_OUT_OF_MEMORY] The memory is insufficient");
215 pReqInfo = new (std::nothrow) _DataControlRequestInfo(__pSqlDataControlEvent);
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 r = pDcMgr->AddRequestInfo(pReqId, pReqInfo);
220 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to add request info", GetErrorMessage(r));
222 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
225 SysPropagate(NID_APP, r);
227 pDcMgr->RemoveRequestInfo(*pReqId);
238 r = pAppManagerImpl->LaunchApp(__appId, pArg);
239 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
253 _SqlDataControlImpl::Select(const String& dataId, const IList* pColumnList, const String* pWhere,
254 const String *pOrder, RequestId& reqId, int pageNo, int countPerPage)
256 SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
257 SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
258 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
259 "The SELECT query is not permitted by DataControl provider.");
261 const String* pColumn = null;
263 result r = E_SUCCESS;
264 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl SELECT");
266 ArrayList* pArgList = new ArrayList();
267 pArgList->Construct();
269 pArgList->Add(*(new String(dataId))); // list(0): data ID
270 long long argSize = dataId.GetLength() * sizeof(wchar_t);
272 if (pColumnList != null)
274 int columnCount = pColumnList->GetCount();
275 SysTryCatch(NID_APP, columnCount > 0, r = E_INVALID_ARG, E_INVALID_ARG,
276 "[E_INVALID_ARG] The specified pColumnList parameter is empty.");
278 pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): selected column count
279 SysLog(NID_APP, "[DC_CALLER_SEND] selected column count: %d", columnCount);
282 while (i < columnCount) // list(2): column list
284 pColumn = dynamic_cast< const String* >(pColumnList->GetAt(i));
285 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
286 "[E_INVALID_ARG] The object is not String class.");
288 pArgList->Add(*(new String(*pColumn)));
289 argSize += pColumn->GetLength() * sizeof(wchar_t);
290 SysLog(NID_APP, "[DC_CALLER_SEND] column[%d]: %ls", i, pColumn->GetPointer());
296 pArgList->Add(*(new String(L"NULL")));
299 if (pWhere != null) // list(3): where clause
301 pArgList->Add(*(new String(*pWhere)));
302 argSize += pWhere->GetLength() * sizeof(wchar_t);
303 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
307 pArgList->Add(*(new String(L"NULL")));
310 if (pOrder != null) // list(4): order clause
312 pArgList->Add(*(new String(*pOrder)));
313 argSize += pOrder->GetLength() * sizeof(wchar_t);
314 SysLog(NID_APP, "[DC_CALLER_SEND] pOrder: %ls", pOrder->GetPointer());
318 pArgList->Add(*(new String(L"NULL")));
320 SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
321 "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
323 pArgList->Add(*(new String(Integer::ToString(pageNo)))); // list(5): page number
325 pArgList->Add(*(new String(Integer::ToString(countPerPage)))); // list(6): count per page
327 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, pArgList, &id);
328 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
330 reqId = static_cast< RequestId >(id);
332 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pColumnList: 0x%x, pWhere: 0x%x, pOrder: 0x%x, req: %d, pageNo: %d, countPerPage: %d",
333 dataId.GetPointer(), pColumnList, pWhere, pOrder, reqId, pageNo, countPerPage);
337 pArgList->RemoveAll(true);
344 _SqlDataControlImpl::SqlDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int option)
346 ArrayList* pResultList = null;
347 String* pResult = null;
348 String* pProviderId = null;
349 String* pDataId = null;
350 String* pErrorMessage = null;
351 String* pErrorMsg = null;
352 String* pTmpPath = null;
353 String* pInsertRowId = null;
354 _DataControlResultSetEnumerator* pResultSetEnum = null;
359 bool providerResult = true;
360 //bundle* origBundle = null;
361 bundle* resBundle = null;
362 _SqlDataControlEventArg* pEventArg = null;
363 const char* p = null;
364 result r = E_SUCCESS;
366 SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
367 SysLog(NID_APP, "appsvc result value: %d", res);
369 resBundle = pResArg->GetBundle();
372 p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
373 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
377 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
378 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
379 if (pReqInfo == null)
381 SysLog(NID_APP, "No request info of reqId %d", reqId);
385 _SqlDataControlEvent* pSqlDataControlEvent = dynamic_cast< _SqlDataControlEvent* >(pReqInfo->GetEvent());
386 SysTryCatch(NID_APP, pSqlDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
387 "[E_SYSTEM] invalid request info");
389 // Remove the request from the queue
390 SysLog(NID_APP, "Remove the request, req: %d", reqId);
392 pDcMgr->RemoveRequestInfo(key);
394 if (pSqlDataControlEvent != null && typeid(pSqlDataControlEvent) == typeid(_SqlDataControlEvent*))
397 pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
398 SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
400 pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
401 SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
402 Integer::Parse(*pResult, providerRes);
403 providerResult = static_cast< bool >(providerRes);
405 pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
406 SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
407 pErrorMsg = new (std::nothrow) String(*pErrorMessage);
408 SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
409 "[E_OUT_OF_MEMORY] The memory is insufficient.");
412 //origBundle = pArg->GetBundle();
414 p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
415 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
416 requestType = atoi(p);
418 p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
419 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
420 pProviderId = new (std::nothrow) String(p);
421 SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
422 "[E_OUT_OF_MEMORY] The memory is insufficient.");
424 p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
425 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
426 pDataId = new (std::nothrow) String(p);
427 SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
428 "[E_OUT_OF_MEMORY] The memory is insufficient.");
430 SysSecureLog(NID_APP, "[DC_CALLER_RECV] provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls ",
431 providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
433 switch (static_cast <_DataControlRequestType>(requestType))
435 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
437 pResultSetEnum = new (std::nothrow) _DataControlResultSetEnumerator;
438 SysTryCatch(NID_APP, pResultSetEnum, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
439 "[E_OUT_OF_MEMORY] The memory is insufficient.");
441 if (providerResult == true)
443 pTmpPath = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
444 if (pTmpPath == null)
446 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
447 delete pResultSetEnum;
451 SysLog(NID_APP, "[DC_CALLER_RECV] tempPath: %ls", pTmpPath->GetPointer());
452 if (pTmpPath->Equals(L"NoResultSet", true) == false) // Result set exists
454 pResultSetEnum->SetPath(*pTmpPath);
458 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, static_cast <RequestId>(reqId),
459 *pProviderId, *pDataId, pResultSetEnum, -1, providerResult, pErrorMsg);
460 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
461 pSqlDataControlEvent->Fire(*pEventArg);
465 r = File::Remove(*pTmpPath); // Remove temporary file after releasing _ResultSetEnumerator.
466 SysTryLog(NID_APP, !IsFailed(r), "Failed to remove temp file for result set: %ls", pTmpPath->GetPointer());
471 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
473 long long insertRowId = -1;
474 if (providerResult == true)
476 pInsertRowId = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
477 SysTryCatch(NID_APP, pInsertRowId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
479 LongLong::Parse(*pInsertRowId, insertRowId);
482 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, static_cast <RequestId>(reqId),
483 *pProviderId, *pDataId, null, insertRowId, 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);
488 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
490 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, static_cast <RequestId>(reqId),
491 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
492 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
493 pSqlDataControlEvent->Fire(*pEventArg);
496 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
498 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, static_cast <RequestId>(reqId),
499 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
500 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
501 pSqlDataControlEvent->Fire(*pEventArg);
508 pResultList->RemoveAll(true);
514 p = appsvc_get_data(pArg->GetBundle(), OSP_K_REQUEST_ID);
515 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
516 launchReqId = atoi(p);
523 pResultList->RemoveAll(true);
532 _SqlDataControlImpl::Insert(const String& dataId, const IMap& insertMap, RequestId& reqId)
534 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
535 "The INSERT query is not permitted by DataControl provider.");
542 IMapEnumerator* pMapEnum = null;
543 result r = E_SUCCESS;
544 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl INSERT");
546 columnCount = insertMap.GetCount();
547 SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
549 ArrayList* pArgList = new (std::nothrow) ArrayList();
550 pArgList->Construct();
552 pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
553 long long argSize = dataId.GetLength() * sizeof(wchar_t);
555 pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: inserted column count
556 SysLog(NID_APP, "[DC_CALLER_SEND] inserted column count: %d", columnCount);
558 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
559 String tmpPath(_DATACONTROL_REQUEST_DIR);
560 tmpPath.Append(App::GetInstance()->GetAppId());
562 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
563 SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
564 uniqueId = pDcMgr->GetUniqueId();
566 tmpPath.Append(uniqueId);
567 SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
569 r = request.Construct(tmpPath, L"w+", true);
570 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
571 GetErrorMessage(r), tmpPath.GetPointer());
573 pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path
576 pMapEnum = const_cast< IMap* >(&insertMap)->GetMapEnumeratorN();
577 while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
579 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
581 String* pColumn = null;
582 String* pValue = null;
583 unique_ptr< char[] > pData(null);
585 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
586 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
587 "[E_INVALID_ARG] The object is not String class.");
589 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
590 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
591 GetErrorMessage(GetLastResult()));
593 length = strlen(pData.get());
594 r = request.Write(&length, sizeof(int)); // data length
595 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
597 r = request.Write(pData.get(), length); // data
598 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
600 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
601 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
602 "[E_INVALID_ARG] The object is not String class.");
604 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
605 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
606 GetErrorMessage(GetLastResult()));
608 length = strlen(pData.get());
609 r = request.Write(&length, sizeof(int)); // data length
610 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
612 r = request.Write(pData.get(), length); // data
613 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
615 argSize += pColumn->GetLength() * sizeof(wchar_t);
616 argSize += pValue->GetLength() * sizeof(wchar_t);
618 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
619 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
620 "[E_INVALID_ARG] The object is not String class.");
622 pArgList->Add(*(new String(*pColumn)));
623 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
625 String* 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 pArgList->Add(*(new String(*pValue)));
630 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
632 argSize += pColumn->GetLength() * sizeof(wchar_t);
633 argSize += pValue->GetLength() * sizeof(wchar_t);
637 SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
638 "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
640 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
644 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, pArgList, &id);
645 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
647 reqId = static_cast< RequestId >(id);
649 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, insertMap: 0x%x, req: %d", dataId.GetPointer(), &insertMap, reqId);
653 pArgList->RemoveAll(true);
661 _SqlDataControlImpl::Update(const String& dataId, const IMap& updateMap, const String* pWhere, RequestId& reqId)
663 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
664 "The UPDATE query is not permitted by DataControl provider.");
671 IMapEnumerator* pMapEnum = null;
672 result r = E_SUCCESS;
673 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl UPDATE");
675 columnCount = updateMap.GetCount();
676 SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
678 ArrayList* pArgList = new ArrayList();
679 pArgList->Construct();
681 pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
682 long long argSize = dataId.GetLength() * sizeof(wchar_t);
684 pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: updated column count
685 SysLog(NID_APP, "[DC_CALLER_SEND] updated column count: %d", columnCount);
687 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
688 String tmpPath(_DATACONTROL_REQUEST_DIR);
689 tmpPath.Append(App::GetInstance()->GetAppId());
691 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
692 SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
693 uniqueId = pDcMgr->GetUniqueId();
695 tmpPath.Append(uniqueId);
696 SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
698 r = request.Construct(tmpPath, L"w+", true);
699 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
700 GetErrorMessage(r), tmpPath.GetPointer());
702 pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path
705 pMapEnum = const_cast< IMap* >(&updateMap)->GetMapEnumeratorN();
706 while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
708 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
710 String* pColumn = null;
711 String* pValue = null;
712 unique_ptr< char[] > pData(null);
714 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
715 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
716 "[E_INVALID_ARG] The object is not String class.");
718 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
719 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
720 GetErrorMessage(GetLastResult()));
722 length = strlen(pData.get());
723 r = request.Write(&length, sizeof(int)); // data length
724 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
726 r = request.Write(pData.get(), length); // data
727 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
729 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
730 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
731 "[E_INVALID_ARG] The object is not String class.");
733 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
734 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
735 GetErrorMessage(GetLastResult()));
737 length = strlen(pData.get());
738 r = request.Write(&length, sizeof(int)); // data length
739 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
741 r = request.Write(pData.get(), length); // data
742 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
744 argSize += pColumn->GetLength() * sizeof(wchar_t);
745 argSize += pValue->GetLength() * sizeof(wchar_t);
747 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
748 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
749 "[E_INVALID_ARG] The object is not String class.");
751 pArgList->Add(*(new String(*pColumn)));
752 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
754 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
755 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
756 "[E_INVALID_ARG] The object is not String class.");
758 pArgList->Add(*(new String(*pValue)));
759 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
761 argSize += pColumn->GetLength() * sizeof(wchar_t);
762 argSize += pValue->GetLength() * sizeof(wchar_t);
767 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
771 if (pWhere != null) // list: where clause
773 pArgList->Add(new (std::nothrow) String(*pWhere));
774 argSize += pWhere->GetLength() * sizeof(wchar_t);
775 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
779 pArgList->Add(new (std::nothrow) String(L"NULL"));
781 SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
782 "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
784 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, pArgList, &id);
785 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
787 reqId = static_cast< RequestId >(id);
789 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, updateMap: 0x%x, pWhere: 0x%x, req: %d",
790 dataId.GetPointer(), &updateMap, pWhere, reqId);
794 pArgList->RemoveAll(true);
802 _SqlDataControlImpl::Delete(const String& dataId, const String* pWhere, RequestId& reqId)
804 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
805 "The DELETE query is not permitted by DataControl provider.");
808 result r = E_SUCCESS;
809 SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl DELETE");
811 ArrayList* pArgList = new ArrayList();
812 pArgList->Construct();
814 pArgList->Add(*(new String(dataId))); // list(0): data ID
815 long long argSize = dataId.GetLength() * sizeof(wchar_t);
817 if (pWhere != null) // list(1): where clause
819 pArgList->Add(*(new String(*pWhere)));
820 argSize += pWhere->GetLength() * sizeof(wchar_t);
821 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
825 pArgList->Add(*(new String(L"NULL")));
827 SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
828 "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
830 r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, pArgList, &id);
831 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
833 reqId = static_cast< RequestId >(id);
835 SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pWhere: 0x%x, req: %d", dataId.GetPointer(), pWhere, reqId);
839 pArgList->RemoveAll(true);
846 _SqlDataControlImpl::SetSqlDataControlResponseListener(ISqlDataControlResponseListener* pListener)
848 result r = E_SUCCESS;
850 if (__pPreviousListener != null)
852 r = __pSqlDataControlEvent->RemoveListener(*__pPreviousListener);
853 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
854 __pPreviousListener = null;
857 if (pListener != null)
859 r = __pSqlDataControlEvent->AddListener(*pListener);
864 case E_OBJ_ALREADY_EXIST:
866 case E_INVALID_OPERATION:
867 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
870 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
876 __pPreviousListener = pListener;
883 _SqlDataControlImpl::CreateSqlDataControl(const String& appId, const String& providerId, const String& access)
885 unique_ptr<SqlDataControl> pDc(new (std::nothrow) SqlDataControl);
886 SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
888 _SqlDataControlImpl* pDcImpl = _SqlDataControlImpl::GetInstance(*pDc);
889 pDcImpl->__appId = appId;
890 pDcImpl->__providerId = providerId;
891 unique_ptr<_SqlDataControlEvent> pSqlDataControlEvent(new (std::nothrow) _SqlDataControlEvent);
892 SysTryReturn(NID_IO, pSqlDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
894 pDcImpl->__pSqlDataControlEvent = pSqlDataControlEvent.release();
896 if (access == L"readonly")
898 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
900 else if (access == L"writeonly")
902 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
904 else if (access == L"readwrite")
906 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
910 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
911 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
914 return pDc.release();