Skip to content

Commit d5790d4

Browse files
committed
update conversation instead of deleting and reinserting
1 parent 0dd3029 commit d5790d4

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/NovaGram/Database.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function initializeQueries(): void{
8484
"deleteConversation" => "DELETE FROM {$this->tableNames['conversations']} WHERE chat_id = :chat_id AND name = :name",
8585
"setConversation" => "INSERT INTO {$this->tableNames['conversations']}(chat_id, name, value, additional_param) VALUES (:chat_id, :name, :value, :additional_param)",
8686
"getConversation" => "SELECT * FROM {$this->tableNames['conversations']} WHERE chat_id = :chat_id AND name = :name",
87+
"updateConversation" => "UPDATE {$this->tableNames['conversations']} SET value = :value, additional_param = :additional_param WHERE chat_id = :chat_id AND name = :name",
8788
"getConversationsByChat" => "SELECT * FROM {$this->tableNames['conversations']} WHERE chat_id = :chat_id",
8889
"getConversationsByValue" => "SELECT * FROM {$this->tableNames['conversations']} WHERE value = :value",
8990
"getConversationsByName" => "SELECT * FROM {$this->tableNames['conversations']} WHERE name = :name"
@@ -115,15 +116,21 @@ public function deleteConversation(int $chat_id, string $name): void{
115116
}
116117

117118
public function setConversation(int $chat_id, string $name, $value, array $additional_param = []): void{
118-
$this->deleteConversation($chat_id, $name);
119-
$this->query($this->queries['setConversation'], [
119+
$this->query($this->existConversation($chat_id, $name) ? $this->queries['updateConversation'] : $this->queries['setConversation'], [
120120
':chat_id' => $chat_id,
121121
':name' => $name,
122122
':value' => serialize($value),
123123
':additional_param' => serialize($additional_param),
124124
]);
125125
}
126126

127+
public function existConversation(int $chat_id, string $name): bool{
128+
return $this->existQuery($this->queries['getConversation'], [
129+
':chat_id' => $chat_id,
130+
':name' => $name,
131+
]);
132+
}
133+
127134
public function getConversation(int $chat_id, string $name){
128135
$row = $this->query($this->queries['getConversation'], [
129136
':chat_id' => $chat_id,

0 commit comments

Comments
 (0)