simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.65k stars 372 forks source link

Would like a simple way to obtain rowid with my Data #1443

Closed 4WayMitja closed 3 years ago

4WayMitja commented 3 years ago

I can't get data list with rowid for all rows of my table This is the minimal example code that I have:

class IdWithStudent {
  final int studentId;
  final StudentTableData student;

  IdWithStudent(this.studentId, this.student);
}

class StudentTable extends Table {
  TextColumn get name => text().named('name')();
  IntColumn get passedExams => integer().named('passedExams').withDefault(const Constant(0))();
  IntColumn get enrollmentId => integer().named('enrollmentId')();

  @override
  String get tableName => 'Student';
}

@UseDao(tables: [StudentTable])
class StudentDao extends DatabaseAccessor<ApplicationDb> with _$StudentDaoMixin {
  StudentDao(this.db) : super(db);

  final ApplicationDb db;

  Future<List<StudentTableData>> getAll() => select(studentTable).get();

  // TODO implement Future<List<IdWithStudent>> getAllWithRowId() => // code
}

using

[flutter --version]
Flutter 1.22.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 9b2d32b605 (7 months ago) • 2021-01-22 14:36:39 -0800
Engine • revision 2f0af37152
Tools • Dart 2.10.5
[pubspec.yaml]
sqflite: ^1.3.2
moor: ^3.4.0
sqlite3_flutter_libs: ^0.4.0+1

I would like to obtain IdWithStudent from my Student table. Relevant example from sqlite:

(getAll) sqlite> select * from Student;
Mikhail Tal|3|202100001
Mikhail Botvinnik|5|202100002
Boris Spassky|3|202100003
Tigran Petrosian|17|202100004
Bobby Fischer|8|202100005

(getAllWithRowId) sqlite> select rowid,* from Student;
1|Mikhail Tal|3|202100001
2|Mikhail Botvinnik|5|202100002
3|Boris Spassky|3|202100003
4|Tigran Petrosian|17|202100004
5|Bobby Fischer|8|202100005

I would like to obtain rowid (studentId) and data (StudentTableData) for all rows of my table I would like an easy way to implement Future<List> getAllWithRowId() for my StudentDao class.

4WayMitja commented 3 years ago

Relevant link with example using custom select statements: https://moor.simonbinder.eu/docs/using-sql/custom_queries/#custom-select-statements

simolus3 commented 3 years ago

You can also use the rowId extension on a table. So the usage would look something like this:

final query = select(students).addColumns([students.rowId]);
return query.map((row) => IdWithStudent(row.read(students.rowId, row.readTable(students))));

But I think the easiest way would be to add an alias to the rowid to the table.

4WayMitja commented 3 years ago

Hi based on your solution for newer version I manage to put together solution for older version:

class StudentWithId {
  final StudentTableData student;
  final int studentId;

  StudentWithId(this.student, this.studentId);
}

Future<List<StudentWithId>> getAllWithRowId() {
    final query = customSelect('SELECT *, rowid FROM Student);
    return query.map((row) => StudentWithId(StudentTableData.fromData(row.data, db), row.readInt('rowid'))).get();
  }