From ed462245730bd7832019bd43e0bc1c9d1c055e8e Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Sun, 29 Aug 2021 15:41:41 +0100 Subject: Add SQLite support, fix un-thread-safe DB caches, small performance f… (#172) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add SQLite support, fix un-thread-safe DB caches, small performance fixes Signed-off-by: kim (grufwub) * add SQLite licenses to README Signed-off-by: kim (grufwub) * appease the linter, and fix my dumbass-ery Signed-off-by: kim (grufwub) * make requested changes Signed-off-by: kim (grufwub) * add back comment Signed-off-by: kim (grufwub) --- internal/db/bundb/errors.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 internal/db/bundb/errors.go (limited to 'internal/db/bundb/errors.go') diff --git a/internal/db/bundb/errors.go b/internal/db/bundb/errors.go new file mode 100644 index 000000000..7602d5e1d --- /dev/null +++ b/internal/db/bundb/errors.go @@ -0,0 +1,43 @@ +package bundb + +import ( + "github.com/jackc/pgconn" + "github.com/superseriousbusiness/gotosocial/internal/db" + "modernc.org/sqlite" + sqlite3 "modernc.org/sqlite/lib" +) + +// processPostgresError processes an error, replacing any postgres specific errors with our own error type +func processPostgresError(err error) db.Error { + // Attempt to cast as postgres + pgErr, ok := err.(*pgconn.PgError) + if !ok { + return err + } + + // Handle supplied error code: + // (https://www.postgresql.org/docs/10/errcodes-appendix.html) + switch pgErr.Code { + case "23505" /* unique_violation */ : + return db.ErrAlreadyExists + default: + return err + } +} + +// processSQLiteError processes an error, replacing any sqlite specific errors with our own error type +func processSQLiteError(err error) db.Error { + // Attempt to cast as sqlite + sqliteErr, ok := err.(*sqlite.Error) + if !ok { + return err + } + + // Handle supplied error code: + switch sqliteErr.Code() { + case sqlite3.SQLITE_CONSTRAINT_UNIQUE: + return db.ErrAlreadyExists + default: + return err + } +} -- cgit v1.2.3