Closed as not planned
Description
GORM Playground Link
Description
package main
import (
"context"
"database/sql"
"fmt"
"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
db := newdb()
rec := &User{}
res := db.First(rec, User{UserID: 123, DeletedAt: gorm.DeletedAt{Valid: true}})
if err := res.Error; err != nil {
panic(err)
}
q := res.Statement.SQL.String()
fmt.Println(q)
// SELECT * FROM "users" WHERE ("users"."user_id" = $1 AND "users"."deleted_at" = $2) AND "users"."deleted_at" IS NULL ORDER BY "users"."user_id" LIMIT $3
}
type User struct {
UserID uint32 `gorm:"primaryKey;autoIncrement"` //
Session uint32
CreatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func newdb() *gorm.DB {
dialer := postgres.Dialector{Config: &postgres.Config{
Conn: &mockConnPool{},
}}
db, err := gorm.Open(dialer, &gorm.Config{DryRun: true})
if err != nil {
panic(err)
}
return db
}
type mockConnPool struct{}
func (mockConnPool) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
return nil, nil
}
func (mockConnPool) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return nil, nil
}
func (mockConnPool) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return nil, nil
}
func (mockConnPool) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
return nil
}