4)Handling data control requests流程:
3. 实例
1)SqlDataControl (Using a SqlDataControl )
获取Data Control实例
String providerId(L"http://tizen.org/datacontrol/provider/example"); SqlDataControl* pDc = AppManager::GetSqlDataControlN(providerId);
String dataId(L"test"); String person(L"person"); String number(L"number"); RequestId reqId; ArrayList columnList; columnList.Construct(); columnList.Add(person); columnList.Add(number); String where(L"group = 'friend'"); String order(L"person ASC"); pDc->Select(dataId, &columnList, &where, &order, reqId); ArrayList.RemoveAll();
class ExampleSqlDataControlResponseListener: public Tizen::App::ISqlDataControlResponseListener
void ExampleSqlDataControlResponseListener::OnSqlDataControlInsertResponseReceived(RequestId reqId, const String& providerId, const String& dataId, long long insertRowId, bool providerResult, const String* pErrorMsg) { // __insertRowId is a private variable of this class __insertRowId = insertRowId; return; }
ExampleSqlDataControlResponseListener* pResponseListener = null; pResponseListener = new ExampleSqlDataControlResponseListener(); pDc->SetSqlDataControlResponseListener(pResponseListener); delete pDc;
class MyServiceApp: public Tizen::App::ServiceApp, public Tizen::App::ISqlDataControlProviderEventListener
void MyServiceApp::OnSqlDataControlSelectRequestReceived(RequestId reqId, const String& providerId, const String& dataId, const IList* pcolumnList, const String* pWhere, const String* pOrder) { Database* pDb = null; DbEnumerator* pEnum = null; String errorMsg; result r = E_SUCCESS; if (providerId == L"http://tizen.org/datacontrol/provider/example") { if (dataId == L"test") { String sql = SqlStatementBuilder::CreateSelectStatement(dataId, pColumnList, pWhere, pOrder, null, null, null); String dbPath(App::GetInstance()->GetAppRootPath() + L"data/test.db"); pDb = new Database(); r = pDb->Construct(dbPath, "r"); pEnum = pDb->QueryN(sql); r = GetLastResult(); } else { // Error handling } } else { // Error handling } r = DataControlProviderManager::GetInstance()->SendSqlDataControlSelectResult(reqId, pDbEnum); delete pEnum; delete pDb; return; }
void MyServiceApp::OnSqlDataControlInsertRequestReceived(RequestId reqId, const Tizen::Base::String& providerId, const Tizen::Base::String& dataId, const Tizen::Base::Collection::IMap& insertMap) { Database* pDb = null; String errorMsg; long long insertRowId = -1; result r = E_SUCCESS; if (providerId == L"http://tizen.org/datacontrol/provider/example") { if (dataId == L"test") { String sql = SqlStatementBuilder::CreateInsertStatement(dataId, insertMap); String dbPath(App::GetInstance()->GetAppDataPath() + L"test.db"); pDb = new Database(); r = pDb->Construct(dbPath, "a+"); r = pDb->ExecuteSql(sql, true); insertRowId = pDb->GetLastInsertRowId(); } else { // Error handling } } else { // Error handling } r = DataControlProviderManager::GetInstance()->SendSqlDataControlInsertResult(reqId, insertRowId); delete pDb; return; }
bool MyServiceApp::OnAppInitializing(Tizen::App::AppRegistry& appRegistry) { result r = E_SUCCESS; DataControlProviderManager* pDcMgr = DataControlProviderManager::GetInstance(); r = pDcMgr->SetSqlDataControlProviderEventListener(this); return true; }
String providerId(L"http://tizen.org/datacontrol/provider/example"); MapDataControl* pDc = AppManager::GetMapDataControlN(providerId);
String dataId(L"test"); String person(L"person"); pDc->GetValue(dataId, person, reqId);
class ExampleMapDataControlResponseListener: public Tizen::App::IMapDataControlResponseListener
void ExampleMapDataControlResponseListener:: OnMapDataControlGetValueResponseReceived(RequestId reqId, const String& providerId, const String& dataId, IList& resultValueList, bool providerResult, const String* pErrorMsg) { int count = resultValueList.GetCount(); for (int i = 0; i < count; i++) { String pPerson = static_cast< String* >(resultValueList.GetAt(i)); AppLog("%dth person: %ls", i, pPerson->GetPointer()); } return; }
ExampleMapDataControlResponseListener* pResponseListener = null; pResponseListener = new ExampleMapDataControlResponseListener(); pDc->SetMapDataControlResponseListener(pResponseListener); delete pDc;
class MyServiceApp: public Tizen::App::ServiceApp, public Tizen::App::IMapDataControlProviderEventListener
void MyServiceApp::OnMapDataControlGetValueRequestReceived(RequestId reqId, const Tizen::Base::String& providerId, const Tizen::Base::String& dataId, const Tizen::Base::String& key), { Registry* pReg = null; String retValue; ArrayList* pResultValueList = null; String errorMsg; result r = E_SUCCESS; if (providerId.compareTo(L"http://tizen.org/datacontrol/provider/example") == 0) { if (dataId.compareTo(L"test") == 0) { String regPath(App::GetInstance()->GetAppRootPath() + L"data/test.ini"); pReg = new Registry(); r = pReg->Construct(regPath, false); r = pReg->GetValue(dataId, key, retValue); pResultValueList = new ArrayList(); pResultValueList->Add(retValue); delete pReg; } else { errorMsg.Append(L"The data ID is invalid."); } } else { errorMsg.Append(L"The provider ID is invalid."); } r = DataControlProviderManager::GetInstance()->SendMapDataControlResult(reqId, pResultValueList); if (pResultValueList) { pResultValueList->RemoveAll(); delete pResultValueList; } return; }
bool MyServiceApp::OnAppInitializing(Tizen::App::AppRegistry& appRegistry) { result r = E_SUCCESS; DataControlProviderManager* pDcMgr = DataControlProviderManager::GetInstance(); r = pDcMgr->SetMapDataControlProviderEventListener(this); return true; }