From 174c63cdcbec4d3c8d155fc6cb5300077d9e6d5d Mon Sep 17 00:00:00 2001 From: SuperMarioDaBom Date: Fri, 21 Feb 2025 09:54:23 -0800 Subject: [PATCH] Update to newer database mechanism (postgresql) --- database/connect_postgres.go | 1 + database/end_call.go | 16 ++++++---------- database/end_call_ringing.go | 24 ++++++------------------ database/get_call_info_by_caller.go | 27 ++++++++------------------- database/get_call_info_by_target.go | 27 ++++++++------------------- database/init_postgres.go | 26 ++++++++++++++++++++++++++ database/new_call.go | 18 ++++++------------ 7 files changed, 61 insertions(+), 78 deletions(-) create mode 100644 database/init_postgres.go diff --git a/database/connect_postgres.go b/database/connect_postgres.go index e6c0e72..5c0db1a 100644 --- a/database/connect_postgres.go +++ b/database/connect_postgres.go @@ -21,4 +21,5 @@ func ConnectPostgres() { globals.Logger.Success("Connected to Postgres!") + InitPostgres() } diff --git a/database/end_call.go b/database/end_call.go index ee268eb..b5908a8 100644 --- a/database/end_call.go +++ b/database/end_call.go @@ -1,18 +1,14 @@ package database import ( - "context" - - "go.mongodb.org/mongo-driver/bson" + "github.com/PretendoNetwork/nex-go/v2/types" + "github.com/PretendoNetwork/wiiu-chat/globals" ) -func EndCall(caller uint32) { - filter := bson.D{ - {"caller_pid", caller}, - } - - _, err := callsCollection.DeleteOne(context.TODO(), filter) +func EndCall(caller types.PID) { + _, err := Postgres.Exec(`DELETE FROM ongoingcalls WHERE caller_pid = $1;`, caller) if err != nil { - panic(err) + globals.Logger.Critical(err.Error()) + return } } diff --git a/database/end_call_ringing.go b/database/end_call_ringing.go index e4b0461..9f1ee15 100644 --- a/database/end_call_ringing.go +++ b/database/end_call_ringing.go @@ -1,26 +1,14 @@ package database import ( - "context" - - "go.mongodb.org/mongo-driver/bson" + "github.com/PretendoNetwork/nex-go/v2/types" + "github.com/PretendoNetwork/wiiu-chat/globals" ) -func EndCallRinging(caller uint32) { - filter := bson.D{ - {"caller_pid", caller}, - } - - update := bson.D{ - { - "$set", bson.D{ - {"ringing", false}, - }, - }, - } - - _, err := callsCollection.UpdateOne(context.TODO(), filter, update) +func EndCallRinging(caller types.PID) { + _, err := Postgres.Exec(`UPDATE ongoingcalls SET (ringing = $1) WHERE caller_pid = $2;`, false, caller) if err != nil { - panic(err) + globals.Logger.Critical(err.Error()) + return } } diff --git a/database/get_call_info_by_caller.go b/database/get_call_info_by_caller.go index 039c189..8ba2ea5 100644 --- a/database/get_call_info_by_caller.go +++ b/database/get_call_info_by_caller.go @@ -1,27 +1,16 @@ package database import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "github.com/PretendoNetwork/nex-go/v2/types" + "github.com/PretendoNetwork/wiiu-chat/globals" ) -func GetCallInfoByCaller(caller uint32) (uint32, uint32, bool) { // caller pid, target pid, ringing - var result bson.M - filter := bson.D{ - {"caller_pid", caller}, - } - - err := callsCollection.FindOne(context.TODO(), filter, options.FindOne()).Decode(&result) +func GetCallInfoByCaller(caller types.PID) (caller_pid types.PID, target_pid types.PID, ringing types.Bool) { + row := Postgres.QueryRow(`SELECT (caller_pid, target_pid, ringing) FROM ongoingcalls WHERE caller_pid = $1;`, caller) + err := row.Scan(&caller_pid, &target_pid, &ringing) if err != nil { - if err == mongo.ErrNoDocuments { - return 0, 0, false - } else { - panic(err) - } - } else { - return uint32(result["caller_pid"].(int64)), uint32(result["target_pid"].(int64)), result["ringing"].(bool) + globals.Logger.Critical(err.Error()) + return 0, 0, false } + return caller_pid, target_pid, ringing } diff --git a/database/get_call_info_by_target.go b/database/get_call_info_by_target.go index cbe4581..e002855 100644 --- a/database/get_call_info_by_target.go +++ b/database/get_call_info_by_target.go @@ -1,27 +1,16 @@ package database import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "github.com/PretendoNetwork/nex-go/v2/types" + "github.com/PretendoNetwork/wiiu-chat/globals" ) -func GetCallInfoByTarget(target uint32) (uint32, uint32, bool) { // caller pid, target pid, ringing - var result bson.M - filter := bson.D{ - {"target_pid", target}, - } - - err := callsCollection.FindOne(context.TODO(), filter, options.FindOne()).Decode(&result) +func GetCallInfoByTarget(target types.PID) (caller_pid types.PID, target_pid types.PID, ringing types.Bool) { + row := Postgres.QueryRow(`SELECT (caller_pid, target_pid, ringing) FROM ongoingcalls WHERE target_pid = $1;`, target) + err := row.Scan(&caller_pid, &target_pid, &ringing) if err != nil { - if err == mongo.ErrNoDocuments { - return 0, 0, false - } else { - panic(err) - } - } else { - return uint32(result["caller_pid"].(int64)), uint32(result["target_pid"].(int64)), result["ringing"].(bool) + globals.Logger.Warning(err.Error()) + return 0, 0, false } + return caller_pid, target_pid, ringing } diff --git a/database/init_postgres.go b/database/init_postgres.go new file mode 100644 index 0000000..619dcaf --- /dev/null +++ b/database/init_postgres.go @@ -0,0 +1,26 @@ +package database + +import "github.com/PretendoNetwork/wiiu-chat/globals" + +func InitPostgres() { + var err error + + _, err = Postgres.Exec(`CREATE TABLE IF NOT EXISTS ongoingcalls ( + caller_pid integer UNIQUE PRIMARY KEY, + target_pid integer, + ringing bool + )`) + if err != nil { + globals.Logger.Critical(err.Error()) + return + } + + // Empty out call list if there happens to be old data lingering + _, err = Postgres.Exec(`DELETE FROM ongoingcalls;`) + if err != nil { + globals.Logger.Critical(err.Error()) + return + } + + globals.Logger.Success("Postgres tables created") +} diff --git a/database/new_call.go b/database/new_call.go index 12299ee..a917c6e 100644 --- a/database/new_call.go +++ b/database/new_call.go @@ -1,20 +1,14 @@ package database import ( - "context" - - "go.mongodb.org/mongo-driver/bson" + "github.com/PretendoNetwork/nex-go/v2/types" + "github.com/PretendoNetwork/wiiu-chat/globals" ) -func NewCall(caller uint32, target uint32) { - document := bson.D{ - {"caller_pid", caller}, - {"target_pid", target}, - {"ringing", true}, - } - - _, err := callsCollection.InsertOne(context.TODO(), document) +func NewCall(caller types.PID, target types.PID) { + _, err := Postgres.Exec(`INSERT INTO ongoingcalls (caller_pid, target_pid, ringing) VALUES ($1, $2, $3);`, caller, target, true) if err != nil { - panic(err) + globals.Logger.Critical(err.Error()) + return } }