Sqler class

SQL Query Builder Library for MySQL

This library provides a comprehensive set of classes for building SQL queries programmatically using a fluent interface. It supports all major SQL operations including SELECT, INSERT, UPDATE, DELETE, and complex operations like JOINs, subqueries, and conditional expressions.

Key features:

  • Fluent interface with method chaining
  • Type-safe query construction
  • Support for parameterized queries
  • Comprehensive WHERE clause building
  • JOIN operations (INNER, LEFT, RIGHT)
  • Aggregate functions and mathematical expressions
  • CASE statements and subqueries
  • Proper SQL escaping and formatting

Main classes:

  • Sqler: The main query builder class
  • QField: Represents database fields with proper quoting
  • QVar: Represents values with proper escaping
  • Where: Base class for WHERE conditions
  • Join: Represents JOIN operations
  • QOrder: Represents ORDER BY specifications

Example usage:

var query = Sqler()
  .addSelect(QSelect('users.name'))
  .addSelect(QSelect('profiles.bio'))
  .from(QField('users'))
  .join(LeftJoin('profiles', On([
    Condition(QField('users.id'), QO.EQ, QField('profiles.user_id'))
  ])))
  .where(WhereOne(QField('users.active'), QO.EQ, QVar(true)))
  .orderBy(QOrder('users.name'))
  .limit(10);

String sql = query.toSQL();
// Generates: SELECT `users`.`name`, `profiles`.`bio` FROM `users`
//           LEFT JOIN `profiles` ON ( ( `users`.`id` = `profiles`.`user_id` ) )
//           WHERE ( `users`.`active` = true ) ORDER BY `users`.`name` ASC LIMIT 10

A SQL query builder for MySQL that provides a fluent interface for constructing SQL statements including SELECT, INSERT, UPDATE, and DELETE operations.

The Sqler class implements the builder pattern, allowing method chaining to construct complex SQL queries programmatically. It supports all major SQL operations including joins, where clauses, grouping, ordering, and parameterized queries.

Example usage:

var query = Sqler()
  .addSelect(QSelect('name'))
  .addSelect(QSelect('email'))
  .from(QField('users'))
  .where(WhereOne(QField('active'), QO.EQ, QVar(true)))
  .orderBy(QOrder('name'))
  .limit(10);

String sql = query.toSQL(); // Generates: SELECT `name`, `email` FROM `users` WHERE ( `active` = true ) ORDER BY `name` ASC LIMIT 10
Implemented types

Constructors

Sqler.new()
Creates a new instance of Sqler query builder.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addParam(String key, QVar value) Sqler
Adds a single parameter to the query for parameterized queries.
addParams(Map<String, QVar> params) Sqler
Adds multiple parameters to the query for parameterized queries.
addSelect(QSelectField select) Sqler
Adds a single SELECT field to the query.
clearDelete() Sqler
Clears the DELETE flag from the query.
clearFrom() Sqler
Clears all FROM tables from the query.
clearGroupBy() Sqler
Clears all GROUP BY fields from the query.
clearHaving() Sqler
Clears all HAVING conditions from the query.
clearInsert() Sqler
Clears all INSERT values from the query.
clearJoins() Sqler
Clears all JOIN operations from the query.
clearLimit() Sqler
Clears the LIMIT clause from the query.
clearOrderBy() Sqler
Clears all ORDER BY specifications from the query.
clearParams() Sqler
Clears all parameters from the query.
clearSelect() Sqler
Clears all SELECT fields from the query.
clearUpdate() Sqler
Clears all UPDATE field-value pairs from the query.
clearWhere() Sqler
Clears all WHERE conditions from the query.
copyWith({List<QSelectField>? selects, bool? delete, List<QField>? from, List<Where>? where, Map<String, QVar>? params, List<QField>? groupBy, List<Having>? having, List<QOrder>? orderBy, Limit? limit, List<Join>? joins, List<Map<String, QVar>>? insert, Map<String, QVar>? update}) Sqler
Creates a copy of this query with optional overrides for specific properties.
delete() Sqler
Sets the query to be a DELETE operation.
from(QField from) Sqler
Adds a table or source to the FROM clause.
groupBy(List<String> groupBy) Sqler
Sets the GROUP BY fields for the query.
hasFrom() bool
Checks if the query has any FROM tables.
hasGroupBy() bool
Checks if the query has any GROUP BY fields.
hasHaving() bool
Checks if the query has any HAVING conditions.
hasInsert() bool
Checks if the query has any INSERT values.
hasJoins() bool
Checks if the query has any JOIN operations.
hasLimit() bool
Checks if the query has a LIMIT clause.
hasOrderBy() bool
Checks if the query has any ORDER BY specifications.
hasSelect() bool
Checks if the query has any SELECT fields.
hasUpdate() bool
Checks if the query has any UPDATE field-value pairs.
hasWhere() bool
Checks if the query has any WHERE conditions.
having(Having having) Sqler
Adds a HAVING condition to the query.
insert(QField table, List<Map<String, QVar>> values) Sqler
Sets up the query for an INSERT operation with the specified values.
isDelete() bool
Checks if the query is a DELETE operation.
join(Join join) Sqler
Adds a JOIN operation to the query.
limit(int limit, [int? offset]) Sqler
Sets the LIMIT and optional OFFSET for the query.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
orderBy(QOrder orderBy) Sqler
Adds an ORDER BY specification to the query.
removeFrom(QField from) Sqler
Removes a specific FROM table from the query by comparing SQL output.
removeGroupBy(QField groupBy) Sqler
Removes a specific GROUP BY field from the query by comparing SQL output.
removeHaving(Having having) Sqler
Removes a specific HAVING condition from the query by comparing SQL output.
removeInsert(Map<String, dynamic> insert) Sqler
Removes a specific INSERT value set from the query.
removeJoin(Join join) Sqler
Removes a specific JOIN operation from the query by comparing SQL output.
removeOrderBy(QOrder orderBy) Sqler
Removes a specific ORDER BY specification from the query by comparing SQL output.
removeParam(String key) Sqler
Removes a parameter from the query by its key.
removeSelect(QSelectField select) Sqler
Removes a specific SELECT field from the query by comparing SQL output.
removeUpdate(String field) Sqler
Removes a specific field from the UPDATE SET clause.
removeWhere(Where where) Sqler
Removes a specific WHERE condition from the query by comparing SQL output.
selects(List<QSelectField> select) Sqler
Adds multiple SELECT fields to the query.
toSQL() String
Generates the final SQL string from the query configuration.
override
toString() String
A string representation of this object.
inherited
update(QField table) Sqler
Sets up the query for an UPDATE operation on the specified table.
updateSet(String field, QVar value) Sqler
Adds a field-value pair to the UPDATE SET clause.
where(Where where) Sqler
Adds a WHERE condition to the query.
whereAnd(List<Condition> conditions) Sqler
Adds multiple WHERE conditions combined with AND.
whereOne(SQL left, QO operator, SQL right) Sqler
Adds a simple WHERE condition comparing two SQL expressions.
whereOr(List<Condition> conditions) Sqler
Adds multiple WHERE conditions combined with OR.

Operators

operator ==(Object other) bool
The equality operator.
inherited