mirror of
https://github.com/TrentSPalmer/libre_gps_parser.git
synced 2024-11-15 19:41:30 -08:00
157 lines
5.7 KiB
Dart
157 lines
5.7 KiB
Dart
import 'dart:io';
|
|
import 'package:path/path.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
class DatabaseHelper {
|
|
|
|
static final _databaseName = "LnLShare.db";
|
|
static final _databaseVersion = 1;
|
|
|
|
static final table = 'gmaplocations';
|
|
static final weatherTable = 'weather';
|
|
|
|
static final columnMapLocation = 'mapLocation';
|
|
static final columnLatLong = 'latLong';
|
|
static final columnLnlTime = 'lnlTime';
|
|
static final columnViewTime = 'viewTime';
|
|
static final columnElev = 'elev';
|
|
static final columnElevTime = 'elevTime';
|
|
static final columnWeatherID = 'weatherID';
|
|
|
|
static final columnWeatherWeatherID = 'weatherID';
|
|
static final columnWeather = 'weather';
|
|
static final columnWeatherForecast = 'weatherForecast';
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 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,
|
|
version: _databaseVersion,
|
|
onCreate: _onCreate);
|
|
}
|
|
|
|
// SQL code to create the database table
|
|
Future _onCreate(Database db, int version) async {
|
|
await db.execute('''
|
|
CREATE TABLE $table (
|
|
$columnMapLocation VARCHAR UNIQUE,
|
|
$columnLatLong TEXT,
|
|
$columnLnlTime INT,
|
|
$columnViewTime INT,
|
|
$columnElev INT,
|
|
$columnElevTime INT,
|
|
$columnWeatherID INT
|
|
)
|
|
''');
|
|
await db.execute('''
|
|
CREATE TABLE $weatherTable (
|
|
$columnWeatherWeatherID INT UNIQUE,
|
|
$columnWeather TEXT,
|
|
$columnWeatherForecast TEXT
|
|
)
|
|
''');
|
|
}
|
|
|
|
Future<int> insert(Map<String, dynamic> row) async {
|
|
Database db = await instance.database;
|
|
return await db.insert(table, row);
|
|
}
|
|
|
|
Future<int> insertWeatherRow(Map<String, dynamic> row) async {
|
|
Database db = await instance.database;
|
|
return await db.insert(weatherTable, row);
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> queryAllRows() async {
|
|
Database db = await instance.database;
|
|
return await db.query(table);
|
|
}
|
|
|
|
Future<int> queryRowCount() async {
|
|
Database db = await instance.database;
|
|
return Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM $table'));
|
|
}
|
|
|
|
Future<int> queryRowExists(String mapLocation) async {
|
|
Database db = await instance.database;
|
|
return Sqflite.firstIntValue(await db.rawQuery('SELECT EXISTS (SELECT $columnMapLocation FROM $table WHERE $columnMapLocation=\'$mapLocation\')'));
|
|
}
|
|
|
|
Future<int> queryWeatherIDExists(int weatherID) async {
|
|
Database db = await instance.database;
|
|
return Sqflite.firstIntValue(await db.rawQuery('SELECT EXISTS (SELECT $columnWeatherWeatherID FROM $weatherTable WHERE $columnWeatherWeatherID=\'$weatherID\')'));
|
|
}
|
|
|
|
Future<String> queryNewestMapLocation() async {
|
|
Database db = await instance.database;
|
|
List<Map> result = await db.rawQuery('SELECT $columnMapLocation FROM $table ORDER BY $columnViewTime DESC LIMIT 1');
|
|
return (result.length == 0) ? 'Plataea\nGreece\nhttps://maps.app.goo.gl/1NW9z': result[0]['mapLocation'];
|
|
}
|
|
|
|
Future<List<String>> sortedMapLocations() async {
|
|
Database db = await instance.database;
|
|
var result = await db.rawQuery('SELECT $columnMapLocation FROM $table ORDER BY $columnViewTime DESC');
|
|
List<String> result_list = new List<String>();
|
|
for (var i = 0; i < result.length; i++) {
|
|
result_list.add(result[i]['mapLocation']);
|
|
}
|
|
return result_list;
|
|
}
|
|
|
|
Future<String> queryLatNLong(String mapLocation) async {
|
|
Database db = await instance.database;
|
|
List<Map> result = await db.rawQuery('SELECT $columnLatLong FROM $table WHERE $columnMapLocation = ?',[mapLocation]);
|
|
return (result[0]['latLong'] == null) ? 'NA' : result[0]['latLong'];
|
|
}
|
|
|
|
Future<String> queryWeather(int weatherID) async {
|
|
Database db = await instance.database;
|
|
List<Map> result = await db.rawQuery('SELECT $columnWeather FROM $weatherTable WHERE $columnWeatherWeatherID = ?',[weatherID]);
|
|
return result[0]['weather'];
|
|
}
|
|
|
|
Future<int> queryElevation(String mapLocation) async {
|
|
Database db = await instance.database;
|
|
List<Map> result = await db.rawQuery('SELECT $columnElev FROM $table WHERE $columnMapLocation = ?',[mapLocation]);
|
|
return result[0]['elev'];
|
|
}
|
|
|
|
Future<int> queryWeatherID(String mapLocation) async {
|
|
Database db = await instance.database;
|
|
List<Map> result = await db.rawQuery('SELECT $columnWeatherID FROM $table WHERE $columnMapLocation = ?',[mapLocation]);
|
|
return result[0]['weatherID'];
|
|
}
|
|
|
|
Future<int> update(Map<String, dynamic> row) async {
|
|
Database db = await instance.database;
|
|
String mapLocation = row[columnMapLocation];
|
|
return await db.update(table, row, where: '$columnMapLocation = ?', whereArgs: [mapLocation]);
|
|
}
|
|
|
|
Future<int> updateWeathTbl(Map<String, dynamic> row) async {
|
|
Database db = await instance.database;
|
|
int weatherID = row[columnWeatherWeatherID];
|
|
return await db.update(weatherTable, row, where: '$columnWeatherWeatherID = ?', whereArgs: [weatherID]);
|
|
}
|
|
|
|
Future<int> delete(String ml) async {
|
|
Database db = await instance.database;
|
|
return await db.delete(table, where: '$columnMapLocation = ?', whereArgs: [ml]);
|
|
}
|
|
}
|