ADD: added database connection for players data handling and started login funtion with database
This commit is contained in:
@@ -17,6 +17,29 @@ type database struct {
|
||||
db *sql.DB // Pointer to sql.DB
|
||||
}
|
||||
|
||||
func (d *database) SetupTables() error {
|
||||
InitTables(d.db)
|
||||
log.Println("Database setup completed successfully.")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *database) GetDB() *sql.DB {
|
||||
if d.db == nil {
|
||||
log.Println("Database connection is not established. Call Connect() first.")
|
||||
return nil
|
||||
}
|
||||
return d.db
|
||||
}
|
||||
|
||||
// New creates a new database instance with the provided configuration.
|
||||
// It initializes the database connection parameters but does not connect to the database.
|
||||
// This function should be called before calling Connect() to establish the connection.
|
||||
// It returns a pointer to the database instance.
|
||||
// Example usage:
|
||||
// db := database.New("localhost", 5432, "user", "password", "dbname")
|
||||
// It is intended to be used in the main application or setup phase.
|
||||
// This function is not intended to be called during normal application operation.
|
||||
// It is not intended to be called during normal application operation.
|
||||
func New(host string, port int, user, password, dbname string) *database {
|
||||
return &database{
|
||||
host: host,
|
||||
@@ -28,7 +51,7 @@ func New(host string, port int, user, password, dbname string) *database {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *database) Connect() error {
|
||||
func (d *database) Connect() (db *sql.DB) {
|
||||
fmt.Println("Connecting to the database...")
|
||||
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
|
||||
d.host, d.port, d.user, d.password, d.dbname)
|
||||
@@ -49,5 +72,5 @@ func (d *database) Connect() error {
|
||||
|
||||
log.Println("Connected to the database successfully")
|
||||
d.db = db
|
||||
return nil
|
||||
return db
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user