sqlex

package module
v0.0.0-...-096acc9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2021 License: MIT Imports: 11 Imported by: 0

README

sqlex

Simple additional methods to make using https://github.com/jmoiron/sqlx easier for MySQL and Postgres databases.

Example

This library was written to reduce the amount of code that had to be typed when writing out 50+ database model methods when using auto-incrementing primary keys with PostgreSQL or MySQL.

Example sqlx (mysql) insert code:

func (s *MySQL) InsertUser(ctx context.Context, user *myapp.User) (int64, error) {
    query := `INSERT INTO user(email,password,created_at) values (?,?,now())`
    res, err := s.DB.ExecContext(ctx, sql, user.Email, user.Password)

    if err != nil {
        return 0, fmt.Errorf("insertuser: %w", err)
    }

    id, err := res.LastInsertId()
    if err != nil {
        return 0, fmt.Errorf("insertuser: %w", err)
    }
    
    return id, nil
}

with sqlex we can write this much simpler

func (m *Model) InsertUser(ctx context.Context, user *myapp.User) (int64, error) {
    query := `INSERT INTO user(email,password,created_at) values (?,?,now())`
    return m.DB.InsertContext(ctx, query, user.Email, user.Password)
}

Usage

Simply open a sqlex connection the same as you would an sql or sqlx connection.

db, err := sqlex.Open("mysql", "user:pass@tcp(localhost:3306)/myapp?collation=utf8mb4_unicode_ci&parseTime=true")

In addition to all sqlx methods, you now have access to the following helpers:

InsertContext(ctx context.Context, query string, args ...interface{}) (int64, error)
InsertContextPsql(ctx context.Context, query string, args ...interface{}) (int64, error)
InsertIgnoreContext(ctx context.Context, query string, args ...interface{}) (bool, error)
UpdateContext(ctx context.Context, query string, args ...interface{}) (int64, error)
UpdateOneContext(ctx context.Context, query string, args ...interface{}) error
GetOrCreate(ctx context.Context, dest interface{}, selectQuery, insertQuery string, args ...interface{}) error

Furthermore, two additional query wrappers are also available: ErrorWrapper and Logger.

db, err := sqlex.Open(...)

Wrap errors with store caller name
db = &sqlex.ErrorWrapper{db}

Notes

Don't forget to load the actual driver in your application

package main

import (
    ...
    _ "github.com/go-sql-driver/mysql"

Documentation

Index

Constants

View Source
const ER_DUP_ENTRY = 1062

MySQL error code for duplicate rows

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	*sqlx.DB
}

func Open

func Open(driverName, dataSourceName string) (*DB, error)

Open is the same as sql.Open, but returns an *sqlex.DB instead.

func (*DB) GetOrCreate

func (s *DB) GetOrCreate(ctx context.Context, dest interface{}, selectQuery, insertQuery string, args ...interface{}) error

GetOrCreate record using the first argument as the unique lookup key for the record and the rest for the insert

func (*DB) InsertContext

func (s *DB) InsertContext(ctx context.Context, query string, args ...interface{}) (int64, error)

func (*DB) InsertContextPsql

func (s *DB) InsertContextPsql(ctx context.Context, query string, args ...interface{}) (int64, error)

InsertContextPsql inserts a single record reading RETURNING id

func (*DB) InsertIgnoreContext

func (s *DB) InsertIgnoreContext(ctx context.Context, query string, args ...interface{}) (bool, error)

func (*DB) UpdateContext

func (s *DB) UpdateContext(ctx context.Context, query string, args ...interface{}) (int64, error)

func (*DB) UpdateOneContext

func (s *DB) UpdateOneContext(ctx context.Context, query string, args ...interface{}) error

type ErrorWrapper

type ErrorWrapper struct {
	*DB
}

ErrorWrapper for database queries

func (*ErrorWrapper) ExecContext

func (l *ErrorWrapper) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*ErrorWrapper) GetContext

func (l *ErrorWrapper) GetContext(ctx context.Context, DB interface{}, query string, args ...interface{}) error

func (*ErrorWrapper) InsertContext

func (l *ErrorWrapper) InsertContext(ctx context.Context, query string, args ...interface{}) (int64, error)

func (*ErrorWrapper) InsertIgnoreContext

func (l *ErrorWrapper) InsertIgnoreContext(ctx context.Context, query string, args ...interface{}) (bool, error)

func (*ErrorWrapper) PrepareContext

func (l *ErrorWrapper) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (*ErrorWrapper) QueryContext

func (l *ErrorWrapper) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*ErrorWrapper) SelectContext

func (l *ErrorWrapper) SelectContext(ctx context.Context, DBs interface{}, query string, args ...interface{}) error

func (*ErrorWrapper) UpdateContext

func (l *ErrorWrapper) UpdateContext(ctx context.Context, query string, args ...interface{}) (int64, error)

func (*ErrorWrapper) UpdateOneContext

func (l *ErrorWrapper) UpdateOneContext(ctx context.Context, query string, args ...interface{}) error

type Logger

type Logger struct {
	*DB
	Log *log.Logger
}

Logger for database queries

func (*Logger) ExecContext

func (l *Logger) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext shell

func (*Logger) GetContext

func (l *Logger) GetContext(ctx context.Context, DB interface{}, query string, args ...interface{}) error

func (*Logger) InsertContext

func (l *Logger) InsertContext(ctx context.Context, query string, args ...interface{}) (int64, error)

func (*Logger) InsertIgnoreContext

func (l *Logger) InsertIgnoreContext(ctx context.Context, query string, args ...interface{}) (bool, error)

func (*Logger) PrepareContext

func (l *Logger) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext shell

func (*Logger) QueryContext

func (l *Logger) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext shell

func (*Logger) QueryRowContext

func (l *Logger) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext shell

func (*Logger) SelectContext

func (l *Logger) SelectContext(ctx context.Context, DBs interface{}, query string, args ...interface{}) error

func (*Logger) UpdateContext

func (l *Logger) UpdateContext(ctx context.Context, query string, args ...interface{}) (int64, error)

func (*Logger) UpdateOneContext

func (l *Logger) UpdateOneContext(ctx context.Context, query string, args ...interface{}) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL