Compare commits

..

No commits in common. "5bbe08ded5d06ceb45322e407aa4db628b870d6f" and "ff6de847317e1c04d9318f9f035faf3275ee5cb7" have entirely different histories.

4 changed files with 63 additions and 105 deletions

2
.gitignore vendored
View File

@ -1,2 +0,0 @@
__debug*
.*.sw*

View File

@ -1,24 +0,0 @@
package main
import (
"database/sql"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
)
func ConnectAnvilDB(dsn string) *bun.DB {
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook(
bundebug.WithVerbose(true),
bundebug.FromEnv("BUNDEBUG"),
))
return db
}
//func GetFeeds(db *bun.DB)

62
main.go
View File

@ -1,9 +1,14 @@
package main package main
import ( import (
"fmt"
"context" "context"
"errors" "errors"
"fmt" "database/sql"
"github.com/uptrace/bun"
"github.com/uptrace/bun/extra/bundebug"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
) )
var feeds []Feed var feeds []Feed
@ -16,57 +21,36 @@ func main() {
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)))
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook(
bundebug.WithVerbose(true),
bundebug.FromEnv("BUNDEBUG"),
))
anvildb := ConnectAnvilDB(dsn)
// Turn these into funcs with error handling, logging, return vals etc. // Turn these into funcs with error handling, logging, return vals etc.
feederr := anvildb.NewSelect(). feederr := db.NewSelect().Model(&feeds).OrderExpr("title ASC").Limit(10).Scan(ctx)
Model(&Feed{}).
OrderExpr("title ASC").
Limit(10).
Scan(ctx, &feeds)
if feederr != nil { if feederr != nil {
errors.Join(feederr, err) errors.Join(feederr, err)
} }
categoryerr := anvildb.NewSelect(). categoryerr := db.NewSelect().Model(&categories).OrderExpr("title ASC").Scan(ctx)
Model(&Category{}).
OrderExpr("title ASC").
Scan(ctx, &categories)
if categoryerr != nil { if categoryerr != nil {
errors.Join(categoryerr, err) errors.Join(categoryerr, err)
} }
entryerr := anvildb.NewSelect(). entryerr := db.NewSelect().Model(&entries).OrderExpr("published_at ASC").Limit(10).Scan(ctx)
Model(&Entry{}).
OrderExpr("published_at ASC").
Limit(10).
Scan(ctx, &entries)
if entryerr != nil { if entryerr != nil {
errors.Join(entryerr, err) errors.Join(entryerr, err)
} }
usererr := anvildb.NewSelect(). usererr := db.NewSelect().Model(&users).OrderExpr("id ASC").Scan(ctx)
Model(&User{}).
OrderExpr("id ASC").
Scan(ctx, &users)
if usererr != nil { if usererr != nil {
errors.Join(usererr, err) errors.Join(usererr, err)
} }
fmt.Printf("%+v\n", entries)
for _, val := range entries { fmt.Printf("%+v\n", categories)
fmt.Printf("val = %s\n", val) fmt.Printf("%+v\n", feeds)
} fmt.Printf("%+v\n", users)
for _, val := range categories { //
fmt.Printf("val = %s\n", val)
}
for _, val := range feeds {
fmt.Printf("val = %s\n", val)
}
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)
} }

View File

@ -17,7 +17,7 @@ type Feed struct {
ID int64 `bun:"id,pk,autoincrement"` ID int64 `bun:"id,pk,autoincrement"`
CategoryID int64 `bun:"category_id"` CategoryID int64 `bun:"category_id"`
Title string `bun:"title"` Title string `bun:"title"`
// SELECT id,category_id,title FROM feeds ORDER BY id; //SELECT id,category_id,title FROM feeds ORDER BY id;
} }
type Entry struct { type Entry struct {
bun.BaseModel `bun:"table:entries"` bun.BaseModel `bun:"table:entries"`
@ -25,13 +25,13 @@ type Entry struct {
ID int64 `bun:"id,pk,autoincrement"` ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"` Title string `bun:"title"`
Author string `bun:"author"` Author string `bun:"author"`
// SELECT id,title,author FROM entries ORDER BY published_at LIMIT 10; //SELECT id,title,author FROM entries ORDER BY published_at LIMIT 10;
} }
type Category struct { type Category struct {
bun.BaseModel `bun:"table:categories"` bun.BaseModel `bun:"table:categories"`
ID int64 `bun:"id,pk,autoincrement"` ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"` Title string `bun:"title"`
//SELECT id,title from categories; //SELECT id,title from categories;
} }