{"id":13412113,"url":"https://github.com/hexdigest/prep","last_synced_at":"2025-03-14T17:31:39.975Z","repository":{"id":57496815,"uuid":"113921082","full_name":"hexdigest/prep","owner":"hexdigest","description":"Prep finds all SQL statements in a Go package and instruments db connection with prepared statements","archived":false,"fork":false,"pushed_at":"2017-12-19T17:35:51.000Z","size":33,"stargazers_count":32,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-07-31T20:49:51.371Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hexdigest.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}},"created_at":"2017-12-11T23:47:38.000Z","updated_at":"2024-03-07T22:59:58.000Z","dependencies_parsed_at":"2022-08-31T04:41:35.905Z","dependency_job_id":null,"html_url":"https://github.com/hexdigest/prep","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/hexdigest%2Fprep","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexdigest%2Fprep/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexdigest%2Fprep/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexdigest%2Fprep/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hexdigest","download_url":"https://codeload.github.com/hexdigest/prep/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243618772,"owners_count":20320289,"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":[],"created_at":"2024-07-30T20:01:21.126Z","updated_at":"2025-03-14T17:31:34.963Z","avatar_url":"https://github.com/hexdigest.png","language":"Go","readme":"# Prep [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/hexdigest/prep/blob/master/LICENSE) [![Build Status](https://travis-ci.org/hexdigest/prep.svg?branch=master)](https://travis-ci.org/hexdigest/prep) [![Coverage Status](https://coveralls.io/repos/github/hexdigest/prep/badge.svg?branch=master)](https://coveralls.io/github/hexdigest/prep?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/hexdigest/prep)](https://goreportcard.com/report/github.com/hexdigest/prep) [![GoDoc](https://godoc.org/github.com/hexdigest/prep?status.svg)](http://godoc.org/github.com/hexdigest/prep)\nPrep finds all SQL statements in a Go package and instruments db connection with prepared statements.\nIt allows you to benefit from the prepared SQL statements almost without any changes to your code.\n\nPrep consists of two parts:\n* A command line tool that finds all SQL statements in your code\n* A package that instruments your code with prepared SQL statements using the found ones\n\n# Usage\n\n## Generate a list of SQL statements used in your application\n\n$ cat example.go\n```go\nfunc main() {\n\tdb, err := sql.Open(\"mysql\", \"user:pass@tcp(localhost:3306)/mysql\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tconst query = `SELECT CONCAT(\"Hello \", ?, \"!\")`\n\tvar s string\n\tif err := db.QueryRow(query, \"World\").Scan(\u0026s); err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(s)\n}\n```\n\nLet's generate a list of the SQL statements used in your package:\n\n```bash\n$ prep -f github.com/hexdigest/prepdemo\n$ cat prepared_statements.go\n```\n\n```go\n//go:generate prep -f github.com/hexdigest/prepdemo\n\npackage main\n\nvar prepStatements = []string{\n\t\"SELECT CONCAT(\\\"Hello \\\", ?, \\\"!\\\")\",\n}\n```\n\n# Using prepared statements\n\n```go\nfunc main() {\n\tsqlDB, err := sql.Open(\"mysql\", \"root:root@tcp(localhost:3306)/mysql\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tdb, err := prep.NewConnection(sqlDB, prepStatements)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tconst query = `SELECT CONCAT(\"Hello \", ?, \"!\")`\n\tvar s string\n\tif err := db.QueryRow(query, \"World\").Scan(\u0026s); err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(s)\n}\n```\n\nTake a look at the line:\n```go\ndb, err := prep.NewConnection(sqlDB, prepStatements)\n```\n\nIt instruments your connection with prepared statements found by the generator.\nThe generated code already contains //go:generate instruction, so in order to update the statements list you can simply run:\n\n```bash\n$ go generate\n```\n\n# Some synthetic benchmarks\n\n```bash\n$ go test -bench=.\nBenchmarkPostgresWithoutPreparedStatements-4   \t   20000\t     59941 ns/op\t    1183 B/op\t      32 allocs/op\nBenchmarkPostgresWithPreparedStatements-4      \t   50000\t     41560 ns/op\t    1021 B/op\t      26 allocs/op\nBenchmarkMySQLWithoutPreparedStatements-4      \t   50000\t     26454 ns/op\t     827 B/op\t      23 allocs/op\nBenchmarkMySQLWithPreparedStatements-4         \t  200000\t      9509 ns/op\t     634 B/op\t      19 allocs/op\nPASS\nok  \tgithub.com/hexdigest/prep\t7.884s\n```\n","funding_links":[],"categories":["Uncategorized","Database","数据库","数据库  `go语言实现的数据库`","Data Integration Frameworks","Generators"],"sub_categories":["Database Tools","Advanced Console UIs","数据库工具"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhexdigest%2Fprep","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhexdigest%2Fprep","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhexdigest%2Fprep/lists"}