-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
387 additions
and
626 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package providers | ||
package xtemplate | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package xtemplate | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
func WithDB(name string, db *sql.DB, opt *sql.TxOptions) Option { | ||
return func(c *Config) error { | ||
if db == nil { | ||
return fmt.Errorf("cannot create database provider with nil sql.DB. name: %s", name) | ||
} | ||
if err := c.CheckName(name); err != nil { | ||
return err | ||
} | ||
c.Databases = append(c.Databases, DotDBConfig{Name: name, DB: db, TxOptions: opt}) | ||
return nil | ||
} | ||
} | ||
|
||
type DotDBConfig struct { | ||
*sql.DB `json:"-"` | ||
*sql.TxOptions `json:"-"` | ||
Name string `json:"name"` | ||
Driver string `json:"driver"` | ||
Connstr string `json:"connstr"` | ||
MaxOpenConns int `json:"max_open_conns"` | ||
} | ||
|
||
var _ CleanupDotProvider = &DotDBConfig{} | ||
|
||
func (d *DotDBConfig) FieldName() string { return d.Name } | ||
func (d *DotDBConfig) Value(r Request) (any, error) { | ||
if d.DB == nil { | ||
db, err := sql.Open(d.Driver, d.Connstr) | ||
if err != nil { | ||
return &DotDB{}, fmt.Errorf("failed to open database with driver name '%s': %w", d.Driver, err) | ||
} | ||
db.SetMaxOpenConns(d.MaxOpenConns) | ||
if err := db.Ping(); err != nil { | ||
return &DotDB{}, fmt.Errorf("failed to ping database on open: %w", err) | ||
} | ||
d.DB = db | ||
} | ||
return &DotDB{d.DB, GetLogger(r.R.Context()), r.R.Context(), d.TxOptions, nil}, nil | ||
} | ||
func (dp *DotDBConfig) Cleanup(v any, err error) error { | ||
d := v.(*DotDB) | ||
if err != nil { | ||
return errors.Join(err, d.rollback()) | ||
} else { | ||
return errors.Join(err, d.commit()) | ||
} | ||
} |
Oops, something went wrong.