🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

github.com/popgogo/sql-query-builder

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/popgogo/sql-query-builder

v1.0.0
Source
Go
Version published
Created
Source

QueryBuilder Package

The QueryBuilder package provides a flexible and intuitive way to construct SQL queries programmatically in Go. It is designed to simplify the process of building complex queries by offering a chainable API for defining query components such as fields, conditions, joins, and Common Table Expressions (CTEs).

Features

  • Dynamic Query Building: Construct SQL queries dynamically with support for SELECT, WHERE, JOIN, and CTEs.
  • Chainable API: Easily chain methods to build queries step by step.
  • Parameterized Queries: Automatically generates parameterized queries to prevent SQL injection.

Usage

Creating a QueryBuilder Instance

To start building a query, create a new QueryBuilder instance by specifying the table name:

qb := querybuilder.NewQueryBuilder("users")

Selecting Fields

Specify the fields to include in the SELECT statement:

qb.Select("id", "name", "email")

Adding Conditions

Add conditions to the WHERE clause using Where and OrWhere methods:

qb.Where("age", ">", 18).OrWhere("status", "=", "active")

Adding Joins

Define relationships between tables using the Join method:

qb.Join("orders", "id", "user_id")

Adding Common Table Expressions (CTEs)

Include CTEs in your query using the AddCTE method:

cte := querybuilder.NewQueryBuilder("recent_orders").Select("id", "user_id").Where("created_at", ">", "2025-01-01")
qb.AddCTE("recent_orders_cte", cte)

Building the Query

Generate the final SQL query and its arguments:

query, args := qb.BuildQuery()
fmt.Println("Query:", query)
fmt.Println("Arguments:", args)

Example

Here is a complete example of building a query:

qb := querybuilder.NewQueryBuilder("users")
qb.Select("users.id", "users.name", "orders.total")
  .Join("orders", "id", "user_id")
  .Where("users.age", ">", 18)
  .OrWhere("users.status", "=", "active")

query, args := qb.BuildQuery()
fmt.Println("Query:", query)
fmt.Println("Arguments:", args)

API Reference

NewQueryBuilder(tableName string) *QueryBuilder

Creates a new QueryBuilder instance for the specified table.

Select(fields ...string) *QueryBuilder

Specifies the fields to include in the SELECT statement.

Where(field, operator string, value interface{}) *QueryBuilder

Adds a condition to the WHERE clause.

OrWhere(field, operator string, value interface{}) *QueryBuilder

Adds an OR condition to the WHERE clause.

Join(table, foreignKey, primaryKey string) *QueryBuilder

Adds a JOIN clause to the query.

AddCTE(name string, queryBuilder *QueryBuilder) *QueryBuilder

Adds a Common Table Expression (CTE) to the query.

BuildQuery() (string, []interface{})

Generates the final SQL query and its arguments.

License

This package is open-source and available under the MIT License.

FAQs

Package last updated on 04 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts