Coder Social home page Coder Social logo

goquery's Introduction

GoQuery - Dynamic Query Builder in Golang

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.

Usage

To use GoQuery, follow these steps:

  1. First, include GoQuery in your project:

    import "github.com/cetinboran/goquery"
  2. 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)
  3. 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 to false, 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 to true, the args 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 to false: Values are directly embedded into the string, and args will be empty.
      • safe set to true: Values are parameterized with placeholders, and args 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 with true is a recommended practice for building secure SQL queries to prevent SQL injection.
  4. 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)
    }

Example Usage

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)
	}
}

Contribution

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.

License

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.

goquery's People

Contributors

cetinboran avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.