stardb/builder.go

94 lines
1.9 KiB
Go
Raw Normal View History

2026-03-07 19:27:44 +08:00
package stardb
import (
"fmt"
"strings"
)
// QueryBuilder helps build SQL queries
type QueryBuilder struct {
table string
columns []string
where []string
whereArgs []interface{}
orderBy string
limit int
offset int
}
// NewQueryBuilder creates a new query builder
func NewQueryBuilder(table string) *QueryBuilder {
return &QueryBuilder{
table: table,
columns: []string{"*"},
}
}
// Select sets the columns to select
func (qb *QueryBuilder) Select(columns ...string) *QueryBuilder {
qb.columns = columns
return qb
}
// Where adds a WHERE condition
func (qb *QueryBuilder) Where(condition string, args ...interface{}) *QueryBuilder {
qb.where = append(qb.where, condition)
qb.whereArgs = append(qb.whereArgs, args...)
return qb
}
// OrderBy sets the ORDER BY clause
func (qb *QueryBuilder) OrderBy(orderBy string) *QueryBuilder {
qb.orderBy = orderBy
return qb
}
// Limit sets the LIMIT
func (qb *QueryBuilder) Limit(limit int) *QueryBuilder {
qb.limit = limit
return qb
}
// Offset sets the OFFSET
func (qb *QueryBuilder) Offset(offset int) *QueryBuilder {
qb.offset = offset
return qb
}
// Build builds the SQL query and returns query string and args
func (qb *QueryBuilder) Build() (string, []interface{}) {
var parts []string
// SELECT
parts = append(parts, fmt.Sprintf("SELECT %s FROM %s",
strings.Join(qb.columns, ", "), qb.table))
// WHERE
if len(qb.where) > 0 {
parts = append(parts, "WHERE "+strings.Join(qb.where, " AND "))
}
// ORDER BY
if qb.orderBy != "" {
parts = append(parts, "ORDER BY "+qb.orderBy)
}
// LIMIT
if qb.limit > 0 {
parts = append(parts, fmt.Sprintf("LIMIT %d", qb.limit))
}
// OFFSET
if qb.offset > 0 {
parts = append(parts, fmt.Sprintf("OFFSET %d", qb.offset))
}
return strings.Join(parts, " "), qb.whereArgs
}
// Query executes the query
func (qb *QueryBuilder) Query(db *StarDB) (*StarRows, error) {
query, args := qb.Build()
return db.Query(query, args...)
}