sqlmapper

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

Github stars Tracking Chart

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
}
    

Main metrics

Overview
Name With Owneraobt/sqlmapper
Primary LanguageGo
Program languageGo (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2018-04-19 09:43:24
Pushed At2020-09-10 06:14:15
Last Commit At2020-09-10 14:13:31
Release Count0
用户参与
Stargazers Count103
Watchers Count5
Fork Count35
Commits Count15
Has Issues Enabled
Issues Count3
Issue Open Count3
Pull Requests Count1
Pull Requests Open Count0
Pull Requests Close Count0
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private