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 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 insert(Map row) async { Database db = await instance.database; return await db.insert(table, row); } Future insertWeatherRow(Map row) async { Database db = await instance.database; return await db.insert(weatherTable, row); } Future>> queryAllRows() async { Database db = await instance.database; return await db.query(table); } Future queryRowCount() async { Database db = await instance.database; return Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM $table')); } Future 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 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 queryNewestMapLocation() async { Database db = await instance.database; List 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> sortedMapLocations() async { Database db = await instance.database; var result = await db.rawQuery('SELECT $columnMapLocation FROM $table ORDER BY $columnViewTime DESC'); List result_list = new List(); for (var i = 0; i < result.length; i++) { result_list.add(result[i]['mapLocation']); } return result_list; } Future queryLatNLong(String mapLocation) async { Database db = await instance.database; List result = await db.rawQuery('SELECT $columnLatLong FROM $table WHERE $columnMapLocation = ?',[mapLocation]); return (result[0]['latLong'] == null) ? 'NA' : result[0]['latLong']; } Future queryWeather(int weatherID) async { Database db = await instance.database; List result = await db.rawQuery('SELECT $columnWeather FROM $weatherTable WHERE $columnWeatherWeatherID = ?',[weatherID]); return result[0]['weather']; } Future queryElevation(String mapLocation) async { Database db = await instance.database; List result = await db.rawQuery('SELECT $columnElev FROM $table WHERE $columnMapLocation = ?',[mapLocation]); return result[0]['elev']; } Future queryWeatherID(String mapLocation) async { Database db = await instance.database; List result = await db.rawQuery('SELECT $columnWeatherID FROM $table WHERE $columnMapLocation = ?',[mapLocation]); return result[0]['weatherID']; } Future update(Map row) async { Database db = await instance.database; String mapLocation = row[columnMapLocation]; return await db.update(table, row, where: '$columnMapLocation = ?', whereArgs: [mapLocation]); } Future updateWeathTbl(Map row) async { Database db = await instance.database; int weatherID = row[columnWeatherWeatherID]; return await db.update(weatherTable, row, where: '$columnWeatherWeatherID = ?', whereArgs: [weatherID]); } Future delete(String ml) async { Database db = await instance.database; return await db.delete(table, where: '$columnMapLocation = ?', whereArgs: [ml]); } }