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);
|
ConfigTable get config => _config ??= ConfigTable(this);
|
||||||
WithDefaults _withDefaults;
|
WithDefaults _withDefaults;
|
||||||
WithDefaults get withDefaults => _withDefaults ??= WithDefaults(this);
|
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 _noIds;
|
||||||
NoIds get noIds => _noIds ??= NoIds(this);
|
NoIds get noIds => _noIds ??= NoIds(this);
|
||||||
WithConstraints _withConstraints;
|
WithConstraints _withConstraints;
|
||||||
|
@ -1212,17 +1216,8 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
|
||||||
@override
|
@override
|
||||||
Iterable<TableInfo> get allTables => allSchemaEntities.whereType<TableInfo>();
|
Iterable<TableInfo> get allTables => allSchemaEntities.whereType<TableInfo>();
|
||||||
@override
|
@override
|
||||||
List<DatabaseSchemaEntity> get allSchemaEntities => [
|
List<DatabaseSchemaEntity> get allSchemaEntities =>
|
||||||
config,
|
[config, withDefaults, myTrigger, noIds, withConstraints, mytable, email];
|
||||||
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
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class MultipleResult {
|
class MultipleResult {
|
||||||
|
|
|
@ -23,7 +23,7 @@ class DaoGenerator extends Generator implements BaseGenerator {
|
||||||
|
|
||||||
for (final table in dao.tables) {
|
for (final table in dao.tables) {
|
||||||
final infoType = table.tableInfoName;
|
final infoType = table.tableInfoName;
|
||||||
final getterName = table.tableFieldName;
|
final getterName = table.dbGetterName;
|
||||||
classScope
|
classScope
|
||||||
.leaf()
|
.leaf()
|
||||||
.write('$infoType get $getterName => db.$getterName;\n');
|
.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.
|
/// A human readable name of this entity, like the table name.
|
||||||
String get displayName;
|
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;
|
final String dartTypeName;
|
||||||
|
|
||||||
/// The getter name used for this table in a generated database or dao class.
|
/// 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 {
|
String get tableInfoName {
|
||||||
// if this table was parsed from sql, a user might want to refer to it
|
// if this table was parsed from sql, a user might want to refer to it
|
||||||
// directly because there is no user defined parent class.
|
// 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';
|
String tableInfoNameForTableClass(String className) => '\$${className}Table';
|
||||||
|
|
|
@ -38,4 +38,7 @@ class MoorTrigger implements MoorSchemaEntity {
|
||||||
final node = (declaration as MoorTriggerDeclaration).node;
|
final node = (declaration as MoorTriggerDeclaration).node;
|
||||||
return node.span.text;
|
return node.span.text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get dbGetterName => dbFieldName(displayName);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,18 +31,28 @@ class DatabaseWriter {
|
||||||
'$className.connect(DatabaseConnection c): super.connect(c); \n');
|
'$className.connect(DatabaseConnection c): super.connect(c); \n');
|
||||||
}
|
}
|
||||||
|
|
||||||
final tableGetters = <MoorTable, String>{};
|
final entityGetters = <MoorSchemaEntity, String>{};
|
||||||
|
|
||||||
for (final table in db.tables) {
|
for (final entity in db.entities) {
|
||||||
tableGetters[table] = table.tableFieldName;
|
entityGetters[entity] = entity.dbGetterName;
|
||||||
final tableClassName = table.tableInfoName;
|
if (entity is MoorTable) {
|
||||||
|
final tableClassName = entity.tableInfoName;
|
||||||
|
|
||||||
writeMemoizedGetter(
|
writeMemoizedGetter(
|
||||||
buffer: dbScope.leaf(),
|
buffer: dbScope.leaf(),
|
||||||
getterName: table.tableFieldName,
|
getterName: entity.dbGetterName,
|
||||||
returnType: tableClassName,
|
returnType: tableClassName,
|
||||||
code: '$tableClassName(this)',
|
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.
|
// 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('@override\nList<DatabaseSchemaEntity> get allSchemaEntities ')
|
||||||
..write('=> [');
|
..write('=> [');
|
||||||
|
|
||||||
var first = true;
|
schemaScope
|
||||||
for (final entity in db.entities) {
|
..write(db.entities.map((e) => entityGetters[e]).join(', '))
|
||||||
if (!first) {
|
// close list literal, getter and finally the class
|
||||||
schemaScope.write(', ');
|
..write('];\n}');
|
||||||
}
|
|
||||||
|
|
||||||
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}');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -394,12 +394,12 @@ class QueryWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _writeReadsFrom() {
|
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('}');
|
_buffer..write('readsFrom: {')..write(from)..write('}');
|
||||||
}
|
}
|
||||||
|
|
||||||
void _writeUpdates() {
|
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('}');
|
_buffer..write('updates: {')..write(from)..write('}');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue