Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elgs/gosqlcrud
A Go library to work with SQL database using standard `database/sql` api. It supports SQL to array/maps/structs, and CRUD operations on structs.
https://github.com/elgs/gosqlcrud
database golang orm sql
Last synced: 3 months ago
JSON representation
A Go library to work with SQL database using standard `database/sql` api. It supports SQL to array/maps/structs, and CRUD operations on structs.
- Host: GitHub
- URL: https://github.com/elgs/gosqlcrud
- Owner: elgs
- License: mit
- Created: 2023-03-31T20:54:22.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-04-05T13:19:41.000Z (7 months ago)
- Last Synced: 2024-06-14T01:30:43.402Z (5 months ago)
- Topics: database, golang, orm, sql
- Language: Go
- Homepage:
- Size: 63.5 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gosqlcrud
A Go library to work with SQL database using standard `database/sql` api. It supports SQL to array/maps/structs, and CRUD operations on structs.
# Installation
`go get -u github.com/elgs/gosqlcrud`
## Example
Please note for `Exec`, `QueryToArrays`, `QueryToMaps`, `QueryToStructs`, you are responsible for preventing SQL injection in the SQL queries. For `Retrieve`, `Create`, `Update`, `Delete`, the library will take care of it.
```go
package gosqlcrudimport (
"database/sql"
"testing""github.com/stretchr/testify/assert"
_ "modernc.org/sqlite"
)type Test struct {
Id int `db:"ID" pk:"true"`
Name string `db:"NAME"`
}func TestQueries(t *testing.T) {
db, err := sql.Open("sqlite", ":memory:")
assert.NoError(t, err)
result, err := Exec(db, "CREATE TABLE test (ID INTEGER PRIMARY KEY, NAME TEXT)") // Exec
assert.NoError(t, err)
assert.Equal(t, int64(0), result["last_insert_id"])
assert.Equal(t, int64(0), result["rows_affected"])tx, err := db.Begin()
assert.NoError(t, err)
result, err = Exec(tx, "INSERT INTO test (ID, NAME) VALUES (?, ?)", 1, "Alpha") // Exec
assert.NoError(t, err)
assert.Equal(t, int64(1), result["last_insert_id"])
assert.Equal(t, int64(1), result["rows_affected"])result, err = Exec(tx, "INSERT INTO test (ID, NAME) VALUES (?, ?)", 2, "Beta") // Exec
assert.NoError(t, err)
assert.Equal(t, int64(2), result["last_insert_id"])
assert.Equal(t, int64(1), result["rows_affected"])result, err = Exec(tx, "INSERT INTO test (ID, NAME) VALUES (?, ?)", 3, "Gamma") // Exec
assert.NoError(t, err)
assert.Equal(t, int64(3), result["last_insert_id"])
assert.Equal(t, int64(1), result["rows_affected"])
tx.Commit()cols, resultArray, err := QueryToArrays(db, "SELECT * FROM test WHERE ID > ?", 1) // QueryToArrays
assert.NoError(t, err)
assert.Equal(t, "id", cols[0])
assert.Equal(t, "name", cols[1])
assert.Equal(t, int64(2), resultArray[0][0])
assert.Equal(t, "Beta", resultArray[0][1])
assert.Equal(t, int64(3), resultArray[1][0])
assert.Equal(t, "Gamma", resultArray[1][1])resultMaps, err := QueryToMaps(db, "SELECT * FROM test WHERE ID < ?", 3) // QueryToMaps
assert.NoError(t, err)
assert.Equal(t, int64(1), resultMaps[0]["id"])
assert.Equal(t, "Alpha", resultMaps[0]["name"])
assert.Equal(t, int64(2), resultMaps[1]["id"])
assert.Equal(t, "Beta", resultMaps[1]["name"])resultStructs := []Test{}
err = QueryToStructs(db, &resultStructs, "SELECT NAME,ID FROM test WHERE ID > ?", 0) // QueryToStructs
assert.NoError(t, err)
assert.Equal(t, "Alpha", resultStructs[0].Name)
assert.Equal(t, 1, resultStructs[0].Id)
assert.Equal(t, "Beta", resultStructs[1].Name)
assert.Equal(t, 2, resultStructs[1].Id)
assert.Equal(t, "Gamma", resultStructs[2].Name)
assert.Equal(t, 3, resultStructs[2].Id)resultStruct := Test{Id: 1}
err = Retrieve(db, &resultStruct, "test") // Retrieve
assert.NoError(t, err)
assert.Equal(t, "Alpha", resultStruct.Name)
assert.Equal(t, 1, resultStruct.Id)
resultStruct.Id = 2
err = Retrieve(db, &resultStruct, "test") // Retrieve
assert.NoError(t, err)
assert.Equal(t, "Beta", resultStruct.Name)
assert.Equal(t, 2, resultStruct.Id)
resultStruct.Id = 3
err = Retrieve(db, &resultStruct, "test") // Retrieve
assert.NoError(t, err)
assert.Equal(t, "Gamma", resultStruct.Name)
assert.Equal(t, 3, resultStruct.Id)
resultStruct.Id = 4
err = Retrieve(db, &resultStruct, "test") // Retrieve
assert.Error(t, err)data := Test{Id: 4, Name: "Delta"}
result, err = Create(db, &data, "test") // Create
assert.NoError(t, err)
assert.Equal(t, int64(4), result["last_insert_id"])
assert.Equal(t, int64(1), result["rows_affected"])resultStruct.Id = 4
err = Retrieve(db, &resultStruct, "test") // Retrieve
assert.NoError(t, err)
assert.Equal(t, "Delta", resultStruct.Name)
assert.Equal(t, 4, resultStruct.Id)data.Name = "Omega"
result, err = Update(db, &data, "test") // Update
assert.NoError(t, err)
assert.Equal(t, int64(4), result["last_insert_id"])
assert.Equal(t, int64(1), result["rows_affected"])resultStruct.Id = 4
err = Retrieve(db, &resultStruct, "test") // Retrieve
assert.NoError(t, err)
assert.Equal(t, "Omega", resultStruct.Name)
assert.Equal(t, 4, resultStruct.Id)result, err = Delete(db, &data, "test") // Delete
assert.NoError(t, err)
assert.Equal(t, int64(4), result["last_insert_id"])
assert.Equal(t, int64(1), result["rows_affected"])resultStruct.Id = 4
err = Retrieve(db, &resultStruct, "test") // Retrieve
assert.Error(t, err)
}
```