mirror of https://github.com/AMT-Cheif/drift.git
Implement @DataClassName
This commit is contained in:
parent
14ea7c36e0
commit
268f028206
|
@ -12,12 +12,12 @@ class Users extends Table {
|
|||
TextColumn get name => text().withLength(min: 6, max: 32)();
|
||||
}
|
||||
|
||||
@DataClassName('TodoEntry')
|
||||
class Todos extends Table {
|
||||
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
TextColumn get name => text().withLength(min: 6, max: 10)();
|
||||
TextColumn get content => text().named('body')();
|
||||
|
||||
}
|
||||
|
||||
@UseSally(tables: [Products, Users, Todos])
|
||||
|
|
|
@ -82,24 +82,24 @@ class _$UsersTable extends Users implements TableInfo<Users, User> {
|
|||
}
|
||||
}
|
||||
|
||||
class Todo {
|
||||
class TodoEntry {
|
||||
final int id;
|
||||
final String name;
|
||||
final String content;
|
||||
Todo({this.id, this.name, this.content});
|
||||
TodoEntry({this.id, this.name, this.content});
|
||||
@override
|
||||
int get hashCode =>
|
||||
((id.hashCode) * 31 + name.hashCode) * 31 + content.hashCode;
|
||||
@override
|
||||
bool operator ==(other) =>
|
||||
identical(this, other) ||
|
||||
(other is Todo &&
|
||||
(other is TodoEntry &&
|
||||
other.id == id &&
|
||||
other.name == name &&
|
||||
other.content == content);
|
||||
}
|
||||
|
||||
class _$TodosTable extends Todos implements TableInfo<Todos, Todo> {
|
||||
class _$TodosTable extends Todos implements TableInfo<Todos, TodoEntry> {
|
||||
final GeneratedDatabase db;
|
||||
_$TodosTable(this.db);
|
||||
@override
|
||||
|
@ -117,10 +117,10 @@ class _$TodosTable extends Todos implements TableInfo<Todos, Todo> {
|
|||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => Set();
|
||||
@override
|
||||
Todo map(Map<String, dynamic> data) {
|
||||
TodoEntry map(Map<String, dynamic> data) {
|
||||
final intType = db.typeSystem.forDartType<int>();
|
||||
final stringType = db.typeSystem.forDartType<String>();
|
||||
return Todo(
|
||||
return TodoEntry(
|
||||
id: intType.mapFromDatabaseResponse(data['id']),
|
||||
name: stringType.mapFromDatabaseResponse(data['name']),
|
||||
content: stringType.mapFromDatabaseResponse(data['body']),
|
||||
|
|
|
@ -104,7 +104,6 @@ create an issue.
|
|||
- Custom primary keys
|
||||
- Stabilize all end-user APIs
|
||||
- Support default values and expressions, auto-increment
|
||||
- Allow custom table names for the generated dart types
|
||||
##### Definitely planned for the future
|
||||
- Allow using DAOs instead of having to put everything in the main database
|
||||
class.
|
||||
|
|
|
@ -20,3 +20,23 @@ abstract class Table {
|
|||
}
|
||||
|
||||
class PrimaryKey {}
|
||||
|
||||
/// A class to to be used as an annotation on [Table] classes to customize the
|
||||
/// name for the data class that will be generated for the table class. The data
|
||||
/// class is a dart object that will be used to represent a row in the table.
|
||||
/// {@template sally:custom_data_class}
|
||||
/// By default, sally will attempt to use the singular form of the table name
|
||||
/// when naming data classes (e.g. a table named "Users" will generate a data
|
||||
/// class called "User"). However, this doesn't work for irregular plurals and
|
||||
/// you might want to choose a different name, for which this annotation can be
|
||||
/// used.
|
||||
/// {@template}
|
||||
class DataClassName {
|
||||
|
||||
final String name;
|
||||
|
||||
/// Customize the data class name for a given table.
|
||||
/// {@macro sally:custom_data_class}
|
||||
const DataClassName(this.name);
|
||||
|
||||
}
|
||||
|
|
|
@ -19,7 +19,22 @@ class TableParser extends ParserBase {
|
|||
fromClass: element,
|
||||
columns: _parseColumns(element),
|
||||
sqlName: sqlName,
|
||||
dartTypeName: dataClassNameForClassName(element.name));
|
||||
dartTypeName: _readDartTypeName(element));
|
||||
}
|
||||
|
||||
String _readDartTypeName(ClassElement element) {
|
||||
final nameAnnotation = element.metadata.singleWhere(
|
||||
(e) => e.computeConstantValue().type.name == 'DataClassName',
|
||||
orElse: () => null);
|
||||
|
||||
if (nameAnnotation == null) {
|
||||
return dataClassNameForClassName(element.name);
|
||||
} else {
|
||||
return nameAnnotation
|
||||
.constantValue
|
||||
.getField('name')
|
||||
.toStringValue();
|
||||
}
|
||||
}
|
||||
|
||||
String _parseTableName(ClassElement element) {
|
||||
|
|
Loading…
Reference in New Issue