mirror of https://github.com/AMT-Cheif/drift.git
Generate fields for triggers
This commit is contained in:
parent
3ee05bf647
commit
8df6ab3a14
|
@ -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 {
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -38,4 +38,7 @@ class MoorTrigger implements MoorSchemaEntity {
|
|||
final node = (declaration as MoorTriggerDeclaration).node;
|
||||
return node.span.text;
|
||||
}
|
||||
|
||||
@override
|
||||
String get dbGetterName => dbFieldName(displayName);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
returnType: tableClassName,
|
||||
code: '$tableClassName(this)',
|
||||
);
|
||||
writeMemoizedGetter(
|
||||
buffer: dbScope.leaf(),
|
||||
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}');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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('}');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue