2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 MpPlaylistDB.cpp
19 * @brief This is the implementation file for PlaylistDB class.
23 #include "MpCommonUtil.h"
24 #include "MpPlaylistDB.h"
27 using namespace Tizen::App;
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Io;
32 PlaylistDB::PlaylistDB(void)
38 PlaylistDB::~PlaylistDB(void)
45 PlaylistDB::CreatePlaylistDatabase(void)
48 Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
49 Tizen::Base::String sql;
53 if (File::IsFileExist(dbName))
57 Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
58 TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.[%s]", GetErrorMessage(r));
59 r = pDatabase->Construct(dbName, true);
60 TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.[%s]", GetErrorMessage(r));
61 sql.Append(L"CREATE TABLE IF NOT EXISTS playlisttable ( primaryKey INTEGER PRIMARY KEY AUTOINCREMENT, contentId TEXT, playCount INTEGER, playTime TEXT )");
62 r = pDatabase->BeginTransaction();
63 TryCatch(r == E_SUCCESS, r = E_FAILURE, "BeginTransaction failed.[%s]", GetErrorMessage(r));
64 r = pDatabase->ExecuteSql(sql, true);
65 TryCatch(r == E_SUCCESS, r = E_FAILURE, "ExecuteSql failed.[%s]", GetErrorMessage(r));
66 r = pDatabase->CommitTransaction();
67 TryCatch(r == E_SUCCESS, r = E_FAILURE, "CommitTransaction failed.[%s]", GetErrorMessage(r));
80 if (Database::Exists(dbName))
82 Database::Delete(dbName);
88 PlaylistDB::Insert(Tizen::Base::String& contentId, int playCount, Tizen::Base::String& playTime)
91 Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
94 DbStatement* pStmt = null;
95 Tizen::Base::String statement = null;
96 DbEnumerator* pEnum = null;
98 Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
99 TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.%n[%ls]", GetErrorMessage(r));
100 r = pDatabase->Construct(dbName, false);
101 TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.%n[%ls]", GetErrorMessage(r));
102 r = pDatabase->BeginTransaction();
103 TryCatch(r == E_SUCCESS, r = E_FAILURE, "BeginTransaction failed.%n[%ls]", GetErrorMessage(r));
105 statement.Append(L"INSERT INTO playlisttable (contentId, playCount, playTime) VALUES (?, ?, ?)");
106 pStmt = pDatabase->CreateStatementN(statement);
107 TryCatch(pStmt != null, r = E_FAILURE,"CreateStatementN failed with %s", GetErrorMessage(GetLastResult()));
108 r = pStmt->BindString(0, contentId);
109 TryCatch(r == E_SUCCESS, r = E_FAILURE, "contentId BindString failed.%n[%ls]", GetErrorMessage(r));
110 r = pStmt->BindInt(1, playCount);
111 TryCatch(r == E_SUCCESS, r = E_FAILURE, "playCount BindInt failed.%n[%ls]", GetErrorMessage(r));
112 r = pStmt->BindString(2, playTime);
113 TryCatch(r == E_SUCCESS, r = E_FAILURE, "playTime BindString failed.%n[%ls]", GetErrorMessage(r));
114 pEnum = pDatabase->ExecuteStatementN(*pStmt);
115 // AppAssert(!pEnum);
116 r = pDatabase->CommitTransaction();
117 TryCatch(r == E_SUCCESS, r = E_FAILURE, "CommitTransaction failed.%n[%ls]", GetErrorMessage(r));
146 if (Database::Exists(dbName))
148 Database::Delete(dbName);
154 PlaylistDB::Update(Tizen::Base::String& contentId, int playCount, Tizen::Base::String& playTime)
156 AppLogDebug("ENTER");
157 Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
159 result r = E_SUCCESS;
161 DbStatement* pStmt = null;
162 Tizen::Base::String statement = null;
163 DbEnumerator* pEnum = null;
165 Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
166 TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.%n[%ls]", GetErrorMessage(r));
167 r = pDatabase->Construct(dbName, false);
168 TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.%n[%ls]", GetErrorMessage(r));
169 r = pDatabase->BeginTransaction();
170 TryCatch(r == E_SUCCESS, r = E_FAILURE, "BeginTransaction failed.%n[%ls]", GetErrorMessage(r));
172 statement.Append(L"UPDATE playlisttable SET playCount = ?, playTime = ? WHERE contentId = ?");
173 pStmt = pDatabase->CreateStatementN(statement);
175 r = pStmt->BindInt(0, playCount);
176 TryCatch(r == E_SUCCESS, r = E_FAILURE, "playCount BindInt failed.%n[%ls]", GetErrorMessage(r));
177 r = pStmt->BindString(1, playTime);
178 TryCatch(r == E_SUCCESS, r = E_FAILURE, "playTime BindString failed.%n[%ls]", GetErrorMessage(r));
179 r = pStmt->BindString(2, contentId);
180 TryCatch(r == E_SUCCESS, r = E_FAILURE, "contentId BindString failed.%n[%ls]", GetErrorMessage(r));
182 pEnum = pDatabase->ExecuteStatementN(*pStmt);
183 // AppAssert(!pEnum);
185 r = pDatabase->CommitTransaction();
186 TryCatch(r == E_SUCCESS, r = E_FAILURE, "CommitTransaction failed.%n[%ls]", GetErrorMessage(r));
215 if (Database::Exists(dbName))
217 Database::Delete(dbName);
223 PlaylistDB::Delete(Tizen::Base::String& contentId)
225 AppLogDebug("ENTER");
226 Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
227 result r = E_SUCCESS;
229 DbStatement* pStmt = null;
230 Tizen::Base::String statement = null;
231 DbEnumerator* pEnum = null;
233 Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
234 TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.%n[%ls]", GetErrorMessage(r));
235 r = pDatabase->Construct(dbName, false);
236 TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.%n[%ls]", GetErrorMessage(r));
237 r = pDatabase->BeginTransaction();
238 TryCatch(r == E_SUCCESS, r = E_FAILURE, "BeginTransaction failed.%n[%ls]", GetErrorMessage(r));
240 statement.Append(L"DELETE FROM playlisttable where contentId = ?");
241 pStmt = pDatabase->CreateStatementN(statement);
242 r = pStmt->BindString(0, contentId);
243 TryCatch(r == E_SUCCESS, r = E_FAILURE, "contentId BindString failed.%n[%ls]", GetErrorMessage(r));
244 pEnum = pDatabase->ExecuteStatementN(*pStmt);
245 // AppAssert(!pEnum);
246 r = pDatabase->CommitTransaction();
247 TryCatch(r == E_SUCCESS, r = E_FAILURE, "CommitTransaction failed.%n[%ls]", GetErrorMessage(r));
276 if (Database::Exists(dbName))
278 Database::Delete(dbName);
283 Tizen::Io::DbEnumerator*
284 PlaylistDB::SearchN(int index)
286 AppLogDebug("ENTER");
287 Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
289 result r = E_SUCCESS;
291 Tizen::Base::String statement;
292 DbEnumerator* pEnum = null;
294 Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
295 r = pDatabase->Construct(dbName, false);
297 TryReturn(r == E_SUCCESS,null,"pDatabase->Construct failed with error %s",GetErrorMessage(r));
299 if (index == MOST_PLAYED_CONTENT)
301 statement.Append(L"SELECT contentId FROM playlisttable WHERE playCount > 0 ORDER BY playCount DESC");
303 else if (index == RECENTLY_PLAYED_CONTENT)
305 statement.Append(L"SELECT contentId FROM playlisttable WHERE playTime IS NOT '' ORDER BY playTime DESC");
313 pEnum = pDatabase->QueryN(statement);
320 PlaylistDB::Read(Tizen::Base::String& contentId)
322 AppLogDebug("ENTER");
323 Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
325 result r = E_KEY_NOT_FOUND;
327 DbStatement* pStmt = null;
328 Tizen::Base::String statement = null;
329 DbEnumerator* pEnum = null;
330 // int playCount = 0;
332 Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
333 // TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.%n[%ls]", GetErrorMessage(r));
334 r = pDatabase->Construct(dbName, false);
335 TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.[%s]", GetErrorMessage(r));
336 statement.Append(L"SELECT contentId FROM playlisttable WHERE contentId = ?");
337 pStmt = pDatabase->CreateStatementN(statement);
338 TryCatch(pStmt != null, r = E_FAILURE, "CreateStatementN failed with error.[%s]", GetErrorMessage(r));
340 r = pStmt->BindString(0, contentId);
341 TryCatch(r == E_SUCCESS, r = E_FAILURE, "contentId BindString failed.[%s]", GetErrorMessage(r));
343 pEnum = pDatabase->ExecuteStatementN(*pStmt);
345 // AppAssert(!pEnum);
349 while (pEnum->MoveNext()== E_SUCCESS)
351 pEnum->GetStringAt(0, contentId);
352 r = E_KEY_ALREADY_EXIST;
368 //AppLogDebug("Read 8 : %d", playCount);
373 Tizen::Base::Collection::ArrayList*
374 PlaylistDB::ReadValueN(Tizen::Base::String& contentId)
376 AppLogDebug("ENTER");
377 Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
379 result r = E_SUCCESS;
381 Tizen::Base::String statement = null;
382 DbEnumerator* pEnum = null;
384 Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
385 r = pDatabase->Construct(dbName, false);
386 statement.Append(L"SELECT playCount, playTime FROM playlisttable");
388 pEnum = pDatabase->QueryN(statement);
390 Tizen::Base::String playTime;
392 Tizen::Base::Collection::ArrayList* pTempList = new (std::nothrow) ArrayList();
393 pTempList->Construct();
397 while (pEnum->MoveNext()== E_SUCCESS)
399 pEnum->GetIntAt(0, playCount);
400 pEnum->GetStringAt(1, playTime);
401 pTempList->Add(*(new (std::nothrow) Integer(playCount)));
402 pTempList->Add(*(new (std::nothrow) String(playTime)));