feat: abstracción DB multi-engine — CRUD genérico y openers para SQLite, Postgres, ClickHouse, DuckDB
Funciones Go con interfaz unificada para operaciones DB: open, close, create_table, exec, query, insert_row, insert_batch. Openers específicos por engine. Tipo DBConfig para configuración común. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
// PostgresOpen connects to a PostgreSQL server and returns a *sql.DB.
|
||||
// sslmode defaults to "disable" when empty.
|
||||
// Constructs a DSN of the form:
|
||||
//
|
||||
// host=<host> port=<port> user=<user> password=<password> dbname=<dbname> sslmode=<sslmode>
|
||||
func PostgresOpen(host string, port int, user, password, dbname string, sslmode string) (*sql.DB, error) {
|
||||
if sslmode == "" {
|
||||
sslmode = "disable"
|
||||
}
|
||||
dsn := fmt.Sprintf(
|
||||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
|
||||
host, port, user, password, dbname, sslmode,
|
||||
)
|
||||
db, err := sql.Open("pgx", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("postgres_open: open: %w", err)
|
||||
}
|
||||
if err := db.Ping(); err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("postgres_open: ping %s:%d/%s: %w", host, port, dbname, err)
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
Reference in New Issue
Block a user