Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aobt/sqlmapper
sqlmapper is a light mapper between go-struct and table-rows in db
https://github.com/aobt/sqlmapper
db go golang mapper mysql sql struct table
Last synced: about 2 months ago
JSON representation
sqlmapper is a light mapper between go-struct and table-rows in db
- Host: GitHub
- URL: https://github.com/aobt/sqlmapper
- Owner: aobt
- License: mit
- Created: 2018-04-19T09:43:24.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-10T06:14:15.000Z (over 4 years ago)
- Last Synced: 2024-10-16T16:18:56.724Z (2 months ago)
- Topics: db, go, golang, mapper, mysql, sql, struct, table
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 103
- Watchers: 5
- Forks: 48
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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:
```sql
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:
```go
// 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](https://github.com/arthas29/sqlmapper/blob/master/fields_map_test.go) for more):
```go// 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
}
```