-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
48 lines (41 loc) · 926 Bytes
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package database
import (
"errors"
"github.com/Facundo-Arana/GoLang-TP/internal/config"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3" // comento como si supiera de lo que hablo
)
// NewDatabase ...
func NewDatabase(conf *config.Config) (*sqlx.DB, error) {
switch conf.DB.Type {
case "sqlite3":
db, err := sqlx.Open(conf.DB.Driver, conf.DB.Conn)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
default:
return nil, errors.New("invalid db type")
}
}
/*
// Team tiene un nombre
type Team struct {
ID int `db:"id"`
name string `db:"name"`
}
db.MustExec("INSERT INTO team (name) VALUES (?)", "jane doe")
rows, err := db.Queryx("SELECT id, name FROM team")
if err != nil {
panic(err.Error())
}
for rows.Next() {
var t Team
rows.StructScan(&t)
fmt.Println(t)
}
*/