This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
-
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.
Merge pull request #8 from deweppro/dev
add lite query
- Loading branch information
Showing
18 changed files
with
726 additions
and
236 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
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") | ||
) |
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,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()) | ||
} | ||
} |
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,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()) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.