sqlmapper

package module
v0.0.0-...-bc04774 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2020 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sqlmapper is a light mapper between golang struct and table rows in db

example

We need to read/write a table in db, like:

CREATE TABLE `test_table` (
  `field_key` varchar(64) NOT NULL DEFAULT '',
  `field_one` varchar(64) DEFAULT NULL,
  `field_two` tinyint(1) DEFAULT NULL,
  `field_thr` int(12) DEFAULT NULL,
  `field_fou` float DEFAULT NULL,
  PRIMARY KEY (`field_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In golang, we create a struct corresponding to the table, like:

// struct in go such as:
type DemoRow struct {
	FieldKey string  `sql:"field_key"`
	FieldOne string  `sql:"field_one"`
	FieldTwo bool    `sql:"field_two"`
	FieldThr int64   `sql:"field_thr"`
	FieldFou float64 `sql:"field_fou"`
}

Then, we can execute SELECT/INSERT/UPDATE/DELETE without long Hard-Code sql string which is easy to make mistakes.

sample (follow fields_map_test.go for more):


// select single row
// Query by primary key (field[0])
func QueryByKey(ctx context.Context, tx *sql.Tx, db *sql.DB, fieldKey string) (
	*DemoRow, error) {

	var row DemoRow
	row.FieldKey = fieldKey
	fm, err := NewFieldsMap(table, &row)
	if err != nil {
		return nil, err
	}

	objptr, err := fm.SQLSelectByPriKey(ctx, tx, db)
	if err != nil {
		return nil, err
	}

	return objptr.(*DemoRow), nil
}
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Name       string
	Tag        string
	Type       string
	Addr       interface{}
	IntSave    sql.NullInt64
	StringSave sql.NullString
	FloatSave  sql.NullFloat64
	BoolSave   sql.NullBool
}

Field db field describe struct mapping in DB like:

type DemoRow struct {
	FieldKey string  `sql:"field_key"`
	FieldOne string  `sql:"field_one"`
	FieldTwo bool    `sql:"field_two"`
	FieldThr int64   `sql:"field_thr"`
	FieldFou float64 `sql:"field_fou"`
}

type FieldsMap

type FieldsMap interface {

	// GetFields Fields
	GetFields() []Field

	// GetFieldNamesInDB get Names in db from Fields
	GetFieldNamesInDB() []string

	// GetFieldValues get Values in Object(struct)
	GetFieldValues() []interface{}

	// GetFieldValue get Value in Object(struct)
	GetFieldValue(idx int) interface{}

	// GetFieldSaveAddrs get Pointers of Values in Object(struct)
	GetFieldSaveAddrs() []interface{}

	// GetFieldSaveAddr get Pointer of Value in Object(struct)
	GetFieldSaveAddr(idx int) interface{}

	// MapBackToObject mapping back to the original object
	MapBackToObject() interface{}

	////////////////////////////////////////////////////////////////
	// generate SQL string
	// SQLFieldsStr generate sqlstr in db from Fields
	SQLFieldsStr() string

	// SQLFieldsStrForSet generate sqlstr in db from Fields for set
	SQLFieldsStrForSet() string

	////////////////////////////////////////////////////////////////
	// generate statement
	// PrepareStmt prepare statement
	// Must Close after Stmt used
	PrepareStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
		sqlstr string) (*sql.Stmt, error)

	// SQLSelectStmt generate statement for SELECT
	SQLSelectStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
		extStr string) (*sql.Stmt, error)

	// SQLInsertStmt generate statement for INSERT
	SQLInsertStmt(ctx context.Context, tx *sql.Tx, db *sql.DB) (*sql.Stmt, error)

	// SQLUpdateStmt generate statement for UPDATE
	SQLUpdateStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
		extStr string) (*sql.Stmt, error)

	// SQLDeleteStmt generate statement for DELETE
	SQLDeleteStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
		extStr string) (*sql.Stmt, error)

	////////////////////////////////////////////////////////////////
	// exec sql
	// SQLLockByPriKey by primary key (field[0])
	SQLLockByPriKey(ctx context.Context, tx *sql.Tx,
		db *sql.DB) (interface{}, error)

	// SQLSelectByPriKey by primary key (field[0])
	SQLSelectByPriKey(ctx context.Context, tx *sql.Tx,
		db *sql.DB) (interface{}, error)

	// SQLSelectRowsByFieldNameInDB by field name in DB
	SQLSelectRowsByFieldNameInDB(ctx context.Context, tx *sql.Tx,
		db *sql.DB, nameInDB string) ([]interface{}, error)

	// SQLSelectAllRows
	SQLSelectAllRows(ctx context.Context, tx *sql.Tx,
		db *sql.DB) ([]interface{}, error)

	// SQLInsert
	SQLInsert(ctx context.Context, tx *sql.Tx, db *sql.DB) error

	// SQLUpdateByPriKey by primary key (field[0])
	SQLUpdateByPriKey(ctx context.Context, tx *sql.Tx, db *sql.DB) error

	// SQLDeleteByPriKey by primary key (field[0])
	SQLDeleteByPriKey(ctx context.Context, tx *sql.Tx, db *sql.DB) error
}

FieldsMap hold Field

func NewFieldsMap

func NewFieldsMap(table string, objptr interface{}) (FieldsMap, error)

NewFieldsMap new Fields

Jump to

Keyboard shortcuts

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