|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "musicboxapi/configuration" |
| 8 | + "musicboxapi/logging" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + _ "github.com/lib/pq" |
| 14 | +) |
| 15 | + |
| 16 | +var DbInstance *sql.DB |
| 17 | + |
| 18 | +type BaseTable struct { |
| 19 | + DB *sql.DB |
| 20 | +} |
| 21 | + |
| 22 | +func NewBaseTableInstance() BaseTable { |
| 23 | + return BaseTable{ |
| 24 | + DB: DbInstance, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func CreateDatabasConnectionPool() error { |
| 29 | + |
| 30 | + // Will throw an error if its missing a method implementation from interface |
| 31 | + // will throw a compile time error |
| 32 | + var _ ISongTable = (*SongTable)(nil) |
| 33 | + var _ IPlaylistTable = (*PlaylistTable)(nil) |
| 34 | + var _ IPlaylistsongTable = (*PlaylistsongTable)(nil) |
| 35 | + var _ ITasklogTable = (*TasklogTable)(nil) |
| 36 | + |
| 37 | + baseConnectionString := "user=postgres dbname=postgres password=%s %s sslmode=disable" |
| 38 | + password := os.Getenv("POSTGRES_PASSWORD") |
| 39 | + host := "host=127.0.0.1 port=5432" |
| 40 | + |
| 41 | + if configuration.Config.UseDevUrl { |
| 42 | + host = "host=127.0.0.1 port=5433" |
| 43 | + } |
| 44 | + |
| 45 | + connectionString := fmt.Sprintf(baseConnectionString, password, host) |
| 46 | + |
| 47 | + DB, err := sql.Open("postgres", connectionString) |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + logging.Error(fmt.Sprintf("Failed to init database connection: %s", err.Error())) |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + DB.SetMaxOpenConns(10) |
| 55 | + DB.SetMaxIdleConns(5) |
| 56 | + DB.SetConnMaxIdleTime(1 * time.Minute) |
| 57 | + DB.SetConnMaxLifetime(5 * time.Minute) |
| 58 | + |
| 59 | + DbInstance = DB |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +// Base methods |
| 65 | +func (base *BaseTable) InsertWithReturningId(query string, params ...any) (lastInsertedId int, err error) { |
| 66 | + |
| 67 | + if !strings.Contains(query, "RETURNING") { |
| 68 | + logging.Error("Query does not contain RETURNING keyword") |
| 69 | + return -1, errors.New("Query does not contain RETURNING keyword") |
| 70 | + } |
| 71 | + |
| 72 | + transaction, err := base.DB.Begin() |
| 73 | + |
| 74 | + statement, err := transaction.Prepare(query) |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + transaction.Rollback() |
| 78 | + logging.Error(fmt.Sprintf("Prepared statement error: %s", err.Error())) |
| 79 | + return -1, err |
| 80 | + } |
| 81 | + defer statement.Close() |
| 82 | + |
| 83 | + err = statement.QueryRow(params...).Scan(&lastInsertedId) |
| 84 | + |
| 85 | + if err != nil { |
| 86 | + logging.Error(fmt.Sprintf("Queryrow error: %s", err.Error())) |
| 87 | + transaction.Rollback() |
| 88 | + return -1, err |
| 89 | + } |
| 90 | + |
| 91 | + err = transaction.Commit() |
| 92 | + |
| 93 | + if err != nil { |
| 94 | + logging.Error(fmt.Sprintf("Transaction commit error: %s", err.Error())) |
| 95 | + transaction.Rollback() |
| 96 | + return -1, err |
| 97 | + } |
| 98 | + |
| 99 | + return lastInsertedId, nil |
| 100 | +} |
| 101 | +func (base *BaseTable) NonScalarQuery(query string, params ...any) (error error) { |
| 102 | + |
| 103 | + transaction, err := base.DB.Begin() |
| 104 | + |
| 105 | + if err != nil { |
| 106 | + logging.Error(fmt.Sprintf("Transaction error: %s", err.Error())) |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + statement, err := transaction.Prepare(query) |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + logging.Error(fmt.Sprintf("Prepared statement error: %s", err.Error())) |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + defer statement.Close() |
| 118 | + |
| 119 | + _, err = statement.Exec(params...) |
| 120 | + |
| 121 | + if err != nil { |
| 122 | + logging.Error(fmt.Sprintf("Exec error: %s", err.Error())) |
| 123 | + logging.Error(fmt.Sprintf("Query: %s", query)) |
| 124 | + for index := range params { |
| 125 | + logging.Error(params[index]) |
| 126 | + } |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + err = transaction.Commit() |
| 131 | + |
| 132 | + if err != nil { |
| 133 | + logging.Error(fmt.Sprintf("Transaction commit error: %s", err.Error())) |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
0 commit comments