You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.6 KiB
101 lines
2.6 KiB
import 'package:sqflite/sqflite.dart'; |
|
|
|
import '../../../migrations/databasehelper.dart'; |
|
import '../../../models/ip_domain_model.dart'; |
|
|
|
class ControllerLogin { |
|
final conn = SqfliteDatabaseHelper.instance; |
|
|
|
Future<int> addData(IpDomain ipDomain) async { |
|
var dbclient = await conn.db; |
|
int result = 0; // Provide an initial value |
|
try { |
|
result = await dbclient! |
|
.insert(SqfliteDatabaseHelper.ipDomainTable, ipDomain.toJson()); |
|
} catch (e) { |
|
print(e.toString()); |
|
} |
|
return result; |
|
} |
|
|
|
Future<int> updateData(IpDomain ipDomain) async { |
|
var dbclient = await conn.db; |
|
late int result; |
|
try { |
|
result = await dbclient!.update( |
|
SqfliteDatabaseHelper.ipDomainTable, |
|
ipDomain.toJson(), |
|
where: 'id=?', |
|
whereArgs: [ipDomain.id], |
|
); |
|
} catch (e) { |
|
print(e.toString()); |
|
} |
|
return result; |
|
} |
|
|
|
Future<List> fetchData() async { |
|
var dbclient = await conn.db; |
|
List userList = []; |
|
try { |
|
List<Map<String, dynamic>> maps = await dbclient! |
|
.query(SqfliteDatabaseHelper.ipDomainTable, orderBy: 'id DESC'); |
|
for (var item in maps) { |
|
userList.add(item); |
|
} |
|
} catch (e) { |
|
print(e.toString()); |
|
} |
|
return userList; |
|
} |
|
|
|
Future<List<IpDomain>> fetchDataId() async { |
|
var dbclient = await conn.db; |
|
List<IpDomain> ipDomainList = []; |
|
try { |
|
List<Map<String, dynamic>> maps = await dbclient! |
|
.query(SqfliteDatabaseHelper.ipDomainTable, orderBy: 'id DESC'); |
|
for (var item in maps) { |
|
ipDomainList.add(IpDomain.fromJson(item)); |
|
} |
|
} catch (e) { |
|
print(e.toString()); |
|
} |
|
return ipDomainList; |
|
} |
|
|
|
// Future<void> deleteAllData() async { |
|
// var dbClient = await conn.db; |
|
// await dbClient!.delete(SqfliteDatabaseHelper.ipDomainTable); |
|
// } |
|
|
|
Future<void> resetAutoIncrement() async { |
|
var dbClient = await conn.db; |
|
await dbClient!.execute( |
|
'DELETE FROM SQLITE_SEQUENCE WHERE NAME = "${SqfliteDatabaseHelper.ipDomainTable}"'); |
|
} |
|
|
|
Future<void> deleteAllData() async { |
|
var dbClient = await conn.db; |
|
|
|
// Hapus semua data dari tabel |
|
await dbClient!.delete(SqfliteDatabaseHelper.ipDomainTable); |
|
|
|
// Reset auto-increment to 1 |
|
await resetAutoIncrement(); |
|
} |
|
|
|
Future<void> addAllData(List<IpDomain> ipDomainList) async { |
|
var dbclient = await conn.db; |
|
Batch batch = dbclient!.batch(); |
|
|
|
for (var ipDomain in ipDomainList) { |
|
batch.insert( |
|
SqfliteDatabaseHelper.ipDomainTable, |
|
ipDomain.toJson(), |
|
); |
|
} |
|
|
|
await batch.commit(); |
|
} |
|
}
|
|
|