mirror of
https://gitlab.linphone.org/BC/public/linphone-iphone.git
synced 2026-01-17 19:18:06 +00:00
feat(MainDb): use prepared statements for some functions
This commit is contained in:
parent
52861017e2
commit
963811044b
5 changed files with 78 additions and 50 deletions
|
|
@ -54,18 +54,17 @@ namespace Statements {
|
|||
// ---------------------------------------------------------------------------
|
||||
|
||||
constexpr AbstractStatement create[CreateCount] = {
|
||||
[CreateConferenceEventView] =
|
||||
R"(
|
||||
CREATE VIEW IF NOT EXISTS conference_event_view AS
|
||||
SELECT id, type, creation_time, chat_room_id, from_sip_address_id, to_sip_address_id, time, imdn_message_id, state, direction, is_secured, notify_id, device_sip_address_id, participant_sip_address_id, subject
|
||||
FROM event
|
||||
LEFT JOIN conference_event ON conference_event.event_id = event.id
|
||||
LEFT JOIN conference_chat_message_event ON conference_chat_message_event.event_id = event.id
|
||||
LEFT JOIN conference_notified_event ON conference_notified_event.event_id = event.id
|
||||
LEFT JOIN conference_participant_device_event ON conference_participant_device_event.event_id = event.id
|
||||
LEFT JOIN conference_participant_event ON conference_participant_event.event_id = event.id
|
||||
LEFT JOIN conference_subject_event ON conference_subject_event.event_id = event.id
|
||||
)"
|
||||
[CreateConferenceEventView] = R"(
|
||||
CREATE VIEW IF NOT EXISTS conference_event_view AS
|
||||
SELECT id, type, creation_time, chat_room_id, from_sip_address_id, to_sip_address_id, time, imdn_message_id, state, direction, is_secured, notify_id, device_sip_address_id, participant_sip_address_id, subject
|
||||
FROM event
|
||||
LEFT JOIN conference_event ON conference_event.event_id = event.id
|
||||
LEFT JOIN conference_chat_message_event ON conference_chat_message_event.event_id = event.id
|
||||
LEFT JOIN conference_notified_event ON conference_notified_event.event_id = event.id
|
||||
LEFT JOIN conference_participant_device_event ON conference_participant_device_event.event_id = event.id
|
||||
LEFT JOIN conference_participant_event ON conference_participant_event.event_id = event.id
|
||||
LEFT JOIN conference_subject_event ON conference_subject_event.event_id = event.id
|
||||
)"
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
@ -73,6 +72,31 @@ namespace Statements {
|
|||
// ---------------------------------------------------------------------------
|
||||
|
||||
constexpr const char *select[SelectCount] = {
|
||||
[SelectSipAddressId] = R"(
|
||||
SELECT id
|
||||
FROM sip_address
|
||||
WHERE value = :1
|
||||
)",
|
||||
|
||||
[SelectChatRoomId] = R"(
|
||||
SELECT id
|
||||
FROM chat_room
|
||||
WHERE peer_sip_address_id = :1 AND local_sip_address_id = :2
|
||||
)",
|
||||
|
||||
[SelectChatRoomParticipantId] = R"(
|
||||
SELECT id
|
||||
FROM chat_room_participant
|
||||
WHERE chat_room_id = :1 AND participant_sip_address_id = :2
|
||||
)",
|
||||
|
||||
[SelectOneToOneChatRoomId] = R"(
|
||||
SELECT chat_room_id
|
||||
FROM one_to_one_chat_room
|
||||
WHERE participant_a_sip_address_id IN (:1, :2)
|
||||
AND participant_b_sip_address_id IN (:3, :4)
|
||||
)",
|
||||
|
||||
[SelectConferenceEvents] = R"(
|
||||
SELECT conference_event_view.id AS event_id, type, creation_time, from_sip_address.value, to_sip_address.value, time, imdn_message_id, state, direction, is_secured, notify_id, device_sip_address.value, participant_sip_address.value, subject
|
||||
FROM conference_event_view
|
||||
|
|
@ -80,7 +104,7 @@ namespace Statements {
|
|||
LEFT JOIN sip_address AS to_sip_address ON to_sip_address.id = to_sip_address_id
|
||||
LEFT JOIN sip_address AS device_sip_address ON device_sip_address.id = device_sip_address_id
|
||||
LEFT JOIN sip_address AS participant_sip_address ON participant_sip_address.id = participant_sip_address_id
|
||||
WHERE chat_room_id = :chatRoomId
|
||||
WHERE chat_room_id = :1
|
||||
)"
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,10 @@ namespace Statements {
|
|||
};
|
||||
|
||||
enum Select {
|
||||
SelectSipAddressId,
|
||||
SelectChatRoomId,
|
||||
SelectChatRoomParticipantId,
|
||||
SelectOneToOneChatRoomId,
|
||||
SelectConferenceEvents,
|
||||
SelectCount
|
||||
};
|
||||
|
|
|
|||
|
|
@ -34,15 +34,15 @@ class Content;
|
|||
|
||||
class MainDbPrivate : public AbstractDbPrivate {
|
||||
public:
|
||||
struct Statements;
|
||||
struct PreparedStatements;
|
||||
|
||||
mutable std::unordered_map<long long, std::weak_ptr<EventLog>> storageIdToEvent;
|
||||
mutable std::unordered_map<long long, std::weak_ptr<ChatMessage>> storageIdToChatMessage;
|
||||
|
||||
private:
|
||||
std::unique_ptr<Statements> statements;
|
||||
std::unique_ptr<PreparedStatements> preparedStatements;
|
||||
|
||||
void initStatements ();
|
||||
void initPreparedStatements ();
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Low level API.
|
||||
|
|
|
|||
|
|
@ -189,8 +189,8 @@ public:
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
void bind (const T &var, const char *name) {
|
||||
mStmt.exchange(soci::use(var, name));
|
||||
void bind (const T &var) {
|
||||
mStmt.exchange(soci::use(var));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -211,22 +211,21 @@ static inline unique_ptr<soci::statement> makeStatement (soci::session &session,
|
|||
return makeUnique<soci::statement>(session.prepare << stmt);
|
||||
}
|
||||
|
||||
struct MainDbPrivate::Statements {
|
||||
struct MainDbPrivate::PreparedStatements {
|
||||
typedef unique_ptr<soci::statement> Statement;
|
||||
|
||||
Statement selectSipAddressId;
|
||||
Statement selectChatRoomId;
|
||||
Statement select[Statements::SelectCount];
|
||||
};
|
||||
|
||||
void MainDbPrivate::initStatements () {
|
||||
soci::session *session = dbSession.getBackendSession();
|
||||
statements = makeUnique<Statements>();
|
||||
void MainDbPrivate::initPreparedStatements () {
|
||||
L_Q();
|
||||
|
||||
statements->selectSipAddressId = makeStatement(*session, "SELECT id FROM sip_address WHERE value = :sipAddress");
|
||||
statements->selectChatRoomId = makeStatement(
|
||||
*session,
|
||||
"SELECT id FROM chat_room WHERE peer_sip_address_id = :peerSipAddressId AND local_sip_address_id = :localSipAddressId"
|
||||
);
|
||||
soci::session *session = dbSession.getBackendSession();
|
||||
AbstractDb::Backend backend = q->getBackend();
|
||||
|
||||
preparedStatements = makeUnique<PreparedStatements>();
|
||||
for (int i = 0; i < int(Statements::SelectCount); ++i)
|
||||
preparedStatements->select[i] = makeStatement(*session, Statements::get(Statements::Select(i), backend));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
|
@ -424,8 +423,8 @@ void MainDbPrivate::insertChatMessageParticipant (long long eventId, long long s
|
|||
long long MainDbPrivate::selectSipAddressId (const string &sipAddress) const {
|
||||
long long id;
|
||||
|
||||
StatementBind stmt(*statements->selectSipAddressId);
|
||||
stmt.bind(sipAddress, "sipAddress");
|
||||
StatementBind stmt(*preparedStatements->select[Statements::SelectSipAddressId]);
|
||||
stmt.bind(sipAddress);
|
||||
stmt.bindResult(id);
|
||||
|
||||
return stmt.exec() ? id : -1;
|
||||
|
|
@ -434,9 +433,9 @@ long long MainDbPrivate::selectSipAddressId (const string &sipAddress) const {
|
|||
long long MainDbPrivate::selectChatRoomId (long long peerSipAddressId, long long localSipAddressId) const {
|
||||
long long id;
|
||||
|
||||
StatementBind stmt(*statements->selectChatRoomId);
|
||||
stmt.bind(peerSipAddressId, "peerSipAddressId");
|
||||
stmt.bind(localSipAddressId, "localSipAddressId");
|
||||
StatementBind stmt(*preparedStatements->select[Statements::SelectChatRoomId]);
|
||||
stmt.bind(peerSipAddressId);
|
||||
stmt.bind(localSipAddressId);
|
||||
stmt.bindResult(id);
|
||||
|
||||
return stmt.exec() ? id : -1;
|
||||
|
|
@ -456,23 +455,26 @@ long long MainDbPrivate::selectChatRoomId (const ChatRoomId &chatRoomId) const {
|
|||
|
||||
long long MainDbPrivate::selectChatRoomParticipantId (long long chatRoomId, long long participantSipAddressId) const {
|
||||
long long id;
|
||||
soci::session *session = dbSession.getBackendSession();
|
||||
*session << "SELECT id from chat_room_participant"
|
||||
" WHERE chat_room_id = :chatRoomId AND participant_sip_address_id = :participantSipAddressId",
|
||||
soci::into(id), soci::use(chatRoomId), soci::use(participantSipAddressId);
|
||||
return session->got_data() ? id : -1;
|
||||
|
||||
StatementBind stmt(*preparedStatements->select[Statements::SelectChatRoomParticipantId]);
|
||||
stmt.bind(chatRoomId);
|
||||
stmt.bind(participantSipAddressId);
|
||||
stmt.bindResult(id);
|
||||
|
||||
return stmt.exec() ? id : -1;
|
||||
}
|
||||
|
||||
long long MainDbPrivate::selectOneToOneChatRoomId (long long sipAddressIdA, long long sipAddressIdB) const {
|
||||
long long id;
|
||||
soci::session *session = dbSession.getBackendSession();
|
||||
*session << "SELECT chat_room_id"
|
||||
" FROM one_to_one_chat_room"
|
||||
" WHERE participant_a_sip_address_id IN (:sipAddressIdA, :sipAddressIdB)"
|
||||
" AND participant_b_sip_address_id IN (:sipAddressIdABis, :sipAddressIdBBis)",
|
||||
soci::into(id),
|
||||
soci::use(sipAddressIdA), soci::use(sipAddressIdB), soci::use(sipAddressIdA), soci::use(sipAddressIdB);
|
||||
return session->got_data() ? id : -1;
|
||||
|
||||
StatementBind stmt(*preparedStatements->select[Statements::SelectOneToOneChatRoomId]);
|
||||
stmt.bind(sipAddressIdA);
|
||||
stmt.bind(sipAddressIdB);
|
||||
stmt.bind(sipAddressIdA);
|
||||
stmt.bind(sipAddressIdB);
|
||||
stmt.bindResult(id);
|
||||
|
||||
return stmt.exec() ? id : -1;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
|
@ -1414,7 +1416,6 @@ void MainDb::init () {
|
|||
" ON DELETE CASCADE"
|
||||
") " + charset;
|
||||
|
||||
if (linphone_core_conference_server_enabled(getCore()->getCCore())) {
|
||||
*session <<
|
||||
"CREATE TABLE IF NOT EXISTS one_to_one_chat_room ("
|
||||
" chat_room_id" + primaryKeyStr("BIGINT UNSIGNED") + ","
|
||||
|
|
@ -1432,7 +1433,6 @@ void MainDb::init () {
|
|||
" REFERENCES sip_address(id)"
|
||||
" ON DELETE CASCADE"
|
||||
") " + charset;
|
||||
}
|
||||
|
||||
*session <<
|
||||
"CREATE TABLE IF NOT EXISTS chat_room_participant ("
|
||||
|
|
@ -1710,7 +1710,7 @@ void MainDb::init () {
|
|||
d->updateModuleVersion("events", ModuleVersionEvents);
|
||||
d->updateModuleVersion("friends", ModuleVersionFriends);
|
||||
|
||||
d->initStatements();
|
||||
d->initPreparedStatements();
|
||||
}
|
||||
|
||||
bool MainDb::addEvent (const shared_ptr<EventLog> &eventLog) {
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Reference in a new issue