modulepackage
1.0.0
Repository: https://github.com/nam2184/generic-queries.git
Documentation: pkg.go.dev
# README
Go Generic SQLx Queries
This project demonstrates how to use generic functions with sqlx
to perform PostGres database operations with reduced code repetition by leveraging Go generics, mostly for me but idk use it if u want to.
Features
- Generic SQL operation Functions: Dynamically constructs an SQL statement for PostGres from any struct using reflection and
sqlx
.
Setup
-
Clone the repository:
go get github.com/nam2184/generic-queries
-
Add your business logic that specify TableName() and Id() to match QueryTypes interface with db tags for sqlx
-
Initialise database
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
Host, Port, User, Password, Name)
db, err := sqlx.Connect("postgres", psqlInfo)
- You can create a query such as insert with this code for a slice of the table structure
// Define the SQL insert query
tx := db.MustBegin()
defer func() {
if err := tx.Rollback(); err != nil {
t.Fatal(err)
}
}()
queries.InsertQuery[model.Task](tx, nil, tasks)
# Packages
No description provided by the author
# Functions
No description provided by the author
GetConstraintString replaces placeholders with numbered placeholders.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
# Type aliases
No description provided by the author