# HG changeset patch # User Christophe de Vienne <christophe@cdevienne.info> # Date 1602537724 -7200 # Mon Oct 12 23:22:04 2020 +0200 # Node ID 86f7892ca412dbd41844d019a65a519bd6c2d254 # Parent 9a712f68c50a74e5b69a09e4c75674fdb0e5e41d database: more helpers diff --git a/database/sql.go b/database/sql.go --- a/database/sql.go +++ b/database/sql.go @@ -6,6 +6,7 @@ "strings" "github.com/Masterminds/squirrel" + "github.com/jmoiron/sqlx" "github.com/lann/builder" "github.com/rs/zerolog" ) @@ -39,6 +40,7 @@ ExecContext(context.Context, string, ...interface{}) (sql.Result, error) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error + QueryxContext(context.Context, string, ...interface{}) (*sqlx.Rows, error) } func setPlaceHolderFormat(query squirrel.Sqlizer) squirrel.Sqlizer { @@ -96,6 +98,18 @@ return e.ExecContext(ctx, sqlQuery, args...) } +// QueryContext runs a squirrel query on the given db or tx +func QueryContext(ctx context.Context, e SQLExecutor, query squirrel.Sqlizer, log *zerolog.Logger) (*sqlx.Rows, error) { + query = setPlaceHolderFormat(query) + sqlQuery, args, err := query. + ToSql() + if err != nil { + return nil, err + } + SQLTrace(log, sqlQuery, args) + return e.QueryxContext(ctx, sqlQuery, args...) +} + // ValuesMap returns the values for a list of columns as a map. If a column does // not exits, the corresponding value is set to nil func ValuesMap(m Mapped, columns ...string) map[string]interface{} { diff --git a/database/sql_helper.go b/database/sql_helper.go --- a/database/sql_helper.go +++ b/database/sql_helper.go @@ -5,6 +5,7 @@ "database/sql" sq "github.com/Masterminds/squirrel" + "github.com/jmoiron/sqlx" "github.com/rs/zerolog" ) @@ -55,6 +56,11 @@ return ExecContext(h.ctx, h.sqle, query, &h.log) } +// Query executes a query +func (h *SQLHelper) Query(query sq.Sqlizer) (*sqlx.Rows, error) { + return QueryContext(h.ctx, h.sqle, query, &h.log) +} + // Insert inserts a Mapped into the db func (h *SQLHelper) Insert(instances ...Mapped) (sql.Result, error) { query := SQLInsert(instances...) diff --git a/scripts/generate_db_helpers.go b/scripts/generate_db_helpers.go --- a/scripts/generate_db_helpers.go +++ b/scripts/generate_db_helpers.go @@ -272,13 +272,22 @@ Sql() string } +func NewColumn(table TableSchema, name string) Column { + return Column{table, name, ""} +} + type Column struct { table TableSchema name string + alias string } func (c Column) Sql() string { - return c.table.GetName()+"."+c.name + sql := c.table.GetName()+"."+c.name + if c.alias != "" { + sql += " AS " + c.alias + } + return sql } func (c Column) ToSql() (string, []interface{}, error ) { @@ -296,6 +305,50 @@ return squirrel.Eq{c.Sql(): value} } +func (c Column) Ne(value interface{}) squirrel.NotEq{ + return squirrel.NotEq{c.Sql(): value} +} + +func (c Column) Gt(value interface{}) squirrel.Gt{ + return squirrel.Gt{c.Sql(): value} +} + +func (c Column) Gte(value interface{}) squirrel.GtOrEq{ + return squirrel.GtOrEq{c.Sql(): value} +} + +func (c Column) Lt(value interface{}) squirrel.Lt{ + return squirrel.Lt{c.Sql(): value} +} + +func (c Column) Lte(value interface{}) squirrel.LtOrEq{ + return squirrel.LtOrEq{c.Sql(): value} +} + +func (c Column) Like(value interface{}) squirrel.Like{ + return squirrel.Like{c.Sql(): value} +} + +func (c Column) NotLike(value interface{}) squirrel.NotLike{ + return squirrel.NotLike{c.Sql(): value} +} + +func (c Column) As(name string) Column { + c.alias = name + return c +} + +func (c Column) Name() string { + if c.alias != "" { + return c.alias + } + return c.table.GetName() + "." + c.name +} + +func (c Column) ColName() string { + return c.name +} + type Join struct { from Column to Column @@ -425,7 +478,7 @@ func New{{.Name}}TableSchema() *{{.Name}}TableSchema { t := {{.Name}}TableSchema{} {{- range .Fields}} - t.{{.Name}} = Column{&t, "{{.Column}}"} + t.{{.Name}} = NewColumn(&t, "{{.Column}}") {{- end}} return &t } @@ -449,7 +502,7 @@ func (t {{.Name}}TableSchema) As(name string) *{{.Name}}TableSchema { t.alias = name {{- range .Fields}} - t.{{.Name}} = Column{&t, "{{.Column}}"} + t.{{.Name}} = NewColumn(&t, "{{.Column}}") {{- end}} return &t }