QueryBuilder Module

The QueryBuilder provides a fluent interface for constructing SQL queries safely.

class wsqlite.builders.query_builder.QueryBuilder(table_name: str)[source]

Bases: object

Builder for constructing SQL queries safely.

__init__(table_name: str)[source]

Initialize query builder.

where(field: str, operator: str, value: Any) QueryBuilder[source]

Add WHERE condition.

order_by(field: str, descending: bool = False) QueryBuilder[source]

Add ORDER BY clause.

limit(limit: int) QueryBuilder[source]

Add LIMIT clause.

offset(offset: int) QueryBuilder[source]

Add OFFSET clause.

build_select() tuple[str, tuple][source]

Build SELECT query.

build_count() tuple[str, tuple][source]

Build COUNT query.

build_delete() tuple[str, tuple][source]

Build DELETE query.

reset() QueryBuilder[source]

Reset the builder to initial state.

Methods

Method

Description

where()

Add WHERE condition

order_by()

Add ORDER BY clause

limit()

Add LIMIT clause

offset()

Add OFFSET clause

build_select()

Build SELECT query

build_count()

Build COUNT query

build_delete()

Build DELETE query

reset()

Reset builder state

Operators

The following operators are supported in the where() method:

  • = - Equals

  • < - Less than

  • > - Greater than

  • <= - Less than or equal

  • >= - Greater than or equal

  • != - Not equals

  • LIKE - Pattern matching

  • IN - Value in list

  • IS NULL - Null check

  • IS NOT NULL - Non-null check

Example Usage

from wsqlite import QueryBuilder

# Build SELECT query
qb = (
    QueryBuilder("users")
    .where("age", ">", 18)
    .where("city", "=", "NYC")
    .order_by("name", descending=True)
    .limit(10)
    .offset(20)
)

query, values = qb.build_select()
print(f"Query: {query}")
print(f"Values: {values}")

# Output:
# Query: SELECT * FROM users WHERE age > ? AND city = ? ORDER BY name DESC LIMIT ? OFFSET ?
# Values: (18, 'NYC', 10, 20)

# Build DELETE query
qb = QueryBuilder("users").where("id", "=", 1)
query, values = qb.build_delete()
print(f"DELETE Query: {query}")