• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #define MLOG_TAG "SmartAlbum"
16 
17 #include "medialibrary_smartalbum_db.h"
18 #include "media_log.h"
19 #include "medialibrary_errno.h"
20 #include "rdb_utils.h"
21 
22 using namespace std;
23 using namespace OHOS::NativeRdb;
24 
25 namespace OHOS {
26 namespace Media {
27 static const std::string SMARTALBUM_DB_COND = SMARTALBUM_DB_ID + " = ?";
InsertSmartAlbumInfo(const ValuesBucket & values,const shared_ptr<RdbStore> & rdbStore)28 int64_t MediaLibrarySmartAlbumDb::InsertSmartAlbumInfo(const ValuesBucket &values, const shared_ptr<RdbStore> &rdbStore)
29 {
30     CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ALBUM_OPER_ERR, "Invalid RDB store");
31     int64_t outRowId(0);
32     int32_t albumId = 0;
33     ValuesBucket value = const_cast<ValuesBucket &>(values);
34     ValueObject valueObject;
35     if (value.GetObject(SMARTALBUM_DB_ID, valueObject)) {
36             valueObject.GetInt(albumId);
37         }
38     int32_t insertResult = rdbStore->Insert(outRowId, SMARTALBUM_TABLE, values);
39     CHECK_AND_RETURN_RET_LOG(insertResult == NativeRdb::E_OK, E_ALBUM_OPER_ERR, "Insert failed");
40     return outRowId;
41 }
InsertCategorySmartAlbumInfo(const ValuesBucket & values,const shared_ptr<RdbStore> & rdbStore)42 int64_t MediaLibrarySmartAlbumDb::InsertCategorySmartAlbumInfo(const ValuesBucket &values,
43     const shared_ptr<RdbStore> &rdbStore)
44 {
45     CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ALBUM_OPER_ERR, "Invalid RDB store");
46     int64_t outRowId(0);
47     int32_t albumId = 0;
48     ValuesBucket value = const_cast<ValuesBucket &>(values);
49     ValueObject valueObject;
50     if (value.GetObject(SMARTALBUM_DB_ID, valueObject)) {
51             valueObject.GetInt(albumId);
52         }
53     int32_t insertResult = rdbStore->Insert(outRowId, CATEGORY_SMARTALBUM_MAP_TABLE, values);
54     CHECK_AND_RETURN_RET_LOG(insertResult == NativeRdb::E_OK, E_ALBUM_OPER_ERR, "Insert failed");
55     return outRowId;
56 }
DeleteSmartAlbumInfo(const int32_t albumId,const shared_ptr<RdbStore> & rdbStore)57 int32_t MediaLibrarySmartAlbumDb::DeleteSmartAlbumInfo(const int32_t albumId, const shared_ptr<RdbStore> &rdbStore)
58 {
59     CHECK_AND_RETURN_RET_LOG((rdbStore != nullptr) && (albumId > 0), E_ALBUM_OPER_ERR, "Invalid input");
60     int32_t deletedRows(E_ALBUM_OPER_ERR);
61     vector<string> whereArgs = { std::to_string(albumId)};
62     int32_t deleteResult = rdbStore->Delete(deletedRows, SMARTALBUM_TABLE, SMARTALBUM_DB_COND, whereArgs);
63     CHECK_AND_RETURN_RET_LOG(deleteResult == NativeRdb::E_OK, E_ALBUM_OPER_ERR, "Delete failed");
64     return (deletedRows > 0) ? E_SUCCESS : E_FAIL;
65 }
66 }  // namespace Media
67 }  // namespace OHOS
68