QueryBuilder class
Laravel-style Query Builder.
Provides a fluent API for building and executing SQL queries. The builder is schema-aware and will automatically filter insert/update data to only include columns that exist in the table.
Getting Started
// Get all users
final users = await DB.table('users').get();
// Get first user matching condition
final user = await DB.table('users').where('id', 1).first();
Selecting Data
// Select specific columns
final names = await DB.table('users')
.select(['name', 'email'])
.get();
// With conditions
final activeUsers = await DB.table('users')
.where('is_active', true)
.where('role', 'admin')
.get();
// With ordering and limits
final recent = await DB.table('posts')
.orderBy('created_at', 'desc')
.limit(10)
.get();
Inserting Data
// Insert single record
await DB.table('users').insert({
'name': 'John Doe',
'email': 'john@example.com',
});
// Schema-aware: unknown columns are automatically filtered out
await DB.table('users').insert({
'name': 'Jane',
'unknown_field': 'ignored', // Won't cause error
});
Updating Data
await DB.table('users')
.where('id', 1)
.update({'name': 'Updated Name'});
Deleting Data
await DB.table('users')
.where('id', 1)
.delete();
Constructors
- QueryBuilder(String _table)
- Create a new query builder for a table.
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
-
count(
) → Future< int> - Count the number of matching rows.
-
delete(
) → Future< int> - Delete records matching the query.
-
exists(
) → Future< bool> - Check if any rows exist matching the query.
-
first(
) → Future< Map< String, dynamic> ?> - Get the first result.
-
get(
) → Future< List< Map< >String, dynamic> > - Execute the query and get all results.
-
insert(
Map< String, dynamic> data) → Future<int> - Insert a new record (schema-aware).
-
insertAll(
List< Map< records) → Future<String, dynamic> >void> - Insert multiple records.
-
limit(
int count) → QueryBuilder - Limit the number of results.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
offset(
int count) → QueryBuilder - Offset the results.
-
orderBy(
String column, [String direction = 'asc']) → QueryBuilder - Add an order by clause.
-
pluck<
T> (String column) → Future< List< T> > - Get a list of values for a single column.
-
select(
List< String> columns) → QueryBuilder - Select specific columns.
-
toString(
) → String -
A string representation of this object.
inherited
-
truncate(
) → Future< void> - Truncate the table (delete all rows).
-
update(
Map< String, dynamic> data) → Future<int> - Update records matching the query (schema-aware).
-
value<
T> (String column) → Future< T?> - Get a single value from the first result.
-
where(
String column, dynamic operatorOrValue, [dynamic value]) → QueryBuilder - Add a where clause.
-
whereNotNull(
String column) → QueryBuilder - Add a where not null clause.
-
whereNull(
String column) → QueryBuilder - Add a where null clause.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited