История изменений
Исправление thegoldone, (текущая версия) :
Изменения
diff --git a/origin.go b/changed.go
index a9192fa..f4c2ea3 100644
--- a/origin.go
+++ b/changed.go
@@ -1,11 +1,15 @@
package main
import (
+ "context"
+ "database/sql"
"fmt"
"log"
- "github.com/go-pg/pg/v10"
- "github.com/go-pg/pg/v10/orm"
+ "github.com/uptrace/bun"
+ "github.com/uptrace/bun/dialect/mysqldialect"
+
+ _ "github.com/go-sql-driver/mysql"
)
type Person struct {
@@ -29,28 +33,24 @@ func (u Person) String() string {
func main() {
- url := fmt.Sprintf("postgres://%s:%s@%s:%d/%s%s",
- user, password, host, port, dbname, false)
- opt, errors := pg.ParseURL(url)
- if errors != nil {
- log.Fatal(errors)
- }
+ url := fmt.Sprintf("%s:%s@%s:%d/%s", user, password, host, port, dbname)
- db := pg.Connect(opt)
+ sqldb, err := sql.Open("mysql", url)
+ if err != nil {
+ log.Fatal(err)
+ }
+ db := bun.NewDB(sqldb, mysqldialect.New())
defer db.Close()
+ var ctx = context.Background()
model := (*Person)(nil)
- err := db.Model(model).DropTable(&orm.DropTableOptions{
- IfExists: true,
- })
+ _, err = db.NewDropTable().Model(model).IfExists().Exec(ctx)
if err != nil {
log.Fatal(err)
}
- err = db.Model(model).CreateTable(&orm.CreateTableOptions{
- Temp: false,
- })
+ _, err = db.NewCreateTable().Model(model).IfNotExists().Temp().Exec(ctx)
if err != nil {
log.Fatal(err)
}
@@ -62,16 +62,16 @@ func main() {
Age: 35,
Language: []string{"Go"},
}
- _, err = db.Model(Person1).Insert()
+ _, err = db.NewInsert().Model(Person1).Exec(ctx)
if err != nil {
log.Fatal(err)
}
- _, err = db.Model(&Person{
+ _, err = db.NewInsert().Model(&Person{
Name: "Kelly",
Age: 35,
Language: []string{"Golang", "Python"},
- }).Insert()
+ }).Exec(ctx)
if err != nil {
log.Fatal(err)
}
@@ -79,9 +79,9 @@ func main() {
fmt.Println("Inserted data")
emp := new(Person)
- err = db.Model(emp).
+ _, err = db.NewSelect().Model(emp).
Where("Person.id = ?", Person1.Id).
- Select()
+ Exec(ctx)
if err != nil {
log.Fatal(err)
}
Итоговый файл
package main
import (
"context"
"database/sql"
"fmt"
"log"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/mysqldialect"
_ "github.com/go-sql-driver/mysql"
)
type Person struct {
Id int64
Name string
Age int64
Language []string
}
const (
host = "127.0.0.1"
port = 5433
user = "user"
password = "password"
dbname = "database"
)
func (u Person) String() string {
return fmt.Sprintf("Person<%d %s %v %l>", u.Id, u.Name, u.Age, u.Language)
}
func main() {
url := fmt.Sprintf("%s:%s@%s:%d/%s", user, password, host, port, dbname)
sqldb, err := sql.Open("mysql", url)
if err != nil {
log.Fatal(err)
}
db := bun.NewDB(sqldb, mysqldialect.New())
defer db.Close()
var ctx = context.Background()
model := (*Person)(nil)
_, err = db.NewDropTable().Model(model).IfExists().Exec(ctx)
if err != nil {
log.Fatal(err)
}
_, err = db.NewCreateTable().Model(model).IfNotExists().Temp().Exec(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println("Created table")
Person1 := &Person{
Name: "John",
Age: 35,
Language: []string{"Go"},
}
_, err = db.NewInsert().Model(Person1).Exec(ctx)
if err != nil {
log.Fatal(err)
}
_, err = db.NewInsert().Model(&Person{
Name: "Kelly",
Age: 35,
Language: []string{"Golang", "Python"},
}).Exec(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println("Inserted data")
emp := new(Person)
_, err = db.NewSelect().Model(emp).
Where("Person.id = ?", Person1.Id).
Exec(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Query for id=1 returned: ")
fmt.Println(emp)
}
Исходная версия thegoldone, :
Изменения
diff --git a/main.go b/origin.go
index f4c2ea3..a9192fa 100644
--- a/main.go
+++ b/origin.go
@@ -1,15 +1,11 @@
package main
import (
- "context"
- "database/sql"
"fmt"
"log"
- "github.com/uptrace/bun"
- "github.com/uptrace/bun/dialect/mysqldialect"
-
- _ "github.com/go-sql-driver/mysql"
+ "github.com/go-pg/pg/v10"
+ "github.com/go-pg/pg/v10/orm"
)
type Person struct {
@@ -33,24 +29,28 @@ func (u Person) String() string {
func main() {
- url := fmt.Sprintf("%s:%s@%s:%d/%s", user, password, host, port, dbname)
-
- sqldb, err := sql.Open("mysql", url)
- if err != nil {
- log.Fatal(err)
+ url := fmt.Sprintf("postgres://%s:%s@%s:%d/%s%s",
+ user, password, host, port, dbname, false)
+ opt, errors := pg.ParseURL(url)
+ if errors != nil {
+ log.Fatal(errors)
}
- db := bun.NewDB(sqldb, mysqldialect.New())
+ db := pg.Connect(opt)
+
defer db.Close()
- var ctx = context.Background()
model := (*Person)(nil)
- _, err = db.NewDropTable().Model(model).IfExists().Exec(ctx)
+ err := db.Model(model).DropTable(&orm.DropTableOptions{
+ IfExists: true,
+ })
if err != nil {
log.Fatal(err)
}
- _, err = db.NewCreateTable().Model(model).IfNotExists().Temp().Exec(ctx)
+ err = db.Model(model).CreateTable(&orm.CreateTableOptions{
+ Temp: false,
+ })
if err != nil {
log.Fatal(err)
}
@@ -62,16 +62,16 @@ func main() {
Age: 35,
Language: []string{"Go"},
}
- _, err = db.NewInsert().Model(Person1).Exec(ctx)
+ _, err = db.Model(Person1).Insert()
if err != nil {
log.Fatal(err)
}
- _, err = db.NewInsert().Model(&Person{
+ _, err = db.Model(&Person{
Name: "Kelly",
Age: 35,
Language: []string{"Golang", "Python"},
- }).Exec(ctx)
+ }).Insert()
if err != nil {
log.Fatal(err)
}
@@ -79,9 +79,9 @@ func main() {
fmt.Println("Inserted data")
emp := new(Person)
- _, err = db.NewSelect().Model(emp).
+ err = db.Model(emp).
Where("Person.id = ?", Person1.Id).
- Exec(ctx)
+ Select()
if err != nil {
log.Fatal(err)
}
Итоговый файл
package main
import (
"context"
"database/sql"
"fmt"
"log"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/mysqldialect"
_ "github.com/go-sql-driver/mysql"
)
type Person struct {
Id int64
Name string
Age int64
Language []string
}
const (
host = "127.0.0.1"
port = 5433
user = "user"
password = "password"
dbname = "database"
)
func (u Person) String() string {
return fmt.Sprintf("Person<%d %s %v %l>", u.Id, u.Name, u.Age, u.Language)
}
func main() {
url := fmt.Sprintf("%s:%s@%s:%d/%s", user, password, host, port, dbname)
sqldb, err := sql.Open("mysql", url)
if err != nil {
log.Fatal(err)
}
db := bun.NewDB(sqldb, mysqldialect.New())
defer db.Close()
var ctx = context.Background()
model := (*Person)(nil)
_, err = db.NewDropTable().Model(model).IfExists().Exec(ctx)
if err != nil {
log.Fatal(err)
}
_, err = db.NewCreateTable().Model(model).IfNotExists().Temp().Exec(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println("Created table")
Person1 := &Person{
Name: "John",
Age: 35,
Language: []string{"Go"},
}
_, err = db.NewInsert().Model(Person1).Exec(ctx)
if err != nil {
log.Fatal(err)
}
_, err = db.NewInsert().Model(&Person{
Name: "Kelly",
Age: 35,
Language: []string{"Golang", "Python"},
}).Exec(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println("Inserted data")
emp := new(Person)
_, err = db.NewSelect().Model(emp).
Where("Person.id = ?", Person1.Id).
Exec(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Query for id=1 returned: ")
fmt.Println(emp)
}