Generate fields for triggers

This commit is contained in:
Simon Binder 2020-01-03 15:47:18 +01:00
parent 3ee05bf647
commit 8df6ab3a14
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
7 changed files with 42 additions and 43 deletions

View File

@ -1091,6 +1091,10 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
ConfigTable get config => _config ??= ConfigTable(this);
WithDefaults _withDefaults;
WithDefaults get withDefaults => _withDefaults ??= WithDefaults(this);
Trigger _myTrigger;
Trigger get myTrigger => _myTrigger ??= Trigger(
'CREATE TRIGGER my_trigger AFTER INSERT ON config BEGIN\n INSERT INTO with_defaults VALUES (new.config_key, LENGTH(new.config_value));\nEND;',
'my_trigger');
NoIds _noIds;
NoIds get noIds => _noIds ??= NoIds(this);
WithConstraints _withConstraints;
@ -1212,17 +1216,8 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
@override
Iterable<TableInfo> get allTables => allSchemaEntities.whereType<TableInfo>();
@override
List<DatabaseSchemaEntity> get allSchemaEntities => [
config,
withDefaults,
Trigger(
'CREATE TRIGGER my_trigger AFTER INSERT ON config BEGIN\n INSERT INTO with_defaults VALUES (new.config_key, LENGTH(new.config_value));\nEND;',
'my_trigger'),
noIds,
withConstraints,
mytable,
email
];
List<DatabaseSchemaEntity> get allSchemaEntities =>
[config, withDefaults, myTrigger, noIds, withConstraints, mytable, email];
}
class MultipleResult {

View File

@ -23,7 +23,7 @@ class DaoGenerator extends Generator implements BaseGenerator {
for (final table in dao.tables) {
final infoType = table.tableInfoName;
final getterName = table.tableFieldName;
final getterName = table.dbGetterName;
classScope
.leaf()
.write('$infoType get $getterName => db.$getterName;\n');

View File

@ -15,4 +15,7 @@ abstract class MoorSchemaEntity implements HasDeclaration {
/// A human readable name of this entity, like the table name.
String get displayName;
/// The getter in a generated database accessor referring to this model.
String get dbGetterName;
}

View File

@ -42,7 +42,8 @@ class MoorTable implements MoorSchemaEntity {
final String dartTypeName;
/// The getter name used for this table in a generated database or dao class.
String get tableFieldName => _dbFieldName(_baseName);
@override
String get dbGetterName => dbFieldName(_baseName);
String get tableInfoName {
// if this table was parsed from sql, a user might want to refer to it
// directly because there is no user defined parent class.
@ -131,6 +132,6 @@ class MoorTable implements MoorSchemaEntity {
}
}
String _dbFieldName(String className) => ReCase(className).camelCase;
String dbFieldName(String className) => ReCase(className).camelCase;
String tableInfoNameForTableClass(String className) => '\$${className}Table';

View File

@ -38,4 +38,7 @@ class MoorTrigger implements MoorSchemaEntity {
final node = (declaration as MoorTriggerDeclaration).node;
return node.span.text;
}
@override
String get dbGetterName => dbFieldName(displayName);
}

View File

@ -31,18 +31,28 @@ class DatabaseWriter {
'$className.connect(DatabaseConnection c): super.connect(c); \n');
}
final tableGetters = <MoorTable, String>{};
final entityGetters = <MoorSchemaEntity, String>{};
for (final table in db.tables) {
tableGetters[table] = table.tableFieldName;
final tableClassName = table.tableInfoName;
for (final entity in db.entities) {
entityGetters[entity] = entity.dbGetterName;
if (entity is MoorTable) {
final tableClassName = entity.tableInfoName;
writeMemoizedGetter(
buffer: dbScope.leaf(),
getterName: table.tableFieldName,
getterName: entity.dbGetterName,
returnType: tableClassName,
code: '$tableClassName(this)',
);
} else if (entity is MoorTrigger) {
writeMemoizedGetter(
buffer: dbScope.leaf(),
getterName: entity.dbGetterName,
returnType: 'Trigger',
code: 'Trigger(${asDartLiteral(entity.create)}, '
'${asDartLiteral(entity.displayName)})',
);
}
}
// Write fields to access an dao. We use a lazy getter for that.
@ -73,22 +83,9 @@ class DatabaseWriter {
..write('@override\nList<DatabaseSchemaEntity> get allSchemaEntities ')
..write('=> [');
var first = true;
for (final entity in db.entities) {
if (!first) {
schemaScope.write(', ');
}
if (entity is MoorTable) {
schemaScope.write(tableGetters[entity]);
} else if (entity is MoorTrigger) {
schemaScope.write('Trigger(${asDartLiteral(entity.create)}, '
'${asDartLiteral(entity.displayName)})');
}
first = false;
}
// finally, close bracket for the allSchemaEntities override and class.
schemaScope.write('];\n}');
schemaScope
..write(db.entities.map((e) => entityGetters[e]).join(', '))
// close list literal, getter and finally the class
..write('];\n}');
}
}

View File

@ -394,12 +394,12 @@ class QueryWriter {
}
void _writeReadsFrom() {
final from = _select.readsFrom.map((t) => t.tableFieldName).join(', ');
final from = _select.readsFrom.map((t) => t.dbGetterName).join(', ');
_buffer..write('readsFrom: {')..write(from)..write('}');
}
void _writeUpdates() {
final from = _update.updates.map((t) => t.tableFieldName).join(', ');
final from = _update.updates.map((t) => t.dbGetterName).join(', ');
_buffer..write('updates: {')..write(from)..write('}');
}
}