|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "fmt" |
| 7 | + "runtime" |
| 8 | + |
| 9 | + "github.com/dipdup-net/go-lib/config" |
| 10 | + "github.com/pkg/errors" |
| 11 | + "github.com/uptrace/bun" |
| 12 | + "github.com/uptrace/bun/dialect/pgdialect" |
| 13 | +) |
| 14 | + |
| 15 | +// Bun - |
| 16 | +type Bun struct { |
| 17 | + sqldb *sql.DB |
| 18 | + conn *bun.DB |
| 19 | +} |
| 20 | + |
| 21 | +// NewBun - |
| 22 | +func NewBun() *Bun { |
| 23 | + return new(Bun) |
| 24 | +} |
| 25 | + |
| 26 | +// DB - |
| 27 | +func (db *Bun) DB() *bun.DB { |
| 28 | + return db.conn |
| 29 | +} |
| 30 | + |
| 31 | +// Connect - |
| 32 | +func (db *Bun) Connect(ctx context.Context, cfg config.Database) error { |
| 33 | + if cfg.Kind != config.DBKindPostgres { |
| 34 | + return errors.Wrap(ErrUnsupportedDatabaseType, cfg.Kind) |
| 35 | + } |
| 36 | + if cfg.Path != "" { |
| 37 | + conn, err := sql.Open("postgres", cfg.Path) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + db.sqldb = conn |
| 42 | + db.conn = bun.NewDB(db.sqldb, pgdialect.New()) |
| 43 | + } else { |
| 44 | + connStr := fmt.Sprintf( |
| 45 | + "postgres://%s:%s@%s:%d/%s?sslmode=disable", |
| 46 | + cfg.User, |
| 47 | + cfg.Password, |
| 48 | + cfg.Host, |
| 49 | + cfg.Port, |
| 50 | + cfg.Database, |
| 51 | + ) |
| 52 | + conn, err := sql.Open("postgres", connStr) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + db.sqldb = conn |
| 57 | + db.conn = bun.NewDB(db.sqldb, pgdialect.New()) |
| 58 | + } |
| 59 | + maxOpenConns := 4 * runtime.GOMAXPROCS(0) |
| 60 | + db.sqldb.SetMaxOpenConns(maxOpenConns) |
| 61 | + db.sqldb.SetMaxIdleConns(maxOpenConns) |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +// Close - |
| 66 | +func (db *Bun) Close() error { |
| 67 | + if err := db.conn.Close(); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + return db.sqldb.Close() |
| 71 | +} |
| 72 | + |
| 73 | +// Exec - |
| 74 | +func (db *Bun) Exec(ctx context.Context, query string, args ...any) (int64, error) { |
| 75 | + result, err := db.conn.ExecContext(ctx, query, args...) |
| 76 | + if err != nil { |
| 77 | + return 0, err |
| 78 | + } |
| 79 | + return result.RowsAffected() |
| 80 | +} |
| 81 | + |
| 82 | +// Ping - |
| 83 | +func (db *Bun) Ping(ctx context.Context) error { |
| 84 | + if db.conn == nil { |
| 85 | + return ErrConnectionIsNotInitialized |
| 86 | + } |
| 87 | + return db.conn.PingContext(ctx) |
| 88 | +} |
| 89 | + |
| 90 | +// State - |
| 91 | +func (db *Bun) State(ctx context.Context, indexName string) (*State, error) { |
| 92 | + var s State |
| 93 | + err := db.conn.NewSelect().Model(&s).Where("index_name = ?", indexName).Limit(1).Scan(ctx) |
| 94 | + return &s, err |
| 95 | +} |
| 96 | + |
| 97 | +// CreateState - |
| 98 | +func (db *Bun) CreateState(ctx context.Context, s *State) error { |
| 99 | + _, err := db.conn.NewInsert().Model(s).Exec(ctx) |
| 100 | + return err |
| 101 | +} |
| 102 | + |
| 103 | +// UpdateState - |
| 104 | +func (db *Bun) UpdateState(ctx context.Context, s *State) error { |
| 105 | + _, err := db.conn.NewUpdate().Model(s).Where("index_name = ?", s.IndexName).Exec(ctx) |
| 106 | + return err |
| 107 | +} |
| 108 | + |
| 109 | +// DeleteState - |
| 110 | +func (db *Bun) DeleteState(ctx context.Context, s *State) error { |
| 111 | + _, err := db.conn.NewDelete().Model(s).Where("index_name = ?", s.IndexName).Exec(ctx) |
| 112 | + return err |
| 113 | +} |
| 114 | + |
| 115 | +// MakeTableComment - |
| 116 | +func (db *Bun) MakeTableComment(ctx context.Context, name string, comment string) error { |
| 117 | + _, err := db.conn.ExecContext(ctx, |
| 118 | + `COMMENT ON TABLE ? IS ?`, |
| 119 | + bun.Ident(name), |
| 120 | + comment) |
| 121 | + |
| 122 | + return err |
| 123 | +} |
| 124 | + |
| 125 | +// MakeColumnComment - |
| 126 | +func (db *Bun) MakeColumnComment(ctx context.Context, tableName string, columnName string, comment string) error { |
| 127 | + _, err := db.conn.ExecContext(ctx, |
| 128 | + `COMMENT ON COLUMN ?.? IS ?`, |
| 129 | + bun.Ident(tableName), |
| 130 | + bun.Ident(columnName), |
| 131 | + comment) |
| 132 | + |
| 133 | + return err |
| 134 | +} |
| 135 | + |
| 136 | +// CreateTable - |
| 137 | +func (db *Bun) CreateTable(ctx context.Context, model any, opts ...CreateTableOption) error { |
| 138 | + if model == nil { |
| 139 | + return nil |
| 140 | + } |
| 141 | + var options CreateTableOptions |
| 142 | + for i := range opts { |
| 143 | + opts[i](&options) |
| 144 | + } |
| 145 | + |
| 146 | + query := db.DB(). |
| 147 | + NewCreateTable(). |
| 148 | + Model(model) |
| 149 | + |
| 150 | + if options.ifNotExists { |
| 151 | + query = query.IfNotExists() |
| 152 | + } |
| 153 | + |
| 154 | + if options.partitionBy != "" { |
| 155 | + query = query.PartitionBy(options.partitionBy) |
| 156 | + } |
| 157 | + |
| 158 | + if options.temporary { |
| 159 | + query = query.Temp() |
| 160 | + } |
| 161 | + |
| 162 | + _, err := query.Exec(ctx) |
| 163 | + return err |
| 164 | +} |
0 commit comments