toSQL<T extends SqlType> 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<T extends SqlType>() {
String sql = '${QField(name).toSQL<T>()} ${type.toSQL<T>()}$_options';
if (isPrimaryKey) {
sql += ' PRIMARY KEY';
}
if (isAutoIncrement) {
sql += SqlType.isMysql<T>() ? ' AUTO_INCREMENT' : ' AUTOINCREMENT';
}
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;
}