Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
SQLite supported
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiOdymel committed Dec 16, 2021
1 parent 541a394 commit d8daf56
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions goshia.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ var (
ErrNotTransaction = errors.New("goshia: processing non-transaction")
)

type Type int

const (
TypeMySQL Type = iota + 1
TypeSQLite
)

// Goshia
type Goshia struct {
Gorm *gorm.DB
Type Type
isTransaction bool
}

Expand All @@ -24,8 +32,13 @@ type Transaction struct {

// New
func New(g *gorm.DB) *Goshia {
typ := TypeMySQL
if g.Dialector.Name() == "sqlite" {
typ = TypeSQLite
}
return &Goshia{
Gorm: g,
Type: typ,
}
}

Expand Down Expand Up @@ -70,8 +83,15 @@ func (g *Goshia) ExecID(q *rushia.Query) (id int, err error) {
if err := tx.Exec(query, params...).Error; err != nil {
return err
}
if err := tx.Raw(`SELECT LAST_INSERT_ID()`).Scan(&id).Error; err != nil {
return err
switch g.Type {
case TypeMySQL:
if err := tx.Raw(`SELECT LAST_INSERT_ID()`).Scan(&id).Error; err != nil {
return err
}
case TypeSQLite:
if err := tx.Raw(`SELECT LAST_INSERT_ROWID()`).Scan(&id).Error; err != nil {
return err
}
}
return nil
}, nil)
Expand Down

0 comments on commit d8daf56

Please sign in to comment.