Skip to content

Commit

Permalink
feat(core): file reader
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielHAlba committed Sep 19, 2022
1 parent 9f2566e commit 1235ee3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
42 changes: 0 additions & 42 deletions sql/db/service.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,8 @@
package db

import (
"database/sql"

"github.com/agflow/tools/log"
)

// Client is a wrapper of a sql.DB client
type Client struct {
DB *sql.DB
}

// Service is an interface of db.Service
type Service interface {
Select(interface{}, string, ...interface{}) error
Close() error
Exec(string, ...interface{}) error
}

// Select selects from `client` using the `query` and `args` and set the result on `dest`
func (c *Client) Select(dest interface{}, query string, args ...interface{}) error {
return Select(c.DB, dest, query, args...)
}

// Exec executes from `client` using the `query` and `args`
func (c *Client) Exec(query string, args ...interface{}) error {
_, err := c.DB.Exec(query, args...)
return err
}

// New return a new db.Client
func New(url string) (*Client, error) {
db, err := sql.Open("postgres", url)
return &Client{DB: db}, err
}

// MustNew return a new db.Client without an error
func MustNew(url string) *Client {
dbSvc, err := New(url)
if err != nil {
log.Fatal(err)
}
return dbSvc
}

// Close closes db connection from the client
func (c *Client) Close() error {
return c.DB.Close()
}
43 changes: 43 additions & 0 deletions sql/db/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package db

import (
"database/sql"

"github.com/agflow/tools/log"
)

// Client is a wrapper of a sql.DB client
type Client struct {
DB *sql.DB
}

// Select selects from `client` using the `query` and `args` and set the result on `dest`
func (c *Client) Select(dest interface{}, query string, args ...interface{}) error {
return Select(c.DB, dest, query, args...)
}

// Exec executes from `client` using the `query` and `args`
func (c *Client) Exec(query string, args ...interface{}) error {
_, err := c.DB.Exec(query, args...)
return err
}

// New return a new db.Client
func New(url string) (*Client, error) {
db, err := sql.Open("postgres", url)
return &Client{DB: db}, err
}

// MustNew return a new db.Client without an error
func MustNew(url string) *Client {
dbSvc, err := New(url)
if err != nil {
log.Fatal(err)
}
return dbSvc
}

// Close closes db connection from the client
func (c *Client) Close() error {
return c.DB.Close()
}
4 changes: 3 additions & 1 deletion sql/query/reader.go → sql/file/reader.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package query
package file

import (
"embed"
Expand Down Expand Up @@ -68,6 +68,8 @@ func mustReadFiles(root string, queriesFS embed.FS, query interface{}) {

// MustLoad loads queries
// that are located in the default sql directory
//
// Deprecated: MustLoadSQLFiles offers more functionality
func MustLoad(queriesFS embed.FS, query interface{}) {
f, err := fs.ReadDir(queriesFS, ".")
if err != nil {
Expand Down

0 comments on commit 1235ee3

Please sign in to comment.