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 ¶
- func SliceToStringFloat(a []float64, sep string) string
- func SliceToStringInt(a []int, sep string) string
- type DBMS
- type DbWrapper
- type Dbal
- func (p *Dbal) Close()
- func (p *Dbal) CountStruct(dest interface{}, condition string, args ...interface{}) (int64, error)
- func (p *Dbal) Delete(table, condition string, args ...interface{}) (rowsAffected int64, err error)
- func (p *Dbal) DeleteStruct(dest interface{}, id interface{}) (rowsAffected int64, err error)
- func (p *Dbal) ExecPrepScalar(id int, args ...interface{}) (interface{}, error)
- func (p *Dbal) ExecPrepScalarEx(key string, args ...interface{}) (interface{}, error)
- func (p *Dbal) ExecPrepStruct(id int, dest interface{}, args ...interface{}) error
- func (p *Dbal) ExecPrepStructEx(key string, dest interface{}, args ...interface{}) error
- func (p *Dbal) ExecProc(proc string, args ...interface{}) (*sqlx.Rows, error)
- func (p *Dbal) ExecProcMap(proc string, args ...interface{}) ([]RowMap, error)
- func (p *Dbal) ExecProcScalar(proc string, args ...interface{}) (interface{}, error)
- func (p *Dbal) ExecProcStruct(proc string, dest interface{}, args ...interface{}) error
- func (p *Dbal) FirstMap(code string, args ...interface{}) (RowMap, error)
- func (p *Dbal) FirstStruct(dest interface{}, condition string, args ...interface{}) error
- func (p *Dbal) InsertStruct(rowStruct interface{}, useTrans bool) error
- func (p *Dbal) NamedExec(query string, args ...interface{}) (rowsAffected int64, err error)
- func (p *Dbal) NonQuery(sql string) (rowsAffected int64, err error)
- func (p *Dbal) Ping() error
- func (p *Dbal) PrepToStr(id int) string
- func (p *Dbal) PrepToStrEx(key string) string
- func (p *Dbal) Prepare(sql string, id int) (int, error)
- func (p *Dbal) PrepareEx(sql, key string) error
- func (p *Dbal) Query(code string, args ...interface{}) (*sqlx.Rows, error)
- func (p *Dbal) QueryMap(code string, args ...interface{}) ([]RowMap, error)
- func (p *Dbal) QuerySliceInt(code string, args ...interface{}) ([]int, error)
- func (p *Dbal) QuerySliceInt64(code string, args ...interface{}) ([]int64, error)
- func (p *Dbal) QuerySliceStr(code string, args ...interface{}) ([]string, error)
- func (p *Dbal) QueryStruct(dest interface{}, condition string, args ...interface{}) error
- func (p *Dbal) QueryStructByID(dest interface{}, id interface{}) error
- func (p *Dbal) QueryStructBySQL(code string, dest interface{}, args ...interface{}) error
- func (p *Dbal) Scalar(code string, args ...interface{}) (interface{}, error)
- func (p *Dbal) Transaction(fn func(*Dbal) error) error
- func (p *Dbal) UpdateStruct(rowStruct interface{}, fieldsCSV string, fieldsInclude bool, useTrans bool) (int64, error)
- func (p *Dbal) UpdateStructMap(structMap interface{}, useTrans bool) (int64, error)
- type RowMap
- type SQLBuilder
- func (s *SQLBuilder) Build() string
- func (s *SQLBuilder) FloatIN(fieldName string, in []float64) string
- func (s *SQLBuilder) From(fromclause string) *SQLBuilder
- func (s *SQLBuilder) GroupBy(groupbyclause string) *SQLBuilder
- func (s *SQLBuilder) IntIN(fieldName string, in []int) string
- func (s *SQLBuilder) Limit(numRows int) *SQLBuilder
- func (s *SQLBuilder) OrderBy(fieldname string, descending bool) *SQLBuilder
- func (s *SQLBuilder) Raw(raw string) *SQLBuilder
- func (s *SQLBuilder) RawBuild() string
- func (s *SQLBuilder) Select(fields string) *SQLBuilder
- func (s *SQLBuilder) StrIN(fieldName string, in []string) string
- func (s *SQLBuilder) Table(modelstruct interface{}, alias string) *SQLBuilder
- func (s *SQLBuilder) Where(whereclause string) *SQLBuilder
- func (s *SQLBuilder) WhereFloatIN(fieldName string, in []float64) *SQLBuilder
- func (s *SQLBuilder) WhereIntIN(fieldName string, in []int) *SQLBuilder
- func (s *SQLBuilder) WhereStrIN(fieldName string, in []string) *SQLBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SliceToStringFloat ¶
SliceToStringFloat convert slice to float64 to comma separated string
func SliceToStringInt ¶
SliceToStringInt convert slice to int to comma separated string
Types ¶
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 WrapDbal ¶
func WrapDbal(db interface{}) *Dbal
WrapDbal Creates new Dbal instance and initialize with given db connection.
func (*Dbal) CountStruct ¶
CountStruct query corresponding table of given struct and count number of records for given condition dest: Pointer to struct.
func (*Dbal) Delete ¶
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 ¶
DeleteStruct delete row from corresponding table of given struct for given ID field Dest should be *struct
func (*Dbal) ExecPrepScalar ¶
ExecPrepScalar execute given prepared statement and return scalar value
func (*Dbal) ExecPrepScalarEx ¶
ExecPrepScalarEx execute given prepared statement found by key and return scalar value
func (*Dbal) ExecPrepStruct ¶
ExecPrepStruct executes given prepared statement and returns struct
func (*Dbal) ExecPrepStructEx ¶
ExecPrepStructEx executes given prepared statement found by key and returns struct
func (*Dbal) ExecProc ¶
ExecProc executes given Stored procedure, return raw *sqlx.Rows.It is responsibility of caller to close rows.
func (*Dbal) ExecProcMap ¶
ExecProcMap executes given Stored procedure, returns slice of RowMap.
func (*Dbal) ExecProcScalar ¶
ExecProcScalar executes given Stored procedure, return return first-most value from proc result
func (*Dbal) ExecProcStruct ¶
ExecProcStruct executes given Stored procedure, fills passed slice of struct. dest: Slice of pointers to struct.
func (*Dbal) FirstMap ¶
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 ¶
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 ¶
InsertStruct inserts data into table using Struct.
func (*Dbal) NamedExec ¶ added in v0.0.5
NamedExec execute query and replace named parameters with value from args.
func (*Dbal) Ping ¶
Ping tries to immediately connect to database and report if any connection error.
func (*Dbal) PrepToStrEx ¶ added in v0.0.4
PrepToStrEx gives given PreparedStatement as string
func (*Dbal) PrepareEx ¶
PrepareEx create prepared statement with given key that can be referenced later, for given query
func (*Dbal) Query ¶
Query executes given query, return raw *sqlx.Rows.It is responsibility of caller to close rows.
func (*Dbal) QuerySliceInt ¶
QuerySliceInt executes given query, return slice []int.
func (*Dbal) QuerySliceInt64 ¶
QuerySliceInt64 executes given query, return slice []int64.
func (*Dbal) QuerySliceStr ¶
QuerySliceStr executes given query, return slice []string.
func (*Dbal) QueryStruct ¶
QueryStruct query corresponsing table of given struct by Primary Key Dest should be []'struct as it will return single record
func (*Dbal) QueryStructByID ¶
QueryStructByID query corresponsing table of given struct by Primary Key Dest should be *struct as it will return single record
func (*Dbal) QueryStructBySQL ¶
QueryStructBySQL executes given query, fills passed slice of struct. dest: Slice of pointers to struct.
func (*Dbal) Scalar ¶
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 ¶
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.
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) 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