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

Commit

Permalink
Merge pull request #8 from deweppro/dev
Browse files Browse the repository at this point in the history
add lite query
  • Loading branch information
markus621 committed Jul 22, 2022
2 parents f69e58c + 694cc7e commit 80fb66d
Show file tree
Hide file tree
Showing 18 changed files with 726 additions and 236 deletions.
123 changes: 115 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

The library provides a nice and simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" that is used to interact with this table. Models allow you to query data in tables, as well as insert new records in the table.

# DEMO
# Examples

## Init connection

```go
package main
Expand All @@ -24,21 +26,71 @@ import (
)

func main() {

conn := mysql.New(&mysql.Config{Pool: []mysql.Item{}})
err := conn.Reconnect()
if err != nil {
conn := mysql.New(&mysql.Config{
Pool: []mysql.Item{
{
Name: "main_db",
Host: "127.0.0.1",
Port: 3306,
Schema: "test_table",
User: "demo",
Password: "1234",
},
}})
defer conn.Close()
if err := conn.Reconnect(); err != nil {
panic(err.Error())
}

db := orm.NewDB(conn, orm.Plugins{Logger: plugins.StdOutLog, Metrics: plugins.StdOutMetric})
pool := db.Pool("")
pool := db.Pool("main_db")

if err = pool.Ping(); err != nil {
if err := pool.Ping(); err != nil {
panic(err.Error())
}

err = pool.Call("demo_metric", func(ctx context.Context, conn *sql.DB) error {
// use pool[main_db] here
err := pool.CallContext("query name", context.Background(), func(ctx context.Context, db *sql.DB) error {...}
err := pool.TxContext("query name", context.Background(), func(context.Context, *sql.Tx) error) error {...}
}
```

## Basic query

```go
package main

import (
"context"
"database/sql"

"github.com/deweppro/go-orm"
"github.com/deweppro/go-orm/plugins"
"github.com/deweppro/go-orm/schema/mysql"
)

func main() {
...

var userName string
err := pool.CallContext("user_name", context.Background(), func(ctx context.Context, db *sql.DB) error {
rows, err := db.QueryContext(ctx, "select `name` from `users` where `id`=?", 10)
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
if err = rows.Scan(&userName); err != nil {
return err
}
}
if err = rows.Close(); err != nil {
return err
}
if err = rows.Err(); err != nil {
return err
}
return nil
})
if err != nil {
Expand All @@ -47,3 +99,58 @@ func main() {
}

```

## Lite query

```go
package main

import (
"context"
"fmt"

"github.com/deweppro/go-orm"
"github.com/deweppro/go-orm/plugins"
"github.com/deweppro/go-orm/schema/mysql"
)

func main() {
...

var userName string
err := pool.QueryContext("user_name", context.Background(), func(q orm.Querier) {
q.SQL("select `name` from `users` limit 1")
q.Bind(func(bind orm.Scanner) error {
return bind.Scan(&userName)
})
})

err = pool.ExecContext("user_name", context.Background(), func(e orm.Executor) {
e.SQL("insert into `users` (`id`, `name`) values (?, ?);")
e.Params(3, "cccc")

e.Bind(func(result orm.Result) error {
fmt.Printf("RowsAffected=%d LastInsertId=%d", result.RowsAffected, result.LastInsertId)
return nil
})
})

err = pool.TransactionContext("", context.Background(), func(v orm.Tx) {
v.Exec(func(e orm.Executor) {
e.SQL("insert into `users` (`id`, `name`) values (?, ?);")
e.Params(3, "cccc")

e.Bind(func(result orm.Result) error {
fmt.Printf("RowsAffected=%d LastInsertId=%d", result.RowsAffected, result.LastInsertId)
return nil
})
})
v.Query(func(q orm.Querier) {
q.SQL("select `name` from `users` limit 1")
q.Bind(func(bind orm.Scanner) error {
return bind.Scan(&userName)
})
})
})
}
```
8 changes: 8 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package orm

import "github.com/deweppro/go-errors"

