From 7b08981322d0e25069d3e523e968f7535aa5c040 Mon Sep 17 00:00:00 2001 From: Muhammad Zakir Ramadhan <61570975+zakirkun@users.noreply.github.com> Date: Mon, 23 Dec 2024 13:17:31 +0700 Subject: [PATCH] update readme --- README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d6e3744..63eceea 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,12 @@ Toki is a fast and efficient SQL query builder for Go that helps you build SQL s - 🚀 High-performance query building - 🔒 Automatic placeholder conversion (? to $1, $2, ...) - 💾 Memory pooling for better performance -- 📦 Transaction support -- 🎯 Dynamic query construction -- 🛡️ SQL injection prevention +- 📦 Transaction support with automatic rollback +- 🎯 Fluent interface for query construction +- 🛡️ Secure parameter binding and SQL injection prevention - 📊 Structure binding support - ⚡ Memory efficient operations -- 🔍 Expression support +- 🔍 Expression and Raw query support ## Installation @@ -73,6 +73,7 @@ func main() { ### Query Building +### SELECT Queries ```go // SELECT query builder. @@ -81,12 +82,16 @@ builder. Where("age > ?", 18). AndWhere("status = ?", "active"). OrderBy("created_at DESC") - +``` +### INSERT Queries +```go // INSERT query builder. Insert("users", "name", "email"). Values("John Doe", "john@example.com") - +``` +### Update Queries +```go // UPDATE query builder. Update("users"). @@ -95,12 +100,23 @@ builder. "updated_at": "NOW()", }). Where("id = ?", 1) - +``` +### Delete Queries +```go // DELETE query builder. Delete("users"). Where("status = ?", "inactive") ``` +### Raw Queries +```go +builder.Raw(` + SELECT u.*, p.name as profile_name + FROM users u + LEFT JOIN profiles p ON p.user_id = u.id + WHERE u.created_at > $1 +`, time.Now().AddDate(0, -1, 0)) +``` ### Transaction Support @@ -187,6 +203,41 @@ query := builder. // Automatically converts to $1, $2, etc. for PostgreSQL // SELECT * FROM users WHERE age > $1 AND status = $2 ``` +## Best Practices + +1. **Use Transactions for Multiple Operations** + ```go + tx, _ := toki.Begin(db) + defer tx.Rollback() + // ... perform operations + tx.Commit() + ``` + +2. **Always Close Rows** + ```go + rows, err := stmt.Query() + if err != nil { + return err + } + defer rows.Close() + ``` + +3. **Use Parameter Binding** + ```go + // Good + Where("id = ?", id) + + // Bad + Where(fmt.Sprintf("id = %d", id)) + ``` + +4. **Utilize Structure Binding** + ```go + type User struct { + ID int `db:"id"` + Name string `db:"name"` + } + ``` ## License @@ -198,5 +249,4 @@ Contributions are welcome! Please feel free to submit a Pull Request. ## Support -If you have any questions or need help, please open an issue in the GitHub repository. - +If you have any questions or need help, please open an issue in the GitHub repository. \ No newline at end of file