toString method
A string representation of this object.
Some classes have a default textual representation,
often paired with a static parse
function (like int.parse).
These classes will provide the textual representation as
their string representation.
Other classes have no meaningful textual representation
that a program will care about.
Such classes will typically override toString
to provide
useful information when inspecting the object,
mainly for debugging or logging.
Implementation
@override
String toString() {
final String toString = '''
// region ${_table.modelName}
class ${_table.modelName} extends TableBase ${_table.abstractModelName != null ? 'implements ${_table.abstractModelName}' : ''} {
${_table.modelName}({$_createBaseConstructure}) { _setDefaultValues(); softDeleteActivated = ${_table.useSoftDeleting.toString()};}
${_table.modelName}.withFields(${_table.createConstructure}){ _setDefaultValues();}
${_table.modelName}.withId(${_table.createConstructureWithId}){ _setDefaultValues();}
// fromMap v2.0
${_table.modelName}.fromMap(Map<String, dynamic> o, {bool setDefaultValues = true}) {
if (setDefaultValues)
{_setDefaultValues();}
$_toFromMapString
}
// FIELDS (${_table.modelName})
$_createProperties // end FIELDS (${_table.modelName})
$_createObjectRelations
$_createObjectCollections
${_table.objectType == ObjectType.table ? 'static const bool _softDeleteActivated=${_table.useSoftDeleting.toString()};' : ''}
${_table.modelName}Manager? __mn${_table.modelName};
${_table.modelName}Manager get _mn${_table.modelName} {
return __mn${_table.modelName} = __mn${_table.modelName} ?? ${_table.modelName}Manager();
}
// METHODS
@override
Map<String, dynamic> toMap({bool forQuery = false, bool forJson = false, bool forView=false}) {
final map = <String, dynamic>{};
$_toMapString
return map;
}
@override
Future<Map<String, dynamic>> toMapWithChildren([bool forQuery = false, bool forJson=false, bool forView=false]) async {
final map = <String, dynamic>{};
$_toMapString
$_createObjectCollectionsToMap
return map;
}
/// This method returns Json String [${_table.modelName}]
@override
String toJson() {
return json.encode(toMap(forJson: true));
}
/// This method returns Json String [${_table.modelName}]
@override
Future<String> toJsonWithChilds() async {
return json.encode(await toMapWithChildren(false,true));
}
@override
List<dynamic> toArgs() {
return[${_table.createListParameterForQuery.replaceAll('this.', '')}];
}
@override
List<dynamic> toArgsWithIds() {
return[${_table.createListParameterForQueryWithId.replaceAll('this.', '')}];
}
$_fromWebUrl
static Future<List<${_table.modelName}>?> fromWebUrl(Uri uri,{Map<String, String>? headers}) async {
try {
final response = await http.get(uri, headers:headers);
return await fromJson(response.body);
} catch (e) {
debugPrint('SQFENTITY ERROR ${_table.modelName}.fromWebUrl: ErrorMessage: \${e.toString()}');
return null;
}
}
Future<http.Response> postUrl(Uri uri, {Map<String, String>? headers}) {
return http.post(uri,
headers: headers, body: toJson());
}
static Future<List<${_table.modelName}>> fromJson(String jsonBody) async{
final Iterable list = await json.decode(jsonBody) as Iterable;
var objList = <${_table.modelName}>[];
try {
objList = list.map((${_table._modelLowerCase}) => ${_table.modelName}.fromMap(${_table._modelLowerCase} as Map<String, dynamic>)).toList();
} catch (e) {
debugPrint('SQFENTITY ERROR ${_table.modelName}.fromJson: ErrorMessage: \${e.toString()}');
}
return objList;
}
static Future<List<${_table.modelName}>> fromMapList(List<dynamic> data,{bool preload=false, List<String>? preloadFields, bool loadParents=false, List<String>? loadedFields, bool setDefaultValues=true}) async{
final List<${_table.modelName}> objList = <${_table.modelName}>[];
loadedFields = loadedFields ?? [];
for (final map in data) {
final obj = ${_table.modelName}.fromMap(map as Map<String, dynamic>, setDefaultValues: setDefaultValues);
${_table.collections!.isNotEmpty || _createObjectRelationsPreLoad.isNotEmpty ? '// final List<String> _loadedFields = List<String>.from(loadedFields);' : ''}
$_toOnetoOneCollections
$_toOnetoManyCollections
$_createObjectRelationsPreLoad
objList.add(obj);
}
return objList;
}
${_table.primaryKeyNames.isEmpty ? '' : '''
/// returns ${_table.modelName} by ID if exist, otherwise returns null
/// Primary Keys: $_getByIdParametersWithTypes
${commentPreload.replaceAll('methodname', 'getById')}
/// <returns>returns [${_table.modelName}] if exist, otherwise returns null
Future<${_table.modelName}?> getById($_getByIdParametersWithTypes, {bool preload=false, List<String>? preloadFields,bool loadParents=false, List<String>? loadedFields}) async{
if(${_table.primaryKeyNames[0]}==null){return null;}
${_table.modelName}? obj;
final data = await _mn${_table.modelName}.getById([$_getByIdParameters]);
if (data.length != 0)
{obj = ${_table.modelName}.fromMap(data[0] as Map<String, dynamic>);
$_toOnetoOneCollections
$_toOnetoManyCollections
$_createObjectRelationsPreLoad
}
else
{obj = null;}
return obj;
}'''}
${_table.objectType == ObjectType.table ? '''
$_saveMethod
$_saveAllMethod
/// Updates if the record exists, otherwise adds a new row
/// <returns>Returns ${_table.primaryKeyType == null || _table.primaryKeyType == PrimaryKeyType.text ? '1' : _table.primaryKeyNames[0]}
${_table.abstractModelName != null ? '@override' : '@override'}
Future<int?> upsert({bool ignoreBatch = true}) async {
try {
final result = await _mn${_table.modelName}.rawInsert(
'INSERT OR REPLACE INTO ${_table.tableName} (${_table.createConstructureWithId.replaceAll('this.', '')}) VALUES ($_createConstructureArgsWithId)', [${_table.createListParameterForQueryWithId.replaceAll('this.', '')}], ignoreBatch); if( result! > 0)
{
saveResult = BoolResult(success: true, successMessage: '${_table.modelName} ${_table.primaryKeyNames[0]}=\$${_table.primaryKeyNames[0]} updated successfully');
} else {
saveResult = BoolResult(
success: false, errorMessage: '${_table.modelName} ${_table.primaryKeyNames[0]}=\$${_table.primaryKeyNames[0]} did not update');
}
return ${_table.primaryKeyTypes[0] == 'String' ? '1' : '${_table.primaryKeyNames[0]}'};
} catch (e) {
saveResult = BoolResult(success: false,errorMessage: '${_table.modelName} Save failed. Error: \${e.toString()}');
return null;
}
}
$_upsertAllMethod
/// Deletes ${_table.modelName}
/// <returns>BoolResult res.success= true (Deleted), false (Could not be deleted)
${_table.abstractModelName != null ? '@override' : '@override'}
Future<BoolResult> delete([bool hardDelete=false]) async {
debugPrint('SQFENTITIY: delete ${_table.modelName} invoked (${_table.primaryKeyNames[0]}=\$${_table.primaryKeyNames[0]})');
$_deleteMethodSingle
}
$_recoverMethodSingle
''' : ''}
${_table.abstractModelName != null ? '@override' : '@override'}
${_table.modelName}FilterBuilder select(
{List<String>? columnsToSelect, bool? getIsDeleted}) {
return ${_table.modelName}FilterBuilder(this, getIsDeleted)
..qparams.selectColumns = columnsToSelect;
}
@override
${_table.modelName}FilterBuilder distinct(
{List<String>? columnsToSelect, bool? getIsDeleted}) {
return ${_table.modelName}FilterBuilder(this, getIsDeleted)
..qparams.selectColumns = columnsToSelect
..qparams.distinct = true;
}
void _setDefaultValues() {
$_createDefaultValues
}
@override
void rollbackPk() {
${_table.primaryKeyNames.isNotEmpty ? '''
if (isInsert == true) {
${_table.primaryKeyNames.map((e) => '$e = null;').join('\n')}
}
''' : ''}
}
// END METHODS
// BEGIN CUSTOM CODE
${_table.customCode != null ? _table.customCode : '''/*
you can define customCode property of your SqfEntityTable constant. For example:
const tablePerson = SqfEntityTable(
tableName: 'person',
primaryKeyName: 'id',
primaryKeyType: PrimaryKeyType.integer_auto_incremental,
fields: [
SqfEntityField('firstName', DbType.text),
SqfEntityField('lastName', DbType.text),
],
customCode: \'\'\'
String fullName()
{
return '\$firstName \$lastName';
}
\'\'\');
*/'''}
// END CUSTOM CODE
}
// endregion ${_table._modelLowerCase}
''';
return toString;
}