ente/lib/db/db_helper.dart

173 lines
5.7 KiB
Dart
Raw Normal View History

2020-03-24 19:59:36 +00:00
import 'dart:io';
2020-03-27 16:07:55 +00:00
import 'package:myapp/models/photo.dart';
2020-03-24 19:59:36 +00:00
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
2020-04-30 16:15:18 +00:00
static final _databaseName = "ente.db";
2020-03-24 19:59:36 +00:00
static final _databaseVersion = 1;
2020-03-27 16:07:55 +00:00
static final table = 'photos';
2020-03-26 14:39:31 +00:00
2020-04-17 20:37:04 +00:00
static final columnGeneratedId = '_id';
2020-04-12 12:38:49 +00:00
static final columnUploadedFileId = 'uploaded_file_id';
2020-04-11 21:29:07 +00:00
static final columnLocalId = 'local_id';
2020-04-24 12:40:24 +00:00
static final columnTitle = 'title';
static final columnPathName = 'path_name';
static final columnRemotePath = 'remote_path';
2020-04-12 12:38:49 +00:00
static final columnIsDeleted = 'is_deleted';
2020-04-13 11:28:01 +00:00
static final columnCreateTimestamp = 'create_timestamp';
2020-03-27 16:07:55 +00:00
static final columnSyncTimestamp = 'sync_timestamp';
2020-03-24 19:59:36 +00:00
// make this a singleton class
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
// only have a single app-wide reference to the database
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
// lazily instantiate the db the first time it is accessed
_database = await _initDatabase();
return _database;
}
2020-03-26 14:39:31 +00:00
2020-03-24 19:59:36 +00:00
// this opens the database (and creates it if it doesn't exist)
_initDatabase() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
return await openDatabase(path,
2020-03-26 14:39:31 +00:00
version: _databaseVersion, onCreate: _onCreate);
2020-03-24 19:59:36 +00:00
}
// SQL code to create the database table
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
2020-04-17 20:37:04 +00:00
$columnGeneratedId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
2020-04-11 21:29:07 +00:00
$columnLocalId TEXT,
2020-04-12 12:38:49 +00:00
$columnUploadedFileId INTEGER NOT NULL,
2020-04-24 12:40:24 +00:00
$columnTitle TEXT NOT NULL,
$columnPathName TEXT NOT NULL,
$columnRemotePath TEXT,
2020-04-12 12:38:49 +00:00
$columnIsDeleted INTEGER DEFAULT 0,
2020-04-13 11:28:01 +00:00
$columnCreateTimestamp TEXT NOT NULL,
2020-03-28 13:56:06 +00:00
$columnSyncTimestamp TEXT
2020-03-24 19:59:36 +00:00
)
''');
}
2020-03-26 14:39:31 +00:00
2020-03-27 16:07:55 +00:00
Future<int> insertPhoto(Photo photo) async {
2020-03-26 14:39:31 +00:00
Database db = await instance.database;
2020-03-30 14:28:46 +00:00
return await db.insert(table, _getRowForPhoto(photo));
}
Future<List<dynamic>> insertPhotos(List<Photo> photos) async {
Database db = await instance.database;
var batch = db.batch();
int batchCounter = 0;
for (Photo photo in photos) {
if (batchCounter == 400) {
await batch.commit();
batch = db.batch();
}
batch.insert(table, _getRowForPhoto(photo));
batchCounter++;
}
return await batch.commit();
2020-03-26 14:39:31 +00:00
}
2020-03-27 16:07:55 +00:00
Future<List<Photo>> getAllPhotos() async {
Database db = await instance.database;
2020-04-13 11:28:01 +00:00
var results = await db.query(table,
2020-04-13 15:01:27 +00:00
where: '$columnIsDeleted = 0', orderBy: '$columnCreateTimestamp DESC');
2020-04-12 12:38:49 +00:00
return _convertToPhotos(results);
}
Future<List<Photo>> getAllDeletedPhotos() async {
Database db = await instance.database;
2020-04-13 11:28:01 +00:00
var results = await db.query(table,
2020-04-13 15:01:27 +00:00
where: '$columnIsDeleted = 1', orderBy: '$columnCreateTimestamp DESC');
2020-03-28 13:56:06 +00:00
return _convertToPhotos(results);
2020-03-27 16:07:55 +00:00
}
2020-03-28 13:56:06 +00:00
Future<List<Photo>> getPhotosToBeUploaded() async {
2020-03-24 19:59:36 +00:00
Database db = await instance.database;
2020-04-12 12:38:49 +00:00
var results = await db.query(table, where: '$columnUploadedFileId = -1');
2020-03-28 13:56:06 +00:00
return _convertToPhotos(results);
2020-03-24 19:59:36 +00:00
}
2020-04-24 12:40:24 +00:00
Future<int> updatePhoto(
int generatedId, String remotePath, int syncTimestamp) async {
2020-03-24 19:59:36 +00:00
Database db = await instance.database;
2020-04-24 12:40:24 +00:00
var values = new Map<String, dynamic>();
values[columnRemotePath] = remotePath;
values[columnSyncTimestamp] = syncTimestamp;
return await db.update(table, values,
where: '$columnGeneratedId = ?', whereArgs: [generatedId]);
2020-03-24 19:59:36 +00:00
}
2020-04-09 19:03:11 +00:00
Future<Photo> getPhotoByPath(String path) async {
2020-03-24 19:59:36 +00:00
Database db = await instance.database;
2020-04-09 19:03:11 +00:00
var rows =
2020-04-24 12:40:24 +00:00
await db.query(table, where: '$columnRemotePath =?', whereArgs: [path]);
2020-04-05 14:45:04 +00:00
if (rows.length > 0) {
2020-04-12 12:38:49 +00:00
return _getPhotofromRow(rows[0]);
2020-04-05 14:45:04 +00:00
} else {
throw ("No cached photo");
}
2020-03-24 19:59:36 +00:00
}
2020-03-26 14:39:31 +00:00
2020-04-18 18:46:38 +00:00
Future<int> markPhotoForDeletion(Photo photo) async {
2020-04-12 12:38:49 +00:00
Database db = await instance.database;
var values = new Map<String, dynamic>();
values[columnIsDeleted] = 1;
return db.update(table, values,
2020-04-17 20:37:04 +00:00
where: '$columnGeneratedId =?', whereArgs: [photo.generatedId]);
2020-04-12 12:38:49 +00:00
}
Future<int> deletePhoto(Photo photo) async {
Database db = await instance.database;
return db.delete(table,
2020-04-17 20:37:04 +00:00
where: '$columnGeneratedId =?', whereArgs: [photo.generatedId]);
2020-04-12 12:38:49 +00:00
}
2020-03-28 13:56:06 +00:00
List<Photo> _convertToPhotos(List<Map<String, dynamic>> results) {
var photos = List<Photo>();
for (var result in results) {
2020-04-12 12:38:49 +00:00
photos.add(_getPhotofromRow(result));
2020-03-28 13:56:06 +00:00
}
return photos;
}
2020-03-30 14:28:46 +00:00
Map<String, dynamic> _getRowForPhoto(Photo photo) {
var row = new Map<String, dynamic>();
2020-04-11 21:29:07 +00:00
row[columnLocalId] = photo.localId;
2020-04-12 12:38:49 +00:00
row[columnUploadedFileId] =
photo.uploadedFileId == null ? -1 : photo.uploadedFileId;
2020-04-24 12:40:24 +00:00
row[columnTitle] = photo.title;
row[columnPathName] = photo.pathName;
row[columnRemotePath] = photo.remotePath;
2020-04-13 11:28:01 +00:00
row[columnCreateTimestamp] = photo.createTimestamp;
2020-03-30 14:28:46 +00:00
row[columnSyncTimestamp] = photo.syncTimestamp;
return row;
}
2020-04-12 12:38:49 +00:00
Photo _getPhotofromRow(Map<String, dynamic> row) {
Photo photo = Photo();
2020-04-17 20:37:04 +00:00
photo.generatedId = row[columnGeneratedId];
2020-04-12 12:38:49 +00:00
photo.localId = row[columnLocalId];
photo.uploadedFileId = row[columnUploadedFileId];
2020-04-24 12:40:24 +00:00
photo.title = row[columnTitle];
photo.pathName = row[columnPathName];
photo.remotePath = row[columnRemotePath];
2020-04-13 11:28:01 +00:00
photo.createTimestamp = int.parse(row[columnCreateTimestamp]);
2020-04-12 12:38:49 +00:00
photo.syncTimestamp = row[columnSyncTimestamp] == null
? -1
: int.parse(row[columnSyncTimestamp]);
return photo;
}
2020-03-26 14:39:31 +00:00
}