-
Notifications
You must be signed in to change notification settings - Fork 4
/
EthTokenholdingsProvider.dart
124 lines (109 loc) · 3.07 KB
/
EthTokenholdingsProvider.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Created by dyf on 2018/9/7.
// Copyright (c) 2018 dyf.
//
import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import '../eth_token_grab/EthTokenholdingsModel.dart';
export '../eth_token_grab/EthTokenholdingsModel.dart';
class EthTokenholdingsProvider {
static EthTokenholdingsProvider _instance;
static EthTokenholdingsProvider get shared {
if (_instance == null) {
_instance = new EthTokenholdingsProvider();
}
return _instance;
}
Database _db;
String _dbName = "eth_tokholdings.db";
String _table = "tokholdings";
Future<String> _getDatabasePath() async {
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, _dbName);
return path;
}
Future<bool> open() async {
String path = await _getDatabasePath();
print("db path: $path");
_db = await openDatabase(path,
version: 1,
onCreate: (Database db, int version ) async {
await db.execute('''
create table if not exists $_table(
id integer primary key autoincrement,
address varchar(50) unique not null,
imgUrl varchar(100)
);'''
);
});
return _db.isOpen;
}
Future<bool> _hasAddress(String address) async {
List<Map> maps = await _db.query(_table,
columns: ["address"],
where: "address = ?",
whereArgs: [address]
);
return maps.length > 0 ? true : false;
}
Future<int> insert(EthTokenholdingsModel m) async {
bool ret = await _hasAddress(m.address);
if (!ret) {
return await _db.insert(_table, m.toMap());
} else {
return update(m);
}
}
Future<int> update(EthTokenholdingsModel m) async {
return await _db.update(_table,
m.toMap(),
where: "address = ?",
whereArgs: [m.address.toLowerCase()]
);
}
Future<int> delete(String address) async {
return await _db.delete(_table,
where: "address = ?",
whereArgs: [address]
);
}
Future<String> getImgUrl(String address) async {
List<Map> maps = await _db.query(_table,
columns: ["imgUrl"],
where: "address = ?",
whereArgs: [address]
);
if (maps.length > 0) {
return maps.first["imgUrl"];
}
return null;
}
Future<EthTokenholdingsModel> getModel(String address) async {
List<Map> maps = await _db.query(_table,
columns: ["address", "imgUrl"],
where: "address = ?",
whereArgs: [address]
);
if (maps.length > 0) {
return new EthTokenholdingsModel.fromMap(maps.first);
}
return null;
}
Future<List<EthTokenholdingsModel>> getModels() async {
List<Map> maps = await _db.query(_table,
columns: ["address", "imgUrl"]
);
if (maps != null && maps.length > 0) {
List<EthTokenholdingsModel> models = new List();
for (var i = 0; i < maps.length; i++) {
Map map = maps.elementAt(i);
var m = EthTokenholdingsModel.fromMap(map);
models.add(m);
}
return models;
}
return null;
}
Future close() async => await _db.close();
}