https://github.com/genesisblock3301/gquery_builder
https://github.com/genesisblock3301/gquery_builder
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/genesisblock3301/gquery_builder
- Owner: GenesisBlock3301
- Created: 2025-09-06T04:36:18.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-09-06T09:12:51.000Z (10 months ago)
- Last Synced: 2025-09-06T11:27:19.790Z (10 months ago)
- Language: Jupyter Notebook
- Size: 3.91 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
| Concept | Example | How we handle it in builder |
| ------------- | ------------------------------- | --------------------------------------------------------------------------------------------------------- |
| **QueryType** | SELECT, INSERT, UPDATE, DELETE | Enum (`QueryType`) because it determines the **type of query** |
| **Clause** | FROM, WHERE, GROUP BY, ORDER BY | Usually **hardcoded in Build()** or handled by methods, because they are **parts of a query**, not a type |
Done:
- Select
- InsertInto
- Update
- Delete
- From
- Where
- DropTable
- DropDatabase
# Go SQL Builder
A simple **SQL query builder in Go** inspired by Python’s `pypika` library.
This project demonstrates **builder pattern**, **interfaces**, and **polymorphism** in Go for constructing SQL queries dynamically.
---
## Features
- **Fluent Builder Pattern**
- Chain methods to build queries iteratively.
- Example: `Query{}.From("customers").Select("*").Where("id = 1")`.
- **Supports Multiple SQL Types**
- `SELECT`
- `INSERT`
- `UPDATE`
- `DELETE`
- `CREATE TABLE`
- `DROP TABLE` / `DROP DATABASE`
- **Interfaces for Polymorphism**
- All query builders implement a `Builder` interface with `Build()` method.
- Enables functions to accept **any builder** type (`SELECT`, `CREATE`, `DROP`) seamlessly.
- **Extensible**
- Easy to add new builders like `CreateIndexBuilder`, `DropUserBuilder`.
- Any new builder only needs to implement `Build()` to integrate with existing functions.
- **Chainable Query Building**
- Methods return the builder itself (`*QueryBuilder`) to allow fluent chaining.
- Example:
```go
q := Query{}
sql := q.From("customers").Select("*").Where("id = 1").Build()
```
- **Flexible Print / Execution**
- `PrintSQL(b Builder)` can print **any query** implementing the `Builder` interface.
- Example:
```go
var b Builder = q.CreateTable("users")
PrintSQL(b)
```
---
## Example Usage
```go
func main() {
q := Query{}
// SELECT
var b Builder = q.From("customers").Select("*").Where("id = 1")
PrintSQL(b) // Output: SELECT * FROM customers WHERE id = 1
// CREATE TABLE
b = q.CreateTable("users")
PrintSQL(b) // Output: CREATE TABLE users (...)
// DROP DATABASE
b = q.DropDatabase("testdb")
PrintSQL(b) // Output: DROP DATABASE testdb
}