{"id":13752244,"url":"https://github.com/elgs/gosqlcrud","last_synced_at":"2026-03-09T14:06:51.615Z","repository":{"id":149994812,"uuid":"621991904","full_name":"elgs/gosqlcrud","owner":"elgs","description":"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.","archived":false,"fork":false,"pushed_at":"2024-12-05T07:24:52.000Z","size":67,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-17T11:47:44.935Z","etag":null,"topics":["database","golang","orm","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elgs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-31T20:54:22.000Z","updated_at":"2024-12-05T07:24:56.000Z","dependencies_parsed_at":"2024-11-16T04:42:42.644Z","dependency_job_id":null,"html_url":"https://github.com/elgs/gosqlcrud","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elgs%2Fgosqlcrud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elgs%2Fgosqlcrud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elgs%2Fgosqlcrud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elgs%2Fgosqlcrud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elgs","download_url":"https://codeload.github.com/elgs/gosqlcrud/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253303307,"owners_count":21886919,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","golang","orm","sql"],"created_at":"2024-08-03T09:01:02.326Z","updated_at":"2026-03-09T14:06:51.067Z","avatar_url":"https://github.com/elgs.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# gosqlcrud\n\nA Go library to work with SQL database using standard `database/sql` api. It supports SQL to array/maps/structs, and CRUD operations on structs.\n\n# Installation\n\n`go get -u github.com/elgs/gosqlcrud`\n\n## Example\n\nPlease 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.\n\n```go\npackage gosqlcrud\n\nimport (\n\t\"database/sql\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t_ \"modernc.org/sqlite\"\n)\n\ntype Test struct {\n\tId   int     `db:\"ID\" pk:\"true\"`\n\tName *string `db:\"NAME\"`\n\t// use pointer type so when Name is nil, it won't be included in the INSERT or UPDATE statements\n}\n\nfunc TestQueries(t *testing.T) {\n\tdb, err := sql.Open(\"sqlite\", \":memory:\")\n\tassert.NoError(t, err)\n\n\t////////////////\n\t//            //\n\t//    Exec    //\n\t//            //\n\t////////////////\n\n\tresult, err := Exec(db, \"CREATE TABLE test (ID INTEGER PRIMARY KEY, NAME TEXT)\") // Exec\n\tassert.NoError(t, err)\n\tassert.Equal(t, int64(0), result.LastInsertId)\n\tassert.Equal(t, int64(0), result.RowsAffected)\n\n\ttx, err := db.Begin()\n\tassert.NoError(t, err)\n\tresult, err = Exec(tx, \"INSERT INTO test (ID, NAME) VALUES (?, ?)\", 1, \"Alpha\") // Exec\n\tassert.NoError(t, err)\n\tassert.Equal(t, int64(1), result.LastInsertId)\n\tassert.Equal(t, int64(1), result.RowsAffected)\n\n\tresult, err = Exec(tx, \"INSERT INTO test (ID, NAME) VALUES (?, ?)\", 2, \"Beta\") // Exec\n\tassert.NoError(t, err)\n\tassert.Equal(t, int64(2), result.LastInsertId)\n\tassert.Equal(t, int64(1), result.RowsAffected)\n\n\tresult, err = Exec(tx, \"INSERT INTO test (ID, NAME) VALUES (?, ?)\", 3, \"Gamma\") // Exec\n\tassert.NoError(t, err)\n\tassert.Equal(t, int64(3), result.LastInsertId)\n\tassert.Equal(t, int64(1), result.RowsAffected)\n\ttx.Commit()\n\n\t/////////////////////////\n\t//                     //\n\t//    QueryToArrays    //\n\t//                     //\n\t/////////////////////////\n\n\tcols, resultArray, err := QueryToArrays(db, \"SELECT * FROM test WHERE ID \u003e ?\", 1) // QueryToArrays\n\tassert.NoError(t, err)\n\tassert.Equal(t, \"id\", cols[0])\n\tassert.Equal(t, \"name\", cols[1])\n\tassert.Equal(t, int64(2), resultArray[0][0])\n\tassert.Equal(t, \"Beta\", resultArray[0][1])\n\tassert.Equal(t, int64(3), resultArray[1][0])\n\tassert.Equal(t, \"Gamma\", resultArray[1][1])\n\n\t///////////////////////\n\t//                   //\n\t//    QueryToMaps    //\n\t//                   //\n\t///////////////////////\n\n\tresultMaps, err := QueryToMaps(db, \"SELECT * FROM test WHERE ID \u003c ?\", 3) // QueryToMaps\n\tassert.NoError(t, err)\n\tassert.Equal(t, int64(1), resultMaps[0][\"id\"])\n\tassert.Equal(t, \"Alpha\", resultMaps[0][\"name\"])\n\tassert.Equal(t, int64(2), resultMaps[1][\"id\"])\n\tassert.Equal(t, \"Beta\", resultMaps[1][\"name\"])\n\n\t//////////////////////////\n\t//                      //\n\t//    QueryToStructs    //\n\t//                      //\n\t//////////////////////////\n\n\tresultStructs := []Test{}\n\terr = QueryToStructs(db, \u0026resultStructs, \"SELECT NAME,ID FROM test WHERE ID \u003e ?\", 0) // QueryToStructs\n\tassert.NoError(t, err)\n\tassert.Equal(t, \"Alpha\", *resultStructs[0].Name)\n\tassert.Equal(t, 1, resultStructs[0].Id)\n\tassert.Equal(t, \"Beta\", *resultStructs[1].Name)\n\tassert.Equal(t, 2, resultStructs[1].Id)\n\tassert.Equal(t, \"Gamma\", *resultStructs[2].Name)\n\tassert.Equal(t, 3, resultStructs[2].Id)\n\n\t////////////////////\n\t//                //\n\t//    Retrieve    //\n\t//                //\n\t////////////////////\n\n\tresultStruct := Test{Id: 1}\n\terr = Retrieve(db, \u0026resultStruct, \"test\") // Retrieve\n\tassert.NoError(t, err)\n\tassert.Equal(t, \"Alpha\", *resultStruct.Name)\n\tassert.Equal(t, 1, resultStruct.Id)\n\tresultStruct.Id = 2\n\terr = Retrieve(db, \u0026resultStruct, \"test\") // Retrieve\n\tassert.NoError(t, err)\n\tassert.Equal(t, \"Beta\", *resultStruct.Name)\n\tassert.Equal(t, 2, resultStruct.Id)\n\tresultStruct.Id = 3\n\terr = Retrieve(db, \u0026resultStruct, \"test\") // Retrieve\n\tassert.NoError(t, err)\n\tassert.Equal(t, \"Gamma\", *resultStruct.Name)\n\tassert.Equal(t, 3, resultStruct.Id)\n\tresultStruct.Id = 4\n\terr = Retrieve(db, \u0026resultStruct, \"test\") // Retrieve\n\tassert.Error(t, err)\n\n\t//////////////////\n\t//              //\n\t//    Create    //\n\t//              //\n\t//////////////////\n\n\tname := \"Delta\"\n\tdata := Test{Id: 4, Name: \u0026name}\n\tresult, err = Create(db, \u0026data, \"test\") // Create\n\tassert.NoError(t, err)\n\tassert.Equal(t, int64(4), result.LastInsertId)\n\tassert.Equal(t, int64(1), result.RowsAffected)\n\n\tresultStruct.Id = 4\n\terr = Retrieve(db, \u0026resultStruct, \"test\") // Retrieve\n\tassert.NoError(t, err)\n\tassert.Equal(t, \"Delta\", *resultStruct.Name)\n\tassert.Equal(t, 4, resultStruct.Id)\n\n\t//////////////////\n\t//              //\n\t//    Update    //\n\t//              //\n\t//////////////////\n\n\tname = \"Omega\"\n\t*data.Name = name\n\tresult, err = Update(db, \u0026data, \"test\") // Update\n\tassert.NoError(t, err)\n\tassert.Equal(t, int64(4), result.LastInsertId)\n\tassert.Equal(t, int64(1), result.RowsAffected)\n\n\tresultStruct.Id = 4\n\terr = Retrieve(db, \u0026resultStruct, \"test\") // Retrieve\n\tassert.NoError(t, err)\n\tassert.Equal(t, name, *resultStruct.Name)\n\tassert.Equal(t, 4, resultStruct.Id)\n\n\tdata.Name = nil\n\tresult, err = Update(db, \u0026data, \"test\") // Update\n\tassert.NoError(t, err)\n\tassert.Equal(t, int64(0), result.RowsAffected)\n\n\tresultStruct.Id = 4\n\terr = Retrieve(db, \u0026resultStruct, \"test\") // Retrieve\n\tassert.NoError(t, err)\n\tassert.Equal(t, name, *resultStruct.Name)\n\tassert.Equal(t, 4, resultStruct.Id)\n\n\t//////////////////\n\t//              //\n\t//    Delete    //\n\t//              //\n\t//////////////////\n\n\tresult, err = Delete(db, \u0026data, \"test\") // Delete\n\tassert.NoError(t, err)\n\tassert.Equal(t, int64(4), result.LastInsertId)\n\tassert.Equal(t, int64(1), result.RowsAffected)\n\n\tresultStruct.Id = 4\n\terr = Retrieve(db, \u0026resultStruct, \"test\") // Retrieve\n\tassert.Error(t, err)\n}\n\nfunc TestReflect(t *testing.T) {\n\tname := \"test\"\n\ttest := Test{Id: 1, Name: \u0026name}\n\n\tfields := StructFieldToDbField(\u0026test)\n\tassert.Equal(t, \"ID\", fields[0])\n\tassert.Equal(t, \"NAME\", fields[1])\n\n\tnonPkMap, pkMap := StructToDbMap(\u0026test)\n\tassert.Equal(t, \"test\", *nonPkMap[\"NAME\"].(*string))\n\tassert.Equal(t, 1, pkMap[\"ID\"])\n}\n\nfunc TestSqlSafe(t *testing.T) {\n\tss := []string{\n\t\t\"asdf\",\n\t\t\"asdf'asdf\",\n\t\t\"asdf--asdf\",\n\t}\n\tfor i := range ss {\n\t\tSqlSafe(\u0026ss[i])\n\t}\n\n\tassert.Equal(t, \"asdf\", ss[0])\n\tassert.Equal(t, \"asdf''asdf\", ss[1])\n\tassert.Equal(t, \"asdfasdf\", ss[2])\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felgs%2Fgosqlcrud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felgs%2Fgosqlcrud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felgs%2Fgosqlcrud/lists"}