sqlmapper

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

Github星跟蹤圖

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
}
    

主要指標

概覽
名稱與所有者aobt/sqlmapper
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2018-04-19 09:43:24
推送於2020-09-10 06:14:15
最后一次提交2020-09-10 14:13:31
發布數0
用户参与
星數103
關注者數5
派生數35
提交數15
已啟用問題?
問題數3
打開的問題數3
拉請求數1
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?