Skip to content

Commit

Permalink
expose db and remove QueryAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
NoBypass committed Mar 26, 2024
1 parent e6a071e commit a264b8b
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 31 deletions.
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ db, err := surgo.Connect("127.0.0.1:8000",
surgo.Namespace("namespace"))
```

### Query agent
The idea of the query agent is to allow the user to define their own way to query tha database. Query agent is an
interface that has to be implemented in order to use it. The interface is as follows:
```go
type QueryAgent interface {
Query(query string, params map[string]any) (map[string]any, error)
Close() error
}
```
Setting a custom QueryAgent is useful when you want to use a different way to query the database or implement for
example a custom caching or tracing system.

## Querying

### Scanning
Expand Down
4 changes: 2 additions & 2 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func (db *DB) Scan(scan any, query string, args ...any) error {
// Exec executes the query and returns the result. Parameters are supported as
// a map or simply multiple arguments. For Example:
//
// db.Exec("SELECT * FROM table WHERE id = $id", map[string]any{"id": 1})
// DB.Exec("SELECT * FROM table WHERE id = $id", map[string]any{"id": 1})
//
// or
//
// db.Exec("SELECT * FROM table WHERE id = $1", 1)
// DB.Exec("SELECT * FROM table WHERE id = $1", 1)
func (db *DB) Exec(query string, args ...any) ([]Result, error) {
params, err := parseParams(args)
if err != nil {
Expand Down
16 changes: 3 additions & 13 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Option struct {
}

type DB struct {
db QueryAgent
DB QueryAgent
}

type QueryAgent interface {
Expand All @@ -34,10 +34,6 @@ func Connect(url string, options ...Option) (*DB, error) {
confMap[option.key] = option.val
}

if _, ok := confMap["agent"]; !ok {
confMap["agent"] = DefaultAgent
}

if _, err = db.Signin(confMap); err != nil {
return nil, err
}
Expand All @@ -56,7 +52,7 @@ func Connect(url string, options ...Option) (*DB, error) {
}

return &DB{
db: confMap["agent"].(AgentFunc)(db),
DB: DefaultAgent(db),
}, nil
}

Expand All @@ -71,7 +67,7 @@ func MustConnect(url string, options ...Option) *DB {
}

func (db *DB) Close() {
db.db.Close()
db.DB.Close()
}

func User(username string) Option {
Expand All @@ -93,9 +89,3 @@ func Database(database string) Option {
var DefaultAgent = func(db *surrealdb.DB) QueryAgent {
return db
}

type AgentFunc func(db *surrealdb.DB) QueryAgent

func CustomAgent(agentFunc AgentFunc) Option {
return Option{"agent", agentFunc}
}
2 changes: 1 addition & 1 deletion intergration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type testObject struct {
TimeSince int `db:"time_since"`
TimeSince int `DB:"time_since"`
Name string
}

Expand Down
6 changes: 3 additions & 3 deletions internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (db *DB) query(query string, params map[string]any) ([]Result, error) {
if !strings.HasSuffix(query, ";") {
query = query + ";"
}
resp, err := db.db.Query(query, params)
resp, err := db.DB.Query(query, params)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func structToMap[T any](content T) map[string]any {
for i := range nFields {
field := t.Field(i)
name := field.Name
if tag, ok := field.Tag.Lookup("db"); ok {
if tag, ok := field.Tag.Lookup("DB"); ok {
name = tag
}

Expand Down Expand Up @@ -165,7 +165,7 @@ func fillData(scan any, data any) error {
for k, v := range m {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Tag.Get("db") == k || field.Name == k || strings.ToLower(field.Name) == k {
if field.Tag.Get("DB") == k || field.Name == k || strings.ToLower(field.Name) == k {
setVal(reflect.ValueOf(scan).Elem().Field(i), reflect.ValueOf(v))
}
}
Expand Down

0 comments on commit a264b8b

Please sign in to comment.