parent
ff6de84731
commit
b886c39ac9
2 changed files with 102 additions and 62 deletions
@ -1,56 +1,87 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
import ( |
||||||
"fmt" |
"context" |
||||||
"context" |
"database/sql" |
||||||
"errors" |
"errors" |
||||||
"database/sql" |
"fmt" |
||||||
|
|
||||||
"github.com/uptrace/bun" |
"github.com/uptrace/bun" |
||||||
"github.com/uptrace/bun/extra/bundebug" |
|
||||||
"github.com/uptrace/bun/dialect/pgdialect" |
"github.com/uptrace/bun/dialect/pgdialect" |
||||||
"github.com/uptrace/bun/driver/pgdriver" |
"github.com/uptrace/bun/driver/pgdriver" |
||||||
|
"github.com/uptrace/bun/extra/bundebug" |
||||||
) |
) |
||||||
|
|
||||||
var feeds []Feed |
var feeds []Feed |
||||||
var categories []Category |
var categories []Category |
||||||
var entries []Entry |
var entries []Entry |
||||||
var users []User |
var users []User |
||||||
|
|
||||||
func main() { |
func main() { |
||||||
var err error |
var err error |
||||||
ctx := context.Background() |
ctx := context.Background() |
||||||
dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable" |
dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable" |
||||||
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
|
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
|
||||||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn))) |
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn))) |
||||||
db := bun.NewDB(sqldb, pgdialect.New()) |
db := bun.NewDB(sqldb, pgdialect.New()) |
||||||
|
|
||||||
db.AddQueryHook(bundebug.NewQueryHook( |
db.AddQueryHook(bundebug.NewQueryHook( |
||||||
bundebug.WithVerbose(true), |
bundebug.WithVerbose(true), |
||||||
bundebug.FromEnv("BUNDEBUG"), |
bundebug.FromEnv("BUNDEBUG"), |
||||||
)) |
)) |
||||||
|
|
||||||
|
// Turn these into funcs with error handling, logging, return vals etc.
|
||||||
// Turn these into funcs with error handling, logging, return vals etc.
|
feederr := db.NewSelect(). |
||||||
feederr := db.NewSelect().Model(&feeds).OrderExpr("title ASC").Limit(10).Scan(ctx) |
Model(&feeds). |
||||||
|
OrderExpr("title ASC"). |
||||||
if feederr != nil { |
Limit(10). |
||||||
errors.Join(feederr, err) |
Scan(ctx) |
||||||
} |
|
||||||
categoryerr := db.NewSelect().Model(&categories).OrderExpr("title ASC").Scan(ctx) |
if feederr != nil { |
||||||
if categoryerr != nil { |
errors.Join(feederr, err) |
||||||
errors.Join(categoryerr, err) |
} |
||||||
} |
categoryerr := db.NewSelect(). |
||||||
entryerr := db.NewSelect().Model(&entries).OrderExpr("published_at ASC").Limit(10).Scan(ctx) |
Model(&categories). |
||||||
if entryerr != nil { |
OrderExpr("title ASC"). |
||||||
errors.Join(entryerr, err) |
Scan(ctx) |
||||||
} |
if categoryerr != nil { |
||||||
usererr := db.NewSelect().Model(&users).OrderExpr("id ASC").Scan(ctx) |
errors.Join(categoryerr, err) |
||||||
if usererr != nil { |
} |
||||||
errors.Join(usererr, err) |
entryerr := db.NewSelect(). |
||||||
} |
Model(&entries). |
||||||
fmt.Printf("%+v\n", entries) |
OrderExpr("published_at ASC"). |
||||||
fmt.Printf("%+v\n", categories) |
Limit(10). |
||||||
fmt.Printf("%+v\n", feeds) |
Scan(ctx) |
||||||
fmt.Printf("%+v\n", users) |
if entryerr != nil { |
||||||
//
|
errors.Join(entryerr, err) |
||||||
|
} |
||||||
|
usererr := db.NewSelect(). |
||||||
|
Model(&users). |
||||||
|
OrderExpr("id ASC"). |
||||||
|
Scan(ctx) |
||||||
|
if usererr != nil { |
||||||
|
errors.Join(usererr, err) |
||||||
|
} |
||||||
|
|
||||||
|
fmt.Printf("[]Entry\n") |
||||||
|
for _, val := range entries { |
||||||
|
fmt.Printf("val = %s\n", val) |
||||||
|
} |
||||||
|
fmt.Printf("[]Category\n") |
||||||
|
for _, val := range categories { |
||||||
|
|
||||||
|
fmt.Printf("val = %s\n", val) |
||||||
|
} |
||||||
|
fmt.Printf("[]Feed\n") |
||||||
|
for _, val := range feeds { |
||||||
|
fmt.Printf("val = %s\n", val) |
||||||
|
} |
||||||
|
fmt.Printf("[]User\n") |
||||||
|
for _, val := range users { |
||||||
|
fmt.Printf("val = %s\n", val) |
||||||
|
} |
||||||
|
// fmt.Printf("%+v\n", entries)
|
||||||
|
// fmt.Printf("%+v\n", categories)
|
||||||
|
// fmt.Printf("%+v\n", feeds)
|
||||||
|
// fmt.Printf("%+v\n", users)
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue