-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
138 additions
and
36 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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//go:build postgres | ||
|
||
package provider | ||
|
||
import ( | ||
"github.com/wintbiit/ninedns/model" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type PostgresProvider struct { | ||
Provider | ||
*gorm.DB | ||
dsn string | ||
} | ||
|
||
func init() { | ||
constructors["postgres"] = newPostgresProvider | ||
} | ||
|
||
func newPostgresProvider(dsn string) (Provider, error) { | ||
provider := &PostgresProvider{ | ||
dsn: dsn, | ||
} | ||
|
||
db, err := gorm.Open(postgres.Open(provider.dsn), &gorm.Config{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
provider.DB = db | ||
|
||
return provider, nil | ||
} | ||
|
||
func (p *PostgresProvider) Provide(ruleset string) ([]model.Record, error) { | ||
tx := p.Begin() | ||
defer tx.Rollback() | ||
|
||
var records []model.Record | ||
|
||
if err := tx.Table(ruleset).Find(&records).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
tx.Commit() | ||
|
||
return records, nil | ||
} | ||
|
||
func (p *PostgresProvider) AutoMigrate(table string) error { | ||
return p.DB.Table(table).AutoMigrate(&model.Record{}) | ||
} |
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,3 +1,5 @@ | ||
//go:build sqlite | ||
|
||
package provider | ||
|
||
import ( | ||
|