dal

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: GPL-3.0 Imports: 8 Imported by: 0

README

dal

Database abstraction layer for postgres, mysql and sqlite

Documentation

Overview

Package dal is Database Abstration Layer for different DBMS (Postgres, SQL Server etc.) It allow to use Struct and Map to insert update data.

Auther: Santosh Gupta. Company: Mahendra Educational Pvt. Ltd. Date: 2016-06-28.

To generate doc godoc -html mahendras/dal.orig > dal.html

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SliceToStringFloat

func SliceToStringFloat(a []float64, sep string) string

SliceToStringFloat convert slice to float64 to comma separated string

func SliceToStringInt

func SliceToStringInt(a []int, sep string) string

SliceToStringInt convert slice to int to comma separated string

Types

type DBMS

type DBMS string

DBMS is type to facilitate using supported Dbms constants.

const (
	DbmsPostgreSQL DBMS = "postgres"
	DbmsSQLServer  DBMS = "mssql"
	DbmsSQLite     DBMS = "sqlite3"
)

Define DBMS and set driver name.

type DbWrapper

type DbWrapper interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryRowx(query string, args ...interface{}) *sqlx.Row
	Preparex(query string) (*sqlx.Stmt, error)
}

DbWrapper defines interface that provider must support.

type Dbal

type Dbal struct {
	Db interface{}
	// contains filtered or unexported fields
}

Dbal Struct represents Core Database Abstraction Layer.

func NewDbal

func NewDbal(dbms DBMS, connString string, fn func(*sqlx.DB)) (*Dbal, error)

NewDbal creates a new Dbal object and initialize with Db connection.

func WrapDbal

func WrapDbal(db interface{}) *Dbal

WrapDbal Creates new Dbal instance and initialize with given db connection.

func (*Dbal) Close

func (p *Dbal) Close()

Close closes database connection.

func (*Dbal) CountStruct

func (p *Dbal) CountStruct(dest interface{}, condition string, args ...interface{}) (int64, error)

CountStruct query corresponding table of given struct and count number of records for given condition dest: Pointer to struct.

func (*Dbal) Delete

func (p *Dbal) Delete(table, condition string, args ...interface{}) (rowsAffected int64, err error)

Delete removes data from table. condition: WHERE condition to filter rows. Do NOT pass 'WHERE' keyword itseelf in condition. args: parameters for condition

func (*Dbal) DeleteStruct

func (p *Dbal) DeleteStruct(dest interface{}, id interface{}) (rowsAffected int64, err error)

DeleteStruct delete row from corresponding table of given struct for given ID field Dest should be *struct

func (*Dbal) ExecPrepScalar

func (p *Dbal) ExecPrepScalar(id int, args ...interface{}) (interface{}, error)

ExecPrepScalar execute given prepared statement and return scalar value

func (*Dbal) ExecPrepScalarEx

func (p *Dbal) ExecPrepScalarEx(key string, args ...interface{}) (interface{}, error)

ExecPrepScalarEx execute given prepared statement found by key and return scalar value

func (*Dbal) ExecPrepStruct

func (p *Dbal) ExecPrepStruct(id int, dest interface{}, args ...interface{}) error

ExecPrepStruct executes given prepared statement and returns struct

func (*Dbal) ExecPrepStructEx

func (p *Dbal) ExecPrepStructEx(key string, dest interface{}, args ...interface{}) error

ExecPrepStructEx executes given prepared statement found by key and returns struct

func (*Dbal) ExecProc

func (p *Dbal) ExecProc(proc string, args ...interface{}) (*sqlx.Rows, error)

ExecProc executes given Stored procedure, return raw *sqlx.Rows.It is responsibility of caller to close rows.

func (*Dbal) ExecProcMap

func (p *Dbal) ExecProcMap(proc string, args ...interface{}) ([]RowMap, error)

ExecProcMap executes given Stored procedure, returns slice of RowMap.

func (*Dbal) ExecProcScalar

func (p *Dbal) ExecProcScalar(proc string, args ...interface{}) (interface{}, error)

ExecProcScalar executes given Stored procedure, return return first-most value from proc result

func (*Dbal) ExecProcStruct

func (p *Dbal) ExecProcStruct(proc string, dest interface{}, args ...interface{}) error

ExecProcStruct executes given Stored procedure, fills passed slice of struct. dest: Slice of pointers to struct.

func (*Dbal) FirstMap

func (p *Dbal) FirstMap(code string, args ...interface{}) (RowMap, error)

FirstMap executes given query, return Rowmap for first row from result. It will be good to pass sql that only select single row instead of loading thousands of rows and then discarding them.

func (*Dbal) FirstStruct

func (p *Dbal) FirstStruct(dest interface{}, condition string, args ...interface{}) error

FirstStruct query corresponding table of given struct, fill given struct with first row of result. It will be good to pass sql that only select single row instead of loading thousands of rows and then discarding them. dest: Pointer to struct.

func (*Dbal) InsertStruct

func (p *Dbal) InsertStruct(rowStruct interface{}, useTrans bool) error

InsertStruct inserts data into table using Struct.

func (*Dbal) NamedExec added in v0.0.5

func (p *Dbal) NamedExec(query string, args ...interface{}) (rowsAffected int64, err error)

NamedExec execute query and replace named parameters with value from args.

func (*Dbal) NonQuery

func (p *Dbal) NonQuery(sql string) (rowsAffected int64, err error)

NonQuery allow to execute DML statements like modifying/creating a table.

func (*Dbal) Ping

func (p *Dbal) Ping() error

Ping tries to immediately connect to database and report if any connection error.

func (*Dbal) PrepToStr added in v0.0.4

func (p *Dbal) PrepToStr(id int) string

PrepToStr gives given PreparedStatement as string

func (*Dbal) PrepToStrEx added in v0.0.4

func (p *Dbal) PrepToStrEx(key string) string

PrepToStrEx gives given PreparedStatement as string

func (*Dbal) Prepare

func (p *Dbal) Prepare(sql string, id int) (int, error)

Prepare create prepared statement for given query

func (*Dbal) PrepareEx

func (p *Dbal) PrepareEx(sql, key string) error

PrepareEx create prepared statement with given key that can be referenced later, for given query

func (*Dbal) Query

func (p *Dbal) Query(code string, args ...interface{}) (*sqlx.Rows, error)

Query executes given query, return raw *sqlx.Rows.It is responsibility of caller to close rows.

func (*Dbal) QueryMap

func (p *Dbal) QueryMap(code string, args ...interface{}) ([]RowMap, error)

QueryMap executes given query, return map[string]interface.

func (*Dbal) QuerySliceInt

func (p *Dbal) QuerySliceInt(code string, args ...interface{}) ([]int, error)

QuerySliceInt executes given query, return slice []int.

func (*Dbal) QuerySliceInt64

func (p *Dbal) QuerySliceInt64(code string, args ...interface{}) ([]int64, error)

QuerySliceInt64 executes given query, return slice []int64.

func (*Dbal) QuerySliceStr

func (p *Dbal) QuerySliceStr(code string, args ...interface{}) ([]string, error)

QuerySliceStr executes given query, return slice []string.

func (*Dbal) QueryStruct

func (p *Dbal) QueryStruct(dest interface{}, condition string, args ...interface{}) error

QueryStruct query corresponsing table of given struct by Primary Key Dest should be []'struct as it will return single record

func (*Dbal) QueryStructByID

func (p *Dbal) QueryStructByID(dest interface{}, id interface{}) error

QueryStructByID query corresponsing table of given struct by Primary Key Dest should be *struct as it will return single record

func (*Dbal) QueryStructBySQL

func (p *Dbal) QueryStructBySQL(code string, dest interface{}, args ...interface{}) error

QueryStructBySQL executes given query, fills passed slice of struct. dest: Slice of pointers to struct.

func (*Dbal) Scalar

func (p *Dbal) Scalar(code string, args ...interface{}) (interface{}, error)

Scalar executes given query and return value of first column from first row. It will be good to pass sql that only select single column, single row instead of loading thousands of rows and then discarding them.

func (*Dbal) Transaction

func (p *Dbal) Transaction(fn func(*Dbal) error) error

Transaction executes given function inside database Transaction.

func (*Dbal) UpdateStruct

func (p *Dbal) UpdateStruct(rowStruct interface{}, fieldsCSV string, fieldsInclude bool, useTrans bool) (int64, error)

UpdateStruct updates data into table using struct. fieldCSV: list of fields to be excluded / included for update. fieldsInclude: If false fields will be excluded, if true then struct tags 'noupdate' will be ignored and only fields given in fieldsCSV will be updated.

func (*Dbal) UpdateStructMap

func (p *Dbal) UpdateStructMap(structMap interface{}, useTrans bool) (int64, error)

UpdateStructMap updates data into table using map[string]*struct.

type RowMap

type RowMap map[string]interface{}

RowMap is type for map[string]interface for easy accessing row data as Map.

type SQLBuilder

type SQLBuilder struct {
	// contains filtered or unexported fields
}

SQLBuilder allow to dynamically build SQL to query database-tables

func NewSQLBuilder

func NewSQLBuilder() *SQLBuilder

NewSQLBuilder returns new instance of SQLBuilder

func (*SQLBuilder) Build

func (s *SQLBuilder) Build() string

Build finally build return SQL

func (*SQLBuilder) FloatIN

func (s *SQLBuilder) FloatIN(fieldName string, in []float64) string

FloatIN returns IN clause for given field and array (slice) for selected DBMS format

func (*SQLBuilder) From

func (s *SQLBuilder) From(fromclause string) *SQLBuilder

From specifies the FROM clause of sql, it appends FROM keyword itself.

func (*SQLBuilder) GroupBy

func (s *SQLBuilder) GroupBy(groupbyclause string) *SQLBuilder

GroupBy specifies the GROUP BY clause of sql, it appends GROUP BY keyword itself.

func (*SQLBuilder) IntIN

func (s *SQLBuilder) IntIN(fieldName string, in []int) string

IntIN returns IN clause for given field and array (slice) for selected DBMS format

func (*SQLBuilder) Limit

func (s *SQLBuilder) Limit(numRows int) *SQLBuilder

Limit limits number of resultant rows

func (*SQLBuilder) OrderBy

func (s *SQLBuilder) OrderBy(fieldname string, descending bool) *SQLBuilder

OrderBy specifies the ORDER BY clause of sql, it appends ORDER BY keyword itself.

func (*SQLBuilder) Raw

func (s *SQLBuilder) Raw(raw string) *SQLBuilder

Raw specifies the raw sql where only table and alias replacement to be made

func (*SQLBuilder) RawBuild

func (s *SQLBuilder) RawBuild() string

RawBuild finally replace alias with tables and return RawSQL

func (*SQLBuilder) Select

func (s *SQLBuilder) Select(fields string) *SQLBuilder

Select specifies the SELECT, INSERT, UPDATE or DELETE clause

func (*SQLBuilder) StrIN

func (s *SQLBuilder) StrIN(fieldName string, in []string) string

StrIN returns IN clause for given field and array (slice) for selected DBMS format

func (*SQLBuilder) Table

func (s *SQLBuilder) Table(modelstruct interface{}, alias string) *SQLBuilder

Table adds table that is being used in sql, also allow to replace alias withtable name

func (*SQLBuilder) Where

func (s *SQLBuilder) Where(whereclause string) *SQLBuilder

Where specifies the WHERE clause of sql, it appends WHERE keyword itself.

func (*SQLBuilder) WhereFloatIN

func (s *SQLBuilder) WhereFloatIN(fieldName string, in []float64) *SQLBuilder

WhereFloatIN returns IN clause for given field and array (slice) for selected DBMS format

func (*SQLBuilder) WhereIntIN

func (s *SQLBuilder) WhereIntIN(fieldName string, in []int) *SQLBuilder

WhereIntIN returns IN clause for given field and array (slice) for selected DBMS format

func (*SQLBuilder) WhereStrIN

func (s *SQLBuilder) WhereStrIN(fieldName string, in []string) *SQLBuilder

WhereStrIN returns IN clause for given field and array (slice) for selected DBMS format

Jump to

Keyboard shortcuts

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