mirror of
https://github.com/dstark5/Openlib.git
synced 2025-08-06 07:10:06 +08:00
227 lines
6.7 KiB
Dart
227 lines
6.7 KiB
Dart
import 'dart:io';
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
class Sqlite {
|
|
static Future<Database> initDb() async {
|
|
var databasesPath = await getDatabasesPath();
|
|
String path = '$databasesPath/mylibrary.db';
|
|
bool isMobile = Platform.isAndroid || Platform.isIOS;
|
|
|
|
Database dbInstance = await openDatabase(
|
|
path,
|
|
version: 5,
|
|
onCreate: (Database db, int version) async {
|
|
await db.execute(
|
|
'CREATE TABLE mybooks (id TEXT PRIMARY KEY, title TEXT,author TEXT,thumbnail TEXT,link TEXT,publisher TEXT,info TEXT,format TEXT,description TEXT)');
|
|
await db.execute(
|
|
'CREATE TABLE preferences (name TEXT PRIMARY KEY,value BOOLEAN)');
|
|
if (isMobile) {
|
|
await db.execute(
|
|
'CREATE TABLE bookposition (fileName TEXT PRIMARY KEY,position TEXT)');
|
|
await db.execute(
|
|
'CREATE TABLE browserOptions (name TEXT PRIMARY KEY,value TEXT)');
|
|
}
|
|
},
|
|
onUpgrade: (db, oldVersion, newVersion) async {
|
|
List<dynamic> isTableExist = await db.query('sqlite_master',
|
|
where: 'name = ?', whereArgs: ['bookposition']);
|
|
List<dynamic> isPreferenceTableExist = await db.query('sqlite_master',
|
|
where: 'name = ?', whereArgs: ['preferences']);
|
|
List<dynamic> isbrowserOptionsExist = await db.query('sqlite_master',
|
|
where: 'name = ?', whereArgs: ['browserOptions']);
|
|
if (isPreferenceTableExist.isEmpty) {
|
|
await db.execute(
|
|
'CREATE TABLE preferences (name TEXT PRIMARY KEY,value BOOLEAN)');
|
|
}
|
|
if (isMobile && isTableExist.isEmpty) {
|
|
await db.execute(
|
|
'CREATE TABLE bookposition (fileName TEXT PRIMARY KEY,position TEXT)');
|
|
}
|
|
if (isMobile && isbrowserOptionsExist.isEmpty) {
|
|
await db.execute(
|
|
'CREATE TABLE browserOptions (name TEXT PRIMARY KEY,value TEXT)');
|
|
}
|
|
},
|
|
);
|
|
return dbInstance;
|
|
}
|
|
}
|
|
|
|
class MyBook {
|
|
final String id;
|
|
final String title;
|
|
final String? author;
|
|
final String? thumbnail;
|
|
final String link;
|
|
final String? publisher;
|
|
final String? info;
|
|
final String? description;
|
|
final String? format;
|
|
|
|
MyBook(
|
|
{required this.id,
|
|
required this.title,
|
|
required this.author,
|
|
required this.thumbnail,
|
|
required this.link,
|
|
required this.publisher,
|
|
required this.info,
|
|
required this.format,
|
|
required this.description});
|
|
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'id': id,
|
|
'title': title,
|
|
'author': author,
|
|
'thumbnail': thumbnail,
|
|
'link': link,
|
|
'publisher': publisher,
|
|
'info': info,
|
|
'format': format,
|
|
'description': description
|
|
};
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return 'MyBook{id: $id,title: $title,author: $author,thumbnail: $thumbnail,link: $link,publisher: $publisher,info: $info,format: $format,description:$description}';
|
|
}
|
|
}
|
|
|
|
class MyLibraryDb {
|
|
Database dbInstance;
|
|
String tableName = 'mybooks';
|
|
|
|
MyLibraryDb({required this.dbInstance});
|
|
|
|
Future<void> insert(MyBook book) async {
|
|
await dbInstance.insert(
|
|
tableName,
|
|
book.toMap(),
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
|
|
Future<void> delete(String id) async {
|
|
await dbInstance.delete(
|
|
tableName,
|
|
where: 'id = ?',
|
|
whereArgs: [id],
|
|
);
|
|
}
|
|
|
|
Future<MyBook?> getId(String id) async {
|
|
List<Map<String, dynamic>> data =
|
|
await dbInstance.query(tableName, where: 'id = ?', whereArgs: [id]);
|
|
List<MyBook> book = listMapToMyBook(data);
|
|
if (book.isNotEmpty) {
|
|
return book.first;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<bool> checkIdExists(String id) async {
|
|
List<Map<String, dynamic>> data =
|
|
await dbInstance.query(tableName, where: 'id = ?', whereArgs: [id]);
|
|
List<MyBook> book = listMapToMyBook(data);
|
|
if (book.isNotEmpty) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
Future<List<MyBook>> getAll() async {
|
|
final List<Map<String, dynamic>> maps = await dbInstance.query(tableName);
|
|
return listMapToMyBook(maps);
|
|
}
|
|
|
|
List<MyBook> listMapToMyBook(List<Map<String, dynamic>> maps) {
|
|
List<MyBook> myBookList = List.generate(maps.length, (i) {
|
|
return MyBook(
|
|
id: maps[i]['id'],
|
|
title: maps[i]['title'],
|
|
author: maps[i]['author'],
|
|
thumbnail: maps[i]['thumbnail'],
|
|
link: maps[i]['link'],
|
|
publisher: maps[i]['publisher'],
|
|
info: maps[i]['info'],
|
|
format: maps[i]['format'],
|
|
description: maps[i]['description']);
|
|
});
|
|
return myBookList.reversed.toList();
|
|
}
|
|
|
|
Future<void> saveBookState(String fileName, String position) async {
|
|
await dbInstance.insert(
|
|
'bookposition',
|
|
{'fileName': fileName, 'position': position},
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
|
|
Future<void> deleteBookState(String fileName) async {
|
|
await dbInstance.delete(
|
|
'bookposition',
|
|
where: 'fileName = ?',
|
|
whereArgs: [fileName],
|
|
);
|
|
}
|
|
|
|
Future<String?> getBookState(String fileName) async {
|
|
List<Map<String, dynamic>> data = await dbInstance
|
|
.query('bookposition', where: 'fileName = ?', whereArgs: [fileName]);
|
|
List<dynamic> dataList = List.generate(data.length, (i) {
|
|
return {'fileName': data[i]['fileName'], 'position': data[i]['position']};
|
|
});
|
|
if (dataList.isNotEmpty) {
|
|
return dataList[0]['position'];
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<void> savePreference(String name, bool value) async {
|
|
int boolInt = value ? 1 : 0;
|
|
await dbInstance.insert(
|
|
'preferences',
|
|
{'name': name, 'value': boolInt},
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
|
|
Future<bool> getPreference(String name) async {
|
|
List<Map<String, dynamic>> data = await dbInstance
|
|
.query('preferences', where: 'name = ?', whereArgs: [name]);
|
|
List<dynamic> dataList = List.generate(data.length, (i) {
|
|
return {'name': data[i]['name'], 'value': data[i]['value']};
|
|
});
|
|
if (dataList.isNotEmpty) {
|
|
return dataList[0]['value'] == 0 ? false : true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<void> setBrowserOptions(String name, String value) async {
|
|
await dbInstance.insert(
|
|
'browserOptions',
|
|
{'name': name, 'value': value},
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
|
|
Future<String> getBrowserOptions(String name) async {
|
|
List<Map<String, dynamic>> data = await dbInstance
|
|
.query('browserOptions', where: 'name = ?', whereArgs: [name]);
|
|
List<dynamic> dataList = List.generate(data.length, (i) {
|
|
return {'name': data[i]['name'], 'value': data[i]['value']};
|
|
});
|
|
if (dataList.isNotEmpty) {
|
|
return dataList[0]['value'];
|
|
} else {
|
|
return "";
|
|
}
|
|
}
|
|
}
|