Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.34 KB | None | 0 0
  1. import 'dart:io';
  2.  
  3. import 'package:path_provider/path_provider.dart';
  4. import 'package:sqflite/sqflite.dart';
  5. import 'package:path/path.dart' as path;
  6.  
  7. class SqfliteStore {
  8.   static const String TABLE_NAME_STR = "kv_str";
  9.   static const String TABLE_NAME_INT = "kv_int";
  10.   Database db;
  11.  
  12.   @override
  13.   Future init() async {
  14.     var dir = await getApplicationDocumentsDirectory();
  15.     var dbPath = path.join(dir.path, 'sqlite.db');
  16.     try {
  17.       await File(dbPath).delete();
  18.     } catch (e) {
  19.       //ignore
  20.     }
  21.     ;
  22.     db = await openDatabase(
  23.       dbPath,
  24.       onCreate: (db, version) async {
  25.         await db.execute(
  26.           'CREATE TABLE $TABLE_NAME_STR (key TEXT PRIMARY KEY, value TEXT)',
  27.         );
  28.         await db.execute(
  29.           'CREATE TABLE $TABLE_NAME_INT (key TEXT PRIMARY KEY, value INTEGER)',
  30.         );
  31.       },
  32.       version: 1,
  33.     );
  34.   }
  35.  
  36.   Batch startBatch() {
  37.     return db.batch();
  38.   }
  39.  
  40.   Future<List<dynamic>> commitBatch(Batch batch) {
  41.     return batch.commit();
  42.   }
  43.  
  44.   @override
  45.   Future<int> getInt(String key) async {
  46.     var result = await db.query(
  47.       TABLE_NAME_INT,
  48.       where: "key = ?",
  49.       whereArgs: [key],
  50.     );
  51.     return result[0]['value'] as int;
  52.   }
  53.  
  54.   @override
  55.   void getIntBatch(String key, Batch b) async {
  56.     b.query(
  57.       TABLE_NAME_INT,
  58.       where: "key = ?",
  59.       whereArgs: [key],
  60.     );
  61.     return;
  62.   }
  63.  
  64.   @override
  65.   Future putInt(String key, int value) {
  66.     return db.insert(
  67.       TABLE_NAME_INT,
  68.       {'key': key, 'value': value},
  69.       conflictAlgorithm: ConflictAlgorithm.replace,
  70.     );
  71.   }
  72.  
  73.  
  74.   @override
  75.   void putIntBatched(String key, int value, Batch b) {
  76.     return b.insert(
  77.       TABLE_NAME_INT,
  78.       {'key': key, 'value': value},
  79.       conflictAlgorithm: ConflictAlgorithm.replace,
  80.     );
  81.   }
  82.  
  83.  
  84.   @override
  85.   Future deleteInt(String key) {
  86.     return db.delete(
  87.       TABLE_NAME_INT,
  88.       where: "key = ?",
  89.       whereArgs: [key],
  90.     );
  91.   }
  92.  
  93.   @override
  94.   void deleteIntBatched(String key, Batch b) {
  95.     return b.delete(
  96.       TABLE_NAME_INT,
  97.       where: "key = ?",
  98.       whereArgs: [key],
  99.     );
  100.   }
  101.  
  102.   @override
  103.   Future<String> getString(String key) async {
  104.     var result = await db.query(
  105.       TABLE_NAME_STR,
  106.       where: "key = ?",
  107.       whereArgs: [key],
  108.     );
  109.     return result[0]['value'] as String;
  110.   }
  111.  
  112.   @override
  113.   void getStringBatch(String key, Batch b) async {
  114.     b.query(
  115.       TABLE_NAME_STR,
  116.       where: "key = ?",
  117.       whereArgs: [key],
  118.     );
  119.   }
  120.  
  121.   @override
  122.   Future putString(String key, String value) {
  123.     return db.insert(
  124.       TABLE_NAME_STR,
  125.       {'key': key, 'value': value},
  126.       conflictAlgorithm: ConflictAlgorithm.replace,
  127.     );
  128.   }
  129.  
  130.   @override
  131.   void putStringBatched(String key, String value, Batch b) {
  132.      b.insert(
  133.       TABLE_NAME_STR,
  134.       {'key': key, 'value': value},
  135.       conflictAlgorithm: ConflictAlgorithm.replace,
  136.     );
  137.   }
  138.  
  139.   @override
  140.   Future deleteString(String key) {
  141.     return db.delete(
  142.       TABLE_NAME_STR,
  143.       where: "key = ?",
  144.       whereArgs: [key],
  145.     );
  146.   }
  147.  
  148.   @override
  149.   void deleteStringBatched(String key, Batch b) {
  150.     return b.delete(
  151.       TABLE_NAME_STR,
  152.       where: "key = ?",
  153.       whereArgs: [key],
  154.     );
  155.   }
  156.  
  157.   @override
  158.   Future close() {
  159.     return db.close();
  160.   }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement