123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import 'dart:async';
- import 'package:path/path.dart';
- import 'package:sport/pages/run/location.dart';
- import 'package:sport/pages/web/statistics.dart';
- import 'package:sqflite/sqflite.dart';
- class StatisticsDB {
- static final StatisticsDB _instance = new StatisticsDB.internal();
- factory StatisticsDB() => _instance;
- static Database? _db;
- StatisticsDB.internal();
- Future<Database> get db async {
- if (_db != null) {
- return _db!;
- }
- _db = await initDb();
- return _db!;
- }
- static const String TABLE = 'Statistics';
- initDb() async {
- String databasesPath = await getDatabasesPath();
- String path = join(databasesPath, 'Statistics.db');
- var db = await openDatabase(path, version: 2, onCreate: _onCreate, onUpgrade: _onUpgrade);
- return db;
- }
- int? rid;
- FutureOr<void> _onCreate(Database db, int version) async {
- await db.execute("CREATE TABLE $TABLE(" +
- "id INTEGER PRIMARY KEY, game_id VARCHAR, step INTEGER, distance INTEGER," +
- "jump INTEGER, squat INTEGER, start_time INTEGER, end_time INTEGER, " + // [0 - 7]
- "level INTEGER, score REAL, record INTEGER, mode INTEGER, opponent_id INTEGER" +
- ", status INTEGER" +
- ", data VARCHAR" +
- ")");
- }
- FutureOr<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
- if (newVersion > oldVersion) {
- print("TrackDB _onUpgrade $oldVersion -> $newVersion");
- try {
- await db.execute("drop table $TABLE");
- } catch (e) {
- print(e);
- }
- }
- await _onCreate(db, newVersion);
- }
- //
- // Future<int> insert(Statistics item) async {
- // var dbClient = await db;
- // var id = await dbClient.insert(TABLE, {
- // "rid": rid,
- // "time": item.time,
- // "latitude": item.latitude,
- // "longitude": item.longitude,
- // "speed": item.speed,
- // "altitude": item.altitude,
- // "bearing": item.bearing,
- // "accuracy": item.accuracy,
- // "step": item.step,
- // "state": item.state,
- // });
- // return id;
- // }
- //
- Future<int> save(Statistics statistics) async {
- var dbClient = await db;
- var id = await dbClient.insert(TABLE, statistics.toJson());
- return id;
- }
- update(Statistics statistics) async {
- var dbClient = await db;
- var result = await dbClient.update(TABLE, statistics.toJson(), where: "id = ?", whereArgs: [statistics.id]);
- }
- Future<int> deleteRecord(int id) async {
- var dbClient = await db;
- await dbClient.delete(TABLE, where: "id = ?", whereArgs: [id]);
- return 1;
- }
- Future<List<Map<String, dynamic>>> find() async {
- var dbClient = await db;
- return await dbClient.rawQuery(
- 'SELECT * FROM $TABLE');
- }
- }
|