GoQuery is a library designed to assist in creating SQL queries in the Go programming language. This library simplifies database operations by leveraging struct structures.
To use GoQuery, follow these steps:
-
First, include GoQuery in your project:
import "github.com/cetinboran/goquery"
-
Initialize the
GoQuery
object and set the required parameters:// Define the database table name. query := goquery.GoQueryInit("your_table_name") // Set the struct type. yourStruct := YourStructType{} query.SetStruct(yourStruct) // Set query safety and specify the unique column. query.SetChecks([]bool{true, true, true}) // Mark your fields for query safety, for example. query.SetUnique("unique_column_name", uniqueValue)
-
Create SQL queries:
-
Creating an update query:
updateQuery, args := query.CreateUpdate(true) // Use safe query (true)
or
updateQuery, args := query.CreateUpdate(false) // Without using safe query
- Creating an insert query:
insertQuery, args := query.CreateInsert(true) // Use safe query (true)
or
insertQuery, args := query.CreateInsert(false) // Without using safe query
-
detais: If you set the
safe
parameter tofalse
, the argument interface array will indeed be empty, and the values will be directly embedded into the string. In this case, the SQL query will not use parameterized placeholders. -
However, if you set
safe
totrue
, theargs
interface array will be populated, and the SQL query string will contain placeholders, typically represented as?
. This allows you to prepare the query in advance and then later supply the values, increasing security by protecting against SQL injection attacks. -
So, in summary:
safe
set tofalse
: Values are directly embedded into the string, andargs
will be empty.safe
set totrue
: Values are parameterized with placeholders, andargs
will contain the corresponding values, enhancing security by using prepared statements.- Here's an example of how this affects the query generation:
// When safe is false: query := goquery.GoQueryInit("users") // ... Set other parameters insertQuery, args := query.CreateInsert(false) // insertQuery might look like: "INSERT INTO users (column1, column2) VALUES (value1, value2)" // When safe is true: query := goquery.GoQueryInit("users") // ... Set other parameters insertQuery, args := query.CreateInsert(true) // insertQuery might look like: "INSERT INTO users (column1, column2) VALUES (?, ?)" // args will contain the actual values to be used in place of the placeholders
- Using the
safe
parameter withtrue
is a recommended practice for building secure SQL queries to prevent SQL injection.
-
-
Use SQL queries:
// Example of executing the query in your database (this is just an example and may vary depending on the database used). _, err := db.Exec(updateQuery, args...) if err != nil { log.Fatal(err) }
Here is an example of how to create an SQL query using GoQuery:
package main
import (
"fmt"
"log"
"github.com/cetinboran/goquery"
)
type User struct {
ID int `column:"id"`
Username string `column:"username"`
Email string `column:"email"`
}
func main() {
// Initialize the GoQuery object and set the required parameters.
query := goquery.GoQueryInit("users")
user := User{ID: 1, Username: "john_doe", Email: "[email protected]"}
query.SetStruct(user)
query.SetChecks([]bool{true, true, true})
query.SetUnique("id", user.ID)
// Create an update query.
updateQuery, args := query.CreateUpdate(true)
// Display the generated query.
fmt.Println("Update Query:", updateQuery)
fmt.Println("Query Arguments:", args)
// Execute the query in the database (this is just an example and may vary depending on the database used).
_, err := db.Exec(updateQuery, args...)
if err != nil {
log.Fatal(err)
}
}
If you'd like to contribute to this project, please fork it and submit a pull request. Feel free to report any bugs or issues on the GitHub issues page as well.
This project is licensed under the MIT License. For more information, please see the LICENSE file.
This README file provides essential information to get started with the GoQuery library. Don't forget to create documentation and guides specific to your project for more details.