Created
June 13, 2023 22:20
-
-
Save turtlepile/d25406f4835e030f22a36fa00e976de5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:arkitek_app_internal/data/appConstant.dart'; | |
import 'package:arkitek_app_internal/models/PostDijitalArsiv.dart'; | |
import 'package:arkitek_app_internal/models/PostStokFis.dart'; | |
import 'package:path/path.dart'; | |
import 'package:sqflite/sqflite.dart'; | |
class DBHelper { | |
Database? _db; | |
Future<Database?> get getDb async { | |
_db ??= await initializeDb(); | |
return _db; | |
} | |
Future<Database> initializeDb() async { | |
String dbPath = join(await getDatabasesPath(), "MyApp.db"); | |
var database = await openDatabase(dbPath, version: 1, onCreate: createDb); | |
return database; | |
} | |
Future<void> createDb(Database db, int version) async { | |
//create token table | |
await db.execute( | |
'CREATE TABLE IF NOT EXISTS "userToken" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"access_token" TEXT NOT NULL, "email" TEXT NOT NULL, "password" TEXT NOT NULL);'); | |
await db.execute( | |
'CREATE TABLE IF NOT EXISTS "logBaseTable" ("LOCALID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "OPTIMELOCAL" TEXT, "MODUL" TEXT, "ISLEMOLAY" TEXT, "TEXT" TEXT, "DEVICEID" TEXT, "VERSIONINFO" TEXT, "GONDERILDI" INTEGER, "DENEMECOUNT" INTEGER);'); | |
await db.execute( | |
'CREATE TABLE IF NOT EXISTS "lastNotificationDateTable" ("Date" TEXT);'); | |
} | |
// with Batch, we can insert without indexsiz. faster, no ID returns.. | |
//last notif starts | |
Future<void> insertLastNotificationDateTable( | |
String lastNotificationDate) async { | |
Database? db = await getDb; | |
Batch batch = db!.batch(); | |
batch.insert("lastNotificationDateTable", { | |
'Date': lastNotificationDate, | |
}); | |
await batch.commit(noResult: true); | |
} | |
Future<void> truncateLastNotificationDateTable() async { | |
Database? db = await getDb; | |
Batch batch = db!.batch(); | |
batch.delete("lastNotificationDateTable"); | |
await batch.commit(noResult: true); | |
} | |
Future<DateTime> getLastNotificationDateTable() async { | |
Database? db = await getDb; | |
var result = await db!.query( | |
"lastNotificationDateTable", | |
orderBy: "1 DESC", | |
); | |
DateTime resultDateTime = DateTime.now().subtract(Duration(days: 2)); | |
try { | |
resultDateTime = DateTime.parse(result[0]['Date'].toString()); | |
} catch (e) { | |
resultDateTime = DateTime.now().subtract(Duration(days: 2)); | |
} | |
return resultDateTime; | |
} | |
//Last Notif ends | |
//token start | |
Future<void> insertDBToken( | |
String token, | |
String email, | |
String password, | |
) async { | |
Database? db = await getDb; | |
Batch batch = db!.batch(); | |
batch.insert("userToken", { | |
'access_token': token, | |
"email": email, | |
"password": password, | |
}); | |
await batch.commit(noResult: true); | |
} | |
Future<void> truncateUserTokenTable() async { | |
Database? db = await getDb; | |
Batch batch = db!.batch(); | |
batch.delete("userToken"); | |
await batch.commit(noResult: true); | |
} | |
Future<List<Map<String, dynamic>>> getUserTable() async { | |
Database? db = await getDb; | |
var result = await db!.query( | |
"userToken", | |
orderBy: "id DESC", | |
); | |
return result; | |
} | |
Future updateDBToken(int id, String token) async { | |
Database? db = await getDb; | |
Batch batch = db!.batch(); | |
batch.update("userToken", {'access_token': token}, | |
where: 'id = ?', whereArgs: [id]); | |
return batch.commit(noResult: true); | |
} | |
//token end | |
//LogBase start | |
Future<void> insertLogBase( | |
String hataMesaji, String methodAdi, String formAdi) async { | |
Database? db = await getDb; | |
Batch batch = db!.batch(); | |
batch.insert("logBaseTable", { | |
'OPTIMELOCAL': DateTime.now().toString(), | |
'MODUL': formAdi, | |
'ISLEMOLAY': methodAdi, | |
'TEXT': hataMesaji, | |
'DEVICEID': deviceId, | |
'VERSIONINFO': version, | |
'GONDERILDI': 0, | |
'DENEMECOUNT': 0 | |
}); | |
await batch.commit(noResult: true); | |
} | |
Future<void> truncateLogBase() async { | |
Database? db = await getDb; | |
Batch batch = db!.batch(); | |
batch.delete("logBaseTable"); | |
await batch.commit(noResult: true); | |
} | |
Future<List<Map<String, dynamic>>> getLogBase(var gonderildi) async { | |
Database? db = await getDb; | |
var result = await db!.query("logBaseTable", | |
where: "GONDERILDI != ?", whereArgs: [gonderildi]); | |
return result; | |
} | |
Future updateLogBaseTable(int id, int newValue) async { | |
Database? db = await getDb; | |
// senkron'dan kullanılan | |
await db!.rawQuery( | |
"UPDATE logBaseTable SET DENEMECOUNT = DENEMECOUNT +1, GONDERILDI = $newValue WHERE LOCALID = $id"); | |
} | |
Future<List<Map<String, dynamic>>> getNotSentLogFromDB() async { | |
Database? db = await getDb; | |
var result = | |
await db!.query("logBaseTable", where: "GONDERILDI!=?", whereArgs: [1]); | |
return result; | |
} | |
//LogBase Ends | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment