toSQL method
Generates the SQL field definition for this field.
Returns a complete MySQL field definition including data type, constraints, default values, and comments.
Example output: "field_name
INT NOT NULL AUTO_INCREMENT PRIMARY KEY"
Implementation
@override
String toSQL() {
String sql = '${QField(name).toSQL()} ${type.toSQL()}$_options';
if (isPrimaryKey) {
sql += ' PRIMARY KEY';
}
if (isAutoIncrement) {
sql += ' AUTO_INCREMENT';
}
if (!isNullable) {
sql += ' NOT NULL';
}
if (defaultValue.isNotEmpty) {
final reservedWords = [
'CURRENT_TIMESTAMP',
'NULL',
'TRUE',
'FALSE',
'NOW',
];
if (reservedWords.contains(defaultValue.toUpperCase())) {
sql += ' DEFAULT $defaultValue';
} else {
sql += ' DEFAULT "$defaultValue"';
}
}
if (comment != null) {
sql += ' COMMENT "$comment"';
}
return sql;
}