Add joinType to Relationship

This commit is contained in:
Tobe O 2019-10-09 11:18:58 -04:00
parent 2533902e71
commit fdac775e41
2 changed files with 25 additions and 9 deletions

View file

@ -1,3 +1,5 @@
import 'annotations.dart';
abstract class RelationshipType {
static const int hasMany = 0;
static const int hasOne = 1;
@ -11,12 +13,14 @@ class Relationship {
final String foreignKey;
final String foreignTable;
final bool cascadeOnDelete;
final JoinType joinType;
const Relationship(this.type,
{this.localKey,
this.foreignKey,
this.foreignTable,
this.cascadeOnDelete});
this.cascadeOnDelete,
this.joinType});
}
class HasMany extends Relationship {
@ -24,12 +28,14 @@ class HasMany extends Relationship {
{String localKey,
String foreignKey,
String foreignTable,
bool cascadeOnDelete = false})
bool cascadeOnDelete = false,
JoinType joinType = JoinType.left})
: super(RelationshipType.hasMany,
localKey: localKey,
foreignKey: foreignKey,
foreignTable: foreignTable,
cascadeOnDelete: cascadeOnDelete == true);
cascadeOnDelete: cascadeOnDelete == true,
joinType: joinType);
}
const HasMany hasMany = HasMany();
@ -39,22 +45,29 @@ class HasOne extends Relationship {
{String localKey,
String foreignKey,
String foreignTable,
bool cascadeOnDelete = false})
bool cascadeOnDelete = false,
JoinType joinType = JoinType.left})
: super(RelationshipType.hasOne,
localKey: localKey,
foreignKey: foreignKey,
foreignTable: foreignTable,
cascadeOnDelete: cascadeOnDelete == true);
cascadeOnDelete: cascadeOnDelete == true,
joinType: joinType);
}
const HasOne hasOne = HasOne();
class BelongsTo extends Relationship {
const BelongsTo({String localKey, String foreignKey, String foreignTable})
const BelongsTo(
{String localKey,
String foreignKey,
String foreignTable,
JoinType joinType = JoinType.left})
: super(RelationshipType.belongsTo,
localKey: localKey,
foreignKey: foreignKey,
foreignTable: foreignTable);
foreignTable: foreignTable,
joinType: joinType);
}
const BelongsTo belongsTo = BelongsTo();
@ -66,11 +79,13 @@ class ManyToMany extends Relationship {
{String localKey,
String foreignKey,
String foreignTable,
bool cascadeOnDelete = false})
bool cascadeOnDelete = false,
JoinType joinType = JoinType.left})
: super(
RelationshipType.hasMany, // Many-to-Many is actually just a hasMany
localKey: localKey,
foreignKey: foreignKey,
foreignTable: foreignTable,
cascadeOnDelete: cascadeOnDelete == true);
cascadeOnDelete: cascadeOnDelete == true,
joinType: joinType);
}

View file

@ -159,6 +159,7 @@ belongsToTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('returns values on true subquery', () async {
printSeparator('True subquery test');
var query = BookQuery()..author.where.name.like('%Rowling%');
expect(await query.get(executor), [deathlyHallows]);
});