diff options
author | FChannel <=> | 2021-01-26 13:47:48 -0800 |
---|---|---|
committer | FChannel <=> | 2021-01-26 13:47:48 -0800 |
commit | c171ea82b48f9b2f5c9b8132755e4fb8545290d9 (patch) | |
tree | a18ef8ed17dee37b1b9cf6d273177692957eae01 /Database.go | |
parent | 36e9b49ac109b542e080ce967eb782b885d9b7d3 (diff) |
check for duplicate replies in db
Diffstat (limited to 'Database.go')
-rw-r--r-- | Database.go | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/Database.go b/Database.go index 0d3ceca..f433cbe 100644 --- a/Database.go +++ b/Database.go @@ -211,13 +211,25 @@ func WriteObjectReplyToLocalDB(db *sql.DB, id string, replyto string) { func writeObjectReplyToDB(db *sql.DB, obj ObjectBase) { for _, e := range obj.InReplyTo { - query := `insert into replies (id, inreplyto) values ($1, $2)` + query := `select id from replies where id=$1 and inreplyto=$2` - _, err := db.Exec(query, obj.Id, e.Id) - - if err != nil{ - fmt.Println("error inserting replies") - panic(err) + rows, err := db.Query(query, obj.Id, e.Id) + + CheckError(err, "error selecting replies db") + + defer rows.Close() + + var id string + rows.Next() + rows.Scan(&id) + + if id == "" { + query := `insert into replies (id, inreplyto) values ($1, $2)` + + _, err := db.Exec(query, obj.Id, e.Id) + + + CheckError(err, "error inserting replies db") } update := true @@ -238,13 +250,24 @@ func writeObjectReplyToDB(db *sql.DB, obj ObjectBase) { } if len(obj.InReplyTo) < 1 { - query := `insert into replies (id, inreplyto) values ($1, $2)` + query := `select id from replies where id=$1 and inreplyto=$2` - _, err := db.Exec(query, obj.Id, "") - - if err != nil{ - fmt.Println("error inserting replies cache") - panic(err) + rows, err := db.Query(query, obj.Id, "") + + CheckError(err, "error selecting replies db") + + defer rows.Close() + + var id string + rows.Next() + rows.Scan(&id) + + if id == "" { + query := `insert into replies (id, inreplyto) values ($1, $2)` + + _, err := db.Exec(query, obj.Id, "") + + CheckError(err, "error inserting replies db") } } } |