var (
//ErrInvalidModelPool if sync pool has invalid model type
ErrInvalidModelPool = errors.New("invalid internal model pool")
)
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func NewDB(c schema.Connector, plug Plugins) *DB {
}

//Pool getting pool connections by name
func (d *DB) Pool(name string) StmtInterface {
func (d *DB) Pool(name string) *Stmt {
return newStmt(name, d.conn, d.plug)
}
1 change: 0 additions & 1 deletion errors.go

This file was deleted.

60 changes: 60 additions & 0 deletions example/basic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"context"
"database/sql"

"github.com/deweppro/go-orm"
"github.com/deweppro/go-orm/plugins"
"github.com/deweppro/go-orm/schema/mysql"
)

func main() {
conn := mysql.New(&mysql.Config{
Pool: []mysql.Item{
{
Name: "main_db",
Host: "127.0.0.1",
Port: 3306,
Schema: "test_table",
User: "demo",
Password: "1234",
},
}})
defer conn.Close() //nolint: errcheck
if err := conn.Reconnect(); err != nil {
panic(err.Error())
}

db := orm.NewDB(conn, orm.Plugins{Logger: plugins.StdOutLog, Metrics: plugins.StdOutMetric})
pool := db.Pool("main_db")

if err := pool.Ping(); err != nil {
panic(err.Error())
}

var userName string
err := pool.CallContext("user_name", context.Background(), func(ctx context.Context, db *sql.DB) error {
rows, err := db.QueryContext(ctx, "select `name` from `users` where `id`=?", 10)
if err != nil {
return err
}
defer rows.Close() //nolint: errcheck

for rows.Next() {
if err = rows.Scan(&userName); err != nil {
return err
}
}
if err = rows.Close(); err != nil {
return err
}
if err = rows.Err(); err != nil {
return err
}
return nil
})
if err != nil {
panic(err.Error())
}
}
80 changes: 80 additions & 0 deletions example/lite/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"context"
"fmt"

"github.com/deweppro/go-orm"
"github.com/deweppro/go-orm/plugins"
"github.com/deweppro/go-orm/schema/mysql"
)

func main() {
conn := mysql.New(&mysql.Config{
Pool: []mysql.Item{
{
Name: "main_db",
Host: "127.0.0.1",
Port: 3306,
Schema: "test_table",
User: "demo",
Password: "1234",
},
}})

if err := conn.Reconnect(); err != nil {
panic(err.Error())
}

db := orm.NewDB(conn, orm.Plugins{Logger: plugins.StdOutLog, Metrics: plugins.StdOutMetric})
pool := db.Pool("main_db")

if err := pool.Ping(); err != nil {
panic(err.Error())
}

var userName string
err := pool.QueryContext("user_name", context.Background(), func(q orm.Querier) {
q.SQL("select `name` from `users` limit 1")
q.Bind(func(bind orm.Scanner) error {
return bind.Scan(&userName)
})
})
if err != nil {
panic(err.Error())
}

err = pool.ExecContext("user_name", context.Background(), func(e orm.Executor) {
e.SQL("insert into `users` (`id`, `name`) values (?, ?);")
e.Params(3, "cccc")

e.Bind(func(result orm.Result) error {
fmt.Printf("RowsAffected=%d LastInsertId=%d", result.RowsAffected, result.LastInsertId)
return nil
})
})
if err != nil {
panic(err.Error())
}

err = pool.TransactionContext("", context.Background(), func(v orm.Tx) {
v.Exec(func(e orm.Executor) {
e.SQL("insert into `users` (`id`, `name`) values (?, ?);")
e.Params(3, "cccc")

e.Bind(func(result orm.Result) error {
fmt.Printf("RowsAffected=%d LastInsertId=%d", result.RowsAffected, result.LastInsertId)
return nil
})
})
v.Query(func(q orm.Querier) {
q.SQL("select `name` from `users` limit 1")
q.Bind(func(bind orm.Scanner) error {
return bind.Scan(&userName)
})
})
})
if err != nil {
panic(err.Error())
}
}
33 changes: 0 additions & 33 deletions example/main.go

This file was deleted.

Loading

0 comments on commit 80fb66d

Please sign in to comment.