4 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include "media-ebook-plugin-dbinserter.h"
29 #define LOG_TAG "MEDIA_SERVICE"
31 #define INSERT_QUERY "INSERT INTO words(file_id, word) SELECT id, ? FROM files WHERE path=? ON CONFLICT (file_id, word) DO UPDATE SET frequency=frequency+1;"
32 #define TOKEN_KEY "\\s+"
33 #define SPECIAL_CHAR "[\\{\\}\\[\\]\\/?.,;:|\\)*~`!^\\-_+<>@\\#$%&\\\\=\\(\\\'\\\"]"
35 bool DbInserter::run(const std::string& text)
37 if (!dbHandle || filePath.empty() || text.empty())
40 auto sqlite_handle = static_cast<sqlite3 *>(const_cast<void *>(dbHandle));
42 const std::regex sp(SPECIAL_CHAR);
43 std::string _text = std::regex_replace(text, sp, "");
44 const std::regex re(TOKEN_KEY);
45 auto words_begin = std::sregex_token_iterator(_text.begin(), _text.end(), re, -1);
46 auto words_end = std::sregex_token_iterator();
48 bool isTransaction = (sqlite3_exec(sqlite_handle, "BEGIN;", NULL, NULL, NULL) == SQLITE_OK);
50 sqlite3_stmt *stmt = NULL;
51 sqlite3_prepare_v2(sqlite_handle, INSERT_QUERY, -1, &stmt, NULL);
53 for (auto i = words_begin; i != words_end; ++i) {
54 sqlite3_bind_text(stmt, 1, (*i).str().c_str(), -1, SQLITE_TRANSIENT);
55 sqlite3_bind_text(stmt, 2, filePath.c_str(), -1, SQLITE_TRANSIENT);
60 sqlite3_finalize(stmt);
63 sqlite3_exec(sqlite_handle, "COMMIT;", NULL, NULL, NULL);