MTable class

Represents a MySQL table definition with fields, foreign keys, and table options.

This class implements the SQL interface and provides functionality to generate CREATE TABLE statements for MySQL databases.

Example:

final table = MTable(
  name: 'users',
  fields: [
    MFieldInt(name: 'id', isPrimaryKey: true, isAutoIncrement: true),
    MFieldVarchar(name: 'name', length: 100),
    MFieldVarchar(name: 'email', length: 255),
  ],
);
print(table.toSQL()); // Generates CREATE TABLE statement
Inheritance
Available extensions

Constructors

MTable({required String name, required List<MField> fields, List<ForeignKey> foreignKeys = const [], String charset = 'utf8mb4', String collation = 'utf8mb4_unicode_ci'})
Creates a new MySQL table definition.

Properties

charset String
Character set for the table (default: utf8mb4)
getter/setter pair
collation String
Collation for the table (default: utf8mb4_unicode_ci)
getter/setter pair
engine String
Storage engine for the table (default: InnoDB)
getter/setter pair
fields List<MField>
List of fields/columns in the table
getter/setter pair
foreignKeys List<ForeignKey>
List of foreign key constraints for the table
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
name String
The name of the table
getter/setter pair
qName QField
A QField representation of the table name for query building
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

allSelectFields({String alias = '', String prefix = ''}) List<QSelect>
Generates a list of QSelect objects for all fields in the table.
createForeignKeys(DatabaseDriver conn) Future<SqlDatabaseResult?>

Available on MTable, provided by the MySqlTable extension

Creates foreign key constraints for the table. Generates and executes ALTER TABLE statements to add foreign key constraints defined in the table's foreign keys collection. Each constraint is given a unique name using the current timestamp. Parameters:
createJoin(On on, {String as = ''}) Join
Creates a Join object for this table with the specified join condition.
createLeftJoin(On on, {String as = ''}) LeftJoin
Creates a LeftJoin object for this table with the specified join condition.
createRightJoin(On on, {String as = ''}) RightJoin
Creates a RightJoin object for this table with the specified join condition.
createTable(DatabaseDriver conn) Future<SqlDatabaseResult>

Available on MTable, provided by the MySqlTable extension

Creates the table in the MySQL database. Generates and executes a CREATE TABLE SQL statement based on the table definition. The SQL is generated using the table's toSQL() method. Parameters:
delete(DatabaseDriver conn, Sqler query) Future<SqlDatabaseResult>

Available on MTable, provided by the MySqlTable extension

Executes a DELETE query on the table. Executes the provided query to delete records from the table. The method includes a safety check to ensure the query contains a DELETE statement. Parameters:
dropTable(DatabaseDriver conn) Future<SqlDatabaseResult>

Available on MTable, provided by the MySqlTable extension

Drops the table from the MySQL database. Executes a DROP TABLE IF EXISTS statement to safely remove the table from the database. The IF EXISTS clause prevents errors if the table doesn't exist. Parameters:
execute(DatabaseDriver conn, Sqler sqler) Future<SqlDatabaseResult>

Available on MTable, provided by the MySqlTable extension

Executes a SQL query against the MySQL database connection. This is a low-level method that executes raw SQL queries. It handles exceptions and wraps results in a MySqlResult object for consistent error handling across the application. Parameters:
existsTable(DatabaseDriver conn) Future<bool>

Available on MTable, provided by the MySqlTable extension

Checks if the table exists in the MySQL database. This method queries the MySQL information schema to determine if a table with the current name exists in the database. Parameters:
formValidate(Map<String, Object?> data) Future<Map<String, List<String>>>
Validates the provided data against the table's fields. Returns a map of field names to lists of validation error messages. @data are input data to validate against the table's fields. @returns a map of field names to lists of validation error messages.
formValidateUI(Map<String, Object?> data) Future<FormResult>

Available on MTable, provided by the MySqlTable extension

Validates form data against the table's field definitions. This method performs comprehensive validation of input data based on the validators defined for each table field. It creates a form validator instance and processes all field validations. Parameters:
getFieldsAs([String from = '', String newAlias = '']) List<QSelectField>
Generates a list of QSelectField objects for SELECT queries with aliases.
getFieldsAs(String from, String as) List<QSelectField>

Available on MTable, provided by the MySqlTable extension

Gets table fields as select fields with optional table prefixes and aliases. This method transforms table fields into QSelectField objects suitable for SQL SELECT queries. It handles table prefixes and field aliases. Parameters:
insert(DatabaseDriver conn, Map<String, QVar> data) Future<SqlDatabaseResult>

Available on MTable, provided by the MySqlTable extension

Inserts a single record into the table. Convenience method that wraps insertMany for single record insertion. This is useful when you only need to insert one row. Parameters:
insertMany(DatabaseDriver conn, List<Map<String, QVar>> data) Future<SqlDatabaseResult>

Available on MTable, provided by the MySqlTable extension

Inserts multiple records into the table. Executes a bulk INSERT statement to add multiple rows to the table in a single database operation, which is more efficient than individual inserts. Parameters:
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
select(DatabaseDriver conn, Sqler query) Future<SqlDatabaseResult>

Available on MTable, provided by the MySqlTable extension

Executes a SELECT query on the table. Executes the provided query to retrieve data from the table. The query should be constructed using the Sqler query builder. Parameters:
toSQL<T extends SqlType>() String
Generates the SQL CREATE TABLE statement for this table.
override
toString() String
A string representation of this object.
inherited

Operators

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